import machine
import network
import time
import dht
import urequests
print("--- System Booting Up ---") # THIS GUARANTEES THE SERIAL MONITOR SHOWS SOMETHING
# Try to import OLED display library, catch error if missing
try:
import ssd1306
oled_connected = True
print("OLED Library Loaded.")
except ImportError:
oled_connected = False
print("WARNING: ssd1306 library missing! Display will not work, but simulation will continue.")
# --- Configuration ---
WIFI_SSID = 'Wokwi-GUEST'
WIFI_PASS = ''
THINGSPEAK_API_KEY = 'YOUR_THINGSPEAK_API_KEY'
IOT_INTERVAL_MS = 15000
# --- Hardware Initialization ---
print("Initializing Hardware...")
if oled_connected:
i2c = machine.I2C(0, scl=machine.Pin(22), sda=machine.Pin(21))
display = ssd1306.SSD1306_I2C(128, 64, i2c)
sensor_dht = dht.DHT22(machine.Pin(15))
adc_v = machine.ADC(machine.Pin(34))
adc_c = machine.ADC(machine.Pin(35))
adc_ldr_left = machine.ADC(machine.Pin(32))
adc_ldr_right = machine.ADC(machine.Pin(33))
adc_v.atten(machine.ADC.ATTN_11DB)
adc_c.atten(machine.ADC.ATTN_11DB)
adc_ldr_left.atten(machine.ADC.ATTN_11DB)
adc_ldr_right.atten(machine.ADC.ATTN_11DB)
servo = machine.PWM(machine.Pin(13), freq=50)
def set_servo_angle(angle):
min_duty = 1638
max_duty = 8192
duty = int(min_duty + (max_duty - min_duty) * (angle / 180))
servo.duty_u16(duty)
def connect_wifi():
print("Connecting to WiFi...", end="")
if oled_connected:
display.fill(0)
display.text("Connecting WiFi...", 0, 10)
display.show()
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(WIFI_SSID, WIFI_PASS)
# Wait for connection
while not sta_if.isconnected():
time.sleep(0.5)
print(".", end="")
print("\nWiFi Connected Successfully!")
if oled_connected:
display.fill(0)
display.text("WiFi Connected!", 0, 10)
display.show()
time.sleep(1.5)
# --- Main Program Setup ---
connect_wifi()
servo_angle = 90
set_servo_angle(servo_angle)
last_iot_time = time.ticks_ms()
print("--- Starting Main Loop ---")
# --- Main Loop ---
while True:
try:
sensor_dht.measure()
temp = sensor_dht.temperature()
hum = sensor_dht.humidity()
except OSError:
temp, hum = 0.0, 0.0
voltage = (adc_v.read() / 4095.0) * 24.0
current = (adc_c.read() / 4095.0) * 5.0
power = voltage * current
ldr_left = adc_ldr_left.read()
ldr_right = adc_ldr_right.read()
diff = ldr_left - ldr_right
if abs(diff) > 200:
if ldr_left > ldr_right and servo_angle < 180:
servo_angle += 2
elif ldr_right > ldr_left and servo_angle > 0:
servo_angle -= 2
set_servo_angle(servo_angle)
health_status = "OPTIMAL"
if temp > 45.0:
health_status = "ERR: OVERHEAT"
elif voltage < 5.0 and (ldr_left > 2000 or ldr_right > 2000):
health_status = "ERR: LOW EFF"
if oled_connected:
display.fill(0)
display.text(f"Temp: {temp:.1f}C H: {hum:.0f}%", 0, 0)
display.text(f"V: {voltage:.1f}V I: {current:.2f}A", 0, 12)
display.text(f"Power: {power:.1f}W", 0, 24)
display.text(f"Angle: {servo_angle} deg", 0, 36)
display.text(f"Status: {health_status}", 0, 48)
display.show()
# FORCE PRINT TO SERIAL MONITOR EVERY LOOP
print(f"V:{voltage:.1f}V | I:{current:.2f}A | P:{power:.1f}W | T:{temp:.1f}C | Angle:{servo_angle} | Status:{health_status}")
if time.ticks_diff(time.ticks_ms(), last_iot_time) > IOT_INTERVAL_MS:
print("Sending data to Cloud...")
url = f"http://api.thingspeak.com/update?api_key={THINGSPEAK_API_KEY}&field1={voltage}&field2={current}&field3={power}&field4={temp}"
try:
response = urequests.get(url)
print("--> IoT Update Success. Code:", response.status_code)
response.close()
except Exception as e:
print("--> IoT Update Failed:", e)
last_iot_time = time.ticks_ms()
time.sleep(0.1)Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306