import time
import ntptime
from machine import Pin
import dht
import network
import urequests
# Wi-Fi Credentials
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""
# Time zone info
TZ_INFO = "UTC7"
# DHT pin
DHTPIN = 23
# Google Apps Script ID
GAS_ID = "AKfycbyWNoBABf9bRR6WscEOAKzr47ZMpKYwDrelY_mwGxYuvEh6-Sr2byjsQYJFBx8Inb-oyw" # Google Script id from deploy app >>> Deployment ID:
# Initialize DHT sensor
d = dht.DHT22(Pin(DHTPIN))
# Initialize WiFi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
while not wlan.isconnected():
pass
print("WiFi connected")
# Sync time using ntptime
ntptime.settime()
# Function to send data to Google Sheet
def send_data_to_ggsheet(temp, humid, light):
temp = d.temperature()
humid = d.humidity()
light = 1235
#url = https://script.google.com/macros/s/AKfycbyWNoBABf9bRR6WscEOAKzr47ZMpKYwDrelY_mwGxYuvEh6-Sr2byjsQYJFBx8Inb-oyw/exec
url = "https://script.google.com/macros/s/" + GAS_ID + "/exec?t=" + str(temp) + "&h=" + str(humid) + "&l=" + str(light)
print("Posting Temperature, Humidity, and Light Intensity data to Google Sheet")
try:
response = urequests.get(url)
print("HTTP Status Code:", response.status_code)
payload = response.text
print("Payload:", payload)
print("Temperature: %d°C, Humidity: %d%%, Light: %d%%" % (temp, humid, light))
except Exception as e:
print("Error:", e)
temp = 0
humid = 0
light = 0
print("Temperature: %d°C, Humidity: %d%%, Light: %d%%" % (temp, humid, light))
# Main loop
while True:
time.sleep(0.5)
d.measure()
temp = d.temperature()
humid = d.humidity()
light = 1235
print("Temperature: %d°C, Humidity: %d%%, Light: %d%%" % (temp, humid, light))
send_data_to_ggsheet(temp, humid, light)
time.sleep(60) # Send data to Google Sheet every 10 seconds