# # Project objectives:
# # Read temperature and humidity values from the DHT22 sensor
# # Display the sensor readings in the console
# #
# # Hardware connections used:
# # DHT22 VCC Pin to 3.3V
# # DHT22 SDA Pin to GPIO Pin 15
# # 10k ohm pull-up resistor from DHT22 SDA Pin to 3.3V
# # DHT22 GND Pin to GND
# #
# # Programmer: Adrian Josele G. Quional
# # modules
# from machine import Pin
# from time import sleep
# from dht import DHT22 # if the sensor is DHT11, import DHT11 instead of DHT22
# # creating a DHT object
# # change DHT22 to DHT11 if DHT11 is used
# dht = DHT22(Pin(15))
# # continuously get sensor readings while the board has power
# while True:
# # getting sensor readings
# dht.measure()
# temp = dht.temperature()
# hum = dht.humidity()
# # displaying values to the console
# print(f"Temperature: {temp}°C Humidity: {hum}% ")
# # format method or string concatenation may also be used
# #print("Temperature: {}°C Humidity: {:.0f}% ".format(temp, hum))
# #print("Temperature: " + str(temp) + "°C" + " Humidity: " + str(hum) + "%")
# # delay of 2 secs because DHT22 takes a reading once every 2 secs
# sleep(2)
# print("Program started")
# // === ThingSpeak Configuration ===
# THINGSPEAK_API_KEY = "UDGY5OP8S9DY39PV" // Replace with your API key
# THINGSPEAK_URL = "https://api.thingspeak.com/update"
# // === Sensor Setup ===
# dht_pin = Pin(15) // DHT22 data pin
# dht_sensor = DHT22(dht_pin)
# ldr = ADC(26) // AO pin of LDR module to GP26 (ADC0)
# // === Wi-Fi Setup ===
# def connect_wifi():
# wlan = network.WLAN(network.STA_IF)
# wlan.active(True)
# wlan.connect("Wokwi-GUEST", "") // Wokwi has open WiFi
# print("Connecting to WiFi...", end="")
# while not wlan.isconnected():
# print(".", end="")
# utime.sleep(1)
# print("\nConnected. IP:", wlan.ifconfig()[0])
# // === Read Sensor Values ===
# def read_sensors():
# dht_sensor.measure()
# temperature = dht_sensor.temperature()
# humidity = dht_sensor.humidity()
# ldr_value = ldr.read_u16() // 0–65535
# light_percent = (ldr_value / 65535) * 100
# return temperature, humidity, light_percent
# // === Send Data to ThingSpeak ===
# def send_to_thingspeak(temp, hum, light):
# payload = {
# "api_key": UDGY5OP8S9DY39PV,
# "field1": Temperature ,
# "field2": Humidity,
# "field3": Light Intensity
# }
# try:
# response = urequests.get(THINGSPEAK_URL, params=payload)
# print("Data sent to ThingSpeak! Response:", response.text)
# response.close()
# except Exception as e:
# print("Failed to send data:", e)
# // === Main Execution ===
# connect_wifi()
# while True:
# try:
# temperature, humidity, light = read_sensors()
# print("Temp: {:.1f}°C | Hum: {:.1f}% | Light: {:.2f}%".format(temperature, humidity, light))
# send_to_thingspeak(temperature, humidity, light)
# except Exception as e:
# print("Error reading sensors or sending data:", e)
# utime.sleep(30) // 30 sec delay between updates
from machine import Pin, ADC
from time import sleep
from dht import DHT22
import network
import urequests
print("Program started")
# === Wi-Fi Credentials ===
SSID = "Aditya Wifi"
PASSWORD = "idontknow"
# === ThingSpeak Settings ===
THINGSPEAK_API_KEY = "UDGY5OP8S9DY39PV"
THINGSPEAK_URL = "https://api.thingspeak.com/update"
# === Connect to Wi-Fi ===
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print("Connecting to Wi-Fi...")
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
sleep(1)
print(".", end="")
print("\nConnected to Wi-Fi. IP:", wlan.ifconfig()[0])
# === Read from DHT22 ===
dht = DHT22(Pin(15)) # Assuming DHT22 data pin is connected to GP15
def read_dht():
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
return temp, hum
# === Read from LDR (Light Dependent Resistor) ===
# Assuming LDR is connected to ADC0 (GP26)
ldr_pin = ADC(Pin(26))
def read_ldr():
# Read the analog value from the LDR
ldr_value = ldr_pin.read_u16() # Read a 16-bit unsigned integer value
return ldr_value
# === Send to ThingSpeak ===
def send_to_thingspeak(temp, hum, light_intensity):
payload = {
"api_key": THINGSPEAK_API_KEY, # Fixed: API key must be a string
"field1": temp,
"field2": hum,
"field3": light_intensity # Adding LDR data to field3
}
try:
url_params = "&".join([f"{key}={value}" for key, value in payload.items()])
full_url = f"{THINGSPEAK_URL}?{url_params}"
response = urequests.get(full_url)
print("Sent to ThingSpeak:", response.text)
response.close()
except Exception as e:
print("Failed to send:", e)
# === Main Execution ===
connect_wifi()
while True:
try:
temp, hum = read_dht()
light_intensity = read_ldr()
print(f"Temperature: {temp}°C Humidity: {hum}% Light Intensity: {light_intensity}")
send_to_thingspeak(temp, hum, light_intensity)
except Exception as e:
print("Error:", e)
sleep(15) # Respect ThingSpeak 15-second update limit