I have a Lopy4 and I would like it to be able to communicate with a web server using GET/POST requests in JSON through LoraWAN and TTN. I correctly connected it to TTN using Pycom’s tutorial. I created an end devices and logged in using OTAA. I can see connection messages and data bytes sent in the “live data” in TTN.
But now I would like it to send it to a web server. My web server is ready and correctly handles requests, I tested it separately. I assumed it was the “webhook” functionnality. So I created a new webhook but I can’t find how to get it to pass the received bytes or json data over. I checked all the “enabled messages” checkboxes, it sends data to my webserver when the json is received, but the content of the json is not sent.
NOTE : When I use the End device > Messaging > Downlink function of TTN, I can correctly see all the sent json data on my server. It works as expected. But when the data is sent from the Lopy instead of within TTN, the json received by the webserver contains a lot of info but not the sent data. The sent data appears in live data though. So my problem is really how I could set TTN to handle json data to the webserver. I didn’t find how and didn’t find tutorials.
How could I do that ?
Thanks in advance for any answer,
have a great day
Can you confirm that you are seeing uplink messages in the device console?
And when you click on that line, it shows the details in a box on the right hand side?
This is the JSON that will be transmitted to your web server.
What language have you used on your web server? If you are using PHP the JSON doesn’t arrive as expected.
If you want a “it just works” starter, try the PHP one to be found here:
If it’s a case of understanding the JSON format which will have the data encoded as Base-64 plus, if you have a payload formatter, the JSON split out, then the docs are here:
When my Pycom boots up, here are the messages I get in the live data:
When I click on a line, it shows the details, yep. What I don’t understand is that when I first tried I could correctly see the sent bytes (ie 01 02 03), but now it no longer works… I can’t see the data in the json.
Here is how I am sending it in the Lopy:
from network import LoRa
import socket
import time
import ubinascii
import json
# Initialise LoRa in LORAWAN mode.
# Please pick the region that matches where you are using the device:
# Asia = LoRa.AS923
# Australia = LoRa.AU915
# Europe = LoRa.EU868
# United States = LoRa.US915
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)
# create an OTAA authentication parameters, change them to the provided credentials
app_eui = ubinascii.unhexlify('0000000000000001')
app_key = ubinascii.unhexlify('xxxxx')
#uncomment to use LoRaWAN application provided dev_eui
dev_eui = ubinascii.unhexlify('xxxxx')
# Uncomment for US915 / AU915 & Pygate
# for i in range(0,8):
# lora.remove_channel(i)
# for i in range(16,65):
# lora.remove_channel(i)
# for i in range(66,72):
# lora.remove_channel(i)
# join a network using OTAA (Over the Air Activation)
#uncomment below to use LoRaWAN application provided dev_eui
lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)
#lora.join(activation=LoRa.OTAA, auth=(dev_eui, app_eui, app_key), timeout=0)
# wait until the module has joined the network
while not lora.has_joined():
time.sleep(2.5)
print('Not yet joined...')
print('Joined')
# create a LoRa socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
# set the LoRaWAN data rate
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
# make the socket blocking
# (waits for the data to be sent and for the 2 receive windows to expire)
s.setblocking(True)
# send some data
s.send(bytes([0x01, 0x02, 0x03]))
jsontest = {"name":"nomtest", "pycom":"pycomtest"}
s.send(jsontest)
# make the socket non-blocking
# (because if there's no data received it will block forever...)
s.setblocking(False)
# get any data received (if any...)²
data = s.recv(64)
print(data)
If you click the “Forward uplink data message” - which shows the payload in the MAC payload box, you’ll get the console JSON - everything inside the ‘data’ element is what will have been sent to your integration.
As above, JSON that is transferred does not always ‘arrive’ as expected in the normal way of a GET or POST so you may need to do some Django research.
As for sending actual text (which is the JSON in your code), that’s a big no-no - it consumes far more air time than is necessary and generally clogs things up - and we’d have to explain how to decode the payload with a mix of byte data & text. Please refer to: https://www.thethingsnetwork.org/docs/devices/bytes/ - ignore the pink warning, the techniques are valid for v3.
If your webserver supports PHP you can drop in the TTS.WebHook.Tab.php file for instant results - you can have two Webhooks running side by side - and you can also use MQTT to see what arrives as well.