// Unary plus-operator to cast string result of toFixed to a number:
tlak: +dekodovany_tlak.toFixed(2),
function Decoder(bytes, port) {
// Decode plain text; not recommended
return String.fromCharCode.apply(null, bytes);
if you are an idiot like me and need to decode some text, paste this into your decoder payload function section.
this took me 2 hours to figure out. hope it saves you an hour and 55 minutes.
function Decoder(bytes, port) {
// Decode plain text; not recommended
var text = String.fromCharCode.apply(null, bytes);
return{
text: text,
hello, have you any example please of converting a text in bytes. I’m trying to do so with c++ on raspberry but fail!
The purpose is to send some informations of macaddress of conneted devices.
My string looks like: “54:65:00:AF:99 \n 78:87:09:AR:90 …” i get it in std::string format and i want to split it into bytes[]. thanks!
Hey Arjan
Just wanted to say thanks. You are one of the best communicators / educators I have seen on here. Thanks man.
Question from another dummy…
I have a BME680 sensor is working with my Pi-Zero and I can pull temp, pressure and voc data. The Pi-Supply Lora phat is connected and sending data. My Pi-Supply gateway works fine, I’m connected to TTN and passing decoded variables across to TagoIO which visualizes them. So all great… however i’m having trouble with decoding the data I’m sending to the TTN.
I’m using the “hello world” example script, supplied as part of the setup tutorial, as a template and have added the code to pull the data from the sensor. I’m converting that to HEX and the using the lora.send(temp). temp= the variable containing the hex of the temp integer. Packets are being received but my attempts to decode are failing. My assumption is that the data is being sent as ASCII text.
I have a question in with Pi Supply as to how the RAK811 library is encoding the data using the send command and also how do I decode it in the TTN?
If I use the raw rak811 send command i get…
rak811 -v send --port 1 FF TTN sees 46 46 in the payload
rak811 -v send --port 1 255 TTN sees 32 35 35 in the payload
rak811 -v send --port 1 01 TTN sees 30 31 in the payload
rak811 -v send --port 1 --binary 011001 TTN sees - 01 10 01 in the payload
I’m struggling to work out what the code is doing so I can clean it up. Based on the other post I’m assuming this is being seen as ASCII. How do I need to format my data?
I’d like to send the my temperature integer as HEX. So I was assuming that I would take 25.3456, shorten it to 2 decimal places *100 and send as 2 bits, 09c4. I’ve read about how to be ever more efficient but for now I’d be happy to have this work. Any pointers?
Python code from a coder of 3 months experience…
#!/usr/bin/env python3
from rak811 import Mode, Rak811
import time
import board
from busio import I2C
import adafruit_bme680
i2c = I2C(board.SCL, board.SDA)
bme680 = adafruit_bme680.Adafruit_BME680_I2C(i2c, debug=False)
bme680.sea_level_pressure = 1013.25
while True:
print("\nTemperature: %0.1f C" % bme680.temperature)
print("Gas: %d ohm" % bme680.gas)
print("Humididty: %0.1f %%" % bme680.humidity)
print("Pressure: %0.3f hPa" % bme680.altitude)
temp = (bme680.temperature)
temp = temp*100
temp = round(temp, 0)
tempH = hex(int(temp)).lstrip("0x")
print(tempH)
gas = (bme680.gas)
gasH = hex(int(gas)).lstrip("0x")
print(gasH)
lora = Rak811()
lora.hard_reset()
lora.mode = Mode.LoRaWan
lora.band = 'EU868'
lora.set_config(dev_eui='3939353461xxxxxx',
app_eui='70B3D57ED00xxxxx',
app_key='D4CDC8FCB4C407093796E4298Axxxxxx')
lora.join_otaa()
lora.dr = 5
lora.send(tempH)
time.sleep(1)
rak811 -v send --port 1 --binary (tempH)
lora.close()
time.sleep(60)
TTN Java decoder I’ve been messing about with this…I have no Java knowledge/skills etc
function Decoder(bytes, port) {
var gas = (bytes[1] << 23 ) | bytes[0];
var temp = (bytes[0] << 0) | bytes[0];
return {
gas: gas,
temp: temp
My assumption is that the data is being sent as ASCII text.
rak811 -v send --port 1 FF TTN sees 46 46 in the payload
rak811 -v send --port 1 255 TTN sees 32 35 35 in the payload
rak811 -v send --port 1 01 TTN sees 30 31 in the payload
rak811 -v send --port 1 --binary 011001 TTN sees - 01 10 01 in the payload
Indeed, you’re sending text. That text happens to be the hexadecimal or even binary representation of the bytes you actually want to send, but: it’s still text. (“Hello world” examples are evil for LoRaWAN.)
nospmoth:
I’d like to send the my temperature integer as HEX.
Not really. You’ll want to send binary data; bits or bytes. Those don’t care about a human readable representation, such as hexadecimal.
So, the question for the other readers is: how to send bytes using Python?
Aside: JavaScript is not (at all) the same as Java. But if you would have been sending bytes, then your decoder would almost be fine, though you’ll want to check on shifting bytes, so: 8 bits at a time: var gas = bytes[1]<<8 | bytes[0]
. And on not using the index [0]
multiple times, and support negative temperatures, like explained above too: var temp = (bytes[2]<<24>>16 | bytes[3]) / 100
makes more sense, depending on the Python code you’ll end up with. Also, please see How do I format my forum post? [HowTo].
Arjan,
I used your post Jan 7 '17 for reference. I know this post is old, but perhaps you can help me out?
I am struggling with decoding bytes from a Lora-Sensor.
I coded the sensor-controller (Arduino MKR WAN) as follows.
The data comes off a very sensitive inclinometer, so I want to keep accuracy up to 10-e6
co->rRoll and co->rPitch are double
long l_rRoll = (long)(((double)co->rRoll) * 10e6 ); //mRad
long l_tRoll = (long)(((double)co->tRoll) * 10e6 ); //mRad
char msg[7] = {0,1,2,3,4,5,6};
//Placeholder
msg[0]=0;
//Roll
msg[1] = (byte) (l_rRoll >> (0*8));
msg[2] = (byte) (l_rRoll >> (1*8));
msg[3] = (byte) (l_rRoll >> (2*8));
//Pitch
msg[4] = (byte) (l_rPitch >> (2*8));
msg[5] = (byte) (l_rPitch >> (1*8));
msg[6] = (byte) (l_rPitch >> (0*8));
In the decoder I have the following (I multiply by 1000 to get to rad from mRad):
decoded.rRoll = ( bytes[3]<<16 | bytes[2]<<8 | bytes[1] ) / 10e6 * 1000.0;
decoded.rPitch =( bytes[6]<<16 | bytes[5]<<8 | bytes[4] ) / 10e6 * 1000.0;
The value for Roll is positive (+5.901337) and returns properly: “rRoll”: 5.9013
The value for Pitch is negative (-0.702381) but does return properly: “rPitch”: 5.8674
What am I doing wrong?