zoukankan      html  css  js  c++  java
  • 在树莓派上使用DS18B02,并将数据打印在oled上

    DQ连接树莓派的物理引脚7

    打开总线后,在sys bus w1 devices 中有以28-开头的文件

    第二行末尾除以1000为当前温度


    import  OS
    
    device_file ='/sys/bus/w1/devices/28-012029929465/w1_slave'
    
    def read_temp_raw():
        f = open(device_file,'r')
        lines = f.readlines()
        f.close()
        return lines
    
    def read_temp():
        lines = read_temp_raw()
        while lines[0].strip()[-3:] != 'YES':
            time.sleep(0.2)
            lines = read_temp_raw()
        equals_pos = lines[1].find('t=')
        if equals_pos != -1:
            temp_string = lines[1][equals_pos+2:]
            temp_c = float(temp_string)/1000.0
        return temp_c

    返回温度值的函数


    打开i2c,载入luma库

    $ sudo apt-get update
    $ sudo apt-get install python3 python3-pip python3-pil libjpeg-dev zlib1g-dev libfreetype6-dev liblcms2-dev libopenjp2-7 libtiff5 -y
    $ sudo -H pip3 install luma.oled

    在屏幕上打印字符串

    from luma.core.interface.serial import i2c, spi
    from luma.core.render import canvas
    from luma.oled.device import ssd1306, ssd1325, ssd1331, sh1106
     
    serial = i2c(port=1, address=0x3C)
    device = sh1106(serial)
    
    while True:
        with canvas(device) as dr:
            #print('temp C = %f'%read_temp())
            dr.text((30, 40), str(read_temp()), fill="white")

    最终程序

    #!/usr/bin/python3
    import os,time
    from luma.core.interface.serial import i2c, spi
    from luma.core.render import canvas
    from luma.oled.device import ssd1306, ssd1325, ssd1331, sh1106
     
    serial = i2c(port=1, address=0x3C)
    device = sh1106(serial)
     
    
    device_file ='/sys/bus/w1/devices/28-012029929465/w1_slave'
    
    def read_temp_raw():
        f = open(device_file,'r')
        lines = f.readlines()
        f.close()
        return lines
    
    def read_temp():
        lines = read_temp_raw()
        while lines[0].strip()[-3:] != 'YES':
            time.sleep(0.2)
            lines = read_temp_raw()
        equals_pos = lines[1].find('t=')
        if equals_pos != -1:
            temp_string = lines[1][equals_pos+2:]
            temp_c = float(temp_string)/1000.0
        return temp_c
    while True:
        with canvas(device) as dr:
            #print('temp C = %f'%read_temp())
            dr.text((30, 40), str(read_temp()), fill="white")
        
  • 相关阅读:
    为什么要选择忍者站群?
    SDCMS1.31 如何发布?
    强大的忍者站群
    出现未能加载“OpenWebKitSharp”是什么原因?
    wordpress发布模块如何使用?
    忍者站群做了百度竞价,岂不是跟站群相违背吗?
    点点博客的发布模块,如何使用?
    为什么有时候明明提示登陆成功,却无法获取分类或无法发布?
    被百度K了,怎么办?
    笔记0611
  • 原文地址:https://www.cnblogs.com/SFWR-YOU/p/14483726.html
Copyright © 2011-2022 走看看