Hi, I’m currently trying to get the data out of TTN with a python script running on a server. That works, the code shows the uplink messages.
Now I want to write the data into a mySQL database within the same script. The database and columns have been created already. But somehow the code doesnt’t get to the part where the mySQL statement is formulated and just writes nothing into the database.
I’m completelynew to python, any hints/directions you can give me?
Best Regards
Marco
import time
import ttn
import mysql.connector
cnx = mysql.connector.connect(user='xxx', password='xxx',
host='xxx',
database='xxx')
app_id = "ra-ma_node_01"
access_key = "ttn-account-v2.xxx"
def uplink_callback(msg, client):
print("Received uplink from ", msg.dev_id)
print(msg)
cursor = cnx.cursor()
add_data = "INSERT into ttn_data_01 (payload_fields) VALUES (%s)" %msg.payload_fields
print(add_data)
cursor.execute (add_data)
cnx.commit()
cursor.close()
handler = ttn.HandlerClient(app_id, access_key)
# using mqtt client
mqtt_client = handler.data()
mqtt_client.set_uplink_callback(uplink_callback)
mqtt_client.connect()
time.sleep(60)
mqtt_client.close()
cnx.close()
# using application manager client
app_client = handler.application()
my_app = app_client.get()
print(my_app)
my_devices = app_client.devices()
print(my_devices)