文本, 我会使用一个Elixir实现的模块函数去读取链接到树莓派的DS18B20温度传感器的温度数值.html
文本假设你熟悉linux
数字电路基础git
Elixir编程基础github
树莓派3, B型(图片省略, 下文有)编程
DS18B20传感器segmentfault
4.7KΩ 电阻(一分钱一颗)session
面包板(图片省略, 下文有)app
公母头杜邦线函数
接线图
面包板内部连通图
面包板接线图
黑色为地线
红色为3.3V板供直流电源, 对应PIN1
黄色为数据线
电阻链接在红色和黄色线之间
树莓派主板接线
左侧三根是链接到电脑的串口线, 用于控制台输入输出.
右侧为DS18B20传感器的三个引脚
全体照
mix nerves.new hello_celsius_sensor --target rpi3 cd hello_celsius_sensor mix deps.get
下面的过程和描述和覆盖启动分区中的文件相关
修改 config/config.exs
, 包含以下内容:
use Mix.Config # 日志 config :logger, :console, level: :debug, format: "$date $time $metadata[$level] $message\n", handle_sasl_reports: true, handle_otp_reports: true, utc_log: true # 覆盖 config :nerves, :firmware, rootfs_additions: "config/rootfs-additions" # 固件配置 config :nerves, :firmware, fwup_conf: "config/rpi/fwup.conf"
建立 rootfs-additions
目录
mkdir -p config/rootsfs-additions/etc cd config/rootsfs-additions/etc vi erlinit.config
添加以下内容:
# Additional configuration for erlinit # Turn on the debug prints # -v # Specify the UART port that the shell should use. #-c tty1 -c ttyS0 # If more than one tty are available, always warn if the user is looking at # the wrong one. --warn-unused-tty # Use dtach to capture the iex session so that it can be redirected # to the app's GUI #-s "/usr/bin/dtach -N /tmp/iex_prompt" # Specify the user and group IDs for the Erlang VM #--uid 100 #--gid 200 # Uncomment to hang the board rather than rebooting when Erlang exits # --hang-on-exit # Optionally run a program if the Erlang VM exits #--run-on-exit /bin/sh # Enable UTF-8 filename handling in Erlang and custom inet configuration -e LANG=en_US.UTF-8;LANGUAGE=en;ERL_INETRC=/etc/erl_inetrc # Mount the application partition # See http://www.linuxfromscratch.org/lfs/view/6.3/chapter08/fstab.html about # ignoring warning the Linux kernel warning about using UTF8 with vfat. -m /dev/mmcblk0p3:/root:vfat:: # Erlang release search path -r /srv/erlang # Assign a unique hostname based on the board id -d "/usr/bin/boardid -b rpi -n 4" -n nerves-%.4s
注意-c
参数设置为 ttyS0
是为了可以经过开发电脑查看到树莓派的输出信息, 请参考使用 Elixir 开发嵌入式系统: 串口调试
cp deps/rpi3/nerves_system_rpi3/fwup.conf config
修改 file-resource config.txt
的位置, 其中 NERVES_APP
为当前项目的根目录.
file-resource config.txt { host-path = "${NERVES_APP}/config/rpi/config.txt" }
复制 config.txt
文件.
cp deps/rpi3/nerves_system_rpi3/config.txt config/rpi
DS18B20 使用 Dallas 1-Wire protocol 协议. Nerves 提供了对 1-Wire 协议的支持, 只须要在 config.txt
配置文件激活这个参数便可.