def thread_stats(name): client = mqtt.Client("stats", True) # client.set_will("UNISA/IOT/Gruppo_17/hum", '0', 2, False) # client.set_will("UNISA/IOT/Gruppo_17/light", '0', 2, False) for retry in range(3): try: client.connect("broker.hivemq.com", 60) break except Exception as e: print(e) print(" LOG | MQTT | re-connecting...", retry) if retry >= 2: print(' LOG | MQTT | impossible to connect mqtt server') while True: sleep(10000) print(" LOG | MQTT | connected to MQTT server.") client.loop() # start the mqtt loop while (True): pinToggle(status_led_2) client.publish(str("UNISA/IOT/Gruppo_17/hum"), str(read_hum()), 2) sleep(5000) pinToggle(status_led_2) client.publish(str("UNISA/IOT/Gruppo_17/light"), str(remap(read_light(), 0, 4095, 0, 100)), 2) sleep(5000)
def connect(self): """ .. method:: connect() This method establishes the connection to the WolkAbout IoT platform. If there are actuators it will subscribe to topics that will contain actuator commands and also starts a loop to handle inbound messages. Raises an exception if the connection failed. """ if self.connected: return self.client = mqtt.Client(client_id=self.device.key, clean_session=True) self.client.set_username_pw(self.device.key, self.device.password) self.client.set_will("lastwill/" + self.device.key, "Gone offline", 2, False) try: self.client.connect(self.host, keepalive=60, port=self.port) self.topics = [] self.topics.append( ["service/commands/firmware/" + self.device.key, 0]) self.topics.append(["service/commands/file/" + self.device.key, 0]) self.topics.append(["service/commands/url/" + self.device.key, 0]) self.topics.append(["service/binary/" + self.device.key, 0]) self.topics.append(["pong/" + self.device.key, 0]) self.topics.append( ["configurations/commands/" + self.device.key, 0]) if self.device.actuator_references: for actuator_reference in self.device.actuator_references: topic = [ "actuators/commands/" + self.device.key + "/" + actuator_reference, self.qos ] self.topics.append(topic) self.client.subscribe(self.topics) self.client.on(mqtt.PUBLISH, self.on_mqtt_message) self.client.loop() self.connected = True except Exception as e: raise e
sleep(1000) print("STARTING...") #Main Function try: # Device UID and TOKEN can be created in the ADM panel zapp = zerynthapp.ZerynthApp("p4ZSd_21TIyMjpQE3E5P9g", "QP1MfuJzS6qhh40QI59AMQ", log=True) # Start the Zerynth app instance! zapp.run() #connection wifi/mosquito wifi_connect() #create instance for MQTT connection client = mqtt.Client(MQTT_CLIENTID, True) # and try to connect to "test.mosquitto.org" for retry in range(5): try: client.connect("broker.mqtt-dashboard.com", 60, aconnect_cb=print_connectOK) break except Exception as e: print(e) print("re-connecting...", retry) if retry >= 5: print('imposible to connect mqtt server') while True: sleep(1000)
def send_sample(obj): #função que publica os dados de temperatura print("publishing: ", obj) client.publish("temp/room", str(obj)) def publish_to_self(): client.publish( "desktop/samples", "hello! " + str(random(0, 10))) #quando não consegue enviar dado de temperatura. try: # define a identificação do cliente mqtt que no caso é nosso ESP8266 client = mqtt.Client("zerynth-mqtt", True) # and try to connect to "test.mosquitto.org" #aqui tentamos se conectar ao servidor mqtt(broker) através da url abaixo e a porta 60 for retry in range(10): try: client.connect("test.mosquitto.org", 60) break except Exception as e: print("connecting...") print("connected.") #aqui o ESP8266 assina os tópicos abaixo client.subscribe([["desktop/samples", 1]]) client.subscribe([["desktop/others", 2]]) #habilita a publicação de dados
if ('message' in data): print("is master reset: " + data['message'].topic) print(data['message'].payload) return (data['message'].payload == "Master: Actor-1 reset.") # Funktion zum ausführen eines resets und senden der Quittung def reset(client, data): print("reset: " + data['message'].payload) client.publish("iot/Actor-1/status", "Actor-1 is reset.") mcu.reset() try: # mqtt setup my_client = mqtt.Client("actor-1", True) for retry in range(10): try: my_client.connect(config.MQTT_BROKER_IP_ADDRESS, 60) break except Exception as e: print("connecting...") # mqtt subscribtion + für reset my_client.subscribe([["iot/master", 1]]) my_client.on(mqtt.PUBLISH, reset, is_reset) # Bereitschaft signalisieren my_client.publish("iot/Actor-1/status", "Actor-1 is ready.") my_client.loop()
######################################### # if the temperature exceeds this value the cooling fan will be turned on MAX_TEMP = 37.00 # when the fan is on and temperature reaches this value, the fan turns off MIN_TEMP = 32.00 # time (in seconds) the device can remain uncovered with a high value of brightness UNCOVER_TIME = 10 # time (in seconds) the device has to remain covered COVER_TIME = 8 # time (in seconds) between each iteration of the main loop SLEEP_TIME = 2 BROKER = "test.mosquitto.org" client = mqtt.Client("iot-marco-esp32", True) # temperature and humidity sensor htu = htu21d.HTU21D(I2C0) # cooling fan controlled by a relay on pin D23 fan = myFan.Fan(D23, client) # cardboard panel attached to a servo motor controlled by pin D15 sunshield = mySunshield.Sunshield(D15.PWM, client) # led to activate if the room is too dark led = myLed.Led(D16, client)