|  | 
|  | 1 | +import paho.mqtt.client as mqtt, json, base64 | 
|  | 2 | + | 
|  | 3 | +# The callback for when the client receives a CONNACK response from the server. | 
|  | 4 | +def on_connect(client, userdata, flags, rc): | 
|  | 5 | +    print("Connected with result code "+str(rc)) | 
|  | 6 | + | 
|  | 7 | +    # Subscribing in on_connect() means that if we lose the connection and | 
|  | 8 | +    # reconnect then subscriptions will be renewed. | 
|  | 9 | +    client.subscribe("application/1/device/+/event/up") | 
|  | 10 | + | 
|  | 11 | +# The callback for when a PUBLISH message is received from the server. | 
|  | 12 | +def on_message(client, userdata, msg): | 
|  | 13 | +    Jmsg=json.loads(msg.payload) | 
|  | 14 | +    mydata=Jmsg["object"] | 
|  | 15 | +    if (mydata!=""): | 
|  | 16 | +        print(mydata) | 
|  | 17 | +        Jmydata=json.loads(mydata) | 
|  | 18 | + | 
|  | 19 | +#def bleu(): | 
|  | 20 | + #   msg="blue" | 
|  | 21 | +  #  msg_bytes=msg.encode('ascii') | 
|  | 22 | +   # b64_bytes=base64.b64encode(msg_bytes) | 
|  | 23 | +    #b64_msg=b64_bytes.decode('ascii') | 
|  | 24 | +    #print(b64_msg) | 
|  | 25 | +    #payloadJson={"confirmed":False,"fPort":5,"data":b64_msg} | 
|  | 26 | +    #client.publish(topic="application/1/device/0c7e450102030102/command/down",payload=json.dumps(payloadJson)) | 
|  | 27 | + | 
|  | 28 | +client = mqtt.Client() | 
|  | 29 | +client.on_connect = on_connect | 
|  | 30 | +client.on_message = on_message | 
|  | 31 | + | 
|  | 32 | +client.connect("chirpstack.iut-blagnac.fr", 1883, 60) | 
|  | 33 | + | 
|  | 34 | +# Blocking call that processes network traffic, dispatches callbacks and | 
|  | 35 | +# handles reconnecting. | 
|  | 36 | +# Other loop*() functions are available that give a threaded interface and a | 
|  | 37 | +# manual interface. | 
|  | 38 | +client.loop_forever() | 
0 commit comments