#19_GSheet_DataLogger.py
import gc
import random
import network
import urequests
from time import sleep
# Google Script Deployment ID
GAS_ID = "AKfycbxbJoFT0XarZFCyKBjOHMu7H9ANes_HwV0jyGR5s45mTZsBpGgKga6Az3lt7yfvSZw"
# Wi-Fi credentials
SSID = "Wokwi-GUEST"
PASSWORD = ""
def connect_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print(f"š Connecting to {ssid}...")
wlan.connect(ssid, password)
timeout = 15
while not wlan.isconnected() and timeout > 0:
print(".", end="")
sleep(1)
timeout -= 1
if wlan.isconnected():
print("\nā
Connected! IP:", wlan.ifconfig()[0])
return True
else:
print("\nā Could not connect to Wi-Fi.")
return False
def send_data_to_ggsheet(temp, humid, light, soil):
url = f"https://script.google.com/macros/s/{GAS_ID}/exec?s1={temp:.2f}&s2={humid:.2f}&s3={light}&s4={soil:.2f}"
print("š” Sending data to Google Sheet...")
try:
response = urequests.get(url)
print("ā
Response code:", response.status_code)
print("š Payload:", response.text)
response.close()
return True
except Exception as e:
print("ā HTTP Error:", e)
return False
# Main routine
if connect_wifi(SSID, PASSWORD):
count = 0
while True:
# Fake data
light = random.randint(1500, 45000)
temp = random.uniform(10.0, 60.0)
humid = random.uniform(0.0, 99.0)
soil = random.uniform(10.0, 99.0)
print(f"š” Temp: {temp:.2f} °C, š§ Humi: {humid:.2f} %, āļø Light: {light} lux, š± SoilMC: {soil:.2f} %")
success = send_data_to_ggsheet(temp, humid, light, soil)
if success:
count += 1
print(f"š Data sent ({count}) times")
print("ā³ Waiting...\n")
sleep(10)