# DHT sensor simulation with display on Pico running CircuitPython
#
# DHT sensor:
# https://learn.adafruit.com/dht/dht-circuitpython-code
#
# Use circuitpython in wokwi simulation: https://docs.wokwi.com/guides/circuitpython
#
# Learn more:
# https://www.cytron.io/tutorial/read-and-display-environment-sensor-data-using-raspberry-pi-pico-and-circuitpython
# https://learn.adafruit.com/adafruit-oled-featherwing/python-circuitpython-wiring
# https://learn.adafruit.com/adafruit-oled-featherwing/python-usage
# https://docs.circuitpython.org/projects/ssd1306/en/latest/
#
import board
import time
import busio
import displayio
import terminalio
import adafruit_dht
from adafruit_display_text import label
import adafruit_displayio_ssd1306

# (re)initialize the display bus
displayio.release_displays()

# define the pins to be used for i2c communication:
i2c = busio.I2C(scl=board.GP3, sda=board.GP2)
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)

# declare the display:
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=64)

# Make the display context
display_context = displayio.Group()
display.show(display_context)

# declare DHT sensor object:
dht = adafruit_dht.DHT22(board.GP16)

# draw label for temperature:
ttext_label = label.Label(terminalio.FONT, text="Temperature:", color=0xFFFF00, x=20, y=5)
display_context.append(ttext_label)

# draw temperature label:
temperature = "XX.X"
temperature_label = label.Label(terminalio.FONT, text=temperature, color=0xFFFF00, x=20, y=18)
display_context.append(temperature_label)
temperature_C_label = label.Label(terminalio.FONT, text="C", color=0xFFFF00, x=47, y=18)
display_context.append(temperature_C_label)

# draw label for humidity:
htext_label = label.Label(terminalio.FONT, text="Humidity:", color=0xFFFF00, x=20, y=30)
display_context.append(htext_label)

# draw humidity label:
humidity = "XX"
humidity_label = label.Label(terminalio.FONT, text=humidity, color=0xFFFF00, x=20, y=43)
display_context.append(humidity_label)
humidity_perc_label = label.Label(terminalio.FONT, text="%", color=0xFFFF00, x=33, y=43)
display_context.append(humidity_perc_label)

while True:
    try:
        # get temperature and format it:
        temperature = "{:.1f}".format(dht.temperature)
        # update temperature in label:
        temperature_label.text = temperature

        # get humidity and format it:
        humidity = "{:.0f}".format(dht.humidity)
        # update humidity in label:
        humidity_label.text = humidity

    except RuntimeError as e:
        # Reading doesn't always work! Just print error and we'll try again
        print("Reading from DHT failure: ", e.args)

    # refresh every 5 seconds:
    time.sleep(5)
$abcdeabcde151015202530354045505560fghijfghij
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT