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