Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send local stats telemetry to phone every 15 minutes #4475

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/mesh/RadioLibInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ class RadioLibInterface : public RadioInterface, protected concurrency::Notified
*/
static void isrTxLevel0(), isrLevel0Common(PendingISR code);

/**
* Debugging counts
*/
uint32_t rxBad = 0, rxGood = 0, txGood = 0;

MeshPacketQueue txQueue = MeshPacketQueue(MAX_TX_QUEUE);

protected:
Expand Down Expand Up @@ -109,6 +104,11 @@ class RadioLibInterface : public RadioInterface, protected concurrency::Notified
*/
virtual void enableInterrupt(void (*)()) = 0;

/**
* Debugging counts
*/
uint32_t rxBad = 0, rxGood = 0, txGood = 0;

public:
RadioLibInterface(LockingArduinoHal *hal, RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst,
RADIOLIB_PIN_TYPE busy, PhysicalLayer *iface = NULL);
Expand Down
46 changes: 46 additions & 0 deletions src/modules/Telemetry/DeviceTelemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "NodeDB.h"
#include "PowerFSM.h"
#include "RTC.h"
#include "RadioLibInterface.h"
#include "Router.h"
#include "configuration.h"
#include "main.h"
Expand All @@ -31,6 +32,10 @@ int32_t DeviceTelemetryModule::runOnce()
// Just send to phone when it's not our time to send to mesh yet
// Only send while queue is empty (phone assumed connected)
sendTelemetry(NODENUM_BROADCAST, true);
if (lastSentStatsToPhone == 0 || (uptimeLastMs - lastSentStatsToPhone) >= sendStatsToPhoneIntervalMs) {
sendLocalStatsToPhone();
lastSentStatsToPhone = uptimeLastMs;
}
}
return sendToPhoneIntervalMs;
}
Expand Down Expand Up @@ -84,6 +89,13 @@ meshtastic_Telemetry DeviceTelemetryModule::getDeviceTelemetry()
meshtastic_Telemetry t = meshtastic_Telemetry_init_zero;
t.which_variant = meshtastic_Telemetry_device_metrics_tag;
t.time = getTime();
t.variant.device_metrics = meshtastic_DeviceMetrics_init_zero;
t.variant.device_metrics.has_air_util_tx = true;
t.variant.device_metrics.has_battery_level = true;
t.variant.device_metrics.has_channel_utilization = true;
t.variant.device_metrics.has_voltage = true;
t.variant.device_metrics.has_uptime_seconds = true;

t.variant.device_metrics.air_util_tx = airTime->utilizationTXPercent();
#if ARCH_PORTDUINO
t.variant.device_metrics.battery_level = MAGIC_USB_BATTERY_LEVEL;
Expand All @@ -98,6 +110,40 @@ meshtastic_Telemetry DeviceTelemetryModule::getDeviceTelemetry()
return t;
}

void DeviceTelemetryModule::sendLocalStatsToPhone()
{
meshtastic_Telemetry telemetry = meshtastic_Telemetry_init_zero;
telemetry.which_variant = meshtastic_Telemetry_local_stats_tag;
telemetry.variant.local_stats = meshtastic_LocalStats_init_zero;
telemetry.time = getTime();
telemetry.variant.local_stats.uptime_seconds = getUptimeSeconds();
telemetry.variant.local_stats.channel_utilization = airTime->channelUtilizationPercent();
telemetry.variant.local_stats.air_util_tx = airTime->utilizationTXPercent();
telemetry.variant.local_stats.num_online_nodes = numOnlineNodes;
telemetry.variant.local_stats.num_total_nodes = nodeDB->getNumMeshNodes();
if (RadioLibInterface::instance) {
telemetry.variant.local_stats.num_packets_tx = RadioLibInterface::instance->txGood;
telemetry.variant.local_stats.num_packets_rx = RadioLibInterface::instance->rxGood;
telemetry.variant.local_stats.num_packets_rx_bad = RadioLibInterface::instance->rxBad;
}

LOG_INFO(
"(Sending local stats): uptime=%i, channel_utilization=%f, air_util_tx=%f, num_online_nodes=%i, num_total_nodes=%i\n",
telemetry.variant.local_stats.uptime_seconds, telemetry.variant.local_stats.channel_utilization,
telemetry.variant.local_stats.air_util_tx, telemetry.variant.local_stats.num_online_nodes,
telemetry.variant.local_stats.num_total_nodes);

LOG_INFO("num_packets_tx=%i, num_packets_rx=%i, num_packets_rx_bad=%i\n", telemetry.variant.local_stats.num_packets_tx,
telemetry.variant.local_stats.num_packets_rx, telemetry.variant.local_stats.num_packets_rx_bad);

meshtastic_MeshPacket *p = allocDataProtobuf(telemetry);
p->to = NODENUM_BROADCAST;
p->decoded.want_response = false;
p->priority = meshtastic_MeshPacket_Priority_BACKGROUND;

service->sendToPhone(p);
}

