Add health telemetry module (#4927)

* Add stub health telemetry module

* Add detection for MAX30102 Health Sensor

It lives on I2C bus at 0x57, which conflicts with an existing
sensor. Add code to check the PARTID register for its response 0x15
per spec.

* Add detection for MLX90614

An IR Temperature sensor suitable for livestock monitoring.

* Add libraries for MLX90614 and MAX30102 sensors

* Fix Trunk

* Add support for MLX90614 IR Temperature Sensor

* Add support for MAX30102 (Temperature)

* Make it build - our first HealthTelemetry on the mesh.

If a MAX30102 is connected, its temperature will be sent to the
mesh as HealthTelemetry.

* Add spo2 and heart rate calculations to MAX30102

* Switch MLX90614 to Adafruit library

Sparkfun was having fun with SDA/SCL variables which we can avoid
by switching to this highly similar library.

* Enable HealthTelemetry if MLX90614 detected

* Change MLX90614 emissivity for human skin.

* Add health screen!

* Remove autogenerated file from branch

* Preparing for review

* Fix MeshService master sync from before.

* Prepare for review

* For the americans

* Fix native build

* Fix for devices with no screen

* Remove extra log causing issues

---------

Co-authored-by: Tom Fifield <tom@tomfifield.net>
This commit is contained in:
Ben Meadors
2024-10-07 19:50:44 -05:00
committed by GitHub
parent 1c54388bb8
commit 411aedaf5d
13 changed files with 515 additions and 2 deletions

View File

@@ -0,0 +1,83 @@
#include "configuration.h"
#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_PORTDUINO)
#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "MAX30102Sensor.h"
#include "TelemetrySensor.h"
#include <spo2_algorithm.h>
MAX30102Sensor::MAX30102Sensor() : TelemetrySensor(meshtastic_TelemetrySensorType_MAX30102, "MAX30102") {}
int32_t MAX30102Sensor::runOnce()
{
LOG_INFO("Init sensor: %s\n", sensorName);
if (!hasSensor()) {
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
}
if (max30102.begin(*nodeTelemetrySensorsMap[sensorType].second, _speed, nodeTelemetrySensorsMap[sensorType].first) ==
true) // MAX30102 init
{
byte brightness = 60; // 0=Off to 255=50mA
byte sampleAverage = 4; // 1, 2, 4, 8, 16, 32
byte leds = 2; // 1 = Red only, 2 = Red + IR
byte sampleRate = 100; // 50, 100, 200, 400, 800, 1000, 1600, 3200
int pulseWidth = 411; // 69, 118, 215, 411
int adcRange = 4096; // 2048, 4096, 8192, 16384
max30102.enableDIETEMPRDY(); // Enable the temperature ready interrupt
max30102.setup(brightness, sampleAverage, leds, sampleRate, pulseWidth, adcRange);
LOG_DEBUG("MAX30102 Init Succeed\n");
status = true;
} else {
LOG_ERROR("MAX30102 Init Failed\n");
status = false;
}
return initI2CSensor();
}
void MAX30102Sensor::setup() {}
bool MAX30102Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
uint32_t ir_buff[MAX30102_BUFFER_LEN];
uint32_t red_buff[MAX30102_BUFFER_LEN];
int32_t spo2;
int8_t spo2_valid;
int32_t heart_rate;
int8_t heart_rate_valid;
float temp = max30102.readTemperature();
measurement->variant.environment_metrics.temperature = temp;
measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.health_metrics.temperature = temp;
measurement->variant.health_metrics.has_temperature = true;
for (byte i = 0; i < MAX30102_BUFFER_LEN; i++) {
while (max30102.available() == false)
max30102.check();
red_buff[i] = max30102.getRed();
ir_buff[i] = max30102.getIR();
max30102.nextSample();
}
maxim_heart_rate_and_oxygen_saturation(ir_buff, MAX30102_BUFFER_LEN, red_buff, &spo2, &spo2_valid, &heart_rate,
&heart_rate_valid);
LOG_DEBUG("heart_rate=%d(%d), sp02=%d(%d)", heart_rate, heart_rate_valid, spo2, spo2_valid);
if (heart_rate_valid) {
measurement->variant.health_metrics.has_heart_bpm = true;
measurement->variant.health_metrics.heart_bpm = heart_rate;
} else {
measurement->variant.health_metrics.has_heart_bpm = false;
}
if (spo2_valid) {
measurement->variant.health_metrics.has_spO2 = true;
measurement->variant.health_metrics.spO2 = spo2;
} else {
measurement->variant.health_metrics.has_spO2 = true;
}
return true;
}
#endif

View File

@@ -0,0 +1,26 @@
#include "configuration.h"
#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_PORTDUINO)
#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "TelemetrySensor.h"
#include <MAX30105.h>
#define MAX30102_BUFFER_LEN 100
class MAX30102Sensor : public TelemetrySensor
{
private:
MAX30105 max30102 = MAX30105();
uint32_t _speed = 200000UL;
protected:
virtual void setup() override;
public:
MAX30102Sensor();
virtual int32_t runOnce() override;
virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
};
#endif

View File

@@ -0,0 +1,44 @@
#include "configuration.h"
#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_PORTDUINO)
#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "MLX90614Sensor.h"
#include "TelemetrySensor.h"
MLX90614Sensor::MLX90614Sensor() : TelemetrySensor(meshtastic_TelemetrySensorType_MLX90614, "MLX90614") {}
int32_t MLX90614Sensor::runOnce()
{
LOG_INFO("Init sensor: %s\n", sensorName);
if (!hasSensor()) {
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
}
if (mlx.begin(nodeTelemetrySensorsMap[sensorType].first, nodeTelemetrySensorsMap[sensorType].second) == true) // MLX90614 init
{
LOG_DEBUG("MLX90614 emissivity: %f", mlx.readEmissivity());
if (fabs(MLX90614_EMISSIVITY - mlx.readEmissivity()) > 0.001) {
mlx.writeEmissivity(MLX90614_EMISSIVITY);
LOG_INFO("MLX90614 emissivity updated. In case of weird data, power cycle.");
}
LOG_DEBUG("MLX90614 Init Succeed\n");
status = true;
} else {
LOG_ERROR("MLX90614 Init Failed\n");
status = false;
}
return initI2CSensor();
}
void MLX90614Sensor::setup() {}
bool MLX90614Sensor::getMetrics(meshtastic_Telemetry *measurement)
{
measurement->variant.environment_metrics.temperature = mlx.readAmbientTempC();
measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.health_metrics.temperature = mlx.readObjectTempC();
measurement->variant.health_metrics.has_temperature = true;
return true;
}
#endif

View File

@@ -0,0 +1,24 @@
#include "configuration.h"
#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_PORTDUINO)
#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "TelemetrySensor.h"
#include <Adafruit_MLX90614.h>
#define MLX90614_EMISSIVITY 0.98 // human skin
class MLX90614Sensor : public TelemetrySensor
{
private:
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
protected:
virtual void setup() override;
public:
MLX90614Sensor();
virtual int32_t runOnce() override;
virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
};
#endif