I’m trying to write a python program that saves the data that I acquire from the things network in a text file. This is my code (very similar to https://www.thethingsnetwork.org/docs/applications/python/).
import time import ttn app_id = "foo" access_key = "ttn-account.eiP<redacted>Zzg" def uplink_callback(msg, client): print("Received uplink from ", msg.dev_id) save_file() print(msg) def save_file(): #Code to save a text file handler = ttn.HandlerClient(app_id, access_key) # using mqtt client mqtt_client = handler.data() mqtt_client.set_uplink_callback(uplink_callback) try: while 1: time.sleep(1) except KeyboardInterrupt: print("Programm closed.") mqtt_client.close() except: print("Error.") mqtt_client.close()
I would create a function (save_file) that saves the information of msg in a file. Now the programm print on screen this information
Received uplink from stm32-b-l072z-lrwan1
MSG(app_id=‘test-application-for-st-lora-nodes’, dev_id=‘stm32-b-l072z-lrwan1’, hardware_serial=‘3435313160379011’, port=2, counter=131, confirmed=True, payload_raw=‘AAD4APg=’, metadata=MSG(time=‘2020-05-29T10:23:05.019674139Z’, frequency=868.3, modulation=‘LORA’, data_rate=‘SF7BW125’, airtime=51456000, coding_rate=‘4/5’, gateways=[MSG(gtw_id=‘eui-0080e1ffff015232’, timestamp=1749644820, time=’’, channel=6, rssi=-57, snr=8.8, rf_chain=0)]))
I need in the file the time, the dev_id and the payload (even if is different from payload that I see on the things network)