import network
import urequests
import dht
from machine import Pin
from time import sleep
import ntptime
import time
# Define DHT sensor
dht_pin = Pin(4)
dht_sensor = dht.DHT22(dht_pin)
ntptime.host = 'pool.ntp.org' # Use this or another known NTP server
# WiFi credentials
ssid = 'Wokwi-GUEST'
password = ''
# Google Apps Script Web App URL
server_url = 'https://script.google.com/macros/s/AKfycbwK0kGtJ6j24viKYRpvL5gBOAwiqagGFh4SnMSNOsQ3jDveHohByTOs2-eT6-gd3IfA7A/exec'
# Connect to WiFi
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
print("Connecting to WiFi...")
sleep(1)
print("Connected to WiFi:", wlan.ifconfig())
# Get current NTP time
def get_ntp_time():
try:
ntptime.settime() # Synchronize the system time with NTP
tm = time.localtime(time.time()) # Get local time
local_time = time.localtime(time.time() + 7 * 3600)
formatted_time = "{:02d}:{:02d}:{:02d}".format(tm[3], tm[4], tm[5])
return formatted_time
except Exception as e:
print("Error getting NTP time:", e)
return None
# Send data to the server
def send_data():
# Read DHT sensor data
try:
dht_sensor.measure() # Trigger the sensor reading
humidity = dht_sensor.humidity() # Get humidity
temperature = dht_sensor.temperature() # Get temperature
except OSError as e:
print("Failed to read from DHT sensor:", e)
humidity = 0
temperature = 0
print(f"Temperature: {temperature}°C, Humidity: {humidity}%")
# Get current time
timestamp = get_ntp_time()
# Prepare JSON payload
json_data = {
"method": "append",
"temperature": temperature,
"humidity": humidity,
"timestamp": timestamp
}
# Send HTTP POST request
try:
response = urequests.post(server_url, json=json_data)
print("Response:", response.status_code, response.text)
response.close()
except Exception as e:
print("Error sending data:", e)
# Main loop
def main_loop():
while True:
# Send data periodically
print("Sending data...")
send_data()
sleep(5) # Send data every 5 seconds (adjust as needed)
# Setup and run
if __name__ == "__main__":
connect_wifi()
main_loop()
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4