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")
        
  • 相关阅读:
    WinForm的Chart控件画条形图
    WinForm的Chart控件画折线图
    自定义控件
    左侧收缩菜单
    数组
    C#生成随机数的三种方法
    WinForm之GDI手动双缓冲技术
    WinForm之GDI画图步骤
    WinForm GDI编程:Graphics画布类
    翻译:《实用的Python编程》08_02_Logging
  • 原文地址:https://www.cnblogs.com/SFWR-YOU/p/14483726.html
Copyright © 2011-2022 走看看