Reputation: 1089
I am facing a small issue while sending my data through LoRa device A. I am sending a hexadecimal string which is defined either as a String or char string ( I only send one of those but with the same outcome so far)
String packet = "025555AD4148E1BE4100A06E421954C5BB";
//char data[] = "025555AD4148E1BE4100A06E421954C5BB";
Nevertheless, when I receive it at the back end, the string looks like this in base64.
msg.payload = MDJhYmFhNmE0MTUyYjhjNDQxMDBjNDgwNDIwMDAwMDcwOQ==
that is actually different from a base64 string received into a different device (LoRa B), even though the sent payload was the same, this second device ( LoRa B device ) receives this msg.payload = AquqakFSuMRBAMSAQgAABwk=
If I decoded LoRA and LoRa B base64 in nodejs with the same function
var b = new Buffer(msg.payload,'base64')
I get the following bunch of characters that are not my hexadecimal string
30326162616136613431353262386334343130306334383034323030303030373039
<= LoRa A
02ABAA6A4152B8C44100C4804200000709
<= LoRa B
So what I think is happening here is that the original hexadecimal string is being split into chars and sent through Lora. Thus what I get is the ascii representation of the hexadecimal, am I right?
The next question would be, how can I get my original hexadecimal string?
Thanks in advance
Regards!
EDIT:
As my educated guess was suggesting, the issue seems to lie on the way the payload is process before being sent and not in the base64 encode/decode
payload = 'MDJhYmFhNmE0MTUyYjhjNDQxMDBjNDgwNDIwMDAwMDcwOQ==';
b = new Buffer(payload,'base64')
console.log("Buffer b raw ");
console.log(b);
console.log("Buffer b stringfied ");
console.log(b.toString());
Returns
Buffer b raw
<Buffer 30 32 61 62 61 61 36 61 34 31 35 32 62 38 63 34 34 31 30 30 63 34 38 30 34 32 30 30 30 30 30 37 30 39>
Buffer b stringfied
02abaa6a4152b8c44100c4804200000709
Looking into macTransmit
function in the code that is being used to transmit in the device, it can be seen that they are converting the packet
into HEX chars
for (int i = 0; i < size; ++i) {
this->loraStream->print(static_cast<char>(NIBBLE_TO_HEX_CHAR(HIGH_NIBBLE(payload[i]))));
this->loraStream->print(static_cast<char>(NIBBLE_TO_HEX_CHAR(LOW_NIBBLE(payload[i]))));}
Upvotes: 1
Views: 1438
Reputation: 365
Your LoRa client library expects you to give it an array of bytes to send, not a string of hex digits.
To send the bytes <025555AD4148E1BE4100A06E421954C5BB>, you need to initialize your packet as:
char packet[] = {0x02, 0x55, 0x55, 0xAD, 0x41, 0x48, 0xE1, 0xBE, 0x41, 0x00, 0xA0, 0x6E, 0x42, 0x19, 0x54, 0xC5, 0xBB};
When you send a string as you did in the OP, that's an array of bytes, too. But each byte is the ASCII encoding of a single hex digit (two hex digits make up a byte).
If you look at this string of ASCII characters
char data[] = "025555AD4148E1BE4100A06E421954C5BB";
as bytes it would start with <30>
, because <30>
is the ASCII encoding of the character '0'. Then would come <32>
, because that's the encoding for ASCII character '2'. So rather than the single byte <02>
at location data[0]
, your message starts with two bytes <30 32>
. You can see where this goes, right? That long buffer <Buffer 30 32 61 62 61 61 36 61 34 31 35 32 62 38 63 34 34 31 30 30 63 34 38 30 34 32 30 30 30 30 30 37 30 39>
is exactly the ASCII representation of the message you sent, "025555AD4148E1BE4100A06E421954C5BB". This confirms that there's nothing wrong with your base64 conversion.
The for
loop you show confirms that the library expects bytes. It is taking each byte of the packet, splitting it into two nibbles, and converting each nibble (hex digit) into the corresponding ASCII character (0-F). It's sending the packet as text characters because the Microchip RN2483 LoRa module is designed to communicate over a serial-style protocol with its host controller. Internally, it converts the text version of the packet back into bytes before transmission.
Upvotes: 2