From ddd5764a8b7f34feff948dfa64b81b644abe7dcd Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 17 Nov 2018 21:39:49 +0100 Subject: [PATCH] BME 680 support: payload converters addes --- include/bme680read.h | 1 - include/globals.h | 6 ++--- include/payload.h | 10 ++++++-- platformio.ini | 2 +- src/TTN/packed_decoder.js | 9 +++++-- src/TTN/plain_decoder.js | 9 +++++++ src/bme680read.cpp | 4 +-- src/payload.cpp | 54 ++++++++++++++++++++++++++++++--------- 8 files changed, 72 insertions(+), 23 deletions(-) diff --git a/include/bme680read.h b/include/bme680read.h index 057cb40d..4573f22f 100644 --- a/include/bme680read.h +++ b/include/bme680read.h @@ -6,7 +6,6 @@ #include #include "Adafruit_BME680.h" -extern Adafruit_BME680 bme; // Make bme instance globally availabe extern bmeStatus_t bme_status; // Make struct for storing gps data globally available diff --git a/include/globals.h b/include/globals.h index b519bb52..1764a308 100644 --- a/include/globals.h +++ b/include/globals.h @@ -47,10 +47,10 @@ typedef struct { } gpsStatus_t; typedef struct { - uint16_t temperature; // Temperature * 100 in degrees Centigrade + float temperature; // Temperature in degrees Centigrade uint16_t pressure; // Barometic pressure in hecto pascals - uint8_t humidity; // Relative humidity in percent - uint32_t gas_resistance; // Resistance in Ohms + float humidity; // Relative humidity in percent + uint16_t gas_resistance; // Resistance in MOhms uint16_t altitude; // Altitude in meters } bmeStatus_t; diff --git a/include/payload.h b/include/payload.h index 5c71a5cd..aaa38566 100644 --- a/include/payload.h +++ b/include/payload.h @@ -10,19 +10,25 @@ #define LPP_BATT_CHANNEL 23 #define LPP_BUTTON_CHANNEL 24 #define LPP_ADR_CHANNEL 25 -#define LPP_TEMP_CHANNEL 26 +#define LPP_TEMPERATURE_CHANNEL 26 #define LPP_ALARM_CHANNEL 27 #define LPP_MSG_CHANNEL 28 +#define LPP_HUMIDITY_CHANNEL 29 +#define LPP_BAROMETER_CHANNEL 30 +#define LPP_GAS_CHANNEL 31 + #endif // MyDevices CayenneLPP 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_TEMPERATURE 103 // 2 bytes, 0.1°C signed MSB #define LPP_DIGITAL_INPUT 0 // 1 byte #define LPP_DIGITAL_OUTPUT 1 // 1 byte #define LPP_ANALOG_INPUT 2 // 2 bytes, 0.01 signed #define LPP_LUMINOSITY 101 // 2 bytes, 1 lux unsigned #define LPP_PRESENCE 102 // 1 byte +#define LPP_HUMIDITY 104 // 1 byte, 0.5 % unsigned +#define LPP_BAROMETER 115 // 2 bytes, hPa unsigned MSB class PayloadConvert { diff --git a/platformio.ini b/platformio.ini index 6abb2b08..f47007a0 100644 --- a/platformio.ini +++ b/platformio.ini @@ -32,7 +32,7 @@ description = Paxcounter is a proof-of-concept ESP32 device for metering passeng release_version = 1.6.63 ; DEBUG LEVEL: For production run set to 0, otherwise device will leak RAM while running! ; 0=None, 1=Error, 2=Warn, 3=Info, 4=Debug, 5=Verbose -debug_level = 3 +debug_level = 0 ; UPLOAD MODE: select esptool to flash via USB/UART, select custom to upload to cloud for OTA upload_protocol = esptool ;upload_protocol = custom diff --git a/src/TTN/packed_decoder.js b/src/TTN/packed_decoder.js index fd9b955e..6050f828 100644 --- a/src/TTN/packed_decoder.js +++ b/src/TTN/packed_decoder.js @@ -41,6 +41,11 @@ function Decoder(bytes, port) { return decode(bytes, [uint8, uint8], ['rssi', 'beacon']); } + if (port === 7) { + // BME680 sensor data + return decode(bytes, [temperature, uint16, humidity, uint16, uint16], ['temperature', 'pressure', 'humidity', 'gas', 'altitude']); + } + } @@ -132,7 +137,7 @@ var temperature = function (bytes) { if (isNegative) { t = -t; } - return t / 1e2; + return +(t / 100).toFixed(1); }; temperature.BYTES = 2; @@ -142,7 +147,7 @@ var humidity = function (bytes) { } var h = bytesToInt(bytes); - return h / 1e2; + return +(h / 100).toFixed(1); }; humidity.BYTES = 2; diff --git a/src/TTN/plain_decoder.js b/src/TTN/plain_decoder.js index 0de53fc2..dd4cbe38 100644 --- a/src/TTN/plain_decoder.js +++ b/src/TTN/plain_decoder.js @@ -40,6 +40,15 @@ function Decoder(bytes, port) { decoded.rssi = bytes[i++]; decoded.beacon = bytes[i++]; } + + if (port === 7) { + var i = 0; + decoded.temperature = ((bytes[i++] << 8) | bytes[i++]); + decoded.pressure = ((bytes[i++] << 8) | bytes[i++]); + decoded.humidity = ((bytes[i++] << 8) | bytes[i++]); + decoded.gas = ((bytes[i++] << 8) | bytes[i++]); + decoded.altitude = ((bytes[i++] << 8) | bytes[i++]); + } return decoded; diff --git a/src/bme680read.cpp b/src/bme680read.cpp index aa75c162..795280af 100644 --- a/src/bme680read.cpp +++ b/src/bme680read.cpp @@ -30,9 +30,9 @@ bool bme_read(void) { bool ret = bme.performReading(); if (ret) { // read current BME data and buffer in global struct - bme_status.temperature = (uint16_t)(bme.temperature * 100.0); + bme_status.temperature = bme.temperature; bme_status.pressure = (uint16_t)(bme.pressure / 100.0); - bme_status.humidity = (uint8_t)bme.humidity; + bme_status.humidity = bme.humidity; bme_status.gas_resistance = (uint16_t)(bme.gas_resistance / 1000.0); bme_status.altitude = (uint16_t)(bme.readAltitude(SEALEVELPRESSURE_HPA / 1000.0)); diff --git a/src/payload.cpp b/src/payload.cpp index d328f975..c6721b00 100644 --- a/src/payload.cpp +++ b/src/payload.cpp @@ -95,11 +95,12 @@ void PayloadConvert::addGPS(gpsStatus_t value) { void PayloadConvert::addBME(bmeStatus_t value) { #ifdef HAS_BME - buffer[cursor++] = highByte(value.temperature); - buffer[cursor++] = lowByte(value.temperature); + buffer[cursor++] = highByte((int16_t)value.temperature); + buffer[cursor++] = lowByte((int16_t)value.temperature); buffer[cursor++] = highByte(value.pressure); buffer[cursor++] = lowByte(value.pressure); - buffer[cursor++] = (byte)(value.humidity); + buffer[cursor++] = highByte((uint16_t)value.humidity); + buffer[cursor++] = lowByte((uint16_t)value.humidity); buffer[cursor++] = highByte(value.gas_resistance); buffer[cursor++] = lowByte(value.gas_resistance); buffer[cursor++] = highByte(value.altitude); @@ -166,9 +167,9 @@ void PayloadConvert::addGPS(gpsStatus_t value) { void PayloadConvert::addBME(bmeStatus_t value) { #ifdef HAS_BME - writeUint16(value.temperature); + writeTemperature(value.temperature); writeUint16(value.pressure); - writeUint8(value.humidity); + writeHumidity(value.humidity); writeUint16(value.gas_resistance); writeUint16(value.altitude); #endif @@ -250,13 +251,13 @@ void PayloadConvert::addCount(uint16_t value1, uint16_t value2) { #if (PAYLOAD_ENCODER == 3) buffer[cursor++] = LPP_COUNT_WIFI_CHANNEL; #endif - buffer[cursor++] = LPP_LUMINOSITY; // workaround, type meter not found? + buffer[cursor++] = LPP_TEMPERATURE; buffer[cursor++] = highByte(value1); buffer[cursor++] = lowByte(value1); #if (PAYLOAD_ENCODER == 3) buffer[cursor++] = LPP_COUNT_BLE_CHANNEL; #endif - buffer[cursor++] = LPP_LUMINOSITY; // workaround, type meter not found? + buffer[cursor++] = LPP_HUMIDITY; buffer[cursor++] = highByte(value2); buffer[cursor++] = lowByte(value2); } @@ -293,9 +294,10 @@ void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float celsius, buffer[cursor++] = LPP_ANALOG_INPUT; buffer[cursor++] = highByte(volt); buffer[cursor++] = lowByte(volt); -#endif +#endif // HAS_BATTERY_PROBE + #if (PAYLOAD_ENCODER == 3) - buffer[cursor++] = LPP_TEMP_CHANNEL; + buffer[cursor++] = LPP_TEMPERATURE_CHANNEL; #endif buffer[cursor++] = LPP_TEMPERATURE; buffer[cursor++] = highByte(temp); @@ -316,11 +318,39 @@ void PayloadConvert::addGPS(gpsStatus_t value) { buffer[cursor++] = (byte)((lat & 0x0000FF)); buffer[cursor++] = (byte)((lon & 0xFF0000) >> 16); buffer[cursor++] = (byte)((lon & 0x00FF00) >> 8); - buffer[cursor++] = (byte)((lon & 0x0000FF)); + buffer[cursor++] = (byte)(lon & 0x0000FF); buffer[cursor++] = (byte)((alt & 0xFF0000) >> 16); buffer[cursor++] = (byte)((alt & 0x00FF00) >> 8); - buffer[cursor++] = (byte)((alt & 0x0000FF)); + buffer[cursor++] = (byte)(alt & 0x0000FF); +#endif // HAS_GPS +} + +void PayloadConvert::addBME(bmeStatus_t value) { +#ifdef HAS_BME +#if (PAYLOAD_ENCODER == 3) + buffer[cursor++] = LPP_TEMPERATURE_CHANNEL; #endif + buffer[cursor++] = LPP_TEMPERATURE; + buffer[cursor++] = highByte((int16_t)value.temperature); + buffer[cursor++] = lowByte((int16_t)value.temperature); +#if (PAYLOAD_ENCODER == 3) + buffer[cursor++] = LPP_BAROMETER_CHANNEL; +#endif + buffer[cursor++] = LPP_BAROMETER; + buffer[cursor++] = highByte(value.pressure); + buffer[cursor++] = lowByte(value.pressure); +#if (PAYLOAD_ENCODER == 3) + buffer[cursor++] = LPP_HUMIDITY_CHANNEL; +#endif + buffer[cursor++] = LPP_HUMIDITY; + buffer[cursor++] = (byte)value.humidity; +#if (PAYLOAD_ENCODER == 3) + buffer[cursor++] = LPP_GAS_CHANNEL; +#endif + buffer[cursor++] = LPP_ANALOG_INPUT; + buffer[cursor++] = highByte(value.gas_resistance); + buffer[cursor++] = lowByte(value.gas_resistance); +#endif // HAS_BME } void PayloadConvert::addButton(uint8_t value) { @@ -330,7 +360,7 @@ void PayloadConvert::addButton(uint8_t value) { #endif buffer[cursor++] = LPP_DIGITAL_INPUT; buffer[cursor++] = value; -#endif +#endif // HAS_BUTTON } #else