def main(): print("--MQTT-SN -> MQTT bridge initialization--") print("Initializing MQTT-SN protocol") client = Client("Bridge") client.registerCallback(BridgeCallback()) print("Connecting to MQTT-SN broker at " + MQTTSN_BROKER_ADDRESS + ":" + str(MQTTSN_BROKER_PORT)) client.connected_flag = False client.connect(MQTTSN_BROKER_ADDRESS, MQTTSN_BROKER_PORT) client.loop_start() while not client.connected_flag: time.sleep(5) print("Waiting for connection to MQTT-SN broker...") print("Initializing MQTT protocol") mqtt_client = mqtt.Client("Bridge") mqtt_client.on_connect = mqtt_on_connect mqtt_client.on_message = mqtt_on_message print("Connecting to MQTT Broker at " + MQTT_BROKER_ADDRESS) mqtt_client.tls_set(CA_CERTIFICATE, CLIENT_CERTIFICATE, keyfile=PRIVATE_KEY, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) print("Trying connection") mqtt_client.connect(aws_iot_endpoint, BROKERPORT) mqtt_client.loop_start() print("Waiting for connection to complete") time.sleep(CONNECTION_WAIT_SLEEP) client.loop_start() rc, topic_id = client.subscribe(MQTTSN_TOPIC, 0) if rc == 0: print("Subscribed to " + MQTTSN_TOPIC + " MQTT-SN topic.") else: return -1 while True: while not message_queue.empty(): m = message_queue.get() print(m) mqtt_client.publish(MQTT_TOPIC, m, 1)
def main(): print("--MQTT-SN -> MQTT bridge initialization--") print("Initializing MQTT-SN protocol") client = Client("Bridge") client.registerCallback(BridgeCallback()) print("Connecting to MQTT-SN broker at " + MQTTSN_BROKER_ADDRESS + ":" + str(MQTTSN_BROKER_PORT)) client.connected_flag = False client.connect(MQTTSN_BROKER_ADDRESS, MQTTSN_BROKER_PORT) client.loop_start() while not client.connected_flag: time.sleep(5) print("Waiting for connection to MQTT-SN broker...") print("Initializing MQTT protocol") mqtt_client = mqtt.Client("Bridge") mqtt_client.on_connect = mqtt_on_connect mqtt_client.on_message = mqtt_on_message print("Connecting to MQTT Broker at " + MQTT_BROKER_ADDRESS) mqtt_client.connect(MQTT_BROKER_ADDRESS) mqtt_client.loop_start() print("Waiting for connection to complete") time.sleep(CONNECTION_WAIT_SLEEP) client.loop_start() rc, topic_id = client.subscribe(MQTTSN_TOPIC, 0) if rc == 0: print("Subscribed to " + MQTTSN_TOPIC + " MQTT-SN topic.") else: return -1 while True: while not message_queue.empty(): m = message_queue.get() print(m) mqtt_client.publish(MQTT_TOPIC, m, 1)
#while True: print("waiting for gateway threads ", threading.activeCount()) time.sleep(10) ############ print("Gateway found") gateway = gateways.pop(0) host = gateway[0] port = gateway[1] #host="192.168.1.159" #port=1885 print("threads ", threading.activeCount()) print("connecting ", host, " ", port) client.connected_flag = False client.connect(host, port) client.lookfor(MQTTSN.CONNACK) if client.waitfor(MQTTSN.CONNACK) == None: print("connection failed") raise SystemExit("no Connection quitting") time.sleep(2) topic1 = "test" topic1_id = client.register(topic1) print("topic id for topic1 is", topic1_id) empty_queue(4) msg = "aaaaa"