Cayenne_encoding

Hello people. I’m new to the cayenne format still trying to understand completely how it works.!!

In the example ** [arduino-device-lib]/ examples/ CayenneLPP/ CayenneLPP.ino ** for encoding the sensor data by ‘johanstokking’
//
lpp.reset();
lpp.addTemperature(1. 22.5);
lpp.addBarometricPressure(2, 1073.21);
lpp.addGPS(3, 52.37365, 4.88650, 2);
//
are these sensor values predefined for the sake of example?
what if I want to convert the measured values into cayenne format before sending it to a server. say for ex: i have three values from a dht sensor. I want to encode them in cayenne lpp format and then transmit it.

if you are referring to CayenneLPP.ino then have a look at this https://www.thethingsnetwork.org/docs/devices/arduino/api/cayennelpp.html
and for cayenne LPP then http://mydevices.com/cayenne/docs/lora/

@shramik_salgaonkar. thanks for your reply. I have been using the same links for quiet some time now. what I want to focus on the initial stages. I’m not making use of the cayenne dashboard as of now. I’m just trying to encode the measured sensor value in cayenne lpp format. Can tell me if i’m missing something here.

if you have stored your measured sensor value in a variable x then you can just send it using CayenneLPP.ino:

lpp.addTemperature(1, x);

where x is the variable having sensor data and 1 is the channel number.

1 Like

@shramik_salgaonkar thank you I understood. I’ll try that out and get back to you.

1 Like

Hello @shramik_salgaonkar. I have tried something is the approach correct?

#define LPP_Temperature 103
#define LPP_RELATIVE_HUMIDITY 104
#define LPP_TEMPERATURE_SIZE 4
#define LPP_RELATIVE_HUMIDITY_SIZE 3

CayenneLPP lpp(51);

uint8_t addTemperature(uint8_t channel, float celsius);
uint8_t addRelativeHumidity(uint8_t channel, float rh);
uint8_t addAnalogOutput(uint8_t channel, float value);

void loop()
{
free(buffer);
buffer = (uint8_t*) malloc(size);
cursor = 0;

//implementation of temperature
if ((cursor + LPP_TEMPERATURE_SIZE) > maxsize) {
return 0;
}
int16_t val = celsius * 10;
buffer[cursor++] = channel;
buffer[cursor++] = LPP_TEMPERATURE;
buffer[cursor++] = val >> 8;
buffer[cursor++] = val;

return cursor;

//implementation of humidity
 if ((cursor + LPP_RELATIVE_HUMIDITY_SIZE) > maxsize) {
    return 0;
}
buffer[cursor++] = channel; 
buffer[cursor++] = LPP_RELATIVE_HUMIDITY; 
buffer[cursor++] = rh * 2; 

return cursor;

this is how i’m trying to implement the cayenne format before sending it.

lpp.addTemperature(1, x);

or I should perform like this.

if you are using the TTN network server then better use their library. as you dont have to manually generate the payload format.

I’m not sure what libraries they have.

If I have simple code running in arduino IDE like below:

DHT.read11(dht_apin);

Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("%  ");
Serial.print("temperature = ");
Serial.print(DHT.temperature); 
Serial.println("C  ");

I have my values in DHT.humidity and DHT.temperature. I want to follow cayenne format for these values. can I follow like this. Will it do the job for me?

#include <CayenneLPP.h>
{
lpp.addTemperature(1, (DHT.temperature));
lpp.addHumidity(1, (DHT.humidity));
}

yup, that should work without the brackets or:

 float t = DHT.temperature; 
 float h = DHT.humidity;

{
lpp.addTemperature(1, t);
lpp.addHumidity(1, h);
}

@shramik_salgaonkar. sure thanks for the information. i’ll get back to you.

@shramik_salgaonkar. with the above mentioned im not able to print it in the serial monitor. Is it possible to see the payload in cayenne format in serial monitor too.?

what is the current output you are getting? is there any issue with the code and not working as you want?

I’m trying to encode the payload in cayenne lpp before it is sent to the middleware. And i’m expecting the result in the serial monitor as well in this format. I’m assuming it can be done, i’m not sure if it’s possible. I don’t even know how the cayenne lpp output looks like.

can you share the entire code you are using with the TTN library.

Hello @shramik_salgaonkar. Here is the code that I’m trying to implement cayenne format. I have already made some changes with respect to cayenne format which I have been working on for few days.

#include <TheThingsNetwork.h>
#include <DHT.h>
#include <CayenneLPP.h>

#define loraSerial Serial1
#define debugSerial Serial

// AppEUI and AppKey are always all 0 for Marvinboards
const char *appEui = "0000000000000000";

const char *appKey = "00000000000000000000000000000000";

// Replace REPLACE_ME with TTN_FP_EU868 or TTN_FP_US915
#define freqPlan TTN_FP_EU868

// Grove Port next to the USB connector
#define DHTPIN A3
#define INDUPIN A0
#define LIGHTPIN A5

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);

