import sys,time from umqttsimple import MQTTClient import ubinascii import micropython # Put your own settings here obviously ;) mqtt_server = '10.33.64.2' client_id = 'platejsonloader' topic_pub_base = b'hasp/plate_joul' mqttuser = 'MQTTUSER' mqttpass = 'MQTTPASS' mqttkeepalive = 0 state_stable = False counter = 0 def connect_and_subscribe(): global client_id, mqtt_server, mqttuser, mqttpass, mqttkeepalive, state_stable client = MQTTClient(client_id, mqtt_server, 1883 , mqttuser, mqttpass, mqttkeepalive) client.connect() print('Connected to %s MQTT broker' % (mqtt_server)) state_stable = False return client def restart_and_reconnect(): print('Failed to connect to MQTT broker. Reconnecting...') time.sleep_ms(50) #sys.exit(0) try: client = connect_and_subscribe() except OSError as e: restart_and_reconnect() try: # Using readlines() for i in range (0, 13): print("Clearing page %s"%i ) client.publish(b'%s/command/clearpage' % topic_pub_base, b'%s' % i ) time.sleep_ms(5) file1 = open('pages.jsonl', 'r') jsonlines = file1.readlines() count = 0 for jsonl in jsonlines: print("Line{}: {}".format(count, jsonl.strip())) if len(jsonl.strip()) >0 : count+=1 client.publish(b'%s/command/jsonl' % topic_pub_base, b'%s' % jsonl.strip() ) time.sleep_ms(5) # I use this to trigger actions from other scripts that watch this (reinjecting values to be precise) client.publish(b'%s/LWT' % topic_pub_base, b'online') except OSError as e: restart_and_reconnect()