Cayenne_encoding

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.

First of all, which device are you using?

‘marvin LoRa. RN2483’

the network server is TTN?
so lets start with basic code arduino-device-lib/CayenneLPP.ino at master · TheThingsNetwork/arduino-device-lib · GitHub .
Just add your appEui appKey and freqPlan, and see if you get any output in the serial monitor.

Yes. I’ll try to do that now.

@shramik_salgaonkar. I tried the sample code with the ‘status’ and ‘join’ details i’m getting the following output:

-- LOOP
Sending: mac tx uncnf 1 016700E1027329EC038807FDD800BEE10000C8
Successful transmission

this is the cayenne LPP format.
01 —> channel 1
67 —> temperature data type
00E1 → data
similarly for others

@shramik_salgaonkar. thanks i understood it. My assumption of expected output was wrong. I’m getting the o/p. I’ll try and implement it to my original code.

Can you suggest me in this regard.?

@shramik_salgaonkar.

I meant are there any other formats similar to Cayenne LPP.?

What datatype can I use for inductive sensor from cayenne docs. The function of this sensor is to send a ‘boolean’ payload?

i am not familer with any other. for boolean you can use addDigitalInput with 1 or 0.

sure no problem.

I’ll try this out.

In my original code which I have posted above is it possible to send whole of the payload (payload[10]) in cayenne format. Is it possible to send a datatype which is not there in the cayenne docs?

why are you combining two separate code? there are limited data types and only those are supported.

I’m not combining two separate code. I will still be using only my original code. Before using cayenne I had put all the datatypes into one single payload (payload[6] like this) and used to send to the ttn. I was thinking if I could do that with cayenne format. Can I write one manually which is not there in the list?