Skip to content

Commit f74c0e4

Browse files
committed
Add option to monitor battery level
1 parent ff2f81d commit f74c0e4

File tree

6 files changed

+58
-3
lines changed

6 files changed

+58
-3
lines changed

config.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,8 @@
104104
#define CFG_PTT_PIN 12 // PTT pin
105105
#define CFG_PTT_TX_DELAY_MS 50 // delay between relay switching ON and transmission startup
106106
#define CFG_PTT_TX_TAIL_MS 10 // delay between stopping transmission and relay switching OFF
107+
108+
// Enable modem telemetry
109+
#define CFG_TLM_ENABLE false // enable modem battery monitor
110+
#define CFG_TLM_BAT_MON_PIN 36 // battery ADC pin
111+
#define CFG_TLM_BAT_MON_CAL 0.37f // calibration coefficient

esp32_loraprs.ino

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ void initializeConfig(LoraPrs::Config &cfg) {
9494
cfg.PttPin = CFG_PTT_PIN;
9595
cfg.PttTxDelayMs = CFG_PTT_TX_DELAY_MS;
9696
cfg.PttTxTailMs = CFG_PTT_TX_TAIL_MS;
97+
98+
// battery level monitor
99+
cfg.TlmEnable = CFG_TLM_ENABLE;
100+
cfg.TlmBatMonPin = CFG_TLM_BAT_MON_PIN;
101+
cfg.TlmBatMonCal = CFG_TLM_BAT_MON_CAL;
97102
}
98103

99104
LoraPrs::Service loraPrsService;

kiss_processor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Processor {
3939
SetHardware = 0x06,
4040
SignalReport = 0x07,
4141
RebootRequested = 0x08,
42+
Telemetry = 0x09,
4243
NoCmd = 0x80
4344
};
4445

@@ -77,7 +78,7 @@ class Processor {
7778

7879
private:
7980
bool receiveByte(byte rxByte);
80-
bool receiveByteRaw(byte rxByte);
81+
bool receiveByteRaw(byte rxByte);
8182
bool receiveByteKiss(byte rxByte);
8283

8384
void processData(byte rxByte);

loraprs_config.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ struct Config
7575
int PttPin; // esp pin to set high on transmit
7676
int PttTxDelayMs; // ptt tx delay
7777
int PttTxTailMs; // ptt tx tail
78+
79+
// enable modem telemetry
80+
bool TlmEnable; // true - enable modem telemetry event
81+
int TlmBatMonPin; // battery monitor pin
82+
float TlmBatMonCal; // calibration coefficient
7883
};
7984

8085
} // LoraPrs

loraprs_service.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ void Service::setup(const Config &conf)
8787
LOG_INFO("External PTT is enabled");
8888
pinMode(config_.PttPin, OUTPUT);
8989
}
90+
91+
// telemetry event
92+
if (config_.TlmEnable) {
93+
LOG_INFO("Telemetry event is enabled");
94+
telemetryTimer_.every(CfgTelemetryPeriodMs, sendModemTelemetryTimer, this);
95+
}
9096
}
9197

9298
void Service::printConfig() {
@@ -301,6 +307,11 @@ void Service::loop()
301307
csmaSlotTimePrev_ = currentTime;
302308
}
303309
}
310+
311+
// timers
312+
if (config_.TlmEnable) {
313+
telemetryTimer_.tick();
314+
}
304315
delay(CfgPollDelayMs);
305316
}
306317

@@ -455,6 +466,22 @@ void Service::sendSignalReportEvent(int rssi, float snr)
455466
sendRigToSerial(Cmd::SignalReport, (const byte *)&signalReport, sizeof(SignalReport));
456467
}
457468

469+
bool Service::sendModemTelemetryTimer(void *param)
470+
{
471+
((Service *)param)->sendModemTelemetry();
472+
return true;
473+
}
474+
475+
void Service::sendModemTelemetry()
476+
{
477+
float batVoltage = 2 * analogRead(config_.TlmBatMonPin) * (3.3 / 4096.0) + config_.TlmBatMonCal;
478+
LOG_INFO("Battery voltage", batVoltage);
479+
480+
struct Telemetry telemetry;
481+
telemetry.batteryVoltage = htobe16(100 * batVoltage);
482+
sendRigToSerial(Cmd::Telemetry, (const byte *)&telemetry, sizeof(Telemetry));
483+
}
484+
458485
bool Service::sendAx25PayloadToRig(const AX25::Payload &payload)
459486
{
460487
int bytesWritten;

loraprs_service.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#ifndef LORAPRS_SEVICE_H
1+
#ifndef LORAPRS_SERVICE_H
22
#define LORAPRS_SERVICE_H
33

44
#include <Arduino.h>
@@ -16,6 +16,7 @@
1616

1717
#include <WiFi.h>
1818
#include <endian.h>
19+
#include <arduino-timer.h>
1920

2021
#include "BluetoothSerial.h"
2122
#include "ble_serial.h"
@@ -54,6 +55,8 @@ class Service : public Kiss::Processor
5455
void onAprsisDataAvailable();
5556

5657
void sendSignalReportEvent(int rssi, float snr);
58+
static bool sendModemTelemetryTimer(void *param);
59+
void sendModemTelemetry();
5760
void sendPeriodicBeacon();
5861
void sendToAprsis(const String &aprsMessage);
5962
bool sendAx25PayloadToRig(const AX25::Payload &payload);
@@ -110,14 +113,20 @@ class Service : public Kiss::Processor
110113
int16_t rssi;
111114
int16_t snr;
112115
} __attribute__((packed));
116+
117+
struct Telemetry {
118+
int16_t batteryVoltage;
119+
} __attribute__((packed));
113120

114121
private:
115-
const String CfgLoraprsVersion = "LoRAPRS 1.0.5";
122+
const String CfgLoraprsVersion = "LoRAPRS 1.0.6";
116123

117124
// processor config
118125
const int CfgConnRetryMs = 500; // connection retry delay, e.g. wifi
119126
const int CfgPollDelayMs = 20; // main loop delay
120127
const int CfgConnRetryMaxTimes = 10; // number of connection retries
128+
const int CfgTelemetryPeriodMs = 30000; // how often to send telemetry event
129+
121130
static const int CfgMaxPacketSize = 256; // maximum packet size
122131
static const int CfgRadioQueueSize = 1024; // radio queue size
123132

@@ -168,6 +177,9 @@ class Service : public Kiss::Processor
168177
std::shared_ptr<WiFiServer> kissServer_;
169178
WiFiClient kissConnnection_;
170179
bool isKissClientConnected_;
180+
181+
// modem telemetry
182+
Timer<1> telemetryTimer_;
171183
};
172184

173185
} // LoraPrs

0 commit comments

Comments
 (0)