• 树莓派I2C连接18B20


    按图连接设备

    载入模块

    sudo modprobe w1-gpio
    sudo modprobe w1-therm
    cd /sys/bus/w1/devices/

    显示结果

    ls
    pi@raspberrypi:~$ cd /sys/bus/w1/devices/
    pi@raspberrypi:/sys/bus/w1/devices$ ls
    28-00000494cb79 w1_bus_master1

    查看当前温度

    cd 28-00000494cb79
    cat w1_slave
    显示结果:
    70 01 4b 46 7f ff 10 10 e1 : crc=e1 YES
    70 01 4b 46 7f ff 10 10 e1 t=23000
    第二行的t=23000就是当前的温度值,要换算成摄氏度,除以1000,即当前温度为23000/1000=23摄氏度。

    将温度数据上传到yeelink的demo

    #coding=utf8
    #!/usr/bin/env python
    import time
    import json
    import urllib
    import urllib2
    import sys
    
    def DS18B20(name):
        tfile = open("/sys/bus/w1/devices/%s/w1_slave" %name) 
        #读取文件所有内容
        text = tfile.read()
        #关闭文件
        tfile.close()
        #用换行符分割字符串成数组,并取第二行
        secondline = text.split("
    ")[1]
        #用空格分割字符串成数组,并取最后一个,即t=23000
        temperaturedata = secondline.split(" ")[9]
        #取t=后面的数值,并转换为浮点型
        temperature = float(temperaturedata[2:])
        #转换单位为摄氏度
        temperature = temperature / 1000
        return temperature
     
    def update(name):
        temp_value = DS18B20(name)
        if(temp_value<50):
            temp_str = str(temp_value)
            url = 'http://api.yeelink.net/v1.0/device/1000/sensor/1000/datapoints'
            values = json.dumps({"value":temp_str})
            headers = {'U-ApiKey':'123456abcdef'}
            req = urllib2.Request(url, values, headers)
            urllib2.urlopen(req)
            return temp_str
        else:
            return ""
           
    if __name__ == '__main__':
        name = sys.argv[1]
        while True:
            temp = update(name)
            if(temp):
                print '成功 ' + temp
            time.sleep(20)
    
  • 相关阅读:
    Mysql常用命令
    Centos7初始配置
    centos7配置mysql
    CentOS7 配置阿里云yum源
    Dockerfile
    Centos7部署hadoop 3
    Windows10安装docker
    centos7安装docker
    大数据与分布式架构
    Centos7单主机部署 LAMP + phpmyadmin 服务
  • 原文地址:https://www.cnblogs.com/catmelo/p/3154300.html
Copyright © 2020-2023  润新知