Read LoRa CayenneLPP message and decode

Hi,
I’m new on CayenneLpp world.

Using RFM95W SX1276 as sender and TTGO LoRa32 as receiver, with sandeepmistry/LoRa@^0.7.2 library.

Sending multiple sensor’s data (temp, hum, luminance), how to read and decode the complete LoRa message on receiver ?

Thank’s

there is an example for receiver arduino-LoRa/LoRaReceiver.ino at master · sandeepmistry/arduino-LoRa · GitHub

Sorry, I know this sample, but it is too simple… :relieved:

I’m using the CayenneLPP message protocol to send and receive the data.

Did you try the code? what are you receiving?

Hi shramik,
this is the Sender code :

  if (LoRa.beginPacket())
  {
    LoRa.write(lpp.getBuffer(), lpp.getSize()); 
    if (LoRa.endPacket())
    {
      Serial.println("Lora Transmission OK");
    }
    else
    {
      Serial.println("Lora Transmission Error");
    }
  }
  else
  {
    Serial.println("Lora Transmission Error");
  }

  Serial.print("Bytes sent : ");
  Serial.println(lpp.getSize());

  uint8_t *payload = lpp.getBuffer();

  for (unsigned char i = 0; i < lpp.getSize(); i++)
  {
    Serial.print("0x");
    Serial.print(payload[i], HEX);
    Serial.print(" ");
  }

  // print checkmark
  Serial.println();
  Serial.print("LoRa message sent  \u2714\r\n");

  // DECODE

  // uint8_t *payload = lpp.getBuffer();
  StaticJsonDocument<512> jsonBuffer;
  JsonArray root = jsonBuffer.to<JsonArray>();

  lpp.decode(lpp.getBuffer(), lpp.getSize(), root);

  serializeJsonPretty(root, Serial);
  Serial.println();

This is the sender code result

Bytes sent : 11
0x1 0x67 0x0 0xD4 0x2 0x68 0x4A 0x3 0x74 0x1 0xF6
LoRa message sent  ✔
[
  {
	"channel": 1,
	"type": 103,
	"name": "temperature",
	"value": 21.2
  },
  {
	"channel": 2,
	"type": 104,
	"name": "humidity",
	"value": 37
  },
  {
	"channel": 3,
	"type": 116,
	"name": "voltage",
	"value": 5.02
  }
]

Then, the receiver code

uint8_t *payload;

