コード例 #1
0
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)
コード例 #2
0
ファイル: bridge.py プロジェクト: duiliol/iot_exam
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)
コード例 #3
0
ファイル: pub-sub-sn.py プロジェクト: aleparmi/SerialBLEChat
"""

while not client.connected_flag:
  time.sleep(1)
  print("waiting for connection")

"""
#quit()

print("threads ", threading.activeCount())
topic1 = "bulbs1"
print("topic for topic1 is", topic1)
print("connected now subscribing")
client.subscribed_flag = False
while True:
    rc, topic1_id = client.subscribe(topic1, 1)
    if rc == None:
        print("subscribe failed")
        raise SystemExit("Subscription failed quitting")
    if rc == 0:
        print("subscribed ok to ", topic1)
        break
count = 0
msg = "test message"

try:
    while True:
        nmsg = msg + str(count)
        count += 1
        print("publishing message ", nmsg)
        id = client.publish(topic1_id, nmsg, qos=0)
コード例 #4
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)