# import urequests as requests
# import random
# import time
# import network
# import gc
# from machine import Pin
# from time import sleep
# import dht
# sensor = dht.DHT22(Pin(14))
# def do_connect():
# sta_if = network.WLAN(network.STA_IF)
# if not sta_if.isconnected():
# print('connecting to network...')
# sta_if.active(True)
# sta_if.connect("Wokwi-GUEST","")
# while not sta_if.isconnected():
# pass
# print('network config:', sta_if.ifconfig())
# do_connect()
# # Server URL to send the data
# SERVER_URL = "https://wajahat.pythonanywhere.com/api/update-dht-data"
# server_url_2 = "https://wajahat.pythonanywhere.com/api/get-led-state"
# def generate_random_data():
# # Generate random temperature and humidity values
# # temperature = random.uniform(30, 90)
# # humidity = random.uniform(40, 90)
# sensor.measure()
# temp = sensor.temperature()
# hum = sensor.humidity()
# print({"temperature": temp, "humidity": hum})
# return {"temperature": temp, "humidity": hum}
# def send_dht_data_to_server(data):
# global SERVER_URL, server_url_2
# try:
# # Send the data to the server using a POST request
# response = requests.post(SERVER_URL, json=data)
# if (response.status_code == 200) and (response2.status_code == 200):
# print("Data sent and receive successfully to the server.")
# else:
# print("Failed to send data and receive to the server. Status code:", response.status_code)
# response.close() # Close the connection to free memory
# except Exception as e:
# print("Error occurred while sending data to the server:", str(e))
# # return data
# prev_button_state = {"state": None}
# if __name__ == "__main__":
# while True:
# try:
# # Generate random data
# dht_data = generate_random_data()
# # Send the data to the server
# send_dht_data_to_server(dht_data)
# response2 = requests.get(server_url_2)
# data = response2.json()
# # print(response2)
# button_state = data
# if button_state != prev_button_state:
# print(f'Button state: {button_state}')
# prev_button_state = button_state
# # Free up memory
# gc.collect()
# # Wait for some time before sending the next data
# time.sleep(1)
# except KeyboardInterrupt:
# break
# # Close the network connection when done
# network.WLAN(network.STA_IF).disconnect()
import time
import requests
import machine
import network
import gc
from machine import Pin
from time import sleep
import dht
sensor = dht.DHT22(Pin(14))
def do_connect():
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect("Wokwi-GUEST","")
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
do_connect()
led = machine.Pin(21,machine.Pin.OUT)
def get_button_state():
response = requests.get("https://wajahat.pythonanywhere.com/api/get-led-state")
data = response.json()
return data
# Initialize the previous state to an initial value
prev_button_state = {"state": None}
while True:
button_state = get_button_state()
if button_state != prev_button_state:
print(f'Button state: {button_state}')
prev_button_state = button_state
if prev_button_state['led_state'] == 1:
print("Led ON")
led.on()
else:
print("LED OFF")
led.off()
time.sleep(1) # Check the state every 1 second