from machine import Pin, ADC, I2C
import dht
import time
import network
import urequests
import ssd1306
# ==========================================
# CONFIGURATION
# ==========================================
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""
API_URL = "https://shops-webster-beautifully-bedrooms.trycloudflare.com/sensor-data"
# ==========================================
# CONNECT TO WIFI
# ==========================================
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
print("Connecting to WiFi...")
wifi.connect(WIFI_SSID, WIFI_PASSWORD)
while not wifi.isconnected():
print("Waiting for WiFi...")
time.sleep(1)
print("WiFi Connected!")
print("IP Address:", wifi.ifconfig()[0])
# ==========================================
# SENSOR SETUP
# ==========================================
# DHT22 - Temperature + Humidity
dht_sensor = dht.DHT22(Pin(4))
# Soil moisture simulation - Potentiometer
soil_sensor = ADC(Pin(34))
soil_sensor.atten(ADC.ATTN_11DB)
# Light sensor
light_sensor = ADC(Pin(35))
light_sensor.atten(ADC.ATTN_11DB)
# ==========================================
# OLED SETUP
# ==========================================
i2c = I2C(
0,
scl=Pin(22),
sda=Pin(21),
freq=400000
)
oled = ssd1306.SSD1306_I2C(
128,
64,
i2c
)
print()
print("================================")
print("SMART AGRICULTURE SYSTEM STARTED")
print("================================")
print()
# ==========================================
# MAIN LOOP
# ==========================================
while True:
try:
# ----------------------------------
# READ TEMPERATURE + HUMIDITY
# ----------------------------------
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
# ----------------------------------
# READ SOIL MOISTURE
# ----------------------------------
soil_raw = soil_sensor.read()
soil_percent = int(
(soil_raw / 4095) * 100
)
soil_percent = max(
0,
min(100, soil_percent)
)
# ----------------------------------
# READ LIGHT
# ----------------------------------
light_raw = light_sensor.read()
light_percent = int(
(light_raw / 4095) * 100
)
light_percent = max(
0,
min(100, light_percent)
)
# ==================================
# DISPLAY IN SERIAL MONITOR
# ==================================
print("------------------------------")
print("SENSOR READINGS")
print("------------------------------")
print("Temperature :", temperature, "C")
print("Humidity :", humidity, "%")
print("Soil :", soil_percent, "%")
print("Light :", light_percent, "%")
# ==================================
# DISPLAY ON OLED
# ==================================
oled.fill(0)
oled.text(
"SMART AGRI",
0,
0
)
oled.text(
"Temp: {:.1f} C".format(temperature),
0,
16
)
oled.text(
"Hum : {:.1f} %".format(humidity),
0,
28
)
oled.text(
"Soil: {} %".format(soil_percent),
0,
40
)
oled.text(
"Light:{} %".format(light_percent),
0,
52
)
oled.show()
# ==================================
# CREATE JSON PAYLOAD
# ==================================
payload = {
"temperature": temperature,
"humidity": humidity,
"soil_moisture": soil_percent,
"light": light_percent
}
print()
print("Sending to FastAPI...")
print(payload)
# ==================================
# SEND DATA
# ==================================
response = None
try:
response = urequests.post(
API_URL,
json=payload
)
print(
"Server Status:",
response.status_code
)
print(
"Server Response:",
response.text
)
except Exception as api_error:
print(
"API ERROR:",
api_error
)
finally:
if response is not None:
response.close()
except Exception as sensor_error:
print(
"SENSOR ERROR:",
sensor_error
)
print()
print("Next reading in 5 seconds...")
print()
time.sleep(5)