def start_process(): mac = ubinascii.hexlify(network.WLAN().config('mac'), ':').decode() try: set_wifi() client = MQTTClient("umqtt_client", "10.0.0.233") connect_server(client) print("-------press ctrl-c to stop the polling") # create and publish control message control_message = {"mac": mac} client.publish(MQTT_CONTROL_CHANNEL, json.dumps(control_message)) print("Publish control message : " + str(control_message)) print() time.sleep(5) client.check_msg() # process configuration sensors = process_configuration() while True: for sensor in sensors: publish_data_message(client, sensor) # catches the ctrl-c command, which breaks the loop above except KeyboardInterrupt: print("Continuous polling stopped") finally: client.disconnect()
def main(server=SERVER): c = MQTTClient(CLIENT_ID, server, user='******', password='******', keepalive=alive_time) c.set_callback(sub_cb) c.connect() c.subscribe(TOPIC1) c.subscribe(TOPIC2) c.subscribe(TOPIC3) c.subscribe(TOPIC4) # retain the last published value by MQTT broker c.publish(b"icreate/feeds/wecreate.relay1/get", b"hello") c.publish(b"icreate/feeds/wecreate.relay2/get", b"hello") c.publish(b"icreate/feeds/wecreate.relay3/get", b"hello") c.publish(b"icreate/feeds/wecreate.relay4/get", b"hello") print("Connected to %s, subscribed to %s, %s, %s and %s topics" % (server, TOPIC1, TOPIC2, TOPIC3, TOPIC4)) # function to continuously ping the MQTT broker in order to keep the connection alive def ping_wait(): while True: for i in range(alive_time): time.sleep(1) c.ping() print("Pinging...") #for i in range(4): #pingLed.value(not pingLed.value()) #time.sleep(0.1) # start a thread to ping the broker th.start_new_thread(ping_wait, ()) try: while True: c.wait_msg() except OSError as e: print("Reconnecting...") machine.reset() finally: c.disconnect()
def ler(): Ok.on() try: mqtt = MQTTClient(client_id, config.aws_ip, user=config.mqtt_user, password=config.mqtt_pass) mqtt.set_callback(sub_cb) mqtt.connect() mqtt.subscribe("esp32/pin1") mqtt.subscribe("esp32/red") mqtt.subscribe("esp32/green") mqtt.subscribe("esp32/blue") while True: mqtt.wait_msg() time.sleep(1) mqtt.disconnect() except Exception as e: print("Falha!", e) Ok.off() time.sleep(10) machine.reset()
class iotNetwork: netCfg = None wdogTime = 5000 # ms tim = machine.Timer(-1) netConnected = False wdogTriggered = False def __init__(self, w=5000): self.wdogTime = w cfgFile = open('iotdb.db', 'r+b') self.netCfg = btree.open(cfgFile) def wdog(self, b): print("Wdog fired.") self.wdogTriggered = True machine.reset() def connect(self): print("Connect") self.tim.init(period=self.wdogTime, mode=machine.Timer.ONE_SHOT, callback=self.wdog) self.sta_if = network.WLAN(network.STA_IF) if self.sta_if.isconnected(): print("Already connected, disconecting ...") self.sta_if.disconnect() print("... done.") print('connecting to network...') self.sta_if.active(True) essid = (self.netCfg[b"ESSID"]).decode() password = (self.netCfg[b"PASSWD"]).decode() self.sta_if.connect(essid, password) while not self.sta_if.isconnected(): pass print("... Connected") self.tim.init(period=-1, mode=machine.Timer.ONE_SHOT) def disconnect(self): print("Disconnect") self.sta_if.disconnect() print("Disconnect") count = 0 while self.sta_if.isconnected(): count += 1 print(count) print("Disconnected") def connectMQTT(self): print("MQTT...") self.tim.init(period=self.wdogTime, mode=machine.Timer.ONE_SHOT, callback=self.wdog) mqttHost = (self.netCfg[b"MQTT_HOST"]).decode() mqttPort = int((self.netCfg[b"PORT"]).decode()) self.base = (self.netCfg[b"MQTT_BASE"]).decode() clientId = ubinascii.hexlify(machine.unique_id()) try: self.client = MQTTClient(clientId, mqttHost) self.client.connect() except: sleep(10) self.tim.init(period=-1, mode=machine.Timer.ONE_SHOT) print("...Done") def checkMQTT(self): self.client.check_msg() def disconnectMQTT(self): print("MQTT Disconnect") self.client.disconnect() print("MQTT Disconnected") def publishMQTT(self, topic, message): print("Publish: " + topic + "->" + message) print(self.base + topic, message) self.client.publish(self.base + topic, message) def subscribeMQTT(self, topic, cb): self.client.set_callback(cb) self.client.subscribe(self.base + topic) print("Subscribing to " + self.base + topic) def ifconfig(self): print(self.sta_if.ifconfig()) def getIP(self): n = self.sta_if.ifconfig() return (n[0])
def restart_and_reconnect(): print('Ongelmia. Boottaillaan.') machine.reset() # resetoidaan try: client.connect() except OSError: print("Ei voida yhdistaa mqtt! ") restart_and_reconnect() while True: lampo = None kosteus = None try: lampo, kosteus = lue_lampo_kosteus() except TypeError: pass akkutila = lue_akkujannite() if (lampo is not None) and (kosteus is not None) and akkutila > 0: laheta_arvot_mqtt(lampo, kosteus, akkutila) try: client.disconnect() except OSError: pass except KeyboardInterrupt: raise print("Nukkumaan %s millisekunniksi!" % NUKKUMIS_AIKA) machine.deepsleep(NUKKUMIS_AIKA)