Basic Python non-LoRa MQTT client

Hi, I am trying to create a basic Python non-LoRa client to send IoT data to TTN. So far, I am following the API refence as well as some additional info found on StackOverflow but still unable even to connect to TTN. Has anyone some sample code on which I can build or can some point out where I miss or go wring in my code?
Thx a lot in advance for any feedback

import time
import ttn
import base64

app_id = “Application ID from the TTN console”
access_key = “ttn-account-v2.etcetcetcetcetc…”
device_id = “Device ID from the TTN console”

def connect_callback(msg, client):
print("Connected : ", msg.dev_id)
print(msg)

def uplink_callback(msg, client):
print("Received uplink from ", msg.dev_id)
print(msg)

handler = ttn.HandlerClient(app_id, access_key)

mqtt_client = handler.data()
mqtt_client.set_uplink_callback(uplink_callback)
mqtt_client.set_connect_callback(connect_callback)
mqtt_client.connect()
mqtt_client.send(device_id, base64.encodebytes(bytes(“DATA I WANT TO SEND”, ‘utf-8’)).decode(), port=1, sched=“replace”)
time.sleep(5)
mqtt_client.close()

P:\Python\9.Projects\MQTT>python mqtt-ttn-publish.py
Traceback (most recent call last):
File “mqtt-ttn-publish.py”, line 26, in
mqtt_client.connect()
File “C:\Python\lib\site-packages\ttn\ttnmqtt.py”, line 74, in connect
self._connect()
File “C:\Python\lib\site-packages\ttn\ttnmqtt.py”, line 60, in _connect
self.__client.connect(addr, port, 60)
File “C:\Python\lib\site-packages\paho\mqtt\client.py”, line 937, in connect
return self.reconnect()
File “C:\Python\lib\site-packages\paho\mqtt\client.py”, line 1071, in reconnect
sock = self._create_socket_connection()
File “C:\Python\lib\site-packages\paho\mqtt\client.py”, line 3522, in _create_socket_connection
return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
File “C:\Python\lib\socket.py”, line 727, in create_connection
raise err
File “C:\Python\lib\socket.py”, line 716, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

You can only use MQTT to receive application data (uplinks) from TTN and push downlinks to the nodes. TTN does not provide a generic IoT publishing platform.

Is extracting data from LoRaWAN devices what you want to do? You subject suggests it might not be.

1 Like

I have a RasPi with LoRaWAN sending data to TTN. Now I need to add another Pi, 'out-of-reach" of the gateway :-(, but with access to WiFi. I want to send data from Pi to TTN. This would allow me to extract and graph with same app (Telegraf -> Influx -> Grafana) all data together.

And, as a starter, the connect fails so hence TTN does not yet even know what I want to do (uplink or downlink)

Sorry, your use case can not be implemented with TTN, you are not the first to want this but TTN does not provide the capabilities you need. You will need to find another way to feed the data into your application.

With regards to the connection issue, the TTN Python API code is discontinued and no longer supported. Combined with the fact that what you want is a non started anyway it isn’t worth the trouble of debugging the connection issue.

1 Like

Neither Telegraf nor of course Influx are specific to TTN, what you need to do is come up with another scheme for injecting your non-TTN IoT data at one of those points.

Surely those solutions have many examples for a variety of types of sources.

And you can make your other data feeds similar in their content semantics to your TTN ones, ie, same packet contents and even go so far as to give them nominally similar (if perhaps irrelevant) metadata, though establishing a more sensible union of what both offer would probably be the best.

Looks like you haven’t got the ttn python library installed…

If you installed python from the python site using the windows installer hopefully you have python3.x installed? If you do on your cmd prompt type: “pip install ttn” without the quotes.

^ disregard that I just read through the error msg.
I’ll see what my ttn read application script says, I don’t use the ttn node-red or python libraries, found it easier to use the std paho-mqtt for python and std mqtt node-red libraries.

#!/usr/bin/env python3
import time
import ttn

app_id  = "ttgo_tbeam_device"  # <- your device app id here
app_key = "ttn-account-v2.xxx" # <- your device app key here

def uplink_callback(msg, client):
  print("Received uplink from ", msg.dev_id)
  print(msg)

handler = ttn.HandlerClient(app_id, app_key)

# using mqtt client
mqtt_client = handler.data()
mqtt_client.set_uplink_callback(uplink_callback)
mqtt_client.connect()
time.sleep(60) # sleep(60) # Time to run in seconds 60 = 1 minute
mqtt_client.close()

Can also try this one.

import time
import ttn

""" TTN application credentials """
app_id = "your-ttn-app-id"
access_key = "ttn-account-v2.xxxx"

""" confirm uplink connection """
def uplink_callback(msg, client):
  print("Received uplink from ", msg.dev_id)
  print(msg)

handler = ttn.HandlerClient(app_id, access_key)

mqtt_client = handler.data()
mqtt_client.set_uplink_callback(uplink_callback)

""" using mqtt client  """
while True:
    try:
        mqtt_client.connect()
        time.sleep(60)
        mqtt_client.close()
    except Exception as e:
        print(e)
        pass

Thanks for the answer, however it will not solve the issue of TTN not being suited to push non LoRaWAN device data to an application.

1 Like