Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Paket-Transfer Lorawan converting paket to float and back
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Paket-Transfer Lorawan converting paket to float and back

by pede on Sun Oct 11, 2020 8:28 am

Hi,

I have a question referring the code examples from
https://learn.adafruit.com/multi-device ... adafruitio.

I don't plan to use adafruit io or TTN, because ein want to use it as my private gateway (I use the several feathers RFM 95 with serveal sensors attached and the Lora radio bonnet with a raspberry zero and circuit python.

I understand the code examples quit well, but I struggle a little with the packet conversion part:

# sensor data
bme280_data = bytearray(8)
# Build packet with float data and headers

# packet header with feather node ID
bme280_data[0] = FEATHER_ID
# Temperature data
bme280_data[1] = (temp_val >> 8) & 0xff
bme280_data[2] = temp_val & 0xff

# Humid data
bme280_data[3] = (humid_val >> 8) & 0xff
bme280_data[4] = humid_val & 0xff

# Pressure data
bme280_data[5] = (pres_val >> 16) & 0xff
bme280_data[6] = (pres_val >> 8) & 0xff
bme280_data[7] = pres_val & 0xff

# Convert bytearray to bytes
bme280_data_bytes = bytes(bme280_data)
# Send the packet data
print('Sending data...')
LED.value = True
rfm9x.send(bme280_data)

I understand that this is byte array and the int-value is bit-shifted to right?

1. question: why do I need the second line?
-> bme280_data[2] = temp_val & 0xff ???

2. question the value need two byte -> shift 16
but what does the 2 second line shift 8 bits?

# Pressure data
bme280_data[5] = (pres_val >> 16) & 0xff
bme280_data[6] = (pres_val >> 8) & 0xff
bme280_data[7] = pres_val & 0xff

The next question is referring the gateway bonnet/raspi config
(convert back)

# check for packet rx
packet = rfm9x.receive()
def pkt_int_to_float(pkt_val_1, pkt_val_2, pkt_val_3=None):
"""Convert packet data to float.
"""
if pkt_val_3 is None:
float_val = pkt_val_1 << 8 | pkt_val_2
else:
float_val = pkt_val_1 << 16 | pkt_val_2 << 8 | pkt_val_3
return float_val/100
......
Question: why is pkt_val_3=None? used in def line?
and when it's none why do you a shift back?
Please explaln the line
float_val = pkt_val_1 << 16 | pkt_val_2 << 8 | pkt_val_3
...
pres_val = pkt_int_to_float(packet[5], packet[6], packet[7])
P5 is shifted 16 bits left, P6 8 bits, and p7 = none?

the rest than I understand again

prev_packet = packet
print('> New Packet!')
# Decode packet
temp_val = pkt_int_to_float(packet[1], packet[2])
humid_val = pkt_int_to_float(packet[3], packet[4])
pres_val = pkt_int_to_float(packet[5], packet[6], packet[7])

Many thx in advane for your answer, is there a tutorial which explans this handling in detail. I only could find some poor explanation using c++.

Peter

pede
 
Posts: 8
Joined: Sun Jul 12, 2020 2:16 pm

Re: Paket-Transfer Lorawan converting paket to float and bac

by tannewt on Mon Oct 12, 2020 5:45 pm

pede wrote:1. question: why do I need the second line?
-> bme280_data[2] = temp_val & 0xff ???

2. question the value need two byte -> shift 16
but what does the 2 second line shift 8 bits?

Question: why is pkt_val_3=None? used in def line?
and when it's none why do you a shift back?


Hi Peter, for 1 and 2, both lines are needed to save each of the two 8 bit parts that make up a 16 bit value. The shift moves the higher bits down into the lower ones that are saved in the bytearray.

I think pkt_val_3 is set to None so that the function can take two arguments instead of 3. The `=None` sets a default value. It should be ignored later when it is None.

I'd recommend reading about Python's struct module. We support it in CircuitPython too and it's much easier to reason about: https://docs.python.org/3/library/struct.html The code you have really should use it rather than doing the byte packing itself.

The bit operations do work like they do in C, so the resources you already found shouldn't hurt.

tannewt
 
Posts: 1837
Joined: Thu Oct 06, 2016 8:48 pm

Re: Paket-Transfer Lorawan converting paket to float and bac

by pede on Tue Oct 13, 2020 5:11 am

Hi, many thx! I will check out the the link .... perhaps I will come back to you after reading

Peter

pede
 
Posts: 8
Joined: Sun Jul 12, 2020 2:16 pm

Please be positive and constructive with your questions and comments.