from machine import Pin, UART, ADC
from time import sleep
from dht import DHT22
# DHT22 on GPIO14
dht_sensor = DHT22(Pin(14))
# LDR on GPIO34
ldr = ADC(Pin(34))
ldr.atten(ADC.ATTN_11DB) # 0 - 3.3V range
# UART
uart = UART(2, baudrate=9600, tx=Pin(17), rx=Pin(16))
while True:
try:
# Getting Temperature and humidity from DHT22
dht_sensor.measure()
t = dht_sensor.temperature()
h = dht_sensor.humidity()
# Getting data from LDR
lux = ldr.read()
# Data Format
msg = "{:.1f},{:.1f},{}\n".format(t, h, lux)
# Sending Data
uart.write(msg)
print("You sent: ", msg.strip())
sleep(2) # Main loop delay
except Exception as e:
print("Error: ", e)