from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import framebuf, sys
import utime
from dht import DHT22
from time import sleep
pix_res_x = 128
pix_res_y = 64
dht = DHT22(Pin(10))
def init_i2c(scl_pin, sda_pin):
i2c_dev = I2C(1, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=200000)
i2c_addr = [hex(ii) for ii in i2c_dev.scan()]
if not i2c_addr:
print('No I2C Display Found')
sys.exit()
else:
print("I2C Address : {}".format(i2c_addr[0]))
print("I2C Configuration: {}".format(i2c_dev))
return i2c_dev
def display_text(oled, temp, hum):
oled.fill(0)
if temp < 20:
oled.text("Oh no! It's too", 0, 0)
oled.text("cold for me!!", 0, 10)
elif temp > 40:
oled.text("Uh oh! It's too", 0, 0)
oled.text("hot! I can't", 0, 10)
oled.text("breathe!", 0, 20)
else:
oled.text("Temp: " + str(temp) + "C", 0, 0)
if hum < 30:
oled.text("I'm so thirsty!", 0, 40)
elif hum > 70:
oled.text("Too humid!", 0, 40)
else:
oled.text("Humidity: " + str(hum) + "%", 0, 40)
oled.show()
def main():
i2c_dev = init_i2c(scl_pin=27, sda_pin=26)
oled = SSD1306_I2C(pix_res_x, pix_res_y, i2c_dev)
while True:
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
display_text(oled, temp, hum)
utime.sleep(2)
if __name__ == '__main__':
main()
pico:GP0
pico:GP1
pico:GND.1
pico:GP2
pico:GP3
pico:GP4
pico:GP5
pico:GND.2
pico:GP6
pico:GP7
pico:GP8
pico:GP9
pico:GND.3
pico:GP10
pico:GP11
pico:GP12
pico:GP13
pico:GND.4
pico:GP14
pico:GP15
pico:GP16
pico:GP17
pico:GND.5
pico:GP18
pico:GP19
pico:GP20
pico:GP21
pico:GND.6
pico:GP22
pico:RUN
pico:GP26
pico:GP27
pico:GND.7
pico:GP28
pico:ADC_VREF
pico:3V3
pico:3V3_EN
pico:GND.8
pico:VSYS
pico:VBUS
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND