Skip to content

Commit

Permalink
Detect charging status by measuring current flow with configured INA …
Browse files Browse the repository at this point in the history
…battery sensor (meshtastic#5271)

* INA219 charging detection

minimal implementation: if there is a configured INA219 sensor for battery monitoring we can take the current flow across the shunt resistor to know if we are charging the battery - negative milliamps indicate charging

* Update Power.cpp

added comments and 2 extra defines to disable and swap detection direction

* Update Power.cpp

fix disabled case

* move getCurrentMa() to new CurrentSensor class

* INA219 charging detection

minimal implementation: if there is a configured INA219 sensor for battery monitoring we can take the current flow across the shunt resistor to know if we are charging the battery - negative milliamps indicate charging

* Update Power.cpp

added comments and 2 extra defines to disable and swap detection direction

* Update Power.cpp

fix disabled case

* move getCurrentMa() to new CurrentSensor class

* add INA3221 charging detection

* RP2040: Update core; add mDNS support (meshtastic#5355)

* Update arduino-pico core

* RP2040: Add mDNS support

* SimpleMDNS `begin` now returns a bool

* Add `-g` option to `debug_build_flags` to link files for gdb

* RAK11310 needs old platform as well

* Change defines to specific architecture

* Core version 4.2.1 is out

* Add sudo to apt-get commands for Raspbian Build (meshtastic#5364)

Without sudo, inadequate permissions to runs the commands meant
the build was failing.

* Typo fix in build_raspbian.yml (meshtastic#5365)

s/sudp/sudo :(:(:(

* Rework some things

* Trunk

* Separate littlefs bundle

* version tags

* Diag

* Add littlefswebui

* Bug fixed in ExternalNotificationModule (meshtastic#5375)

While `nagging` setExternalState wasn't written to Buzzer & Vibra so output was never toggled.

Possible fix for meshtastic#5348

* Cleanup static files from bad Web UI bundle on 2.5.13 release (meshtastic#5376)

* Cleanup static files from bad Web UI bundle on 2.5.13 release

* Check existence first

* Esp32 is the only one we care about

* Move some actions to after `startTransmit()` (meshtastic#5383)

To minimize the time between channel scan and actual transmit

* [create-pull-request] automated change (meshtastic#5380)

Co-authored-by: thebentern <[email protected]>

* Allows all 3 PKI keys to be added to userPrefs.h (meshtastic#4969) and a tool. (meshtastic#5368)

* more userPrefs.h

Added PKI Admin keys to userPrefs.h

* Update userPrefs.h

Allows all 3 PKI keys to be added to userPrefs.h (meshtastic#4969)

* Update NodeDB.cpp

Trunk

* Update userPrefs.h

Changed wording

* Create base64_to_hex.py

A little tool for converting base64 PKI Keys to decoded byte that userPrefs.h can understand.

* more userPrefs.h

Added PKI Admin keys to userPrefs.h

* Update userPrefs.h

Allows all 3 PKI keys to be added to userPrefs.h (meshtastic#4969)

* Update NodeDB.cpp

Trunk

* Update userPrefs.h

Changed wording

* Create base64_to_hex.py

A little tool for converting base64 PKI Keys to decoded byte that userPrefs.h can understand.

* [create-pull-request] automated change (meshtastic#5388)

Co-authored-by: thebentern <[email protected]>

* add smiley emoji (meshtastic#5391)

* add smiley emoji

* clang-formatted

* Anable trace route function on wismeshtap platform (meshtastic#5389)

* fix 'symbal' typo (meshtastic#5395)

* [create-pull-request] automated change (meshtastic#5399)

Co-authored-by: thebentern <[email protected]>

* /api/v1/fromradio: add OPTIONS handler for CORS. (meshtastic#5386)

This avoids hitting the 404 Not Found handler, which breaks connection
keep-alive, so this change fixes a big performance regression for Web Client in
Chrome: meshtastic#5385

Tested on Heltec V3.

Co-authored-by: Ben Meadors <[email protected]>

* Make heart emoji usable (meshtastic#5403)

* Create a specific hw_model for WisMesh Tap (meshtastic#5400)

* Create a specific hw_model for WisMesh Tap

* Trunk

* HAS_ETHERNET

* Remove it altogether

* Don't need these either

* Fix RTC time injection and consolidate position logic (meshtastic#5396)

* Fix RTC time injection and consolidate position logic

* Comment out unused var warning

* Backerds

* Update arduino-pico core to fix sporadic hangs (meshtastic#5406)

* Update platform-raspberrypi also (meshtastic#5407)

* Update arduino-pico core to fix sporadic hangs

* Update platform-raspberrypi also

* --web added to device-install(.sh/.bat) (meshtastic#5405)

* Add --web

* Update device-install.bat

Forgot a "-" a few places.

---------

Co-authored-by: Ben Meadors <[email protected]>

* add GPS in indicator board (meshtastic#5411)

* Fixed NMEA sentence issue in CalTopo as well as bug with no printing all of the nodes (meshtastic#5412)

* --web littlefswebui-* typo fix (meshtastic#5416)

* Add --web

* Update device-install.bat

Forgot a "-" a few places.

* Typo fix.

* Typo fix

---------

Co-authored-by: Ben Meadors <[email protected]>
Co-authored-by: GUVWAF <[email protected]>

* Temporarily disable MDNS when MQTT is enabled (meshtastic#5418)

Leads to a panic

* Check for OkToMqtt flag presence before uplinking to MQTT (meshtastic#5413)

* Check for oktomqtt flag presence before uplinking to MQTT

* Move to mqtt->onSend

* Temetry can respond to want-response for LocalStats variant (meshtastic#5414)

* Seems like the last DIY board that's not "extra" (meshtastic#5420)

* Cherry pick tdeck fixes (meshtastic#5422)

* Try-fix (workaround) T-Deck audio crash

* set T-Deck audio to unused 48 (mem mclk)

* swap mclk to gpio 21

* dreamcatcher: assign GPIO44 to audio mclk

---------

Co-authored-by: mverch67 <[email protected]>

* add canned message and keyboard in indicator board (meshtastic#5410)

* add canned message and keyboard in indicator board

* Added virtual keyboard macro and enabled for Indicator

* Cleanup macros by applying USE_VIRTUAL_KEYBOARD and DISPLAY_CLOCK_FRAME

---------

Co-authored-by: Ben Meadors <[email protected]>

* Update build-native.sh (meshtastic#5415)

* Update build-native.sh

Device-install.sh and device-update.sh are not used on native platform, skip copying to release directory after build and copy native-install.sh and native-run.sh instead.

* Update build-native.sh

Skip native-run.sh copy

* Cleans up visibility in GPS.h (meshtastic#5426)

Signed-off-by: Christopher Hoover <[email protected]>

* Fix admin key loading from userPrefs.h (meshtastic#5417)

* Fix LED pinout for T-Echo board marked v1.0, date 2021-6-28

* Merge PR meshtastic#420

* Fixed double and missing Default class.

* Use correct format specifier and fixed typo.

* Removed duplicate code.

* Fix error: #if with no expression

* Fix warning: extra tokens at end of #endif directive.

* Fix antenna switching logic. Complementary-pin control logic is required on the rp2040-lora board.

* Fix deprecated macros.

* Set RP2040 in dormant mode when deep sleep is triggered.

* Fix array out of bounds read.

* Admin key count needs to be set otherwise the key will be zero loaded after reset.

* Don't reset the admin key size when loading defaults. Preserve an existing key in config if possible.

* Remove log spam when reading INA voltage sensor.

* Remove static declaration for admin keys from userPrefs.h. Load hard coded admin keys in case config file has empty slots.

* Removed newlines from log.

---------

Co-authored-by: Ben Meadors <[email protected]>
Co-authored-by: Thomas Göttgens <[email protected]>

* try to detect dfrobot station to tell it apart from an ublox gps. (meshtastic#5393)

* Remove BMA-423 and STK8X by default (meshtastic#5429)

* Remove BMA-423 by default

* STK

* Wrong macro

* Helps if you include the file

* [create-pull-request] automated change (meshtastic#5431)

Co-authored-by: caveman99 <[email protected]>

* Support for the ClimateGuard RadSens Geiger-Muller tube (meshtastic#5425)

* fixes meshtastic#5434 (meshtastic#5435)

* update libpax
* fix interval init

* Fix memory leaks by adding missing `free()` calls before early returns in `MQTT::onReceive` (meshtastic#5439)

This fix addresses memory leaks in the `MQTT::onReceive` function by ensuring that dynamically allocated resources (`e.channel_id`, `e.gateway_id` and `e.packet`) are properly freed before each early return. Previously, these resources were only freed at the end of the function, leaving them unhandled in certain exit paths. Adding the missing `free()` calls prevents memory leaks and ensures proper resource cleanup in all scenarios.

* Removing 1.0 legacy boards from releases and completely removing Heltec wireless capsule from support (meshtastic#5436)

Co-authored-by: Tom Fifield <[email protected]>

* A second round of cleanup on GPS.h. (meshtastic#5433)

* Move yet more stuff out of GPS.h and into file scope.

* Protect code macros from eating semicolons.

* Remove unused (and unimplemented) getDOPString.

* clang-format with project style file on affected files.

Signed-off-by: Christopher Hoover <[email protected]>

* enable MQTT with TLS on RPi picow (meshtastic#5442)

Co-authored-by: Ben Meadors <[email protected]>

* Don't powersave on Wifi (meshtastic#5443)

* Don't go into light sleep with wifi enabled

* Move

* Trunk

* Revert "Seems like the last DIY board that's not "extra" (meshtastic#5420)" (meshtastic#5446)

This reverts commit e6fb6b1.

* Actually gunzip all the files when building a .deb (meshtastic#5449)

* [create-pull-request] automated change (meshtastic#5457)

Co-authored-by: thebentern <[email protected]>

* Cleanup i2c scan logs and macro to save some bytes and remain consistent (meshtastic#5455)

* Cleanup i2c scan logs and macro to save some bytes and remain consistent

* Functions are better than macros

* Exclude i2c scan for STM32

* Useless log

* Clean up some inline functions (meshtastic#5454)

* Use isWithinTimespanMs to avoid refererence to NodeDb instance inside of NodeDb (meshtastic#5453)

* fix cors for meshtasticd to allow use of cross origin clients (meshtastic#5463)

* Remove ATECC crypto chip placeholder code (meshtastic#5461)

* GPS.h cleanups round 3. (meshtastic#5447)

* GPS.h cleanups round 3.

No effective behavior change.

Protected members can be private so make it so.  (Supporting
subclasses needs a lot more work.)

Moves uBloxGnssModelInfo into file scope.

Moves uBloxProtocolVersion into uBloxGnssModelInfo.

Moves baud rate arrays into file scope.

Removes unused/ unimplemented powerStateToString.

Signed-off-by: Christopher Hoover <[email protected]>

* Trunk Format.

---------

Signed-off-by: Christopher Hoover <[email protected]>
Co-authored-by: Tom Fifield <[email protected]>

* Fix ukrainian fonts (meshtastic#5468)

* FIX:  rollback to !4624

* UPDATE: new 16 and 24 UA Fonts and fixes

* fix: Solve the lightsleep crash problem via disable  lightsleep for indicator. (meshtastic#5470)

* Trunk

* Warnings and log cleanup (meshtastic#5472)

* Don't log if keyboard not found

* Signed comparison issue

* [create-pull-request] automated change (meshtastic#5475)

Co-authored-by: thebentern <[email protected]>

* Adds libusb dev package to Raspbian build steps (meshtastic#5480)

* [create-pull-request] automated change (meshtastic#5478)

Co-authored-by: thebentern <[email protected]>

* Portduino fixes (meshtastic#5479)

* Set config.yaml defaults even if General is missing

* Unsigned values should get %u in logging

* Update arduino-pico core and remove MDNS restriction (meshtastic#5483)

* Update xiao_esp32 fully support L67K (meshtastic#5488)

L67K module hardware changed

* Convert userprefs to a json file instead of header file which has to be included everywhere (meshtastic#5471)

* WIP

* Got string quoting and macro expansion working

* Need the placeholder

* Cleanup

* Missed a user prefs reference

* Update jsonc

* SimRadio: clean-up and emulate collisions (meshtastic#5487)

* Clean up SimRadio and don't let it use PKC

* Add collision emulation for SimRadio

* Add stats from SimRadio to LocalStats

* Make emulating collisions optional

* add nodeId to nodeinfo update log lines and removed redundant nodeinfo update log line (meshtastic#5493)

* Refact the macro definition of GPS initialization of GPSDEFAULTD_NOT_PRESENT and added  seeeed Indicator to this sequence (meshtastic#5494)

Co-authored-by: Ben Meadors <[email protected]>

* Extend Length of Source and Destination Node IDs Logged (meshtastic#5492)

* show 8 chars for logging source and destination ids

* extend legnth of source and destination nodes in log

* Added femtofox configs (meshtastic#5477)

* added femtofox configs

* Rename bin/config.d/femtofox_Waveshare-SX126X-XXXM_AI-Thinker-RA-01SH.yaml to bin/config.d/femtofox/femtofox_Waveshare-SX126X-XXXM_AI-Thinker-RA-01SH.yaml

* moved femtofox configs to subdir

* [Add] LR1110, LR1120 and LR1121 to linux native Portduino (meshtastic#5496)

* Update main.cpp

* Update PortduinoGlue.h

* Update PortduinoGlue.cpp

* Update PortduinoGlue.cpp

* Update PortduinoGlue.cpp

* Update main.cpp

* [create-pull-request] automated change (meshtastic#5500)

Co-authored-by: thebentern <[email protected]>

* Fix minor typos in package workflows (meshtastic#5505)

* Don't use channel index for encrypted packet (meshtastic#5509)

* Don't use channel index for encrypted packet

* Remove assert in `getKey`, set invalid key length
So encrypting will fail without reboot

* Reset channel to 0 when unable to encrypt
Such that the NAK doesn't use the failing channel hash

* Always Announce MDNS meshtastic service (meshtastic#5503)

* refactor server api port into define

* always announce MDNS meshtastic service

* fix nodeDB erase loop when free mem returns invalid value (0, -1). (meshtastic#5519)

Co-authored-by: mverch67 <[email protected]>

* Add heltec capsule back

* Revert "Add heltec capsule back"

This reverts commit fc16d93.

* Lets try this again minus device ui

* Add popular nrf52 pro micro to the builds (meshtastic#5523)

* Add MACAddress to config.yaml (meshtastic#5506)

* Add MACAddress to config.yaml

* Better error handling on native, including failing to launch with blank MAC Address and real hardware.

* Re-arrange Mac Address handling and add MACAddressSource

* Bump portduino to remove macaddr function there

---------

Co-authored-by: Ben Meadors <[email protected]>

* Configure Seeed Xiao S3 RX enable pin (meshtastic#5517)

* Create OpenWRT_One_mikroBUS_sx1262.yaml (meshtastic#5529)

* tlora_v2_1_16: Unset BUTTON_PIN and BUTTON_NEED_PULLUP (meshtastic#5535)

Unset BUTTON_PIN and BUTTON_NEED_PULLUP as the board ships without a user button.

Devices and users expecting a button on GPIO12 have to set [GPIO for user button](https://meshtastic.org/docs/configuration/radio/device/#gpio-for-user-button) to 12 (or any GPIO pin the momentary switch was connected to) to restore functionality.

Signed-off-by: Andrew Yong <[email protected]>

* [create-pull-request] automated change (meshtastic#5530)

Co-authored-by: thebentern <[email protected]>

* Fix detection for some RadSens hardware versions (meshtastic#5542)

Co-authored-by: Jake-B <[email protected]>

* Initialize dmac array to nulls (meshtastic#5538)

* Initialize dmac array to nulls

* Use std::cout for print before console is init.

* Update OpenWRT_One_mikroBUS_sx1262.yaml (meshtastic#5544)

* Add portduino-buildroot variant (meshtastic#5540)

* Add portduino-buildroot variant

* Update platform-native for platform-buildroot

* portduino-buildroot: Define c standard (meshtastic#5547)

* Portduino: Move meshtasticd/web out of /usr/share/doc/ (meshtastic#5548)

* Portduino: fix transitional symlinks (meshtastic#5550)

* Windows Support - Trunk and Platformio (meshtastic#5397) (meshtastic#5518)

* Add support for GPG
* Add usb device support
* Add trunk.io to devcontainer
* Trunk things
* trunk fmt
* formatting
* fix trivy/DS002, checkov/CKV_DOCKER_3
* hide docker extension popup
* fix trivy/DS026, checkov/CKV_DOCKER_2

Co-authored-by: Kalle Lilja <[email protected]>

* Synch minor changes from TFT branch (meshtastic#5520)

* Synch minor changes from TFT branch

Includes:
* New nordicnrf52 minor version (10.5.0 --> 10.6.0)
* Optimisations for T_DECK
* preparation for MESH_TAB
* add ext notification module to portduino

---------

Co-authored-by: mverch67 <[email protected]>

* DIO3_TCXO_VOLTAGE in config.yaml can now take an exact voltage (meshtastic#5558)

* Support TLORA_V3.0 (meshtastic#5563)

- Support TLORA_V3.0. Update of the legendary 2.1_1.6.1 with solar charger, TCXO and IPEX connector.
- 'extra' some short-lived EOL intermediate boards in that range. If possible use T3S3 instead of all of these!
- update trunk to latest version

* Create OpenWRT-One-mikroBUS-LR-IOT-CLICK.yaml (meshtastic#5564)

* Portduino: fix setting hwId via argument (meshtastic#5565)

* INA219 charging detection

minimal implementation: if there is a configured INA219 sensor for battery monitoring we can take the current flow across the shunt resistor to know if we are charging the battery - negative milliamps indicate charging

* Update Power.cpp

added comments and 2 extra defines to disable and swap detection direction

* Trunk Fixes

* Add INA226 support

---------

Signed-off-by: Christopher Hoover <[email protected]>
Signed-off-by: Andrew Yong <[email protected]>
Co-authored-by: Ben Meadors <[email protected]>
Co-authored-by: Jonathan Bennett <[email protected]>
Co-authored-by: GUVWAF <[email protected]>
Co-authored-by: Tom Fifield <[email protected]>
Co-authored-by: Michael Gjelsø <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: thebentern <[email protected]>
Co-authored-by: jcyrio <[email protected]>
Co-authored-by: Daniel.Cao <[email protected]>
Co-authored-by: Catalin Patulea <[email protected]>
Co-authored-by: dylanli <[email protected]>
Co-authored-by: mverch67 <[email protected]>
Co-authored-by: madeofstown <[email protected]>
Co-authored-by: Christopher Hoover <[email protected]>
Co-authored-by: Mictronics <[email protected]>
Co-authored-by: Thomas Göttgens <[email protected]>
Co-authored-by: caveman99 <[email protected]>
Co-authored-by: jake-b <[email protected]>
Co-authored-by: César de Tassis Filho <[email protected]>
Co-authored-by: Tomas Dubec <[email protected]>
Co-authored-by: Liam Cottle <[email protected]>
Co-authored-by: panaceya <[email protected]>
Co-authored-by: virgil <[email protected]>
Co-authored-by: Robert <[email protected]>
Co-authored-by: noon92 <[email protected]>
Co-authored-by: Mark Trevor Birss <[email protected]>
Co-authored-by: broglep <[email protected]>
Co-authored-by: Matthias Granberry <[email protected]>
Co-authored-by: Andrew Yong <[email protected]>
Co-authored-by: Jake-B <[email protected]>
Co-authored-by: Austin <[email protected]>
Co-authored-by: Kalle Lilja <[email protected]>
  • Loading branch information
1 parent fa1a1fd commit 80fc0f2
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 8 deletions.
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,5 @@ lib_deps =
https://github.com/KodinLanewave/[email protected]
mprograms/[email protected]
dfrobot/[email protected]
https://github.com/meshtastic/DFRobot_LarkWeatherStation#4de3a9cadef0f6a5220a8a906cf9775b02b0040d
https://github.com/meshtastic/DFRobot_LarkWeatherStation#4de3a9cadef0f6a5220a8a906cf9775b02b0040d
robtillaart/[email protected]
39 changes: 37 additions & 2 deletions src/Power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ static const uint8_t ext_chrg_detect_value = EXT_CHRG_DETECT_VALUE;
#endif

#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_PORTDUINO)
INA260Sensor ina260Sensor;
INA219Sensor ina219Sensor;
INA226Sensor ina226Sensor;
INA260Sensor ina260Sensor;
INA3221Sensor ina3221Sensor;
#endif

Expand Down Expand Up @@ -413,7 +414,20 @@ class AnalogBatteryLevel : public HasBatteryLevel
#ifdef EXT_CHRG_DETECT
return digitalRead(EXT_CHRG_DETECT) == ext_chrg_detect_value;
#else
#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) && \
!defined(DISABLE_INA_CHARGING_DETECTION)
if (hasINA()) {
// get current flow from INA sensor - negative value means power flowing into the battery
// default assuming BATTERY+ <--> INA_VIN+ <--> SHUNT RESISTOR <--> INA_VIN- <--> LOAD
LOG_DEBUG("Using INA on I2C addr 0x%x for charging detection", config.power.device_battery_ina_address);
#if defined(INA_CHARGING_DETECTION_INVERT)
return getINACurrent() > 0;
#else
return getINACurrent() < 0;
#endif
}
return isBatteryConnect() && isVbusIn();
#endif
#endif
}

Expand Down Expand Up @@ -450,6 +464,9 @@ class AnalogBatteryLevel : public HasBatteryLevel
{
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA219].first == config.power.device_battery_ina_address) {
return ina219Sensor.getBusVoltageMv();
} else if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA226].first ==
config.power.device_battery_ina_address) {
return ina226Sensor.getBusVoltageMv();
} else if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA260].first ==
config.power.device_battery_ina_address) {
return ina260Sensor.getBusVoltageMv();
Expand All @@ -460,6 +477,20 @@ class AnalogBatteryLevel : public HasBatteryLevel
return 0;
}

int16_t getINACurrent()
{
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA219].first == config.power.device_battery_ina_address) {
return ina219Sensor.getCurrentMa();
} else if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA226].first ==
config.power.device_battery_ina_address) {
return ina226Sensor.getCurrentMa();
} else if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA3221].first ==
config.power.device_battery_ina_address) {
return ina3221Sensor.getCurrentMa();
}
return 0;
}

bool hasINA()
{
if (!config.power.device_battery_ina_address) {
Expand All @@ -469,6 +500,10 @@ class AnalogBatteryLevel : public HasBatteryLevel
if (!ina219Sensor.isInitialized())
return ina219Sensor.runOnce() > 0;
return ina219Sensor.isRunning();
} else if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA226].first ==
config.power.device_battery_ina_address) {
if (!ina226Sensor.isInitialized())
return ina226Sensor.runOnce() > 0;
} else if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA260].first ==
config.power.device_battery_ina_address) {
if (!ina260Sensor.isInitialized())
Expand Down Expand Up @@ -1154,4 +1189,4 @@ bool Power::lipoInit()
{
return false;
}
#endif
#endif
6 changes: 5 additions & 1 deletion src/modules/Telemetry/PowerTelemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ int32_t PowerTelemetryModule::runOnce()
// therefore, we should only enable the sensor loop if measurement is also enabled
if (ina219Sensor.hasSensor() && !ina219Sensor.isInitialized())
result = ina219Sensor.runOnce();
if (ina226Sensor.hasSensor() && !ina226Sensor.isInitialized())
result = ina226Sensor.runOnce();
if (ina260Sensor.hasSensor() && !ina260Sensor.isInitialized())
result = ina260Sensor.runOnce();
if (ina3221Sensor.hasSensor() && !ina3221Sensor.isInitialized())
Expand Down Expand Up @@ -170,6 +172,8 @@ bool PowerTelemetryModule::getPowerTelemetry(meshtastic_Telemetry *m)
#if HAS_TELEMETRY && !defined(ARCH_PORTDUINO)
if (ina219Sensor.hasSensor())
valid = ina219Sensor.getMetrics(m);
if (ina226Sensor.hasSensor())
valid = ina226Sensor.getMetrics(m);
if (ina260Sensor.hasSensor())
valid = ina260Sensor.getMetrics(m);
if (ina3221Sensor.hasSensor())
Expand Down Expand Up @@ -253,4 +257,4 @@ bool PowerTelemetryModule::sendTelemetry(NodeNum dest, bool phoneOnly)
return false;
}

#endif
#endif
13 changes: 13 additions & 0 deletions src/modules/Telemetry/Sensor/CurrentSensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "configuration.h"

#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR

#pragma once

class CurrentSensor
{
public:
virtual int16_t getCurrentMa() = 0;
};

#endif
5 changes: 5 additions & 0 deletions src/modules/Telemetry/Sensor/INA219Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ uint16_t INA219Sensor::getBusVoltageMv()
return lround(ina219.getBusVoltage_V() * 1000);
}

int16_t INA219Sensor::getCurrentMa()
{
return lround(ina219.getCurrent_mA());
}

#endif
4 changes: 3 additions & 1 deletion src/modules/Telemetry/Sensor/INA219Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR

#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "CurrentSensor.h"
#include "TelemetrySensor.h"
#include "VoltageSensor.h"
#include <Adafruit_INA219.h>

class INA219Sensor : public TelemetrySensor, VoltageSensor
class INA219Sensor : public TelemetrySensor, VoltageSensor, CurrentSensor
{
private:
Adafruit_INA219 ina219;
Expand All @@ -20,6 +21,7 @@ class INA219Sensor : public TelemetrySensor, VoltageSensor
virtual int32_t runOnce() override;
virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
virtual uint16_t getBusVoltageMv() override;
virtual int16_t getCurrentMa() override;
};

#endif
58 changes: 58 additions & 0 deletions src/modules/Telemetry/Sensor/INA226Sensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "configuration.h"

#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR

#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "INA226.h"
#include "INA226Sensor.h"
#include "TelemetrySensor.h"

INA226Sensor::INA226Sensor() : TelemetrySensor(meshtastic_TelemetrySensorType_INA226, "INA226") {}

int32_t INA226Sensor::runOnce()
{
LOG_INFO("Init sensor: %s", sensorName);
if (!hasSensor()) {
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
}

begin(nodeTelemetrySensorsMap[sensorType].second, nodeTelemetrySensorsMap[sensorType].first);

if (!status) {
status = ina226.begin();
}
return initI2CSensor();
}

void INA226Sensor::setup() {}

void INA226Sensor::begin(TwoWire *wire, uint8_t addr)
{
_wire = wire;
_addr = addr;
ina226 = INA226(_addr, _wire);
_wire->begin();
}

bool INA226Sensor::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 = ina226.getBusVoltage() / 1000;
measurement->variant.environment_metrics.current = ina226.getCurrent_mA();
return true;
}

uint16_t INA226Sensor::getBusVoltageMv()
{
return lround(ina226.getBusVoltage());
}

int16_t INA226Sensor::getCurrentMa()
{
return lround(ina226.getCurrent_mA());
}

#endif
30 changes: 30 additions & 0 deletions src/modules/Telemetry/Sensor/INA226Sensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "configuration.h"

#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR

#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "CurrentSensor.h"
#include "TelemetrySensor.h"
#include "VoltageSensor.h"
#include <INA226.h>

class INA226Sensor : public TelemetrySensor, VoltageSensor, CurrentSensor
{
private:
uint8_t _addr = INA_ADDR;
TwoWire *_wire = &Wire;
INA226 ina226 = INA226(_addr, _wire);

protected:
virtual void setup() override;
void begin(TwoWire *wire = &Wire, uint8_t addr = INA_ADDR);

public:
INA226Sensor();
virtual int32_t runOnce() override;
virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
virtual uint16_t getBusVoltageMv() override;
virtual int16_t getCurrentMa() override;
};

#endif
5 changes: 5 additions & 0 deletions src/modules/Telemetry/Sensor/INA3221Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,9 @@ uint16_t INA3221Sensor::getBusVoltageMv()
return lround(ina3221.getVoltage(BAT_CH) * 1000);
}

int16_t INA3221Sensor::getCurrentMa()
{
return lround(ina3221.getCurrent(BAT_CH));
}

#endif
4 changes: 3 additions & 1 deletion src/modules/Telemetry/Sensor/INA3221Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR

#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "CurrentSensor.h"
#include "TelemetrySensor.h"
#include "VoltageSensor.h"
#include <INA3221.h>

class INA3221Sensor : public TelemetrySensor, VoltageSensor
class INA3221Sensor : public TelemetrySensor, VoltageSensor, CurrentSensor
{
private:
INA3221 ina3221 = INA3221(INA3221_ADDR42_SDA);
Expand Down Expand Up @@ -35,6 +36,7 @@ class INA3221Sensor : public TelemetrySensor, VoltageSensor
int32_t runOnce() override;
bool getMetrics(meshtastic_Telemetry *measurement) override;
virtual uint16_t getBusVoltageMv() override;
virtual int16_t getCurrentMa() override;
};

struct _INA3221Measurement {
Expand Down
6 changes: 4 additions & 2 deletions src/power.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ extern RTC_NOINIT_ATTR uint64_t RTC_reg_b;

#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_PORTDUINO)
#include "modules/Telemetry/Sensor/INA219Sensor.h"
#include "modules/Telemetry/Sensor/INA226Sensor.h"
#include "modules/Telemetry/Sensor/INA260Sensor.h"
#include "modules/Telemetry/Sensor/INA3221Sensor.h"
extern INA260Sensor ina260Sensor;
extern INA219Sensor ina219Sensor;
extern INA226Sensor ina226Sensor;
extern INA260Sensor ina260Sensor;
extern INA3221Sensor ina3221Sensor;
#endif

Expand Down Expand Up @@ -99,4 +101,4 @@ class Power : private concurrency::OSThread
#endif
};

extern Power *power;
extern Power *power;

0 comments on commit 80fc0f2

Please sign in to comment.