Added INA219 Power Monitoring Stuff

This commit is contained in:
Souko Hiabuto 2021-06-13 14:07:19 +02:00
parent 763affded1
commit 49260c0db5
2 changed files with 35 additions and 1 deletions

View File

@ -27,3 +27,4 @@ lib_deps =
smougenot/TM1637@0.0.0-alpha+sha.9486982048 smougenot/TM1637@0.0.0-alpha+sha.9486982048
me-no-dev/ESP Async WebServer @ ^1.2.3 me-no-dev/ESP Async WebServer @ ^1.2.3
sstaub/Ticker @ ^4.2.0 sstaub/Ticker @ ^4.2.0
adafruit/Adafruit INA219 @ ^1.1.1

View File

@ -13,6 +13,8 @@
#endif #endif
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <LittleFS.h> #include <LittleFS.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
// local includes // local includes
#include "defaults.h" #include "defaults.h"
@ -38,11 +40,14 @@ void SevenSeg_Output();
void FactionTicker_callback(); void FactionTicker_callback();
void serialOutTicker_callback(); void serialOutTicker_callback();
void inputGetterTicker_callback(); void inputGetterTicker_callback();
void powerMonitorTicker_callback();
TM1637Display disp_FAC_1(CLK, DIO_FAC_1_7SEG); TM1637Display disp_FAC_1(CLK, DIO_FAC_1_7SEG);
TM1637Display disp_FAC_2(CLK, DIO_FAC_2_7SEG); TM1637Display disp_FAC_2(CLK, DIO_FAC_2_7SEG);
TM1637Display disp_FAC_3(CLK, DIO_FAC_3_7SEG); TM1637Display disp_FAC_3(CLK, DIO_FAC_3_7SEG);
Adafruit_INA219 ina219;
WiFiEventHandler stationConnectedHandler; WiFiEventHandler stationConnectedHandler;
WiFiEventHandler stationDisconnectedHandler; WiFiEventHandler stationDisconnectedHandler;
@ -54,6 +59,7 @@ AsyncWebServer server(80);
Ticker FactionTicker(FactionTicker_callback, 500, 0, MILLIS); Ticker FactionTicker(FactionTicker_callback, 500, 0, MILLIS);
Ticker SerialOutputTicker(serialOutTicker_callback, 5000, 0, MILLIS); Ticker SerialOutputTicker(serialOutTicker_callback, 5000, 0, MILLIS);
Ticker InputGetterTicker(inputGetterTicker_callback, 500, 0, MILLIS); Ticker InputGetterTicker(inputGetterTicker_callback, 500, 0, MILLIS);
Ticker PowerMonitorTicker(powerMonitorTicker_callback, 5000, 0, MILLIS);
Factions activeFaction = NONE; Factions activeFaction = NONE;
@ -276,6 +282,27 @@ void inputGetterTicker_callback()
activeFaction = FACTION_3; 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() void setup()
{ {
pinMode(DIO_FAC_1_TRG, INPUT_PULLUP); pinMode(DIO_FAC_1_TRG, INPUT_PULLUP);
@ -285,6 +312,11 @@ void setup()
Serial.begin(9600); Serial.begin(9600);
Serial.print("\n\n\n"); Serial.print("\n\n\n");
if (ina219.begin())
PowerMonitorTicker.start();
else
Serial.println("Failed to find INA219 chip");
LittleFS.begin(); LittleFS.begin();
WiFi.persistent(false); WiFi.persistent(false);
@ -310,6 +342,7 @@ void loop()
FactionTicker.update(); FactionTicker.update();
SerialOutputTicker.update(); SerialOutputTicker.update();
InputGetterTicker.update(); InputGetterTicker.update();
PowerMonitorTicker.update();
#ifdef CAPTIVE #ifdef CAPTIVE
dnsServer.processNextRequest(); dnsServer.processNextRequest();