'''
Pin names
Name Description
VCC Positive voltage
SDA Digital data pin (input/output)
NC Not connected
GND Ground
For Non l2c (SPI): connect both pins 3 and 4 to GND. This disables the I2C interface.
'''
from machine import Pin, SoftI2C
import ssd1306
import dht, time
# Initialize I2C bus
i2c = SoftI2C(scl=Pin(5), sda=Pin(18))
# Initialize OLED display, pixel size 128x64
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
def display_reading(tempC):
global oled
oled.fill(0)
if( tempC is None ):
oled.text("Updating..",0,0)
else:
oled.text("Temperature:", 0, 0)
oled.text(str(tempC) + "C", 10, 30)
oled.show()
def start_monitoring():
d = dht.DHT22(Pin(2))
display_reading(None)
try:
while True:
d.measure()
tempC = d.temperature()
print("Temperature is:", str(tempC) , "C")
display_reading(tempC)
time.sleep(5)
except Exception as e: print(e,file=sys.stderr)
else:
print('All good. Shutting down.')
finally:
print('Finalizing')
if __name__ == '__main__':
# code to run when file is executed as a script
start_monitoring()