import network
import urequests as requests
import wifi
from machine import Pin
import dht
import time
import thing_speak as tsp
led = Pin(27, Pin.OUT)
dht_sensor = dht.DHT22(Pin(25))
light_sensor = Pin(26, Pin.IN)
def thingspeak_display():
prev_light_state = -1
while True:
dht_sensor.measure()
temp = dht_sensor.temperature()
humid = dht_sensor.humidity()
print(temp, humid)
print("Update to thinkspeak ...")
current_light_state = light_sensor.value()
if(prev_light_state != current_light_state):
prev_light_state = current_light_state
tsp.update_all(temp, humid, current_light_state)
else:
tsp.update_temp_and_humid(temp, humid)
time.sleep(20)
last_command = tsp.read_command()
if(last_command == "100"):
led.on()
elif(last_command == "0"):
led.off()
time.sleep(20)
if __name__ == "__main__":
wifi.connect_internet()
thingspeak_display()