int packetSize = LoRa.parsePacket();
if (packetSize)
{
// received a packet
Serial.printf("Received %i bytes, RSSI : %i  ", packetSize, LoRa.packetRssi());

while (LoRa.available())
{
  payload += (uint8_t)LoRa.read();
}

// print checkmark
Serial.printf(" \u2714\r\n");

for (unsigned char i = 0; i < packetSize; i++)
{
  Serial.print("0x");
  Serial.print(payload[i], HEX);
  Serial.print(" ");
}

Serial.println();

Serial.print("|");
for (unsigned int i = 0; i < packetSize; i++)
{
  Serial.print(payload[i] >> 4, HEX);
  Serial.print(payload[i] & 0xF, HEX);
}
Serial.print("|");

Serial.println();

The received message is

Received 11 bytes, RSSI : -62   ✔
0x45 0x3E 0x0 0x42 0x7F 0x40 0x0 0x72 0x49 0x49 0x49
|453E00427F400072494949|
Received 11 bytes, RSSI : -62   ✔
0xF 0x38 0x44 0x44 0x38 0x44 0xFC 0x4A 0x4A 0x4A 0x34
|0F3844443844FC4A4A4A34|
Received 11 bytes, RSSI : -62   ✔
0xF0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x1F 0xF8 0x7F
|F0000000000000001FF87F|

My question are :

#1. How to read correcty data on this case
#2. How to print on serial correctly data for debugging (there are two temptatives, are correct there ?)
#3. How to decode CayennLPP message from a payload
#4. How to extract a single value (e.g. temperature, or voltage) from received message

Many thanks to all.

The received message does not look like the cayenneLPP format.
can you try:-

void loop() {

  // see if a packet was received

  int packetSize = LoRa.parsePacket();

  if (packetSize) {

    // received packet

    Serial.println(”“);

    Serial.println(“Received packet: “);

    // read the packet

    String message = ”“;

    while (LoRa.available()) {

      message += (char)LoRa.read();

    }
    // print the Packet and RSSI

    Serial.println(message);

    Serial.print(“RSSI: “);

    Serial.println(LoRa.packetRssi());

  }

}

No, the received message is always another content.

Message sent

Lora Transmission OK  ✔  Bytes sent : 11  | 01 67 00 F6 02 68 41 03 74 01 F1 |

Message received

Received 11 bytes, RSSI : -59   ✔  | 41 53 4B 5F 57 44 54 5F 54 49 4D |

I’m struggling to understand.

Did you try the code i shared above? it is not the output it should give.

Can be the different architecture between Sender and Receiver ?

from my platformio.ini

Sender :

[env:sas-sender-struct]
platform = atmelavr
board = pro8MHzatmega328

Receiver

[env:ttgo-lora-receiver-dht11-struct]
platform = espressif32
board = ttgo-lora32-v1

Yes the code as you suggested :

while (LoRa.available())
{
  payload += (char)LoRa.read();
}

just replace this with your code.

? What ???

Surely, wait

while (LoRa.available())
{
  payload = (char)LoRa.read();
}

ERROR

error: invalid conversion from 'char' to 'uint8_t* {aka unsigned char*}' [-fpermissive]
payload = (char)LoRa.read();

can you share the entire code.

Sorry, I missing the new definition of payload.

But defining payload as

char * payload;

the receiver crashing on receiving message.
This is entire code of receiver

void loop()
{
  char * payload;

  int packetSize = LoRa.parsePacket();
  if (packetSize)
  {
	// received a packet
	Serial.printf("Received %i bytes, RSSI : %i  ", packetSize, LoRa.packetRssi());

	// while (LoRa.available())
	// {
	//   payload += (uint8_t)LoRa.read();
	// }

	while (LoRa.available())
	{
	  payload = (char*)LoRa.read();
	}

	Serial.print(" | ");
	for (unsigned int i = 0; i < packetSize; i++)
	{
	  Serial.print(payload[i] >> 4, HEX);
	  Serial.print(payload[i] & 0xF, HEX);
	  Serial.print(" ");
	}
	Serial.print("|");

	Serial.println();

	// String testString = "3E7C3E1C183C7E3C181C57";
	// uint8_t *buffer = (uint8_t *)testString.c_str();

	// StaticJsonDocument<512> jsonBuffer;
	// JsonArray root = jsonBuffer.to<JsonArray>();
	// lpp.decode((uint8_t *)buffer, packetSize, root);

	// serializeJsonPretty(root, Serial);
	// Serial.println();

	// payload.toCharArray(buf, packetSize + 1);
	// Dump payload content for debugging
	// Serial.printf ("LPP Buffer: %s\r\n", printHexBuffer ((uint8_t *)payload, packetSize));
	// String buffera = "00000000000001670101026838037401F6";
	// char buf[buffera.length()];
	// buffera.toCharArray(buf, payload.length());
	// lpp.decode((uint8_t *)buf, packetSize, root);
  }

  delay(10);
}

It should be a String.
can you just copy and paste the code with freq change.

#include <SPI.h>

#include <LoRa.h>

void setup() {

  Serial.begin(9600);

  while (!Serial);

  Serial.println("LoRa Receiver Test");

  // Replace the operating frequency of your shield here.

  // 915E6 for North America, 868E6 for Europe, 433E6 for Asia

  if (!LoRa.begin(868E6)) {

    Serial.println("Starting LoRa failed!");

    while (1);

  }

}

void loop() {

  // see if a packet was received

  int packetSize = LoRa.parsePacket();

  if (packetSize) {

    // received packet

    Serial.println("");

    Serial.println("Received packet: ");

    // read the packet

    String message = "";

    while (LoRa.available()) {

      message += (char)LoRa.read();

    }
    // print the Packet and RSSI

    Serial.println(message);

    Serial.print("RSSI: ");

    Serial.println(LoRa.packetRssi());

  }

}

GOOD ! It working now.
A question : why sending an array of bytes as is
uint8_t *payload = lpp.getBuffer();
I must receive a String ???

Thank’s, many thanks shramik.


This is the final result

Received 11 bytes, RSSI : -61   ✔  | 01 67 00 F9 02 68 41 03 74 01 F3 |
[
  {
	"channel": 1,
	"type": 103,
	"name": "temperature",
	"value": 24.9
  },
  {
	"channel": 2,
	"type": 104,
	"name": "humidity",
	"value": 32.5
  },
  {
	"channel": 3,
	"type": 116,
	"name": "voltage",
	"value": 4.99
  }
]

This is the working receiver code

	void loop()
{
String payload = "";

int packetSize = LoRa.parsePacket();
if (packetSize)
{
// received a packet
Serial.printf("Received %i bytes, RSSI : %i  ", packetSize, LoRa.packetRssi());

while (LoRa.available())
{
  payload += (char)LoRa.read();
}

// print checkmark
Serial.printf(" \u2714 ");

Serial.print(" | ");
for (unsigned int i = 0; i < packetSize; i++)
{
  Serial.print(payload[i] >> 4, HEX);
  Serial.print(payload[i] & 0xF, HEX);
  Serial.print(" ");
}
Serial.print("|");

Serial.println();

// convert String to bytes array
uint8_t *buffer = (uint8_t *)payload.c_str();

StaticJsonDocument<512> jsonBuffer;
JsonArray root = jsonBuffer.to<JsonArray>();
lpp.decode((uint8_t *)buffer, packetSize, root);

serializeJsonPretty(root, Serial);
Serial.println();
}

delay(10);
}
1 Like