#include <Wire.h>
#include <Adafruit_INA219.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include <U8g2lib.h>
extern "C" {
#include "user_interface.h"
}
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
Adafruit_INA219 ina219;
char auth[] = "";
char ssid[] = "IoT";
char pass[] = "";
float shuntvoltage = 0.0;
float busvoltage = 0.0;
float current_mA = 0.0;
float loadvoltage = 0.0;
float cumshuntvoltage = 0.0;
float cumbusvoltage = 0.0;
float cumcurrent_mA = 0.0;
float cumloadvoltage = 0.0;
float prevMA = 0.0;
unsigned int count = 0;
unsigned int hourTime = 0;
unsigned int minTime = 0;
unsigned int secTime = 0;
char * text1 = "Bus Voltage: ";
char * text2 = "Load Voltage: ";
char * text3 = "Current: ";
char * text4 = "Charge Time: ";
byte startTime = 0;
unsigned long time1 = 0;
unsigned long time2 = 0;
void setup()
{
// Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
Wire.begin(2, 0);
u8g2.begin();
u8g2.enableUTF8Print();
prepare();
ina219.begin();
}
void loop()
{
Blynk.run();
time1 = millis();
cumshuntvoltage = 0;
cumbusvoltage = 0;
cumcurrent_mA = 0;
cumloadvoltage = 0;
count = 0;
while (millis() - time1 < 1000)
{
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
loadvoltage = busvoltage + ( shuntvoltage / 1000 );
cumshuntvoltage += shuntvoltage;
cumbusvoltage += busvoltage;
cumcurrent_mA += current_mA;
cumloadvoltage += loadvoltage;
count++;
delay(5);
}
shuntvoltage = cumshuntvoltage / count;
busvoltage = cumbusvoltage / count;
current_mA = cumcurrent_mA / count;
loadvoltage = cumloadvoltage / count;
if (prevMA < 0.1 && current_mA > 10.0)
{
time2 = millis();
startTime = 1;
hourTime = 0;
minTime = 0;
}
if (prevMA > 4.0 && current_mA < 10.0)
{
startTime = 0;
}
if (startTime)
{
secTime = (millis() - time2) / 1000;
if (secTime > 59)
{
minTime++;
time2 = millis();
}
if (minTime > 59)
{
hourTime++;
minTime = 0;
}
}
u8g2.clearBuffer();
u8g2.drawStr(0, 0, text1);
u8g2.setCursor(90, 0);
u8g2.print(busvoltage);
u8g2.print(" v");
u8g2.drawStr(0, 15, text2);
u8g2.setCursor(90, 15);
u8g2.print(loadvoltage);
u8g2.print(" v");
u8g2.drawStr(0, 30, text3);
u8g2.setCursor(78, 30);
u8g2.print(current_mA, 1);
u8g2.print(" mA");
u8g2.drawStr(0, 45, text4);
u8g2.setCursor(85, 45);
u8g2.print(hourTime);
u8g2.print(":");
u8g2.print(minTime);
u8g2.print(":");
u8g2.print(secTime);
u8g2.sendBuffer();
String timeString = String(hourTime) + ":" + String(minTime) + ":" + String(secTime);
Blynk.virtualWrite(V0, busvoltage);
Blynk.virtualWrite(V1, shuntvoltage);
Blynk.virtualWrite(V2, loadvoltage);
Blynk.virtualWrite(V3, current_mA);
Blynk.virtualWrite(V4, current_mA);
Blynk.virtualWrite(V5, busvoltage);
Blynk.virtualWrite(V6, count);
Blynk.virtualWrite(V7, timeString);
Blynk.virtualWrite(V21, wifi_station_get_rssi());
prevMA = current_mA;
} // loop
void prepare()
{
u8g2.setFont(u8g2_font_6x10_tf);
u8g2.setFontRefHeightExtendedText();
u8g2.setDrawColor(1);
u8g2.setFontPosTop();
u8g2.setFontDirection(0);
}
BLYNK_WRITE(V20)
{
byte restartChip = param.asInt();
delay(500);
if (restartChip == 1)
{
ESP.restart();
}
}
Comments