sending packets with Lopy (Lorawan)
I am trying to send values from my TMP 36 sensor through the lopy to my gateway. I am using the loriot network server.
The problem is that when i am trying to send the float data to my gateway i see this message :
The command i use is : s.send(datafromtmp).
I know that loriot shows any message i send in a hex form. I tried to use the binascii.hexlify method but it didnt work.
@jmarcelino Thank you for your answer!
The problem is a socket only understand bytes, it doesn't know what do with integers or floating point values such as your temp.
You need to convert your temp into bytes ( and then back on the other side). The suggested way is using struct.pack() like this:
import struct databytes = struct.pack('d', temp) s.send(databytes)
this is for Python doubles (64 bits floating points), if you want C style 32 bit floats (losing precision but using less bytes) use f instead of d. See the struct docs for more options.
here is the code:
from network import LoRa
#pycom.heartbeat(False) #it turns the pycoms led off
#starting the procedure
lora = LoRa(mode=LoRa.LORAWAN)
print("Dev EUI: ", binascii.hexlify(lora.mac()).upper().decode('utf-8'))
create an ABP authentication params
dev_addr = struct.unpack(">l", binascii.unhexlify(''))
nwk_swkey = binascii.unhexlify('')
app_swkey = binascii.unhexlify('***********')
#joining the network
lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey))
wait until the network is joined
while not lora.has_joined():
print("Joined LoRa Network")
#remove all the non-default channels
create a raw LoRa socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
get the packet received (if any)
data = s.recv(64)
Can you post the part of your code where you create the LoRa socket and join the network?
Alternatively follow the examples:
Finally make sure your LoPy firmware is up to date.