from machine import Pin
from time import sleep

led = Pin(14, Pin.OUT)
led.value(0)

def sub_cb(topic, msg):
  print((topic, msg))
  if msg == b'ON':
      #print('On is received')
      led.value(1)
      client.publish(topic_pub, b'Light is Turned On')
  if msg == b'OFF':
      #print('Off is received')
      led.value(0)
      client.publish(topic_pub, b'Light is Turned Off')

def connect_and_subscribe():
  global client_id, mqtt_server, topic_sub
  client = MQTTClient(client_id, mqtt_server)
  client.set_callback(sub_cb)
  client.connect()
  client.subscribe(topic_sub)
  print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))
  return client

def restart_and_reconnect():
  print('Failed to connect to MQTT broker. Reconnecting...')
  time.sleep(10)
  machine.reset()

try:
  client = connect_and_subscribe()
except OSError as e:
  restart_and_reconnect()

while True:
  try:
    new_message = client.check_msg()
    sleep(1)
  except OSError as e:
    restart_and_reconnect()