Guys,
Not really sure where to post this but Im seeking help again…
Following on from this:
and this:
and this: https://core-electronics.com.au/tutorials/encoding-and-decoding-payloads-on-the-things-network.html
I want to be able to send Battery Voltage and 2x Temperature readings to TTN, ideally in one s.send.
I can send the data as 3 lines of code, i.e:
s.send(bat_volts)
s.send(temp1)
s.send(temp2)
but this seems problematic making sense of the data when it pops into a TTN event in Node-Red (via TTN).
I was hoping to be able to:
s.send(3 bits of data all at once)
That way, when the TTN-event in Node-Red is triggered I can query and manipulate the data as appropriate.
I have looked at s.send(bytes([1, 2, 3])) and this seems to give the answer Im looking for in TTN, but I have no idea yet how to combine and ustruct.pack the 3 variables (floats).
I also read here (ustruct) about ustruct.pack(fmt, v1, v2,). This appears to pack v1 & v2 together but then I cant unpack the two values correctly with ustruct,unpack(fmt, buffer).
To try and show my thinking so far:
cst = 1.234
cat = 10.2
print("CST = ", cst)
print("CAT = ", cat)
# encode the packet, so that it's in BYTES (TTN friendly)
cst_pack = ustruct.pack('f', cst)
cat_pack = ustruct.pack('f', cat)
combined_pack = ustruct.pack('f', cst, cat)
print("cst_pack:", cst_pack)
print("cat_pack:", cat_pack)
print("combined_pack:", combined_pack)
print("Now lets decode...")
print ("Unpacked cst is:", ustruct.unpack('f', cst_pack)[0])
print ("Unpacked cat is:", ustruct.unpack('f', cat_pack)[0])
print ("Unpacked Combined is:", ustruct.unpack('f', combined_pack)[0])
#print ("Unpacked Combined is:", ustruct.unpack('f',combined_pack)[1])
This appears to show that combined_pack is the result of packing the two variables together but I have no ideas on how to un-pack them back to the two variables again…
Any chance you can give me some pointers please?
Thx
J