diff --git a/platformio.ini b/platformio.ini index 4c64e70..6f75ae0 100644 --- a/platformio.ini +++ b/platformio.ini @@ -26,4 +26,5 @@ framework = arduino lib_deps = smougenot/TM1637@0.0.0-alpha+sha.9486982048 me-no-dev/ESP Async WebServer @ ^1.2.3 - sstaub/Ticker @ ^4.2.0 \ No newline at end of file + sstaub/Ticker @ ^4.2.0 + adafruit/Adafruit INA219 @ ^1.1.1 diff --git a/src/main.cpp b/src/main.cpp index f9809a6..d1e80c7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,8 @@ #endif #include #include +#include +#include // local includes #include "defaults.h" @@ -38,11 +40,14 @@ void SevenSeg_Output(); void FactionTicker_callback(); void serialOutTicker_callback(); void inputGetterTicker_callback(); +void powerMonitorTicker_callback(); TM1637Display disp_FAC_1(CLK, DIO_FAC_1_7SEG); TM1637Display disp_FAC_2(CLK, DIO_FAC_2_7SEG); TM1637Display disp_FAC_3(CLK, DIO_FAC_3_7SEG); +Adafruit_INA219 ina219; + WiFiEventHandler stationConnectedHandler; WiFiEventHandler stationDisconnectedHandler; @@ -54,6 +59,7 @@ AsyncWebServer server(80); Ticker FactionTicker(FactionTicker_callback, 500, 0, MILLIS); Ticker SerialOutputTicker(serialOutTicker_callback, 5000, 0, MILLIS); Ticker InputGetterTicker(inputGetterTicker_callback, 500, 0, MILLIS); +Ticker PowerMonitorTicker(powerMonitorTicker_callback, 5000, 0, MILLIS); Factions activeFaction = NONE; @@ -276,6 +282,27 @@ void inputGetterTicker_callback() activeFaction = FACTION_3; } +void powerMonitorTicker_callback() +{ + float shuntvoltage = 0; + float busvoltage = 0; + float current_mA = 0; + float loadvoltage = 0; + float power_mW = 0; + + shuntvoltage = ina219.getShuntVoltage_mV(); + busvoltage = ina219.getBusVoltage_V(); + current_mA = ina219.getCurrent_mA(); + power_mW = ina219.getPower_mW(); + loadvoltage = busvoltage + (shuntvoltage / 1000); + + Serial.printf("Bus Voltage: %f V\n", busvoltage); + Serial.printf("Shunt Voltage: %f mV\n", shuntvoltage); + Serial.printf("Load Voltage: %f V\n", loadvoltage); + Serial.printf("Current: %f mA\n", current_mA); + Serial.printf("Power: %f mW\n", power_mW); +} + void setup() { pinMode(DIO_FAC_1_TRG, INPUT_PULLUP); @@ -285,6 +312,11 @@ void setup() Serial.begin(9600); Serial.print("\n\n\n"); + if (ina219.begin()) + PowerMonitorTicker.start(); + else + Serial.println("Failed to find INA219 chip"); + LittleFS.begin(); WiFi.persistent(false); @@ -310,6 +342,7 @@ void loop() FactionTicker.update(); SerialOutputTicker.update(); InputGetterTicker.update(); + PowerMonitorTicker.update(); #ifdef CAPTIVE dnsServer.processNextRequest();