Sending stream of bytes using LoraWan socket

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
Amir_IOT
Posts: 3
Joined: Sat Jun 06, 2020 5:15 pm

Sending stream of bytes using LoraWan socket

Post by Amir_IOT » Sat Jun 06, 2020 5:47 pm

Good afternoon every one , I'm doing an experiment with LoraWan technology using the Fipy Board (which is based on the ESP32) , i'm sending data packets to the Server (TTN) so i can analyse them later. When i send small packet of data using sockets , Like :

Code: Select all

for i in range(200):

    s.send( b'PKT #' + bytes([i]))
    #time.sleep( 4 )
    rx = s.recv( 256 )    # The bufsize argument of 256 used is the maximum amount of data to be received at once.
    if rx:                      # if you receive any thing from the Gateway printed than go to sleep again
        print(rx)             # If there's something received , Print it...if rx is empty, the socket will terminate
    time.sleep( 6 )
    
The System works fine and i can see my data on the TTN server , However since my experiment include the extraction of many sensor data
and organize those data like stream of bytes my real packet will look like :

Data = [A,213,3255,27,10,0x2342]

when i try to use the same socket method to send those data it fails , i realized that the problem is that the bytes can only contains
from 0 ->255 so i used the following lines :

Code: Select all

i1 = 3255
i2 = 0x2342
for i in range(200):
     to_send = bytes([ord('A'),213]) + i1.to_bytes(2,'big') + bytes([27, 10]) + i2.to_bytes(2,'big')
     s.send(to_send )
but i get this Error :

NotImplementedError: for the line : s.send(to_send ) .......The wearied thing is that the same data construction will work fine

if it was tested on a python IDE which is very confusing !!....when i tested the method .to_bytes() inside a print statement i get an error.

Kindly help me to solve this issue ?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Sending stream of bytes using LoraWan socket

Post by pythoncoder » Sun Jun 07, 2020 8:40 am

Why not use ujson?

Code: Select all

>>> import ujson
>>> data = ['A',213,3255,27,10,0x2342]
>>> ujson.dumps(data).encode()
b'["A", 213, 3255, 27, 10, 9026]'
>>> 
The bytes object will vary in length. This can usually be addressed by appending a newline, with the reader using a readline method.
Peter Hinch
Index to my micropython libraries.

Amir_IOT
Posts: 3
Joined: Sat Jun 06, 2020 5:15 pm

Re: Sending stream of bytes using LoraWan socket

Post by Amir_IOT » Sun Jun 07, 2020 10:23 am

It's Working Perfectly .....thank you so much .....

User avatar
rcolistete
Posts: 352
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Re: Sending stream of bytes using LoraWan socket

Post by rcolistete » Sun Jun 07, 2020 2:18 pm

For LoRa(Wan) packets, "struct.pack()" is often used. Here one example using LoRa (LoRaWan adds another 13 bytes as header) :

Code: Select all

# LoRa-MAC protocol definition : package header with 2 bytes, then follows the useful package content (payload) with 4 bytes, so the package has 6 bytes.
# But the physical layer of LoRa also adds a preamble of 8 bytes (default value) and 5 bytes of CRC, etc, total of about 13 additional bytes.
# B: 1 byte for the deviceId;
# B: 1 byte for the packet size including the header;
# Header ends here;
# Payload starts here :
# H: 2 bytes for the messageId (2^16 = 65.536 messages);
# B: 1 byte for wake and reason codes compacted, see https://docs.pycom.io/firmwareapi/pycom/machine/.
# B: 1 byte for compacted battery voltage, [0-255] in scale (2.60-5.15)V by using (Vbat - 2.6V)*100;

LORA_PKG_FORMAT = "!BBHBB"
# more code...
LoRa_MAC_pkg = struct.pack(LORA_PKG_FORMAT, LORA_DEVICE_ID, struct.calcsize(LORA_PKG_FORMAT), message_count, wake_reset_code, batt_mV_byte)
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

Amir_IOT
Posts: 3
Joined: Sat Jun 06, 2020 5:15 pm

Re: Sending stream of bytes using LoraWan socket

Post by Amir_IOT » Sun Jun 07, 2020 11:51 pm

That's a very valuable information to know.........Thank you

Post Reply