from umqttsimple import MQTTClient
from machine import Pin, SoftI2C
from time import sleep
import network
import random
from i2c_lcd import I2cLcd 

i2c = SoftI2C(scl=Pin(18), sda=Pin(19), freq=400000)
#print(i2c.scan())
lcd = I2cLcd(i2c, 0x27, 2, 16)

def callbackF(topic, payload):
    if topic.decode() == 'name':
      lcd.clear()
      print (payload.decode())
      lcd.putstr(payload.decode())
#      sleep(2)

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('Wokwi-GUEST', '')
while not wlan.isconnected():
  sleep(0.1)

mqttc = MQTTClient(str(random.random()), 'broker.emqx.io', 1883, user='', password='',keepalive=60)
mqttc.connect()
mqttc.set_callback(callbackF)
mqttc.subscribe('name')
lcd.putstr('Waiting for MQTT message ...')

print ('Waiting for MQTT messages')

while True:
    mqttc.check_msg()
    sleep(0.05)
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL