from machine import Pin, ADC, I2C
import time
import utime
from i2c_lcd import I2cLcd
import network
import ujson
from umqtt.simple import MQTTClient
# Pada project ini kita ibaratkan lampu LED adalah pump karena tidak ada komponen pump di Wokwi
# Power untuk pump sebenarnya adalah power adaptor eksternal DC 12V
SOIL_MOISTURE_PIN = 34
RELAY_PIN = 14
LOWER_LIMIT = 30
UPPER_LIMIT = 50
moist_adc = ADC(Pin(SOIL_MOISTURE_PIN))
moist_adc.width(ADC.WIDTH_12BIT)
moist_adc.atten(ADC.ATTN_11DB)
relay = Pin(RELAY_PIN, Pin.OUT)
AddressOfLcd = 0x27
i2c = I2C(scl=Pin(22), sda=Pin(23))
lcd = I2cLcd(i2c, AddressOfLcd, 2, 16)
MQTT_CLIENT_ID = "kipas-angin"
MQTT_BROKER = "mqtt-dashboard.com"
MQTT_USER = ""
MQTT_PASSWORD = ""
MQTT_TOPIC = "wokwi-kipas"
MQTT_TOPIC_MANUAL_MODE = "dashboard"
MQTT_MESSAGE_MANUAL_MODE = "manual mode"
MQTT_MESSAGE_AUTO_MODE = "auto mode"
manual_mode_message = None
def connect_wifi():
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
connect_wifi()
print("Connecting to MQTT server... ", end="")
client = MQTTClient(client_id=MQTT_CLIENT_ID, server=MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD, keepalive=60)
time.sleep(5)
def on_message(topic, msg):
global manual_mode_message
received_message = msg.decode('utf-8')
if received_message == MQTT_MESSAGE_MANUAL_MODE:
manual_mode_message = received_message
print("Changed to manual mode")
relay.value(1)
elif received_message == MQTT_MESSAGE_AUTO_MODE:
manual_mode_message = None
print("Changed to auto mode")
aquasense(moisture_level, pump_status)
def connect_mqtt():
while True:
try:
client.connect()
client.set_callback(on_message)
print("Connected to MQTT server!")
break
except Exception as e:
print("Connection to MQTT server failed. Error:", repr(e))
time.sleep(5)
connect_mqtt()
def read_soil_moisture():
moisture_value = moist_adc.read()
moisture_percentage = ((moisture_value - 3620) / (1680 - 3620)) * 100
return moisture_percentage
def get_pump_status(relay_value):
if relay_value:
return "Watering"
else:
return "Pump is off"
def aquasense(moisture, status):
if moisture < LOWER_LIMIT:
relay.value(1)
lcd.move_to(0, 1)
lcd.putstr(status)
elif moisture > UPPER_LIMIT:
relay.value(0)
lcd.move_to(0, 1)
lcd.putstr(status)
while True:
try:
try:
client.check_msg()
except Exception as e:
print("Error checking MQTT messages:", repr(e))
connect_mqtt()
moisture_level = read_soil_moisture()
pump_status = get_pump_status(relay.value())
current_time = utime.localtime()
timestamp = "{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(
current_time[0], current_time[1], current_time[2],
current_time[3], current_time[4], current_time[5]
)
if manual_mode_message is None:
aquasense(moisture_level, pump_status)
lcd.clear()
lcd.putstr("Moisture: {:.2f}%".format(moisture_level))
lcd.move_to(0, 1)
lcd.putstr(pump_status)
message = ujson.dumps({
"timestamp": timestamp,
"moisture_level": moisture_level,
"status": pump_status
})
client.publish(MQTT_TOPIC, message)
client.subscribe(MQTT_TOPIC_MANUAL_MODE)
time.sleep(1)
except Exception as e:
print("An error occurred:", repr(e))
connect_wifi()
time.sleep(5)
conn.close()