1

我在编码方面还是新手,并且能够将其他来源的一些代码组合在一起,用于我构建的一个简单的气象站。一切似乎都运行良好,但我确实偶尔会出现打嗝,显示屏上的温度读数显示超出 0.1-0.9 的额外小数点。我插入了一个舍入命令来舍入到最接近的小数,我认为这会解决这个问题,但它仍然随机发生。通过外壳显示温度读数时没有问题。似乎只是在显示屏上随机温度读数添加了额外的小数点。我已经包含了图片和代码。绝对会感谢我能解决这个问题的任何帮助。谢谢你。

from machine import Pin
import utime as time
from pico_i2c_lcd import I2cLcd
from machine import I2C
from DHT22 import DHT22

i2c = I2C(id=1,scl=Pin(27),sda=Pin(26),freq=100000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
dht_data = Pin(15,Pin.IN,Pin.PULL_UP)
dht_sensor=DHT22(dht_data,Pin(14,Pin.OUT),dht11=False)

while True:
     T,H = dht_sensor.read()
    #Converted to Fahrenheit (FT) for LCD display
    FT = T*1.8+32
    if T is None:
        print(" sensor error")
    else:
        print("{:3.1f}'C  {:3.1f}%".format(T,H))
    #DHT22 not responsive if delay too short
    time.sleep_ms(500)
    
    lcd.clear()
    lcd.move_to(0,0)
    lcd.putstr('Temp:')
    lcd.move_to(10,0)
    #If using Celsius, change (FT) to (T) and "F" to "C" on line below
    lcd.putstr(str(round(FT, 1))+"F")
    lcd.move_to(0,1)
    lcd.putstr('Humidity:')
    lcd.move_to(10,1)
    lcd.putstr(str(H)+"%")`

它通常看起来像什么

随机出现的打嗝

4

0 回答 0