2022-03-27 14:55:35 +00:00
|
|
|
#include "EnvironmentTelemetry.h"
|
|
|
|
#include "../mesh/generated/telemetry.pb.h"
|
|
|
|
#include "MeshService.h"
|
|
|
|
#include "NodeDB.h"
|
2022-05-07 10:31:21 +00:00
|
|
|
#include "PowerFSM.h"
|
2022-03-27 14:55:35 +00:00
|
|
|
#include "RTC.h"
|
|
|
|
#include "Router.h"
|
|
|
|
#include "configuration.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include <OLEDDisplay.h>
|
|
|
|
#include <OLEDDisplayUi.h>
|
|
|
|
|
|
|
|
// Sensors
|
|
|
|
#include "Sensor/BME280Sensor.h"
|
|
|
|
#include "Sensor/BME680Sensor.h"
|
|
|
|
#include "Sensor/DHTSensor.h"
|
|
|
|
#include "Sensor/DallasSensor.h"
|
|
|
|
#include "Sensor/MCP9808Sensor.h"
|
2022-06-11 21:44:56 +00:00
|
|
|
#include "Sensor/INA260Sensor.h"
|
|
|
|
#include "Sensor/INA219Sensor.h"
|
|
|
|
|
2022-03-27 14:55:35 +00:00
|
|
|
|
|
|
|
BME280Sensor bme280Sensor;
|
|
|
|
BME680Sensor bme680Sensor;
|
|
|
|
DHTSensor dhtSensor;
|
|
|
|
DallasSensor dallasSensor;
|
|
|
|
MCP9808Sensor mcp9808Sensor;
|
2022-06-11 21:44:56 +00:00
|
|
|
INA260Sensor ina260Sensor;
|
|
|
|
INA219Sensor ina219Sensor;
|
2022-03-27 14:55:35 +00:00
|
|
|
|
|
|
|
#define FAILED_STATE_SENSOR_READ_MULTIPLIER 10
|
|
|
|
#define DISPLAY_RECEIVEID_MEASUREMENTS_ON_SCREEN true
|
|
|
|
|
|
|
|
#ifdef HAS_EINK
|
|
|
|
// The screen is bigger so use bigger fonts
|
|
|
|
#define FONT_SMALL ArialMT_Plain_16
|
|
|
|
#define FONT_MEDIUM ArialMT_Plain_24
|
|
|
|
#define FONT_LARGE ArialMT_Plain_24
|
|
|
|
#else
|
|
|
|
#define FONT_SMALL ArialMT_Plain_10
|
|
|
|
#define FONT_MEDIUM ArialMT_Plain_16
|
|
|
|
#define FONT_LARGE ArialMT_Plain_24
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define fontHeight(font) ((font)[1] + 1) // height is position 1
|
|
|
|
|
|
|
|
#define FONT_HEIGHT_SMALL fontHeight(FONT_SMALL)
|
|
|
|
#define FONT_HEIGHT_MEDIUM fontHeight(FONT_MEDIUM)
|
|
|
|
|
|
|
|
int32_t EnvironmentTelemetryModule::runOnce()
|
|
|
|
{
|
|
|
|
#ifndef PORTDUINO
|
2022-06-05 14:50:06 +00:00
|
|
|
int32_t result = INT32_MAX;
|
2022-03-27 14:55:35 +00:00
|
|
|
/*
|
|
|
|
Uncomment the preferences below if you want to use the module
|
|
|
|
without having to configure it from the PythonAPI or WebUI.
|
|
|
|
*/
|
2022-06-05 14:50:06 +00:00
|
|
|
|
2022-03-27 14:55:35 +00:00
|
|
|
/*
|
2022-05-22 11:27:56 +00:00
|
|
|
moduleConfig.telemetry.environment_measurement_enabled = 1;
|
|
|
|
moduleConfig.telemetry.environment_screen_enabled = 1;
|
|
|
|
moduleConfig.telemetry.environment_read_error_count_threshold = 5;
|
|
|
|
moduleConfig.telemetry.environment_update_interval = 600;
|
|
|
|
moduleConfig.telemetry.environment_recovery_interval = 60;
|
|
|
|
moduleConfig.telemetry.environment_sensor_pin = 13; // If one-wire
|
|
|
|
moduleConfig.telemetry.environment_sensor_type = TelemetrySensorType::TelemetrySensorType_BME280;
|
2022-03-27 14:55:35 +00:00
|
|
|
*/
|
2022-05-07 10:31:21 +00:00
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
if (!(moduleConfig.telemetry.environment_measurement_enabled ||
|
|
|
|
moduleConfig.telemetry.environment_screen_enabled)) {
|
2022-03-27 14:55:35 +00:00
|
|
|
// If this module is not enabled, and the user doesn't want the display screen don't waste any OSThread time on it
|
2022-06-05 14:50:06 +00:00
|
|
|
return result;
|
2022-03-27 14:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (firstTime) {
|
|
|
|
// This is the first time the OSThread library has called this function, so do some setup
|
|
|
|
firstTime = 0;
|
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
if (moduleConfig.telemetry.environment_measurement_enabled) {
|
2022-03-27 14:55:35 +00:00
|
|
|
DEBUG_MSG("Environment Telemetry: Initializing\n");
|
|
|
|
// it's possible to have this module enabled, only for displaying values on the screen.
|
|
|
|
// therefore, we should only enable the sensor loop if measurement is also enabled
|
2022-06-05 14:50:06 +00:00
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
switch (moduleConfig.telemetry.environment_sensor_type) {
|
2022-06-05 14:50:06 +00:00
|
|
|
case TelemetrySensorType_DHT11:
|
|
|
|
case TelemetrySensorType_DHT12:
|
|
|
|
case TelemetrySensorType_DHT21:
|
|
|
|
case TelemetrySensorType_DHT22:
|
|
|
|
result = dhtSensor.runOnce();
|
|
|
|
break;
|
|
|
|
case TelemetrySensorType_DS18B20:
|
|
|
|
result = dallasSensor.runOnce();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DEBUG_MSG("Environment Telemetry: No sensor type specified; Checking for detected i2c sensors\n");
|
2022-05-07 10:31:21 +00:00
|
|
|
break;
|
2022-03-27 14:55:35 +00:00
|
|
|
}
|
2022-06-10 17:04:04 +00:00
|
|
|
if (bme680Sensor.hasSensor())
|
2022-06-05 14:50:06 +00:00
|
|
|
result = bme680Sensor.runOnce();
|
2022-06-10 17:04:04 +00:00
|
|
|
if (bme280Sensor.hasSensor())
|
2022-06-05 14:50:06 +00:00
|
|
|
result = bme280Sensor.runOnce();
|
2022-06-10 17:04:04 +00:00
|
|
|
if (mcp9808Sensor.hasSensor())
|
2022-06-05 14:50:06 +00:00
|
|
|
result = mcp9808Sensor.runOnce();
|
2022-06-11 21:44:56 +00:00
|
|
|
if (ina260Sensor.hasSensor())
|
|
|
|
result = ina260Sensor.runOnce();
|
|
|
|
if (ina219Sensor.hasSensor())
|
|
|
|
result = ina219Sensor.runOnce();
|
2022-03-27 14:55:35 +00:00
|
|
|
}
|
2022-06-05 14:50:06 +00:00
|
|
|
return result;
|
2022-03-27 14:55:35 +00:00
|
|
|
} else {
|
|
|
|
// if we somehow got to a second run of this module with measurement disabled, then just wait forever
|
2022-05-22 11:27:56 +00:00
|
|
|
if (!moduleConfig.telemetry.environment_measurement_enabled)
|
2022-06-05 14:50:06 +00:00
|
|
|
return result;
|
2022-03-27 14:55:35 +00:00
|
|
|
// this is not the first time OSThread library has called this function
|
|
|
|
// so just do what we intend to do on the interval
|
2022-05-22 11:27:56 +00:00
|
|
|
if (sensor_read_error_count > moduleConfig.telemetry.environment_read_error_count_threshold) {
|
|
|
|
if (moduleConfig.telemetry.environment_recovery_interval > 0) {
|
2022-03-27 14:55:35 +00:00
|
|
|
DEBUG_MSG("Environment Telemetry: TEMPORARILY DISABLED; The "
|
|
|
|
"telemetry_module_environment_read_error_count_threshold has been exceed: %d. Will retry reads in "
|
|
|
|
"%d seconds\n",
|
2022-05-22 11:27:56 +00:00
|
|
|
moduleConfig.telemetry.environment_read_error_count_threshold,
|
|
|
|
moduleConfig.telemetry.environment_recovery_interval);
|
2022-03-27 14:55:35 +00:00
|
|
|
sensor_read_error_count = 0;
|
2022-05-22 11:27:56 +00:00
|
|
|
return (moduleConfig.telemetry.environment_recovery_interval * 1000);
|
2022-03-27 14:55:35 +00:00
|
|
|
}
|
|
|
|
DEBUG_MSG("Environment Telemetry: DISABLED; The telemetry_module_environment_read_error_count_threshold has "
|
|
|
|
"been exceed: %d. Reads will not be retried until after device reset\n",
|
2022-05-22 11:27:56 +00:00
|
|
|
moduleConfig.telemetry.environment_read_error_count_threshold);
|
2022-06-05 14:50:06 +00:00
|
|
|
return result;
|
2022-03-27 14:55:35 +00:00
|
|
|
|
|
|
|
} else if (sensor_read_error_count > 0) {
|
|
|
|
DEBUG_MSG("Environment Telemetry: There have been %d sensor read failures. Will retry %d more times\n",
|
|
|
|
sensor_read_error_count, sensor_read_error_count, sensor_read_error_count,
|
2022-05-22 11:27:56 +00:00
|
|
|
moduleConfig.telemetry.environment_read_error_count_threshold - sensor_read_error_count);
|
2022-03-27 14:55:35 +00:00
|
|
|
}
|
|
|
|
if (!sendOurTelemetry()) {
|
|
|
|
// if we failed to read the sensor, then try again
|
|
|
|
// as soon as we can according to the maximum polling frequency
|
2022-06-05 14:50:06 +00:00
|
|
|
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
|
2022-03-27 14:55:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-22 11:27:56 +00:00
|
|
|
return getIntervalOrDefaultMs(moduleConfig.telemetry.environment_update_interval);
|
2022-03-27 14:55:35 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GetTimeSinceMeshPacket(const MeshPacket *mp)
|
|
|
|
{
|
|
|
|
uint32_t now = getTime();
|
|
|
|
|
|
|
|
uint32_t last_seen = mp->rx_time;
|
|
|
|
int delta = (int)(now - last_seen);
|
|
|
|
if (delta < 0) // our clock must be slightly off still - not set from GPS yet
|
|
|
|
delta = 0;
|
|
|
|
|
|
|
|
return delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EnvironmentTelemetryModule::wantUIFrame()
|
|
|
|
{
|
2022-05-22 11:27:56 +00:00
|
|
|
return moduleConfig.telemetry.environment_screen_enabled;
|
2022-03-27 14:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float EnvironmentTelemetryModule::CelsiusToFahrenheit(float c)
|
|
|
|
{
|
|
|
|
return (c * 9) / 5 + 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnvironmentTelemetryModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
|
|
|
|
{
|
|
|
|
display->setTextAlignment(TEXT_ALIGN_LEFT);
|
|
|
|
display->setFont(FONT_MEDIUM);
|
|
|
|
display->drawString(x, y, "Environment");
|
|
|
|
if (lastMeasurementPacket == nullptr) {
|
|
|
|
display->setFont(FONT_SMALL);
|
|
|
|
display->drawString(x, y += fontHeight(FONT_MEDIUM), "No measurement");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Telemetry lastMeasurement;
|
|
|
|
|
|
|
|
uint32_t agoSecs = GetTimeSinceMeshPacket(lastMeasurementPacket);
|
2022-04-24 21:12:25 +00:00
|
|
|
const char *lastSender = getSenderShortName(*lastMeasurementPacket);
|
2022-03-27 14:55:35 +00:00
|
|
|
|
|
|
|
auto &p = lastMeasurementPacket->decoded;
|
|
|
|
if (!pb_decode_from_bytes(p.payload.bytes, p.payload.size, Telemetry_fields, &lastMeasurement)) {
|
|
|
|
display->setFont(FONT_SMALL);
|
|
|
|
display->drawString(x, y += fontHeight(FONT_MEDIUM), "Measurement Error");
|
|
|
|
DEBUG_MSG("Environment Telemetry: unable to decode last packet");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
display->setFont(FONT_SMALL);
|
|
|
|
String last_temp = String(lastMeasurement.variant.environment_metrics.temperature, 0) + "°C";
|
2022-05-22 11:27:56 +00:00
|
|
|
if (moduleConfig.telemetry.environment_display_fahrenheit) {
|
2022-03-27 14:55:35 +00:00
|
|
|
last_temp = String(CelsiusToFahrenheit(lastMeasurement.variant.environment_metrics.temperature), 0) + "°F";
|
|
|
|
}
|
2022-04-24 21:12:25 +00:00
|
|
|
display->drawString(x, y += fontHeight(FONT_MEDIUM) - 2, "From: " + String(lastSender) + "(" + String(agoSecs) + "s)");
|
2022-05-07 10:31:21 +00:00
|
|
|
display->drawString(x, y += fontHeight(FONT_SMALL) - 2,
|
|
|
|
"Temp/Hum: " + last_temp + " / " +
|
|
|
|
String(lastMeasurement.variant.environment_metrics.relative_humidity, 0) + "%");
|
|
|
|
if (lastMeasurement.variant.environment_metrics.barometric_pressure != 0)
|
|
|
|
display->drawString(x, y += fontHeight(FONT_SMALL),
|
|
|
|
"Press: " + String(lastMeasurement.variant.environment_metrics.barometric_pressure, 0) + "hPA");
|
2022-03-27 14:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EnvironmentTelemetryModule::handleReceivedProtobuf(const MeshPacket &mp, Telemetry *t)
|
|
|
|
{
|
|
|
|
if (t->which_variant == Telemetry_environment_metrics_tag) {
|
2022-04-24 21:12:25 +00:00
|
|
|
const char *sender = getSenderShortName(mp);
|
2022-03-27 14:55:35 +00:00
|
|
|
|
|
|
|
DEBUG_MSG("-----------------------------------------\n");
|
|
|
|
DEBUG_MSG("Environment Telemetry: Received data from %s\n", sender);
|
|
|
|
DEBUG_MSG("Telemetry->time: %i\n", t->time);
|
|
|
|
DEBUG_MSG("Telemetry->barometric_pressure: %f\n", t->variant.environment_metrics.barometric_pressure);
|
|
|
|
DEBUG_MSG("Telemetry->current: %f\n", t->variant.environment_metrics.current);
|
|
|
|
DEBUG_MSG("Telemetry->gas_resistance: %f\n", t->variant.environment_metrics.gas_resistance);
|
|
|
|
DEBUG_MSG("Telemetry->relative_humidity: %f\n", t->variant.environment_metrics.relative_humidity);
|
|
|
|
DEBUG_MSG("Telemetry->temperature: %f\n", t->variant.environment_metrics.temperature);
|
|
|
|
DEBUG_MSG("Telemetry->voltage: %f\n", t->variant.environment_metrics.voltage);
|
|
|
|
|
|
|
|
lastMeasurementPacket = packetPool.allocCopy(mp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false; // Let others look at this message also if they want
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EnvironmentTelemetryModule::sendOurTelemetry(NodeNum dest, bool wantReplies)
|
|
|
|
{
|
|
|
|
Telemetry m;
|
|
|
|
m.time = getTime();
|
|
|
|
m.which_variant = Telemetry_environment_metrics_tag;
|
|
|
|
|
|
|
|
m.variant.environment_metrics.barometric_pressure = 0;
|
|
|
|
m.variant.environment_metrics.current = 0;
|
|
|
|
m.variant.environment_metrics.gas_resistance = 0;
|
|
|
|
m.variant.environment_metrics.relative_humidity = 0;
|
|
|
|
m.variant.environment_metrics.temperature = 0;
|
|
|
|
m.variant.environment_metrics.voltage = 0;
|
2022-05-07 10:31:21 +00:00
|
|
|
|
2022-03-27 14:55:35 +00:00
|
|
|
DEBUG_MSG("-----------------------------------------\n");
|
|
|
|
DEBUG_MSG("Environment Telemetry: Read data\n");
|
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
switch (moduleConfig.telemetry.environment_sensor_type) {
|
2022-06-05 14:50:06 +00:00
|
|
|
case TelemetrySensorType_DS18B20:
|
2022-06-10 17:04:04 +00:00
|
|
|
if (!dallasSensor.getMetrics(&m))
|
2022-06-05 14:50:06 +00:00
|
|
|
sensor_read_error_count++;
|
|
|
|
break;
|
|
|
|
case TelemetrySensorType_DHT11:
|
|
|
|
case TelemetrySensorType_DHT12:
|
|
|
|
case TelemetrySensorType_DHT21:
|
|
|
|
case TelemetrySensorType_DHT22:
|
2022-06-10 17:04:04 +00:00
|
|
|
if (!dhtSensor.getMetrics(&m))
|
2022-06-05 14:50:06 +00:00
|
|
|
sensor_read_error_count++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DEBUG_MSG("Environment Telemetry: No specified sensor type; Trying any detected i2c sensors\n");
|
|
|
|
}
|
2022-06-10 17:04:04 +00:00
|
|
|
if (bme280Sensor.hasSensor())
|
|
|
|
bme280Sensor.getMetrics(&m);
|
|
|
|
if (bme680Sensor.hasSensor())
|
|
|
|
bme680Sensor.getMetrics(&m);
|
|
|
|
if (mcp9808Sensor.hasSensor())
|
|
|
|
mcp9808Sensor.getMetrics(&m);
|
2022-06-11 21:44:56 +00:00
|
|
|
if (ina219Sensor.hasSensor())
|
|
|
|
ina219Sensor.getMetrics(&m);
|
|
|
|
if (ina260Sensor.hasSensor())
|
|
|
|
ina260Sensor.getMetrics(&m);
|
2022-03-27 14:55:35 +00:00
|
|
|
|
|
|
|
DEBUG_MSG("Telemetry->time: %i\n", m.time);
|
|
|
|
DEBUG_MSG("Telemetry->barometric_pressure: %f\n", m.variant.environment_metrics.barometric_pressure);
|
|
|
|
DEBUG_MSG("Telemetry->current: %f\n", m.variant.environment_metrics.current);
|
|
|
|
DEBUG_MSG("Telemetry->gas_resistance: %f\n", m.variant.environment_metrics.gas_resistance);
|
|
|
|
DEBUG_MSG("Telemetry->relative_humidity: %f\n", m.variant.environment_metrics.relative_humidity);
|
|
|
|
DEBUG_MSG("Telemetry->temperature: %f\n", m.variant.environment_metrics.temperature);
|
|
|
|
DEBUG_MSG("Telemetry->voltage: %f\n", m.variant.environment_metrics.voltage);
|
|
|
|
|
|
|
|
sensor_read_error_count = 0;
|
|
|
|
|
|
|
|
MeshPacket *p = allocDataProtobuf(m);
|
|
|
|
p->to = dest;
|
|
|
|
p->decoded.want_response = wantReplies;
|
|
|
|
|
|
|
|
lastMeasurementPacket = packetPool.allocCopy(*p);
|
|
|
|
DEBUG_MSG("Environment Telemetry: Sending packet to mesh");
|
|
|
|
service.sendToMesh(p);
|
|
|
|
return true;
|
|
|
|
}
|