From f74c0e4019a49a3773abc85550f49c5c2657f30c Mon Sep 17 00:00:00 2001 From: sh123 Date: Sun, 18 Dec 2022 00:01:33 +0200 Subject: [PATCH] Add option to monitor battery level --- config.h | 5 +++++ esp32_loraprs.ino | 5 +++++ kiss_processor.h | 3 ++- loraprs_config.h | 5 +++++ loraprs_service.cpp | 27 +++++++++++++++++++++++++++ loraprs_service.h | 16 ++++++++++++++-- 6 files changed, 58 insertions(+), 3 deletions(-) diff --git a/config.h b/config.h index ec09b6a..7c9649b 100644 --- a/config.h +++ b/config.h @@ -104,3 +104,8 @@ #define CFG_PTT_PIN 12 // PTT pin #define CFG_PTT_TX_DELAY_MS 50 // delay between relay switching ON and transmission startup #define CFG_PTT_TX_TAIL_MS 10 // delay between stopping transmission and relay switching OFF + +// Enable modem telemetry +#define CFG_TLM_ENABLE false // enable modem battery monitor +#define CFG_TLM_BAT_MON_PIN 36 // battery ADC pin +#define CFG_TLM_BAT_MON_CAL 0.37f // calibration coefficient diff --git a/esp32_loraprs.ino b/esp32_loraprs.ino index 4ff793c..b8da02f 100644 --- a/esp32_loraprs.ino +++ b/esp32_loraprs.ino @@ -94,6 +94,11 @@ void initializeConfig(LoraPrs::Config &cfg) { cfg.PttPin = CFG_PTT_PIN; cfg.PttTxDelayMs = CFG_PTT_TX_DELAY_MS; cfg.PttTxTailMs = CFG_PTT_TX_TAIL_MS; + + // battery level monitor + cfg.TlmEnable = CFG_TLM_ENABLE; + cfg.TlmBatMonPin = CFG_TLM_BAT_MON_PIN; + cfg.TlmBatMonCal = CFG_TLM_BAT_MON_CAL; } LoraPrs::Service loraPrsService; diff --git a/kiss_processor.h b/kiss_processor.h index 75da0b9..3d9ba01 100644 --- a/kiss_processor.h +++ b/kiss_processor.h @@ -39,6 +39,7 @@ protected: SetHardware = 0x06, SignalReport = 0x07, RebootRequested = 0x08, + Telemetry = 0x09, NoCmd = 0x80 }; @@ -77,7 +78,7 @@ protected: private: bool receiveByte(byte rxByte); - bool receiveByteRaw(byte rxByte); + bool receiveByteRaw(byte rxByte); bool receiveByteKiss(byte rxByte); void processData(byte rxByte); diff --git a/loraprs_config.h b/loraprs_config.h index 9ff9742..815bc33 100644 --- a/loraprs_config.h +++ b/loraprs_config.h @@ -75,6 +75,11 @@ struct Config int PttPin; // esp pin to set high on transmit int PttTxDelayMs; // ptt tx delay int PttTxTailMs; // ptt tx tail + + // enable modem telemetry + bool TlmEnable; // true - enable modem telemetry event + int TlmBatMonPin; // battery monitor pin + float TlmBatMonCal; // calibration coefficient }; } // LoraPrs diff --git a/loraprs_service.cpp b/loraprs_service.cpp index c470da8..96475bf 100644 --- a/loraprs_service.cpp +++ b/loraprs_service.cpp @@ -87,6 +87,12 @@ void Service::setup(const Config &conf) LOG_INFO("External PTT is enabled"); pinMode(config_.PttPin, OUTPUT); } + + // telemetry event + if (config_.TlmEnable) { + LOG_INFO("Telemetry event is enabled"); + telemetryTimer_.every(CfgTelemetryPeriodMs, sendModemTelemetryTimer, this); + } } void Service::printConfig() { @@ -301,6 +307,11 @@ void Service::loop() csmaSlotTimePrev_ = currentTime; } } + + // timers + if (config_.TlmEnable) { + telemetryTimer_.tick(); + } delay(CfgPollDelayMs); } @@ -455,6 +466,22 @@ void Service::sendSignalReportEvent(int rssi, float snr) sendRigToSerial(Cmd::SignalReport, (const byte *)&signalReport, sizeof(SignalReport)); } +bool Service::sendModemTelemetryTimer(void *param) +{ + ((Service *)param)->sendModemTelemetry(); + return true; +} + +void Service::sendModemTelemetry() +{ + float batVoltage = 2 * analogRead(config_.TlmBatMonPin) * (3.3 / 4096.0) + config_.TlmBatMonCal; + LOG_INFO("Battery voltage", batVoltage); + + struct Telemetry telemetry; + telemetry.batteryVoltage = htobe16(100 * batVoltage); + sendRigToSerial(Cmd::Telemetry, (const byte *)&telemetry, sizeof(Telemetry)); +} + bool Service::sendAx25PayloadToRig(const AX25::Payload &payload) { int bytesWritten; diff --git a/loraprs_service.h b/loraprs_service.h index f58b3fb..72609c3 100644 --- a/loraprs_service.h +++ b/loraprs_service.h @@ -1,4 +1,4 @@ -#ifndef LORAPRS_SEVICE_H +#ifndef LORAPRS_SERVICE_H #define LORAPRS_SERVICE_H #include @@ -16,6 +16,7 @@ #include #include +#include #include "BluetoothSerial.h" #include "ble_serial.h" @@ -54,6 +55,8 @@ private: void onAprsisDataAvailable(); void sendSignalReportEvent(int rssi, float snr); + static bool sendModemTelemetryTimer(void *param); + void sendModemTelemetry(); void sendPeriodicBeacon(); void sendToAprsis(const String &aprsMessage); bool sendAx25PayloadToRig(const AX25::Payload &payload); @@ -110,14 +113,20 @@ private: int16_t rssi; int16_t snr; } __attribute__((packed)); + + struct Telemetry { + int16_t batteryVoltage; + } __attribute__((packed)); private: - const String CfgLoraprsVersion = "LoRAPRS 1.0.5"; + const String CfgLoraprsVersion = "LoRAPRS 1.0.6"; // processor config const int CfgConnRetryMs = 500; // connection retry delay, e.g. wifi const int CfgPollDelayMs = 20; // main loop delay const int CfgConnRetryMaxTimes = 10; // number of connection retries + const int CfgTelemetryPeriodMs = 30000; // how often to send telemetry event + static const int CfgMaxPacketSize = 256; // maximum packet size static const int CfgRadioQueueSize = 1024; // radio queue size @@ -168,6 +177,9 @@ private: std::shared_ptr kissServer_; WiFiClient kissConnnection_; bool isKissClientConnected_; + + // modem telemetry + Timer<1> telemetryTimer_; }; } // LoraPrs