payload decoder reworked for BME680 support

This commit is contained in:
Klaus K Wilting committed 2018-11-18 15:50:57 +01:00
1 parent 02b3f1e2d4
commit 06dcad6484
19 files changed
+105 -92

No files matched your search

+1 -1
View File
@@ -43,7 +43,7 @@ function Decoder(bytes, port) {
if (port === 7) {
// BME680 sensor data
return decode(bytes, [temperature, uint16, humidity, uint16, uint16], ['temperature', 'pressure', 'humidity', 'gas', 'altitude']);
return decode(bytes, [temperature, uint16, humidity, uint16, temperature], ['temperature', 'pressure', 'humidity', 'air', 'altitude']);
}
}
+1 -1
View File
@@ -46,7 +46,7 @@ function Decoder(bytes, port) {
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.air = ((bytes[i++] << 8) | bytes[i++]);
decoded.altitude = ((bytes[i++] << 8) | bytes[i++]);
}
+4 -5
View File
@@ -14,7 +14,7 @@ bmeStatus_t bme_status;
void bme_init(void) {
// initialize BME680 sensor using default i2c address 0x77
if (bme.begin()) {
if (bme.begin(HAS_BME)) {
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
@@ -31,11 +31,10 @@ bool bme_read(void) {
if (ret) {
// read current BME data and buffer in global struct
bme_status.temperature = bme.temperature;
bme_status.pressure = (uint16_t)(bme.pressure / 100.0);
bme_status.pressure = (uint16_t)(bme.pressure / 100.0); // convert Pa -> hPa
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));
bme_status.gas_resistance = (uint16_t)(bme.gas_resistance / 1000.0); // convert Ohm -> kOhm
bme_status.altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
ESP_LOGI(TAG, "BME680 sensor data read success");
} else {
ESP_LOGI(TAG, "BME680 sensor read error");
+1 -1
View File
@@ -10,6 +10,6 @@ void readButton() {
ESP_LOGI(TAG, "Button pressed");
payload.reset();
payload.addButton(0x01);
SendData(BUTTONPORT);
SendPayload(BUTTONPORT);
}
#endif
+6 -6
View File
@@ -23,10 +23,10 @@ void doHousekeeping() {
spi_housekeeping();
lora_housekeeping();
// time sync once per TIME_SYNC_INTERVAL_MIN
#ifdef TIME_SYNC
// time sync once per TIME_SYNC_INTERVAL
#ifdef TIME_SYNC_INTERVAL
if (millis() >= nextTimeSync) {
nextTimeSync = millis() + TIME_SYNC_INTERVAL_MIN *
nextTimeSync = millis() + TIME_SYNC_INTERVAL *
60000; // set up next time sync period
do_timesync();
}
@@ -63,7 +63,7 @@ void doHousekeeping() {
"Memory full, counter cleared (heap low water mark = %d Bytes / "
"free heap = %d bytes)",
esp_get_minimum_free_heap_size(), ESP.getFreeHeap());
SendData(COUNTERPORT); // send data before clearing counters
SendPayload(COUNTERPORT); // send data before clearing counters
reset_counters(); // clear macs container and reset all counters
get_salt(); // get new salt for salting hashes
@@ -90,7 +90,7 @@ void reset_counters() {
}
void do_timesync() {
#ifdef TIME_SYNC
#ifdef TIME_SYNC_INTERVAL
// sync time & date if we have valid gps time
#ifdef HAS_GPS
if (gps.time.isValid()) {
@@ -106,7 +106,7 @@ void do_timesync() {
// Schedule a network time request at the next possible time
LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime);
ESP_LOGI(TAG, "Network time request scheduled");
#endif // TIME_SYNC
#endif // TIME_SYNC_INTERVAL
} // do_timesync()
#ifndef VERBOSE
+1 -1
View File
@@ -13,7 +13,7 @@
#define SPI_SCLK GPIO_NUM_18
#define SPI_CS GPIO_NUM_5
#define HAS_BME 1 // BME680 sensor on I2C bus (SDA=4/SCL=15); comment out if not present
#define HAS_BME 0x77 // BME680 sensor on I2C bus (SDA=4/SCL=15); comment out if not present
#define CFG_sx1276_radio 1 // select LoRa chip
//#define CFG_sx1272_radio 1 // select LoRa chip
+1 -1
View File
@@ -3,7 +3,7 @@
#include <stdint.h>
//#define HAS_BME 1 // BME680 sensor on I2C bus (SDI=21/SCL=22); comment out if not present
//#define HAS_BME 0x77 // BME680 sensor on I2C bus (SDI=21/SCL=22); comment out if not present
// Hardware related definitions for Heltec LoRa-32 Board
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
+2
View File
@@ -10,6 +10,8 @@
// disable brownout detection (avoid unexpected reset on some boards)
#define DISABLE_BROWNOUT 1 // comment out if you want to keep brownout feature
#define HAS_BME 0x76 // BME680 sensor on I2C bus; comment out if not present
#define HAS_LED 13 // ESP32 GPIO12 (pin22) On Board LED
#define LED_ACTIVE_LOW 1 // Onboard LED is active when pin is LOW
//#define HAS_RGB_LED 13 // ESP32 GPIO13 (pin13) On Board Shield WS2812B RGB LED
+1 -1
View File
@@ -8,7 +8,7 @@
// This settings are for boards labeled v1.6 on pcb, NOT for v1.5 or older
*/
//#define HAS_BME 1 // BME680 sensor on I2C bus (SDI=21/SCL=22); comment out if not present
#define HAS_BME 0x77 // BME680 sensor on I2C bus (SDI=21/SCL=22); comment out if not present
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define CFG_sx1276_radio 1 // HPD13A LoRa SoC
+3 -3
View File
@@ -35,8 +35,8 @@ void irqHandler(void *pvParameters) {
doHousekeeping();
// is time to send the payload?
if (InterruptStatus & SENDPAYLOAD_IRQ)
sendPayload();
if (InterruptStatus & SENDCOUNTER_IRQ)
sendCounter();
}
vTaskDelete(NULL); // shoud never be reached
}
@@ -55,7 +55,7 @@ void IRAM_ATTR homeCycleIRQ() {
}
void IRAM_ATTR SendCycleIRQ() {
xTaskNotifyFromISR(irqHandlerTask, SENDPAYLOAD_IRQ, eSetBits, NULL);
xTaskNotifyFromISR(irqHandlerTask, SENDCOUNTER_IRQ, eSetBits, NULL);
portYIELD_FROM_ISR();
}
+1 -1
View File
@@ -107,7 +107,7 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) {
#endif
payload.reset();
payload.addAlarm(rssi, beaconID);
SendData(BEACONPORT);
SendPayload(BEACONPORT);
}
};
+2 -3
View File
@@ -11,7 +11,7 @@
// Payload send cycle and encoding
#define SEND_SECS 30 // payload send cycle [seconds/2] -> 60 sec.
#define PAYLOAD_ENCODER 3 // payload encoder: 1=Plain, 2=Packed, 3=CayenneLPP dynamic, 4=CayenneLPP packed
#define PAYLOAD_ENCODER 2 // payload encoder: 1=Plain, 2=Packed, 3=CayenneLPP dynamic, 4=CayenneLPP packed
// Set this to include BLE counting and vendor filter functions
#define VENDORFILTER 1 // comment out if you want to count things, not people
@@ -74,8 +74,7 @@
#define RESPONSE_TIMEOUT_MS 60000 // firmware binary server connection timeout [milliseconds]
// setting for syncing time of node
#define TIME_SYNC 1 // comment out if no time sync of node necessary
#define TIME_SYNC_INTERVAL_MIN 60 // sync time each ... minutes [default = 60]
//#define TIME_SYNC_INTERVAL 60 // sync time each ... minutes [default = 60], comment out means off
// LMIC settings
// moved to src/lmic_config.h
+32 -18
View File
@@ -95,16 +95,19 @@ void PayloadConvert::addGPS(gpsStatus_t value) {
void PayloadConvert::addBME(bmeStatus_t value) {
#ifdef HAS_BME
buffer[cursor++] = highByte((int16_t)value.temperature);
buffer[cursor++] = lowByte((int16_t)value.temperature);
int16_t temperature = (int16_t)(value.temperature); // float -> int
uint16_t humidity = (uint16_t)(value.humidity); // float -> int
int16_t altitude = (int16_t)(value.altitude); // float -> int
buffer[cursor++] = highByte(temperature);
buffer[cursor++] = lowByte(temperature);
buffer[cursor++] = highByte(value.pressure);
buffer[cursor++] = lowByte(value.pressure);
buffer[cursor++] = highByte((uint16_t)value.humidity);
buffer[cursor++] = lowByte((uint16_t)value.humidity);
buffer[cursor++] = highByte(humidity);
buffer[cursor++] = lowByte(humidity);
buffer[cursor++] = highByte(value.gas_resistance);
buffer[cursor++] = lowByte(value.gas_resistance);
buffer[cursor++] = highByte(value.altitude);
buffer[cursor++] = lowByte(value.altitude);
buffer[cursor++] = highByte(altitude);
buffer[cursor++] = lowByte(altitude);
#endif
}
@@ -171,7 +174,7 @@ void PayloadConvert::addBME(bmeStatus_t value) {
writeUint16(value.pressure);
writeHumidity(value.humidity);
writeUint16(value.gas_resistance);
writeUint16(value.altitude);
writeTemperature(value.altitude);
#endif
}
@@ -331,29 +334,40 @@ void PayloadConvert::addGPS(gpsStatus_t value) {
void PayloadConvert::addBME(bmeStatus_t value) {
#ifdef HAS_BME
// data value conversions to meet cayenne data type definition
// 0.1°C per bit => -3276,7 .. +3276,7 °C
int16_t temperature = (int16_t)(value.temperature * 10.0);
// 0.1 hPa per bit => 0 .. 6553,6 hPa
uint16_t pressure = value.pressure * 10;
// 0.5% per bit => 0 .. 128 %C
uint8_t humidity = (uint8_t)(value.humidity * 2.0);
// 0.01 Ohm per bit => 0 .. 655,36 Ohm
uint16_t gas = value.gas_resistance * 100;
#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);
buffer[cursor++] = LPP_TEMPERATURE; // 2 bytes 0.1 °C Signed MSB
buffer[cursor++] = highByte(temperature);
buffer[cursor++] = lowByte(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);
buffer[cursor++] = LPP_BAROMETER; // 2 bytes 0.1 hPa Unsigned MSB
buffer[cursor++] = highByte(pressure);
buffer[cursor++] = lowByte(pressure);
#if (PAYLOAD_ENCODER == 3)
buffer[cursor++] = LPP_HUMIDITY_CHANNEL;
#endif
buffer[cursor++] = LPP_HUMIDITY;
buffer[cursor++] = (byte)value.humidity;
buffer[cursor++] = LPP_HUMIDITY; // 1 byte 0.5 % Unsigned
buffer[cursor++] = 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);
buffer[cursor++] = LPP_ANALOG_INPUT; // 2 bytes 0.01 Signed
buffer[cursor++] = highByte(gas);
buffer[cursor++] = lowByte(gas);
#endif // HAS_BME
}
+4 -4
View File
@@ -211,7 +211,7 @@ void get_config(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: get device configuration");
payload.reset();
payload.addConfig(cfg);
SendData(CONFIGPORT);
SendPayload(CONFIGPORT);
};
void get_status(uint8_t val[]) {
@@ -225,7 +225,7 @@ void get_status(uint8_t val[]) {
payload.addStatus(voltage, uptime() / 1000, temperatureRead(),
ESP.getFreeHeap(), rtc_get_reset_reason(0),
rtc_get_reset_reason(1));
SendData(STATUSPORT);
SendPayload(STATUSPORT);
};
void get_gps(uint8_t val[]) {
@@ -234,7 +234,7 @@ void get_gps(uint8_t val[]) {
gps_read();
payload.reset();
payload.addGPS(gps_status);
SendData(GPSPORT);
SendPayload(GPSPORT)
#else
ESP_LOGW(TAG, "GPS function not supported");
#endif
@@ -245,7 +245,7 @@ void get_bme(uint8_t val[]) {
#ifdef HAS_BME
payload.reset();
payload.addBME(bme_status);
SendData(BMEPORT);
SendPayload(BMEPORT);
#else
ESP_LOGW(TAG, "BME680 sensor not supported");
#endif
+8 -7
View File
@@ -2,22 +2,24 @@
#include "senddata.h"
// put data to send in RTos Queues used for transmit over channels Lora and SPI
void SendData(uint8_t port) {
void SendPayload(uint8_t port) {
MessageBuffer_t SendBuffer; // contains MessageSize, MessagePort, Message[]
SendBuffer.MessageSize = payload.getSize();
SendBuffer.MessagePort = port;
SendBuffer.MessagePort = PAYLOAD_ENCODER <= 2
? port
: (PAYLOAD_ENCODER == 4 ? LPP2PORT : LPP1PORT);
memcpy(SendBuffer.Message, payload.getBuffer(), payload.getSize());
// enqueue message in device's send queues
lora_enqueuedata(&SendBuffer);
spi_enqueuedata(&SendBuffer);
} // SendData
} // SendPayload
// interrupt triggered function to prepare payload to send
void sendPayload() {
void sendCounter() {
// append counter data to payload
payload.reset();
@@ -48,10 +50,9 @@ void sendPayload() {
ESP_LOGD(TAG, "No valid GPS position or GPS data mode disabled");
}
#endif
SendData(PAYLOAD_ENCODER <= 2 ? COUNTERPORT
: (PAYLOAD_ENCODER == 4 ? LPP2PORT : LPP1PORT));
SendPayload(COUNTERPORT);
} // sendpayload()
} // sendCounter()
void flushQueues() {
lora_queuereset();