bool DeviceTelemetryModule::sendTelemetry(NodeNum dest, bool phoneOnly)
{
meshtastic_Telemetry telemetry = getDeviceTelemetry();
Expand Down
5 changes: 4 additions & 1 deletion src/modules/Telemetry/DeviceTelemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ class DeviceTelemetryModule : private concurrency::OSThread, public ProtobufModu

private:
meshtastic_Telemetry getDeviceTelemetry();
uint32_t sendToPhoneIntervalMs = SECONDS_IN_MINUTE * 1000; // Send to phone every minute
void sendLocalStatsToPhone();
uint32_t sendToPhoneIntervalMs = SECONDS_IN_MINUTE * 1000; // Send to phone every minute
uint32_t sendStatsToPhoneIntervalMs = 15 * SECONDS_IN_MINUTE * 1000; // Send stats to phone every 15 minutes
uint32_t lastSentStatsToPhone = 0;
uint32_t lastSentToMesh = 0;

void refreshUptime()
Expand Down
1 change: 1 addition & 0 deletions src/modules/Telemetry/EnvironmentTelemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ bool EnvironmentTelemetryModule::getEnvironmentTelemetry(meshtastic_Telemetry *m
bool hasSensor = false;
m->time = getTime();
m->which_variant = meshtastic_Telemetry_environment_metrics_tag;
m->variant.environment_metrics = meshtastic_EnvironmentMetrics_init_zero;

#ifdef T1000X_SENSOR_EN // add by WayenWeng
valid = valid && t1000xSensor.getMetrics(m);
Expand Down
3 changes: 3 additions & 0 deletions src/modules/Telemetry/Sensor/AHT10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ bool AHT10Sensor::getMetrics(meshtastic_Telemetry *measurement)
sensors_event_t humidity, temp;
aht10.getEvent(&humidity, &temp);

measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.environment_metrics.has_relative_humidity = true;

measurement->variant.environment_metrics.temperature = temp.temperature;
measurement->variant.environment_metrics.relative_humidity = humidity.relative_humidity;

Expand Down
4 changes: 4 additions & 0 deletions src/modules/Telemetry/Sensor/BME280Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ void BME280Sensor::setup() {}

bool BME280Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.environment_metrics.has_relative_humidity = true;
measurement->variant.environment_metrics.has_barometric_pressure = true;

LOG_DEBUG("BME280Sensor::getMetrics\n");
bme280.takeForcedMeasurement();
measurement->variant.environment_metrics.temperature = bme280.readTemperature();
Expand Down
7 changes: 7 additions & 0 deletions src/modules/Telemetry/Sensor/BME680Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ bool BME680Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
if (bme680.getData(BSEC_OUTPUT_RAW_PRESSURE).signal == 0)
return false;

measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.environment_metrics.has_relative_humidity = true;
measurement->variant.environment_metrics.has_barometric_pressure = true;
measurement->variant.environment_metrics.has_gas_resistance = true;
measurement->variant.environment_metrics.has_iaq = true;

measurement->variant.environment_metrics.temperature = bme680.getData(BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE).signal;
measurement->variant.environment_metrics.relative_humidity =
bme680.getData(BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY).signal;
Expand Down
3 changes: 3 additions & 0 deletions src/modules/Telemetry/Sensor/BMP085Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ void BMP085Sensor::setup() {}

bool BMP085Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.environment_metrics.has_barometric_pressure = true;

LOG_DEBUG("BMP085Sensor::getMetrics\n");
measurement->variant.environment_metrics.temperature = bmp085.readTemperature();
measurement->variant.environment_metrics.barometric_pressure = bmp085.readPressure() / 100.0F;
Expand Down
3 changes: 3 additions & 0 deletions src/modules/Telemetry/Sensor/BMP280Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ void BMP280Sensor::setup() {}

bool BMP280Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.environment_metrics.has_barometric_pressure = true;

LOG_DEBUG("BMP280Sensor::getMetrics\n");
bmp280.takeForcedMeasurement();
measurement->variant.environment_metrics.temperature = bmp280.readTemperature();
Expand Down
6 changes: 6 additions & 0 deletions src/modules/Telemetry/Sensor/DFRobotLarkSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ void DFRobotLarkSensor::setup() {}

bool DFRobotLarkSensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.environment_metrics.has_relative_humidity = true;
measurement->variant.environment_metrics.has_wind_speed = true;
measurement->variant.environment_metrics.has_wind_direction = true;
measurement->variant.environment_metrics.has_barometric_pressure = true;

measurement->variant.environment_metrics.temperature = lark.getValue("Temp").toFloat();
measurement->variant.environment_metrics.relative_humidity = lark.getValue("Humi").toFloat();
measurement->variant.environment_metrics.wind_speed = lark.getValue("Speed").toFloat();
Expand Down
3 changes: 3 additions & 0 deletions src/modules/Telemetry/Sensor/INA219Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ void INA219Sensor::setup() {}

bool INA219Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_voltage = true;
measurement->variant.environment_metrics.has_current = true;

measurement->variant.environment_metrics.voltage = ina219.getBusVoltage_V();
measurement->variant.environment_metrics.current = ina219.getCurrent_mA() * INA219_MULTIPLIER;
return true;
Expand Down
3 changes: 3 additions & 0 deletions src/modules/Telemetry/Sensor/INA260Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ void INA260Sensor::setup() {}

bool INA260Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_voltage = true;
measurement->variant.environment_metrics.has_current = true;

// mV conversion to V
measurement->variant.environment_metrics.voltage = ina260.readBusVoltage() / 1000;
measurement->variant.environment_metrics.current = ina260.readCurrent();
Expand Down
10 changes: 10 additions & 0 deletions src/modules/Telemetry/Sensor/INA3221Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ bool INA3221Sensor::getEnvironmentMetrics(meshtastic_Telemetry *measurement)
{
struct _INA3221Measurement m = getMeasurement(ENV_CH);

measurement->variant.environment_metrics.has_voltage = true;
measurement->variant.environment_metrics.has_current = true;

measurement->variant.environment_metrics.voltage = m.voltage;
measurement->variant.environment_metrics.current = m.current;

Expand All @@ -77,6 +80,13 @@ bool INA3221Sensor::getPowerMetrics(meshtastic_Telemetry *measurement)
{
struct _INA3221Measurements m = getMeasurements();

measurement->variant.power_metrics.has_ch1_voltage = true;
measurement->variant.power_metrics.has_ch1_current = true;
measurement->variant.power_metrics.has_ch2_voltage = true;
measurement->variant.power_metrics.has_ch2_current = true;
measurement->variant.power_metrics.has_ch3_voltage = true;
measurement->variant.power_metrics.has_ch3_current = true;

measurement->variant.power_metrics.ch1_voltage = m.measurements[INA3221_CH1].voltage;
measurement->variant.power_metrics.ch1_current = m.measurements[INA3221_CH1].current;
measurement->variant.power_metrics.ch2_voltage = m.measurements[INA3221_CH2].voltage;
Expand Down
3 changes: 3 additions & 0 deletions src/modules/Telemetry/Sensor/LPS22HBSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ void LPS22HBSensor::setup()

bool LPS22HBSensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.environment_metrics.has_barometric_pressure = true;

sensors_event_t temp;
sensors_event_t pressure;
lps22hb.getEvent(&pressure, &temp);
Expand Down
2 changes: 2 additions & 0 deletions src/modules/Telemetry/Sensor/MCP9808Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ void MCP9808Sensor::setup()

bool MCP9808Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_temperature = true;

LOG_DEBUG("MCP9808Sensor::getMetrics\n");
measurement->variant.environment_metrics.temperature = mcp9808.readTempC();
return true;
Expand Down
1 change: 1 addition & 0 deletions src/modules/Telemetry/Sensor/MLX90632Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void MLX90632Sensor::setup() {}

bool MLX90632Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.environment_metrics.temperature = mlx.getObjectTemp(); // Get the object temperature in Fahrenheit

return true;
Expand Down
1 change: 1 addition & 0 deletions src/modules/Telemetry/Sensor/NAU7802Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ bool NAU7802Sensor::getMetrics(meshtastic_Telemetry *measurement)
return false;
}
}
measurement->variant.environment_metrics.has_weight = true;
// Check if we have correct calibration values after powerup
LOG_DEBUG("Offset: %d, Calibration factor: %.2f\n", nau7802.getZeroOffset(), nau7802.getCalibrationFactor());
measurement->variant.environment_metrics.weight = nau7802.getWeight() / 1000; // sample is in kg
Expand Down
1 change: 1 addition & 0 deletions src/modules/Telemetry/Sensor/OPT3001Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void OPT3001Sensor::setup()

bool OPT3001Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_lux = true;
OPT3001 result = opt3001.readResult();

measurement->variant.environment_metrics.lux = result.lux;
Expand Down
1 change: 1 addition & 0 deletions src/modules/Telemetry/Sensor/RCWL9620Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void RCWL9620Sensor::setup() {}

bool RCWL9620Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_distance = true;
LOG_DEBUG("RCWL9620Sensor::getMetrics\n");
measurement->variant.environment_metrics.distance = getDistance();
return true;
Expand Down
2 changes: 2 additions & 0 deletions src/modules/Telemetry/Sensor/SHT31Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ void SHT31Sensor::setup()

bool SHT31Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.environment_metrics.has_relative_humidity = true;
measurement->variant.environment_metrics.temperature = sht31.readTemperature();
measurement->variant.environment_metrics.relative_humidity = sht31.readHumidity();

Expand Down
3 changes: 3 additions & 0 deletions src/modules/Telemetry/Sensor/SHT4XSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ void SHT4XSensor::setup()

bool SHT4XSensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.environment_metrics.has_relative_humidity = true;

sensors_event_t humidity, temp;
sht4x.getEvent(&humidity, &temp);
measurement->variant.environment_metrics.temperature = temp.temperature;
Expand Down
3 changes: 3 additions & 0 deletions src/modules/Telemetry/Sensor/SHTC3Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ void SHTC3Sensor::setup()

bool SHTC3Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.environment_metrics.has_relative_humidity = true;

sensors_event_t humidity, temp;
shtc3.getEvent(&humidity, &temp);

Expand Down
3 changes: 3 additions & 0 deletions src/modules/Telemetry/Sensor/T1000xSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ float T1000xSensor::getTemp()

bool T1000xSensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.environment_metrics.has_lux = true;

measurement->variant.environment_metrics.temperature = getTemp();
measurement->variant.environment_metrics.lux = getLux();
return true;
Expand Down
1 change: 1 addition & 0 deletions src/modules/Telemetry/Sensor/TSL2591Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void TSL2591Sensor::setup()

bool TSL2591Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_lux = true;
uint32_t lum = tsl.getFullLuminosity();
uint16_t ir, full;
ir = lum >> 16;
Expand Down
3 changes: 3 additions & 0 deletions src/modules/Telemetry/Sensor/VEML7700Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ float VEML7700Sensor::getResolution(void)

bool VEML7700Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.has_lux = true;
measurement->variant.environment_metrics.has_white_lux = true;

int16_t white;
measurement->variant.environment_metrics.lux = veml7700.readLux(VEML_LUX_AUTO);
white = veml7700.readWhite(true);
Expand Down
Loading