How to send data from UDP port to lora sensor node?

Issue: payload not sending to TTN but the joining metadat was successfully can be seen in TTN

Setup: I used raspberry pi 3b+ with rak811 lora piHat this is connected to the opal-RT via ethernet to get the data from real-time simulation through UDP. I am using rak7240 as my gateway.

Server: TTN (network server v2)

Details: 0

I am trying to send data from simulink then using UDP sending this data to raspberry. The data in raspberry pi was displayed correctly. But, when I am trying to send the UDP data to lora it was not able to send. Can someone help me how to fix the code. P.S. I am not expert in programming, I am just starting to learn everything. Thank you. BTW, I just got the basic code of sending data from github. The idea here is raspberry pi will accept data from another machine through UDP protocol. Then, the data will be unpack in raspberry pi, those data will be transmitted using lora piHat going to the gateway, then to network server.

from sys import exit
from time import sleep
from rak811.rak811 import Mode, Rak811
from ttn_secrets import APP_EUI, APP_KEY
import socket, struct, os
import numpy as np
import struct
import socket
lora = Rak811()
      
print('Setup')
lora.hard_reset()
lora.mode = Mode.LoRaWan
lora.band = 'AS923'
lora.set_config(app_eui=APP_EUI, app_key=APP_KEY)
print('Joining')
lora.join_otaa()
lora.dr = 5
IP = ""
PORT = 50000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((IP, PORT))
try:
    while True:
        data, addr = sock.recvfrom(1024)
        data = struct.unpack('dddddddddd', data)
        lora.send(data[0])
        print("x1", data[0])
        sleep(0)
except
    pass
print('Cleaning up')
lora.close()
exit(0)

Hi @pularolando ,

I suggest you isolate the LoRaWAN section first before integrating to UDP. Something like base LoRaWAN uplink.

from rak811.rak811_v3 import Rak811

lora = Rak811()
lora.set_config('lora:work_mode:0')
lora.set_config('lora:join_mode:0')
lora.set_config('lora:region:AS923')
lora.set_config('lora:app_eui:70B3D5xxxxxxxxxx')
lora.set_config('lora:app_key:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
lora.join()
lora.set_config('lora:dr:5')
lora.send('Hello world')
lora.close()

Let’s ensure that this one works first.

Few things to check:

  1. APPEUI, DEVEUI, APPKEY are all correct? It seems you need to use the command line to check the DEVEUI of your device.
  2. Is the region of the gateway and the device the same? I assume you are using AS923 since that’s the one in your code.
  3. What is the FW verion of your RAK811? The python library has a different FW for V2 and V3. The code I posted is for V3.
  4. Antenna properly connected?
  5. Can you see what is the joining frequency of the device?
  6. Can you attempt to rejoin like at least 5 times (just for sanity check)?

Hello @carlrowan,
Thank you so much for your fast response. For the items that you listed for checking: (1) yes, all are correctly stated. In fact, I was able to send random data from rak811 to TTN using my configuration. (2) Region for both TTN and rak811 configuration are also the same. (3) I am using rak811 FW v2. (4) Antenna are correctly placed as I checked the gateway, transmission for joining the TTN via OTAA was logged in. (5) There is no problem in frequency setting as it is successfully sending the joining process and gateway metadata. (6) Yes, I am troubleshooting this for the last 2 weeks. So, basically joining process is always the first step.

I will try your suggestion first using the rak811 v2 FW. Then, I will post the results here. I am afraid I can not upgrade to rak811 v3 since I am using TTN v2. So maybe ( I don’t know yet) if I upgrade the FW version I will be disconnected and can not rejoin. As I don’t want to migrate yet in TTN v3 since I don’t have much time studying the processes in migration to TTN V3.

Thank you so much @carlrowan . I will be back to you soon.

Regards,

Hello @carlrowan ,

Apologies for the delay of my reply. I tried your suggestion and modified the code as shown below.

from random import randint
from sys import exit
from time import sleep

from rak811.rak811 import Mode, Rak811
from ttn_secrets import APP_EUI, APP_KEY

import socket, struct, os
import numpy as np

import struct
import socket

lora = Rak811()

    # Most of the setup should happen only once...

print(‘Setup’)
lora.hard_reset()
lora.mode = Mode.LoRaWan
lora.band = ‘AS923’
lora.set_config(app_eui=APP_EUI, app_key=APP_KEY)

print(‘Joining’)
lora.join_otaa()
lora.dr = 5
lora.send(“hello”)
lora.close()

print(‘Sending packets - Interrupt to cancel loop’)
print(‘You can send downlinks from the TTN console’)

IP = “”
PORT = 50000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((IP, PORT))

while True:
data, addr = sock.recvfrom(1024)
data = struct.unpack(‘dddddddddd’, data)
PV1 = data[0]
PV2 = data[1]
PV3 = data[2]
PV4 = data[3]
PV5 = data[4]
PV6 = data[5]
PV7 = data[6]
PV8 = data[7]
PV9 = data[8]
PV10 = data[9]

data_to_send = struct.pack("dd", PV1,PV2)
lora.send(bytes(data_to_send))
print("x1", data_to_send)

However, upon executing I got this error.

.Do you have any idea how to solve this? I already tried to remove the struct.pack command just send the raw data directly but the error is “struct.error: required argument is not an integer”. As I read some information the struck.pack should be in ‘h’ w/c I already did, I even tried different data types packing but the error is the same with the attach image. do you any idea what my error is?

regards,
Rolando Pula

@carlrowan ,

Hello @carlrowan and other pips here. Just in case you encounter this error post it here in this thread. I solved it by converting the float to string then. The lora.close() should not be placed before the “while True” condition. Doing so, means you are closing the rak811 port communicating to the raspberry pi that is why the error displayed “portNotOpenError”. lora.close should be place in line with the while True condition.

Just a follow-up question @carlrowan, how can I speed up the transmission rate of rak811. As of now, the average trnasmission rate of default settings is around 19-20 packets/min. I already migrated the application to TTN v3. After I configiure all integrations and payload formatter. I;ll be doing the gateway migration to TTN v3 also.

Regards,
Rolando Pula

That would be in breach fo the TTS CE use policy by a huge margin and illegal in most jurisdiction.

You may need to cache a number of packets, depending on the size of the information to send less often. You should also expect 5 to 10% overall uplink loss as suggested by TTI in their documentation.

See this calculator to see what works:

https://avbentem.github.io/airtime-calculator/ttn/eu868/5

Hi @pularolando ,

As @Nick said, you can’t do 19-20 packets/min. If those are LoRa transmissions, it means you transmit every 3 seconds which is not ideal for LoRaWAN.

@nmcc Thank you so much for this info. I will read more about the link you provided to have more understanding regarding my question. Aside from caching, Is there any more method I can choose to?

Thanks again @nmcc .

Regards,
Rolando Pula

Hello @carlrowan,

Can you provide articles I can read regarding lora transmissions, specifically for rak811. Thanks in advance if there is any.

Regards,
Rolando Pula

HI @pularolando ,

What specific topic do you need? Is it about transmission interval? Size of payload?

Hello @carlrowan I think I needed both since as per @nmcc suggestion. I need to cache the data before sending which makes sense. Thank you