import machine # Module to access hardware peripherals like ADC, pins, etc.
import network # Module to handle network connections
import time # Module for handling delays and time-based functions
import urequests # MicroPython library for HTTP requests
import ujson # MicroPython library for working with JSON data
import dht # Library for handling DHT temperature and humidity sensors (not used in this script)
# WiFi credentials
WIFI_SSID = "Wokwi-GUEST" # SSID of the Wi-Fi network
WIFI_PASSWORD = "" # Password for the Wi-Fi network (empty for Wokwi's open network)
# ThingSpeak API settings
THINGSPEAK_API_KEY = "5B7201QQGT4NQCFA" # Write API Key for ThingSpeak
THINGSPEAK_URL = "http://api.thingspeak.com/update" # URL for updating ThingSpeak channel
# LDR sensor connected to an analog pin
LDR_SENSOR_PIN = machine.ADC(1) # Analog pin to which the LDR is connected
# Function to read analog data from the LDR sensor
def read_ldr():
"""
Reads the raw analog data from the LDR sensor using the ADC.
Returns:
int: The raw ADC value (0 to 65535 for a 16-bit ADC).
"""
ldr_data = LDR_SENSOR_PIN.read_u16() # Reads the 16-bit ADC value
return ldr_data
# Function to calculate lux from ADC value
def cal_lux(ADC):
"""
Converts the ADC value from the LDR sensor to a lux value.
Args:
ADC (int): The raw ADC value from the LDR.
Returns:
float: The calculated lux value.
"""
vout = ADC / 65535 * 5 # Convert ADC value to voltage (assumes a 5V system)
RLDR = 2000 * vout / (1 - vout / 5) # Calculate LDR resistance using a voltage divider formula
lux = pow(50 * 1000 * pow(10, 0.7) / RLDR, 1 / 0.7) # Convert resistance to lux using a specific formula
return lux
# Function to send data to ThingSpeak
def send_data_to_thingspeak(ADC, LUX):
"""
Sends the ADC and lux values to ThingSpeak.
Args:
ADC (int): The raw ADC value.
LUX (float): The calculated lux value.
"""
# Prepare data in JSON format
data = {
"api_key": THINGSPEAK_API_KEY, # API key for authentication
"field1": ADC, # ADC value to be sent to field1
"field2": LUX, # Lux value to be sent to field2
}
# Send HTTP POST request to ThingSpeak
response = urequests.post(
THINGSPEAK_URL,
data=ujson.dumps(data), # Convert the data dictionary to a JSON string
headers={"Content-Type": "application/json"} # Specify the content type
)
response.close() # Close the response to free up resources
# Initialize the Wi-Fi connection
wifi = network.WLAN(network.STA_IF) # Set Wi-Fi interface to station mode
wifi.active(True) # Activate the Wi-Fi interface
wifi.connect(WIFI_SSID, WIFI_PASSWORD) # Connect to the specified Wi-Fi network
# Wait for the Wi-Fi connection to establish
while not wifi.isconnected():
time.sleep(1) # Retry every second until connected
print("Connected to Wi-Fi") # Notify that Wi-Fi is connected
# Main loop
while True:
ADC = read_ldr() # Read the raw ADC value from the LDR
LUX = cal_lux(ADC) # Calculate the lux value from the ADC data
print("ADC: {}".format(ADC)) # Print the ADC value to the console
print("LUX: {}".format(LUX)) # Print the lux value to the console
send_data_to_thingspeak(ADC, LUX) # Send the ADC and lux values to ThingSpeak
print("Data sent to ThingSpeak") # Confirm that data was sent
time.sleep(15) # Wait for 15 seconds before repeating (adjustable)
print("") # Print a blank line for readability in the console
Loading
pi-pico-w
pi-pico-w