Skip to content

Instantly share code, notes, and snippets.

@dgomes
Last active December 23, 2020 16:28
Show Gist options
  • Save dgomes/cf701c2213594187c1fa0c4795151be2 to your computer and use it in GitHub Desktop.
Save dgomes/cf701c2213594187c1fa0c4795151be2 to your computer and use it in GitHub Desktop.
TTN Example App
import paho.mqtt.client as mqtt
import base64
import json
server = "eu.thethings.network"
up_topic = '+/devices/+/up'
user = "my_user" #Application ID
password = "ttn-account-v2.verycrazystring" #Application Access Key
def on_message(mqttc, obj, msg):
m = str(msg.payload,'utf-8')
m = json.loads(m)
import pprint
pprint.pprint(m)
message =base64.b64decode(m['payload_raw'])
temp = int.from_bytes(message,byteorder='little')/100
print("Temperature = {}".format(temp))
mqttc = mqtt.Client()
mqttc.username_pw_set(user,password)
mqttc.on_message = on_message
mqttc.connect(server, 1883, 60)
mqttc.subscribe(up_topic, 0)
mqttc.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment