1 2 | apt-get update apt-get upgrade |
1 | sudo nano /boot/config.txt |
1 | dtoverlay=w1-gpio-pullup,gpiopin=4 |
1 2 3 4 | sudo modprobe w1-gpio sudo modprobe w1-therm cd /sys/bus/w1/devices/ ls |
1 2 3 | pi@raspberrypi:~$ cd /sys/bus/w1/devices/ pi@raspberrypi:/sys/bus/w1/devices$ ls 28-00000494cb79 w1_bus_master1 |
1 2 | cd 28-00000494cb79 cat w1_slave |
1 2 | 70 01 4b 46 7f ff 10 10 e1 : crc=e1 YES 70 01 4b 46 7f ff 10 10 e1 t=23000 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #/home/pi/temperature.py #打开温度传感器文件 tfile = open("/sys/bus/w1/devices/28-00000494cb79/w1_slave") #读取文件所有内容 text = tfile.read() #关闭文件 tfile.close() #用换行符分割字符串成数组,并取第二行 secondline = text.split("\n")[1] #用空格分割字符串成数组,并取最后一个,即t=23000 temperaturedata = secondline.split(" ")[9] #取t=后面的数值,并转换为浮点型 temperature = float(temperaturedata[2:]) #转换单位为摄氏度 temperature = temperature / 1000 #打印值 print temperature |
1 2 3 4 | { “timestamp”:”2012-03-15T16:13:14″, “value”:294.34 } |
1 2 3 4 5 6 7 8 9 10 11 12 | #/home/pi/temperature.py tfile = open("/sys/bus/w1/devices/28-00000494cb79/w1_slave") text = tfile.read() tfile.close() secondline = text.split("\n")[1] temperaturedata = secondline.split(" ")[9] temperature = float(temperaturedata[2:]) temperature = temperature / 1000 res = '{"value":%f}' %temperature output = open('/home/pi/datafile.txt', 'w') output.write(res) output.close |
1 2 | sudo python /home/pi/temperature.py curl --request POST --data-binary @"/home/pi/datafile.txt" --header "U-ApiKey:XXXXXXXXXXXXXXXX" http://api.yeelink.net/v1.0/device/1969/sensor/2533/datapoints |
1 2 3 4 5 6 | #为脚本增加可执行权限 sudo chmod +x yeelink.sh #将脚本加入cronjob(计划任务) sudo crontab -e #在cornjob文件中添加下面一行,并保存(表示10分钟执行一下脚本,时间可自行修改) */10 * * * * /home/pi/yeelink.sh |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |