from machine import Pin, SoftI2C
import utime as time
import ujson
import network
from umqtt.simple import MQTTClient
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
MQTT_BROKER = "broker.mqttdashboard.com"
MQTT_PORT = 1883
MQTT_CLIENT = "user1"
MQTT_TELEMETRY_TOPIC = "iot/telemetry"
MQTT_CONTROL_TOPIC = "iot/control"
MQTT_REQUEST_TOPIC = "iot/request"
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""
def connect_to_wifi():
wifi_client = network.WLAN(network.STA_IF)
wifi_client.active(True)
print("Connecting device to WiFi")
wifi_client.connect(WIFI_SSID, WIFI_PASSWORD)
while not wifi_client.isconnected():
print("Connecting")
time.sleep(0.1)
print("WiFi Connected!")
print(wifi_client.ifconfig())
return wifi_client
def connect_to_mqtt():
client = MQTTClient(MQTT_CLIENT, MQTT_BROKER, port=MQTT_PORT)
client.connect()
print("MQTT Connected!")
return client
wifi_client = connect_to_wifi()
mqtt_client = connect_to_mqtt()
button_pin_1 = Pin(34, Pin.IN)
led_1 = Pin(5, Pin.OUT) # D5 number is Output
button_pin_2 = Pin(35, Pin.IN)
led_2 = Pin(2, Pin.OUT) # D2 number is Output
button_pin_3 = Pin(32, Pin.IN)
led_3 = Pin(17, Pin.OUT) # D17 number is Output
button_pin_4 = Pin(33, Pin.IN)
led_4 = Pin(16, Pin.OUT) # D16 number is Output
#Slide switch section
slide_switch_pin = Pin(19, Pin.IN)
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
lcd.putstr("ESP32 Load Calc.")
led_state_1 = led_1.value()
led_state_2 = led_2.value()
led_state_3 = led_3.value()
led_state_4 = led_4.value()
last_button_state_1 = 0 # Assuming the first button is not pressed initially
last_button_state_2 = 0 # Assuming the second button is not pressed initially
last_button_state_3 = 0 # Assuming the third button is not pressed initially
last_button_state_4 = 0 # Assuming the fourth button is not pressed initially
last_slide_switch_state = 0;
print("last_button_state_1 =", last_button_state_1, "led_state_1 =", led_state_1)
print("last_button_state_2 =", last_button_state_2, "led_state_2 =", led_state_2)
print("last_button_state_3 =", last_button_state_3, "led_state_3 =", led_state_3)
print("last_button_state_4 =", last_button_state_4, "led_state_4 =", led_state_4)
loadValue = 0
lcd.clear()
lcd.putstr("Load is: " + str(loadValue)+" Kwatt")
def publish_led_status(loadValue):
message = {
"Power" : loadValue,
"type": "sensor",
"device_id": MQTT_CLIENT
}
mqtt_client.publish(MQTT_TELEMETRY_TOPIC, ujson.dumps(message))
def publish_req_status(loadValue):
message = {
"request" : loadValue,
"type": "sensor",
"device_id": MQTT_CLIENT
}
mqtt_client.publish(MQTT_CONTROL_TOPIC, ujson.dumps(message))
try:
while True:
current_button_state_1 = button_pin_1.value()
current_button_state_2 = button_pin_2.value()
current_button_state_3 = button_pin_3.value()
current_button_state_4 = button_pin_4.value()
slide_switch_state = slide_switch_pin.value()
if slide_switch_state != last_slide_switch_state:
print("Switch State: ", slide_switch_state)
if slide_switch_state == 1:
print("Request mode activate!")
if loadValue == 4:
publish_req_status(0)
print("Request load value: 0 kwatt")
lcd.clear()
lcd.putstr(" ")
lcd.putstr("Request load: 0 ")
elif loadValue == 3:
publish_req_status(1)
print("Request load value: 1 kwatt")
lcd.clear()
lcd.putstr(" ")
lcd.putstr("Request load: 1 ")
elif loadValue == 2:
publish_req_status(2)
print("Request load value: 2 kwatt")
lcd.clear()
lcd.putstr(" ")
lcd.putstr("Request load: 2 ")
elif loadValue == 1:
publish_req_status(3)
print("Request load value: 3 kwatt")
lcd.clear()
lcd.putstr(" ")
lcd.putstr("Request load: 3 ")
elif loadValue == 0:
publish_led_status(4)
print("Request load value: 4 kwatt")
lcd.clear()
lcd.putstr(" ")
lcd.putstr("Request load: 4 ")
last_slide_switch_state = slide_switch_state;
if current_button_state_1 != last_button_state_1:
if led_state_1 == 0 and current_button_state_1 == 1:
led_1.on()
loadValue = loadValue + 1
print("LED 1 On")
print("Load Value", loadValue)
lcd.clear()
lcd.putstr("Load is: " + str(loadValue)+" Kwatt")
publish_led_status(loadValue)
print("Publish load to MQTT: ", loadValue)
elif led_state_1 == 1 and current_button_state_1 == 1:
led_1.off()
loadValue = loadValue - 1
print("LED 1 Off")
print("Load Value", loadValue)
lcd.clear()
lcd.putstr("Load is: " + str(loadValue)+" Kwatt")
publish_led_status(loadValue)
print("Publish load to MQTT: ", loadValue)
led_state_1 = led_1.value()
last_button_state_1 = current_button_state_1
if current_button_state_2 != last_button_state_2:
if led_state_2 == 0 and current_button_state_2 == 1:
led_2.on()
loadValue = loadValue + 1
print("LED 2 On")
print("Load Value", loadValue)
lcd.clear()
lcd.putstr("Load is: " + str(loadValue)+" Kwatt")
publish_led_status(loadValue)
print("Publish load to MQTT: ", loadValue)
elif led_state_2 == 1 and current_button_state_2 == 1:
led_2.off()
loadValue = loadValue - 1
print("LED 2 Off")
print("Load Value", loadValue)
lcd.clear()
lcd.putstr("Load is: " + str(loadValue)+" Kwatt")
publish_led_status(loadValue)
print("Publish load to MQTT: ", loadValue)
led_state_2 = led_2.value()
last_button_state_2 = current_button_state_2
if current_button_state_3 != last_button_state_3:
if led_state_3 == 0 and current_button_state_3 == 1:
led_3.on()
loadValue = loadValue + 1
print("LED 3 On")
print("Load Value", loadValue)
lcd.clear()
lcd.putstr("Load is: " + str(loadValue)+" Kwatt")
publish_led_status(loadValue)
print("Publish load to MQTT: ", loadValue)
elif led_state_3 == 1 and current_button_state_3 == 1:
led_3.off()
loadValue = loadValue - 1
print("LED 3 Off")
print("Load Value", loadValue)
lcd.clear()
lcd.putstr("Load is: " + str(loadValue)+" Kwatt")
publish_led_status(loadValue)
print("Publish load to MQTT: ", loadValue)
led_state_3 = led_3.value()
last_button_state_3 = current_button_state_3
if current_button_state_4 != last_button_state_4:
if led_state_4 == 0 and current_button_state_4 == 1:
led_4.on()
loadValue = loadValue + 1
print("LED 4 On")
print("Load Value", loadValue)
lcd.clear()
lcd.putstr("Load is: " + str(loadValue)+" Kwatt")
publish_led_status(loadValue)
print("Publish load to MQTT: ", loadValue)
elif led_state_4 == 1 and current_button_state_4 == 1:
led_4.off()
loadValue = loadValue - 1
print("LED 4 Off")
print("Load Value", loadValue)
lcd.clear()
lcd.putstr("Load is: " + str(loadValue)+" Kwatt")
publish_led_status(loadValue)
print("Publish load to MQTT: ", loadValue)
led_state_4 = led_4.value()
last_button_state_4 = current_button_state_4
time.sleep(0.1)
# lcd.clear()
except KeyboardInterrupt:
print("Apply keyboard intrrupt")
finally:
mqtt_client.disconnect()
print("MQTT Broker disconnect.")