from machine import Pin, I2C, ADC, PWM
import ssd1306
import time
import network
import BlynkLib
print(" Running the system")
print(" GOOD LUCK ")
ssid = "Wokwi-GUEST"
password = ""
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssid, password)
print("Connecting to WiFi...")
while not wifi.isconnected():
time.sleep(0.5)
print("Connected! IP:", wifi.ifconfig()[0])
BLYNK_TEMPLATE_ID = "TMPL6cjFKR8GD"
BLYNK_TEMPLATE_NAME = "Smart Soil Moisture Watering System"
BLYNK_AUTH = "BHB5g-3LiNEs18ezDgQDxo1nfPdAs1Pr"
blynk = BlynkLib.Blynk(BLYNK_AUTH)
buzzer = PWM(Pin(26))
soil_sensor = ADC(Pin(34))
orangeled = Pin(32, Pin.OUT)
yellowled = Pin(33, Pin.OUT)
greenled = Pin(25, Pin.OUT)
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
last_send_time = 0
while True:
blynk.run()
sensorValue = soil_sensor.read()
humidityPercent = int((sensorValue - 2165) / (3135 - 2165) * 100)
humidityPercent = max(0, min(humidityPercent, 100))
if humidityPercent < 40:
print("KEKERINGAN")
orangeled.on()
yellowled.off()
greenled.off()
buzzer.init(freq=400, duty=50)
time.sleep(0.1)
buzzer.init(freq=1, duty=0)
elif humidityPercent > 60:
print("KELEBIHAN")
orangeled.off()
yellowled.off()
greenled.on()
buzzer.init(freq=2000, duty=50)
time.sleep(0.1)
buzzer.init(freq=1, duty=0)
else:
print("BAGUS")
orangeled.on()
yellowled.on()
greenled.on()
buzzer.init(freq=1, duty=0)
oled.fill(0)
oled.text("Soil Moisture", 0, 0)
oled.text("Percentage: {}%".format(humidityPercent), 0, 20)
oled.show()
current_time = time.time()
if current_time - last_send_time >= 5:
try:
blynk.virtual_write(1, humidityPercent)
print("Sent to Blynk: {}%".format(humidityPercent))
last_send_time = current_time
except Exception as e:
print("Failed to send:", e)
time.sleep(0.1)