def run(): global state global connection while True: # Wait for connection while state != CONNECTED: try: state = CONNECTING connection = MQTTClient(DEVICE_ID, server=HOST, port=8883) connection.connect(ssl=True, certfile='/flash/cert/certificate.crt', keyfile='/flash/cert/privateKey.key', ca_certs='/flash/cert/root-CA.cer') state = CONNECTED except: print('Error connecting to the server') time.sleep(0.5) continue print('Connected!') # Subscribe for messages connection.set_callback(_recv_msg_callback) connection.subscribe(TOPIC_DOWNLOAD) while state == CONNECTED: connection.check_msg() msg = '{"Name":"Pycom", "Data":"Test"}' print('Sending: ' + msg) _send_msg(msg) time.sleep(2.0)
def mp_connect(): global mp print("connecting to mqtt broker ...") BROKER = "iot.eclipse.org" mqtt = MQTTClient(server=BROKER) print("mqtt", mqtt) mqtt.connect() mp = PlotClient(mqtt) print("mp", mp)
def test_publishOne(self): #connect #publishone #disconnect #connect #publishone #... #publishone #disconnect topic = "test_topic" message = "test_message" mqtt_client = MQTTClient() mqtt_client.connect() mqtt_client.publish_one(topic, message) mqtt_client.publish_one(topic, message) #raw_input("Wait for restart push server and return.") mqtt_client.publish_one(topic, message) mqtt_client.publish_one(topic, message) mqtt_client.publish_one(topic, message) mqtt_client.start_send() mqtt_client.disconnect()
class Subscribe(base_calvinsys_object.BaseCalvinsysObject): """ Subscribe to data on given MQTT broker """ def init(self, topics, hostname, port=1883, qos=0, client_id='calvinconstrained', will=None, auth=None, tls=None, transport='tcp', payload_only=False, **kwargs): def sub_cb(topic, msg): self.data.append({"topic": topic.decode('utf-8'), "payload": msg}) self.data = [] self.payload_only = payload_only self.topics = topics self.user = None self.password = None self.ssl = False self.ssl_params = None if auth: user = auth.get("username") password = auth.get("password") if tls: print("Using TLS") self.ssl = True key_file = open(tls.get("keyfile"), "r") key = key_file.read() cert_file = open(tls.get("certfile"), "r") cert = cert_file.read() self.ssl_params = {"key": key, "cert": cert} self.c = MQTTClient(client_id, hostname, port=port, user=self.user, password=self.password, ssl=self.ssl, ssl_params=self.ssl_params) self.c.set_callback(sub_cb) self.c.connect() for topic in self.topics: self.c.subscribe(topic.encode("ascii")) def can_write(self): return True def write(self, data=None): pass def can_read(self): try: data = self.c.check_msg() except: return False return bool(self.data) def read(self): data = self.data.pop(0) if self.payload_only: return data.get("payload") else: return data def close(self): self.c.disconnect()
sys.exit(1) mqtt_queue = Queue() ciao_queue = Queue() try: mqttclient = MQTTClient(shd["conf"]["params"], ciao_queue) except Exception, e: logger.critical("Exception while creating MQTTClient: %s" % e) sys.exit(1) signal.signal(signal.SIGINT, signal.SIG_IGN) #ignore SIGINT(ctrl+c) signal.signal(signal.SIGHUP, signal_handler) signal.signal(signal.SIGTERM, signal_handler) if mqttclient.connect(): logger.info("Connected to %s" % shd['conf']['params']['host']) shd["requests"] = {} ciaoclient = MQTTCiao(shd, mqtt_queue, ciao_queue) ciaoclient.start() # endless loop until SIGHUP/SIGTERM while shd["loop"] : if not mqtt_queue.empty(): entry = mqtt_queue.get() logger.debug("Entry %s" % entry) # if entry received from ciao is an "out" message if entry['type'] == "out":
import machine import time if __name__ == '__main__': #WLAN wlan = WLAN(mode=WLAN.STA) wlan.connect("FD-51", auth=(WLAN.WPA2, "fromage2chevre"), timeout=5000) while not wlan.isconnected(): machine.idle() print("Connected to Wifi") #MQTT mqttServerAddr = "192.168.43.253" client = MQTTClient("lopy", mqttServerAddr, port=1883) client.connect() print("Connected to MQTT at : {}".format(mqttServerAddr)) #LoRa lora = LoRa(mode=LoRa.LORA, frequency=868100000) s = socket.socket(socket.AF_LORA, socket.SOCK_RAW) s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5) s.setblocking(True) print("Listening to LoRaMAC") while True: print("waiting for data to send") loraClientData = int(s.recv(256)) client.publish(topic="test/lopy", msg=str(loraClientData)) print("data sent : {}".format(loraClientData)) time.sleep(0.1)
sys.exit(1) mqtt_queue = Queue() ciao_queue = Queue() try: mqttclient = MQTTClient(shd["conf"]["params"], ciao_queue) except Exception, e: logger.critical("Exception while creating MQTTClient: %s" % e) sys.exit(1) signal.signal(signal.SIGINT, signal.SIG_IGN) #ignore SIGINT(ctrl+c) signal.signal(signal.SIGHUP, signal_handler) signal.signal(signal.SIGTERM, signal_handler) if mqttclient.connect(): logger.info("Connected to %s" % shd['conf']['params']['host']) shd["requests"] = {} ciaoclient = MQTTCiao(shd, mqtt_queue, ciao_queue) ciaoclient.start() # endless loop until SIGHUP/SIGTERM while shd["loop"]: if not mqtt_queue.empty(): entry = mqtt_queue.get() logger.debug("Entry %s" % entry) # if entry received from ciao is an "out" message if entry['type'] == "out":