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)
###### def empty_queue(delay=0): while not message_q.empty(): m = message_q.get() print("Received message ", m) if delay != 0: time.sleep(delay) ######## host = "192.168.100.112" port = 1885 client = Client("linh") #change so only needs name client.message_arrived_flag = False client.registerCallback(MyCallback()) print("threads ", threading.activeCount()) print("connecting ", host) client.connected_flag = False client.connect(host, port) client.lookfor(MQTTSN.CONNACK) try: if client.waitfor(MQTTSN.CONNACK) == None: print("connection failed") raise SystemExit("no Connection quitting") except: print("connection failed")
time.sleep(delay) ######## signal.signal(signal.SIGINT, keyboardInterruptHandler) print("threads ", threading.activeCount()) #if __name__ == "__main__": #host="192.168.100.112" #port=1883 #s_port=1885 #s_group="224.0.18.83" r_port = 1884 #port gateway advertises on r_group = "225.0.18.83" #IP gateways advertises on #r_port=1883 #port gateways advertises on #r_group="225.1.1.1" #IP gateways advertises on client = Client("linh") #change so only needs name print("threads ", threading.activeCount()) client.registerCallback(MyCallback()) client.find_gateway(r_port, r_group) client.loop_start_gw() while not client.GatewayFound: #while True: print("waiting for gateway threads ", threading.activeCount()) time.sleep(10) ############ print("Gateway found") gateway = gateways.pop(0) host = gateway[0]
#parser.add_argument('--awstopic', type=str, default='station', # help='AWS Topic to publish to. Allowed values: \'station\' or \'station2\'') parser.add_argument('--mqttclientid', type=str, default='gw', help='MQTTS Broker Client Id. Allowed values: any.') parser.add_argument( '--mqttstopics', type=str, default='stat_1 stat_2', help= 'MQTTS Topics to forward to AWS Iot Core Broker. Allowed values: \'stat_1\', \'stat_2\'.' 'To forward more than one topic write more topics separated with withespace, ' 'like --mqttstopics "stat_1 stat2".') args = parser.parse_args() #aws_clientId = args.awsclientid #aws_topic = args.awstopic mqtts_topics = args.mqttstopics.split(" ") gateway = Client("gw", port=1885) gateway.registerCallback(Callback()) gateway.connect() for topic in mqtts_topics: if (topic != ""): gateway.subscribe(topic) while True: time.sleep(1)
while not message_q.empty(): m = message_q.get() print(m) if delay != 0: time.sleep(delay) ######## host = "192.168.1.159" port = 1885 #port=10000 m_port = 1885 #port gateways advertises on m_group = "225.0.18.83" #IP gateways advertises on client = Client("linh_pub") #change so only needs name client.registerCallback(Callback()) print("threads ", threading.activeCount()) print("connecting ", host) client.connected_flag = False client.connect(host, port) client.loop_start() client.lookfor(MQTTSN.CONNACK) try: if client.waitfor(MQTTSN.CONNACK) == None: print("connection failed") raise SystemExit("no Connection quitting") except:
* and Eclipse Distribution License v1.0 which accompany this distribution. * * The Eclipse Public License is available at * http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php. * * Contributors: * Ian Craggs - initial API and implementation and/or initial documentation *******************************************************************************/ """ from MQTTSNclient import Callback from MQTTSNclient import Client import MQTTSN import time broker = "192.168.1.159" port = 1884 aclient = Client("register") aclient.registerCallback(Callback()) #aclient.loop_start() aclient.connect(broker, port) result = aclient.register("jkjkjkjkj") print("result from register 1 is", result) result = aclient.register("jkjkjkjkj") print("result from register 1 is", result) result = aclient.register("jkjkjkjkj2") print("result from register 2 is", result) time.sleep(8) #aclient.loop_stop()
aclient.subscribe("sensor2_battery_d15_d45_d75", qos=1) print("Inscrito ao topico - sensor2_battery_d15_d45_d75") print(" ") aclient.subscribe("sensor3_battery_d15_d45_d75", qos=1) print("Inscrito ao topico - sensor3_battery_d15_d45_d75") print(" ") aclient.subscribe("sensor4_battery_d15_d45_d75", qos=2) print("Inscrito ao topico - sensor4_battery_d15_d45_d75") print(" ") aclient.subscribe("sensor5_battery_wetness_rain_temperature", qos=2) print("Inscrito ao topico - sensor5_battery_wetness_rain_temperature") print(" ") #Cliente se conecta ao gateway aclient = Client("Client_SN_Sub", "localhost", port=10000) aclient.registerCallback(Callback()) connect_gateway() subscribe_topic() try: while True: time.sleep(1) #Dorme um segundo volta except KeyboardInterrupt: #SE HOUVER INTERRUPÇÃO VIA TECLADO CANCELA A INSCRIÇÃO DE TODOS OS TÓPICOS E DESCONECTA DO GATEWAY #aclient.unsubscribe('sensor1_battery_d15_d45_d75') #print("Cancelada a inscrição do topico - sensor1_battery_d15_d45_d75") #aclient.unsubscribe('sensor2_battery_d15_d45_d75') #print("Cancelada a inscrição do topico - sensor2_battery_d15_d45_d75")
print(" ") sensor2_battery_d15_d45_d75 = aclient.register("sensor2_battery_d15_d45_d75") print("Topico - sensor2_battery_d15_d45_d75 - foi registrado com sucesso.") print(" ") sensor3_battery_d15_d45_d75 = aclient.register("sensor3_battery_d15_d45_d75") print("Topico - sensor3_battery_d15_d45_d75 - foi registrado com sucesso.") print(" ") sensor4_battery_d15_d45_d75 = aclient.register("sensor4_battery_d15_d45_d75") print("Topico - sensor4_battery_d15_d45_d75 - foi registrado com sucesso.") print(" ") sensor5_battery_wetness_rain_temperature = aclient.register("sensor5_battery_wetness_rain_temperature") print("Topico - sensor5_battery_wetness_rain_temperature - foi registrado com sucesso.") print(" ") #Cliente se concta ao gateway aclient = Client("Client_SN_Pub", "localhost", port=10000) aclient.registerCallback(Callback()) connect_gateway() #Declarando os tópicos e chamando a função registrar sensor1_battery_d15_d45_d75 = "sensor1_battery_d15_d45_d75" sensor2_battery_d15_d45_d75 = "sensor2_battery_d15_d45_d75" sensor3_battery_d15_d45_d75 = "sensor3_battery_d15_d45_d75" sensor4_battery_d15_d45_d75 = "sensor4_battery_d15_d45_d75" sensor5_battery_wetness_rain_temperature = "sensor5_battery_wetness_rain_temperature" register_topic() #TUDO SOBRE O PAYLOAD (CONTEUDO A SER PUBLICADO) arq = ("/home/alcindo/Área de Trabalho/TCC/Dados/modulo_1.xlsx") #Acessa o arquivo do sensor 1