Example #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)
Example #2
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.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)
Example #3
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)
Example #4
0
########
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]
port = gateway[1]
 * 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()