import machine
import urequests
from machine import Pin, ADC
import network
import time
HTTP_HEADERS = {'Content-Type': 'application/json'}
THINGSPEAK_WRITE_API_KEY = '6JEOWRIVWZ6E0USY'
ssid = 'SRKHALL'
password = ''
# Configure Pico W as Station
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.connect(ssid, password)
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
# Define your soil moisture sensor pin (analog pin)
soil_pin = 26 # Adjust this pin according to your wiring
# Initialize the ADC (Analog to Digital Converter) for the soil moisture sensor
adc = ADC(Pin(soil_pin))
max_adc_value = 4095 # Maximum ADC value, might differ based on board
while True:
time.sleep(5) # Adjust the delay as needed
moisture_level = adc.read_u16() # Read the raw ADC value
moisture_percentage = (moisture_level / max_adc_value) * 100 # Convert to percentage
print("Moisture Level:", moisture_percentage)
# Prepare data to send to ThingSpeak
data = {'field5': moisture_percentage} # Change field number accordingly
# Send data to ThingSpeak
request = urequests.post('http://api.thingspeak.com/update?api_key=' + THINGSPEAK_WRITE_API_KEY, json=data, headers=HTTP_HEADERS)
request.close()
print("Data Sent to ThingSpeak:", data)