From a6642ea8b64e4e9a9650da6587ca37bcc56faca9 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 16 Jun 2018 23:14:49 +0200 Subject: [PATCH] CayenneLPP payload encoder added (experimental) --- src/globals.h | 2 +- src/main.cpp | 2 +- src/paxcounter.conf | 3 +- src/payload.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++-- src/payload.h | 45 +++++++++++++++++++++++++----- 5 files changed, 108 insertions(+), 12 deletions(-) diff --git a/src/globals.h b/src/globals.h index e9290285..262fc4c8 100644 --- a/src/globals.h +++ b/src/globals.h @@ -51,7 +51,7 @@ extern TTNplain payload; #elif (PAYLOAD_ENCODER == 2) extern TTNserialized payload; #elif (PAYLOAD_ENCODER == 3) -//extern CayenneLPP payload; +extern CayenneLPP payload; #else #error "No valid payload converter defined" #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5a3a6589..025365dd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -76,7 +76,7 @@ TTNplain payload(PAYLOAD_BUFFER_SIZE); #elif (PAYLOAD_ENCODER == 2) TTNserialized payload(PAYLOAD_BUFFER_SIZE); #elif (PAYLOAD_ENCODER == 3) -//CayenneLPP payload(PAYLOAD_BUFFER_SIZE); +CayenneLPP payload(PAYLOAD_BUFFER_SIZE); #else #error "No valid payload converter defined" #endif diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 31423a3c..b1c31537 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -41,8 +41,9 @@ #define SEND_SECS 30 // [seconds/2] -> 60 sec. #define MEM_LOW 2048 // [Bytes] low memory threshold triggering a send cycle #define RETRANSMIT_RCMD 5 // [seconds] wait time before retransmitting rcommand results -#define PAYLOAD_ENCODER 1 // select payload encoder: 1 = Plain [default], 2 = Lora-hserialized, 3 = Cayenne LPP +#define PAYLOAD_ENCODER 3 // select payload encoder: 1 = Plain [default], 2 = Lora-serialized, 3 = CayenneLPP #define PAYLOAD_BUFFER_SIZE 51 // maximum size of payload block per transmit +#define CAYENNE_LPP 1 // uncomment this, if you need Cayenne LPP payload encoding // Default LoRa Spreadfactor #define LORASFDEFAULT 9 // 7 ... 12 SF, according to LoRaWAN specs diff --git a/src/payload.cpp b/src/payload.cpp index c1c79c86..f1a9df55 100644 --- a/src/payload.cpp +++ b/src/payload.cpp @@ -81,7 +81,7 @@ void TTNplain::addStatus(uint16_t voltage, uint64_t uptime, float cputemp) { TTNserialized::TTNserialized(uint8_t size) { buffer = (uint8_t *)malloc(size); - //LoraEncoder message(buffer); + // LoraEncoder message(buffer); } TTNserialized::~TTNserialized(void) { free(buffer); } @@ -129,4 +129,68 @@ void TTNserialized::addStatus(uint16_t voltage, uint64_t uptime, message.writeTemperature(cputemp); } -/* ---------------- Cayenne LPP format ---------- */ \ No newline at end of file +/* ---------------- Cayenne LPP format ---------- */ + +#ifdef CAYENNE_LPP + +CayenneLPP::CayenneLPP(uint8_t size) { + buffer = (uint8_t *)malloc(size); + cursor = 0; +} + +CayenneLPP::~CayenneLPP(void) { free(buffer); } + +void CayenneLPP::reset(void) { cursor = 0; } + +uint8_t CayenneLPP::getSize(void) { return cursor; } + +uint8_t *CayenneLPP::getBuffer(void) { return buffer; } + +void CayenneLPP::addCount(uint16_t value1, uint16_t value2) { + buffer[cursor++] = LPP_COUNT_WIFI_CHANNEL; + buffer[cursor++] = LPP_ANALOG_INPUT; + buffer[cursor++] = value1 >> 8; + buffer[cursor++] = value1; + buffer[cursor++] = LPP_COUNT_BLE_CHANNEL; + buffer[cursor++] = LPP_ANALOG_INPUT; + buffer[cursor++] = value2 >> 8; + buffer[cursor++] = value2; +} + +void CayenneLPP::addGPS(gpsStatus_t value) { + int32_t lat = value.latitude * (int32_t) 10000; + int32_t lon = value.longitude * (int32_t) 10000; + int32_t alt = value.altitude * (int32_t) 100; + + buffer[cursor++] = LPP_GPS_CHANNEL; + buffer[cursor++] = LPP_GPS; + buffer[cursor++] = lat >> 16; + buffer[cursor++] = lat >> 8; + buffer[cursor++] = lat; + buffer[cursor++] = lon >> 16; + buffer[cursor++] = lon >> 8; + buffer[cursor++] = lon; + buffer[cursor++] = alt >> 16; + buffer[cursor++] = alt >> 8; + buffer[cursor++] = alt; +} + +void CayenneLPP::addConfig(configData_t value) { + buffer[cursor++] = LPP_ADR_CHANNEL; + buffer[cursor++] = LPP_DIGITAL_INPUT; + buffer[cursor++] = value.adrmode; +} + +void CayenneLPP::addStatus(uint16_t voltage, uint64_t uptime, float cputemp) { + buffer[cursor++] = LPP_BATT_CHANNEL; + buffer[cursor++] = LPP_ANALOG_INPUT; + buffer[cursor++] = voltage >> 8; + buffer[cursor++] = voltage; + + buffer[cursor++] = LPP_TEMP_CHANNEL; + buffer[cursor++] = LPP_TEMPERATURE; + buffer[cursor++] = (uint16_t) cputemp >> 8; + buffer[cursor++] = (uint16_t) cputemp; +} + +#endif // CAYENNE_LPP \ No newline at end of file diff --git a/src/payload.h b/src/payload.h index 03311c76..ae30baf5 100644 --- a/src/payload.h +++ b/src/payload.h @@ -14,11 +14,8 @@ public: uint8_t getSize(void); uint8_t *getBuffer(void); - // application payloads void addCount(uint16_t value1, uint16_t value2); void addGPS(gpsStatus_t value); - - // payloads for get rcommands void addConfig(configData_t value); void addStatus(uint16_t voltage, uint64_t uptime, float cputemp); @@ -37,11 +34,8 @@ public: uint8_t getSize(void); uint8_t *getBuffer(void); - // application payloads void addCount(uint16_t value1, uint16_t value2); void addGPS(gpsStatus_t value); - - // payloads for get rcommands void addConfig(configData_t value); void addStatus(uint16_t voltage, uint64_t uptime, float cputemp); @@ -50,4 +44,41 @@ private: LoraEncoder message(byte *buffer); }; -#endif +#ifdef CAYENNE_LPP +// LPP channels +#define LPP_GPS_CHANNEL 20 +#define LPP_COUNT_WIFI_CHANNEL 21 +#define LPP_COUNT_BLE_CHANNEL 22 +#define LPP_BATT_CHANNEL 23 +#define LPP_ADR_CHANNEL 25 +#define LPP_TEMP_CHANNEL 26 +// LPP types +#define LPP_GPS 136 // 3 byte lon/lat 0.0001 °, 3 bytes alt 0.01m +#define LPP_TEMPERATURE 103 // 2 bytes, 0.1°C signed +#define LPP_DIGITAL_INPUT 0 // 1 byte +#define LPP_DIGITAL_OUTPUT 1 // 1 byte +#define LPP_ANALOG_INPUT 2 // 2 bytes, 0.01 signed + +class CayenneLPP { +public: + CayenneLPP(uint8_t size); + ~CayenneLPP(); + + void reset(void); + uint8_t getSize(void); + uint8_t *getBuffer(void); + + void addCount(uint16_t value1, uint16_t value2); + void addGPS(gpsStatus_t value); + void addConfig(configData_t value); + void addStatus(uint16_t voltage, uint64_t uptime, float cputemp); + +private: + uint8_t *buffer; + uint8_t maxsize; + uint8_t cursor; +}; + +#endif // CAYENNE_LPP + +#endif // _PAYLOAD_H_