// define and init variables
uint16_t induVal = 0;
bool induState = false;
uint16_t lightVal = 0;
bool errorState = false;
uint16_t humidity = 0;
uint16_t temperature = 0;
//CayenneLPP lpp(uint8_t size)
//CayenneLPP lpp(51); //maximum payload size that can be sent

void setup() {
    loraSerial.begin(57600);
    debugSerial.begin(9600);

    // Wait a maximum of 10s for Serial Monitor
    while (!debugSerial && millis() < 10000)
    ;

    debugSerial.println("-- STATUS");
    // DevEUI is print with showStatus
    ttn.showStatus();

    debugSerial.println("-- JOIN");
    ttn.join(appEui, appKey);

    dht.begin();
}

void loop()
{
    debugSerial.println("Device Information");
    debugSerial.println("-------------------------------------------");
    ttn.showStatus();
    debugSerial.println();
    debugSerial.println("Sensor Information");
    debugSerial.println("-------------------------------------------");
    debugSerial.println("DATA");
    debugSerial.println();
    // Read sensor values and multiply by 100 to effictively have 2 decimals
    humidity = dht.readHumidity(false) * 100;

    // false: Celsius (default)
    // true: Farenheit
    temperature = dht.readTemperature(false) * 100;

    debugSerial.print("Temperature: ");
    debugSerial.println(temperature);
    debugSerial.print("Humidity: ");
    debugSerial.println(humidity);

    // INDU
    induVal = analogRead(INDUPIN);
    if (induVal < 512) {
    induState = false;
    } else {
    induState = true;
    }
    debugSerial.print("InduSensorState: ");
    debugSerial.println(induState);

    // LIGHT
    lightVal = analogRead(LIGHTPIN);

    if (lightVal < 512) {
    errorState = false;
    } else {
    errorState = true;
    }
    debugSerial.print("ErrorState: ");
    debugSerial.println(errorState);

    //define the data types for temperature and relativeHumidity values in float
     //float t = temperature;
     //float h = humidity;

     //convert the payload into cayenne format
     //lpp.reset();
     //lpp.addTemperature(1, t);
     //lpp.addRelativeHumidity(2, h);
    //
    // PAYLOAD
    // intially payload was 6 increased 4 bytes for two channels and two data types
    byte payload[10] = {0};
    debugSerial.println("Payload");
    payload[2] = highByte(temperature);
    payload[3] = lowByte(temperature);
    payload[6] = highByte(humidity);
    payload[7] = lowByte(humidity);
    payload[8] = errorState;
    payload[9] = induState;





    int loop;
    for(loop = 0; loop < 6; loop++)
    debugSerial.println(payload[loop]);

    // activate to send data through LoRaWAN
    ttn.sendBytes(payload, sizeof(payload));

    debugSerial.println();
    delay(20000);
}
debugSerial.println("Payload");
payload[2] = highByte(temperature);
payload[3] = lowByte(temperature);
payload[6] = highByte(humidity);
payload[7] = lowByte(humidity);
payload[8] = errorState;
payload[9] = induState;





int loop;
for(loop = 0; loop < 6; loop++)
debugSerial.println(payload[loop]);

// activate to send data through LoRaWAN
ttn.sendBytes(payload, sizeof(payload));

debugSerial.println();
delay(20000);

this code is not sending data in cayenne format. you just need to use:

 float t = temperature;
 float h = humidity;

 //convert the payload into cayenne format
 lpp.reset();
 lpp.addTemperature(1, t);
 lpp.addRelativeHumidity(2, h);

@shramik_salgaonkar. Yes I have included this and I have commented it again because it wasn’t working. I’m trying to understand that the variables declared for reading the values from a dht sensor and cayenne format must be same. Is that correct?

you mean the variables t and h? yes you need to use the same variables to send data.

@shramik_salgaonkar this is the output i’m getting. This is not how the cayenne format o/p look like is it?

Current humidity = 54.00% temperature = 22.20C
Current humidity = 54.00% temperature = 22.20C
Current humidity = 55.00% temperature = 22.20C
Current humidity = 55.00% temperature = 22.20C

I tried this and the still the same output. It asked me to declare ‘temperature’, ‘humidity’ separately apart from the ‘t’, ‘h’. I did no changes in the o/p. Strange thing is nothing is printing from the setup part into the monitor.

@shramik_salgaonkar. can you also suggest me some documentation/links. For other types of format apart from cayenne lpp.