import time
import machine
from time import sleep
from machine import I2C, Pin, ADC
from machine import ADC, Pin
from i2c_lcd import I2cLcd
from umqtt.simple import MQTTClient
import ujson
import network
# MQTT Server Parameters
MQTT_CLIENT_ID = "Wokwi-GUEST"
MQTT_BROKER = "broker.emqx.io"
MQTT_USER = "Wokwi-GUEST"
MQTT_PASSWORD = ""
MQTT_TOPIC = "exams/Asoukidis/illumination/"
# 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 display
AddressOfLcd = 0x27
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, AddressOfLcd, 2, 16)
print("display is ready")
print("PROGRAM START")
last_lux = ""
lux =Pin(25,Pin.IN)
while True:
light = lux.value()
print("Illumination:", light, "LUX")
lcd.move_to(0,0)
lcd.putstr("Illumination:")
lcd.move_to(5,1)
lcd.putstr(str(light)+ "LUX")
message = ujson.dumps({
"Asoukidis/illumination": light,
})
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message))
client.publish(MQTT_TOPIC, message)
last_lux = light
sleep(5)