Ah! BIN is being received and printed to HEX. So does that explain when encoding with this json
{"delay": 241, "myFlags": [true, true, true, true, true, false, true, true]}
on TTN to downlink, I am receiving the encoded 3-byte payload “F1 00 FB” that prints the second byte “0” instead of “00”?
data array[i], HEX: F1 0 FB
data array[i], BIN: 11110001 0 11111011
Using a delay greater than 255 …
{"delay": 600, "myFlags": [true, true, true, true, true, false, true, true]}
of course extends into the second byte:
data array[i], HEX: 58 2 FB
data array[i], BIN: 1011000 10 11111011
Here are the TTN custom Payload Formats custom_decoder_2.txt (2.8 KB) and custom_encoder_2.txt (2.1 KB) .
On the node, this receive allowed for decoding when receiving downlink on port 2:
//from: https://github.com/GrumpyOldPizza/ArduinoCore-stm32l0/issues/67
void callback_onReceive() {
onReceive = false;
if (LoRaWAN.parsePacket()) {
uint32_t size;
uint8_t data[256];
uint8_t portNum = LoRaWAN.remotePort();
size = LoRaWAN.read(&data[0], sizeof(data));
Serial.println(" ");
Serial.print("portNum = ");
Serial.print(portNum);
Serial.print(" size = ");
Serial.print(size);
Serial.println(" bytes");
if (size) {
Serial.print(" data array[i], HEX: ");
for (int i = 0; i < size; i++) {
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.println();
}
if (size) {
Serial.print(" data array[i], BIN: ");
for (int i = 0; i < size; i++) {
Serial.print(data[i], BIN);
Serial.print(" ");
}
Serial.println();
}
//read and decode the bitmap from lora-serialization library
if (portNum == 2) {
Serial.println("Port 2 downlink");
if (size) {
//see: https://www.thethingsnetwork.org/forum/t/decrypting-messages-for-dummies/4894/20
int setDelay = data[1] << 8 | data[0];
Serial.print(" Delay: ");
Serial.println(setDelay);
uint8_t bitVal = data[2];
Serial.print(" Bit: ");
for (int i = 7; i > -1; i--) {
Serial.print(bitRead(bitVal, i));
Serial.print(", ");
}
Serial.println(" ");
}
} //end if portNum = 2
} //end parsePacket = T
} //end callback on receive
The following links gave some assistance learning more about bytes and bits:
- Decrypting Messages for Dummies (a TTN favourite)
- Convert various numbers into different bases (many out there!)
- bitRead() example (I reversed this to read left to right in my code)
Thank you very much for your guidance @arjanvanb and your point @mfalkvidd!