# University of Peloponnese - Department of Electrical and Computer Engineering
# Postgraduate Programme - Modern Applications of Electric Power Systems
# Winter semester 2023-2024
# Ε103 - Μicrocontrollers & Ιnformation Τransfer
# Main Project - Dynamic Load Management
# Andreas Asoukidis - Electrical Engineer
# Libraries
import time
from time import sleep
from machine import I2C, Pin, ADC
from i2c_lcd import I2cLcd
import dht
import math
from umqtt.simple import MQTTClient
import ujson
import network
# MQTT Server Parameters
MQTT_CLIENT_ID = "Wokwi-GUEST"
MQTT_BROKER = "broker.emqx.io" ### WARNING ###
MQTT_USER = "Wokwi-GUEST" ### WARNING ###
MQTT_PASSWORD = "" ### WARNING ###
MQTT_TOPIC = "iot/telemetry"
MQTT_TOPIC1 = "iot/request"
MQTT_TOPIC2 = "iot/control"
# WiFi connection
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!")
# MQTT server connection
print("Connecting to MQTT server... ", end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
client.connect()
print("Connected!")
# Settings of buttons & switch
red_button = Pin(32, Pin.IN)
yellow_button = Pin(33, Pin.IN)
green_button = Pin(34, Pin.IN)
blue_button = Pin(35, Pin.IN)
switch = Pin(12, Pin.IN)
last_red_button_state = 0
last_yellow_button_state = 0
last_green_button_state = 0
last_blue_button_state = 0
print("buttons & swicth are ready")
# Settings of leds
red_led = Pin(26, Pin.OUT)
yellow_led = Pin(25, Pin.OUT)
green_led = Pin(14, Pin.OUT)
blue_led = Pin(27, Pin.OUT)
print("leds are ready")
# Settings of display
AddressOfLcd = 0x27
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, AddressOfLcd, 2, 16)
print("display is ready")
# Settings of sensor
sensor = dht.DHT22(Pin(2))
print("sensor is ready")
time.sleep(0.5)
print("PROGRAM START")
prev_total_power = ""
lcd.move_to(0,0)
lcd.putstr("All leds are OFF")
lcd.move_to(0,1)
lcd.putstr("Total Power: 0kW")
while True:
red_led_state = red_led.value()
yellow_led_state = yellow_led.value()
green_led_state = green_led.value()
blue_led_state = blue_led.value()
a=red_led_state
b=yellow_led_state
c=green_led_state
d=blue_led_state
R1="ON"
R2="OFF"
current_red_button_state = red_button.value()
current_yellow_button_state = yellow_button.value()
current_green_button_state = green_button.value()
current_blue_button_state = blue_button.value()
sw_state = switch.value()
if sw_state == 1: ### normal mode ###
if current_red_button_state != last_red_button_state:
if red_led_state == 0 and current_red_button_state == 1:
red_led.on()
print("red led ON")
a=not(red_led_state)
b=yellow_led_state
c=green_led_state
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" red led: ON")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
elif red_led_state == 1 and current_red_button_state == 1:
red_led.off()
print("red led OFF")
a=not(red_led_state)
b=yellow_led_state
c=green_led_state
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" red led: OFF")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
red_led_state = red_led.value()
last_red_button_state = current_red_button_state
if current_yellow_button_state != last_yellow_button_state:
if yellow_led_state == 0 and current_yellow_button_state == 1:
yellow_led.on()
print("yellow led ON")
a=red_led_state
b=not(yellow_led_state)
c=green_led_state
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" yellow led: ON")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
elif yellow_led_state == 1 and current_yellow_button_state == 1:
yellow_led.off()
print("yellow led OFF")
a=red_led_state
b=not(yellow_led_state)
c=green_led_state
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" yellow led: OFF")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
yellow_led_state = yellow_led.value()
last_yellow_button_state = current_yellow_button_state
if current_green_button_state != last_green_button_state:
if green_led_state == 0 and current_green_button_state == 1:
green_led.on()
print("green led ON")
a=red_led_state
b=yellow_led_state
c=not(green_led_state)
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" green led: ON")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
elif green_led_state == 1 and current_green_button_state == 1:
green_led.off()
print("green led OFF")
a=red_led_state
b=yellow_led_state
c=not(green_led_state)
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" green led: OFF")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
green_led_state = green_led.value()
last_green_button_state = current_green_button_state
if current_blue_button_state != last_blue_button_state:
if blue_led_state == 0 and current_blue_button_state == 1:
blue_led.on()
print("blue led ON")
a=red_led_state
b=yellow_led_state
c=green_led_state
d=not(blue_led_state)
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" blue led: ON")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
elif blue_led_state == 1 and current_blue_button_state == 1:
blue_led.off()
print("blue led OFF")
a=red_led_state
b=yellow_led_state
c=green_led_state
d=not(blue_led_state)
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" blue led: OFF")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
blue_led_state = blue_led.value()
last_blue_button_state = current_blue_button_state
X=(a+b+c+d)*2 ### total power calculation ###
message = ujson.dumps({
"Asoukidis_A./Total_Power_kW": X,
})
if message != prev_total_power:
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message,))
client.publish(MQTT_TOPIC, message)
prev_total_power = message
elif sw_state == 0: ### on demand response mode ###
Y = 2*(current_red_button_state or current_yellow_button_state or current_green_button_state or current_blue_button_state)
if current_red_button_state != last_red_button_state: ### red load request ###
if red_led_state == 0 and current_red_button_state == 1 :
last_red_button_state = current_red_button_state
sleep(0.5)
message1 = ujson.dumps({
"Asoukidis_A./Red_Load_Request_kW": Y,
})
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC1, message1,))
client.publish(MQTT_TOPIC1, message1)
if last_red_button_state == 1 and red_led_state == 0:
def Aggregator_response(topic, msg):
if msg.decode() == 'on':
red_led.on()
print("red led ON")
a=not(red_led_state)
b=yellow_led_state
c=green_led_state
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" red led: ON")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
if msg.decode() == 'off':
print("red led OFF")
red_led.off()
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message,))
client.publish(MQTT_TOPIC, message)
client.set_callback(Aggregator_response)
client.subscribe(MQTT_TOPIC2)
if current_yellow_button_state != last_yellow_button_state: ### yellow load request ###
if yellow_led_state == 0 and current_yellow_button_state == 1:
last_yellow_button_state = current_yellow_button_state
sleep(0.5)
message1 = ujson.dumps({
"Asoukidis_A./Yellow_Load_Request_kW": Y,
})
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC1, message1,))
client.publish(MQTT_TOPIC1, message1)
if last_yellow_button_state == 1 and yellow_led_state == 0:
def Aggregator_response(topic, msg):
if msg.decode() == 'on':
yellow_led.on()
print("yellow led ON")
a=red_led_state
b=not(yellow_led_state)
c=green_led_state
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" yellow led: ON")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
if msg.decode() == 'off':
print("yellow led OFF")
yellow_led.off()
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message,))
client.publish(MQTT_TOPIC, message)
client.set_callback(Aggregator_response)
client.subscribe(MQTT_TOPIC2)
if current_green_button_state != last_green_button_state: ### green load request ###
if green_led_state == 0 and current_green_button_state == 1:
last_green_button_state = current_green_button_state
sleep(0.5)
message1 = ujson.dumps({
"Asoukidis_A./Green_Load_Request_kW": Y,
})
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC1, message1,))
client.publish(MQTT_TOPIC1, message1)
if last_green_button_state == 1 and green_led_state == 0:
def Aggregator_response(topic, msg):
if msg.decode() == 'on':
green_led.on()
print("green led ON")
a=red_led_state
b=yellow_led_state
c=not(green_led_state)
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" green led: ON")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
if msg.decode() == 'off':
print("green led OFF")
green_led.off()
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message,))
client.publish(MQTT_TOPIC, message)
client.set_callback(Aggregator_response)
client.subscribe(MQTT_TOPIC2)
if current_blue_button_state != last_blue_button_state: ### blue load request ###
if blue_led_state == 0 and current_blue_button_state == 1:
last_blue_button_state = current_blue_button_state
sleep(0.5)
message1 = ujson.dumps({
"Asoukidis_A./Blue_Load_Request_kW": Y,
})
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC1, message1,))
client.publish(MQTT_TOPIC1, message1)
if last_blue_button_state == 1 and blue_led_state == 0:
def Aggregator_response(topic, msg):
if msg.decode() == 'on':
blue_led.on()
print("blue led ON")
a=red_led_state
b=yellow_led_state
c=green_led_state
d=not(blue_led_state)
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" blue led: ON")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
if msg.decode() == 'off':
print("blue led OFF")
blue_led.off()
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message,))
client.publish(MQTT_TOPIC, message)
client.set_callback(Aggregator_response)
client.subscribe(MQTT_TOPIC2)
X=(a+b+c+d)*2 ### total power calculation ###
message = ujson.dumps({
"Asoukidis_A./Total_Power_kW": X,
})
if message != prev_total_power:
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message,))
client.publish(MQTT_TOPIC, message)
prev_total_power = message
def Aggregator_response(topic, msg):
if msg.decode() == "status": ### status control by Aggregator ###
a=red_led_state
b=yellow_led_state
c=green_led_state
d=blue_led_state
X=(a+b+c+d)*2
if red_led_state == 1:
S1 = (R1)
elif red_led_state == 0:
S1 = (R2)
if yellow_led_state == 1:
S2 = (R1)
elif yellow_led_state == 0:
S2 = (R2)
if green_led_state == 1:
S3 = (R1)
elif green_led_state == 0:
S3 = (R2)
if blue_led_state == 1:
S4 = (R1)
elif blue_led_state ==0:
S4 = (R2)
message = ujson.dumps({
"Asoukidis_A./Total_Power_kW": X,
})
status_message = ujson.dumps({
"green_load":S3,
"blue_load":S4,
"Asoukidis_A./Status_report: "
"red_load":S1,
"yellow_load":S2,
})
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message,))
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, status_message))
client.publish(MQTT_TOPIC, message)
client.publish(MQTT_TOPIC, status_message)
if msg.decode() == 'Asoukidis/red_load_on' and red_led_state == 0: ### loads control by Aggregator ###
red_led.on()
print("red led on")
a=not(red_led_state)
b=yellow_led_state
c=green_led_state
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" red led: ON")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
elif msg.decode() == 'Asoukidis/red_load_off' and red_led_state == 1:
red_led.off()
print("red led off")
a=not(red_led_state)
b=yellow_led_state
c=green_led_state
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" red led: OFF")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
elif msg.decode() == 'Asoukidis/yellow_load_on' and yellow_led_state == 0:
yellow_led.on()
print("yellow led on")
a=red_led_state
b=not(yellow_led_state)
c=green_led_state
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" yellow led: ON")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
elif msg.decode() == 'Asoukidis/yellow_load_off' and yellow_led_state == 1:
yellow_led.off()
print("yellow led off")
a=red_led_state
b=not(yellow_led_state)
c=green_led_state
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" yellow led: OFF")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
elif msg.decode() == 'Asoukidis/green_load_on' and green_led_state == 0:
green_led.on()
print("green led on")
a=red_led_state
b=yellow_led_state
c=not(green_led_state)
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" green led: ON")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
elif msg.decode() == 'Asoukidis/green_load_off' and green_led_state == 1:
green_led.off()
print("green led off")
a=red_led_state
b=yellow_led_state
c=not(green_led_state)
d=blue_led_state
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" green led: OFF")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
elif msg.decode() == 'Asoukidis/blue_load_on' and blue_led_state == 0:
blue_led.on()
print("blue led on")
a=red_led_state
b=yellow_led_state
c=green_led_state
d=not(blue_led_state)
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" blue led: ON")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
elif msg.decode() == 'Asoukidis/blue_load_off' and blue_led_state == 1:
blue_led.off()
print("blue led off")
a=red_led_state
b=yellow_led_state
c=green_led_state
d=not(blue_led_state)
X=(a+b+c+d)*2
lcd.move_to(0,0)
lcd.putstr(" blue led: OFF")
lcd.move_to(0,1)
lcd.putstr("Total Power: "+ str(X) +"kW")
client.set_callback(Aggregator_response)
client.subscribe(MQTT_TOPIC2)
### My code finished here.
### I hope to create new codes in the future!