Вы находитесь на странице: 1из 3

Source Code

#include <Wire.h>
#include <Adafruit_INA219.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMono9pt7b.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
Adafruit_INA219 ina219;
#include "SdFat.h"
SdFat SD;

File TimeFile;
File VoltFile;
File CurFile;

unsigned long previousMillis = 0;


unsigned long interval = 100;
const int chipSelect = 10;
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float energy = 0;

void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
ina219.begin();

SD.begin(chipSelect);
}

void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
ina219values();
TimeFile = SD.open("TIME.txt", FILE_WRITE);
if (TimeFile) {
TimeFile.println(currentMillis);
TimeFile.close();
}

VoltFile = SD.open("VOLT.txt", FILE_WRITE);


if (VoltFile) {
VoltFile.println(loadvoltage);
VoltFile.close();
}

CurFile = SD.open("CUR.txt", FILE_WRITE);


if (CurFile) {
CurFile.println(current_mA);
CurFile.close();
}
displaydata();
}
}

void displaydata() {
display.clearDisplay();
display.setTextColor(WHITE);
// display.setTextSize(1);
display.setFont(&FreeMono9pt7b);
display.setCursor(0, 10);
display.println(loadvoltage,1);
display.setCursor(45, 10);
display.println("V");

if(current_mA > 1000){


display.setCursor(0, 25);
display.println(current_mA/1000,2);
display.setCursor(50, 25);
display.println("A");
}
else{
display.setCursor(0, 25);
display.println(current_mA);
display.setCursor(65, 25);
display.println("mA");
}

if( ((loadvoltage*current_mA) > (1000-10)) ){


display.setCursor(0, 45);
display.println(loadvoltage * current_mA/1000);
display.setCursor(60, 45);
display.println("W");
}
else{
display.setCursor(0, 45);
display.println(loadvoltage * current_mA);
display.setCursor(85, 45);
display.println("mW");
}
if(energy >900){
display.setCursor(0, 60);
display.println(energy/1000);
display.setCursor(65, 60);
display.println("Wh");
}
else{
display.setCursor(0, 60);
display.println(energy);
display.setCursor(65, 60);
display.println("mWh");
}
display.display();
}

void ina219values() {
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
loadvoltage = busvoltage + (shuntvoltage / 1000);
energy = energy + loadvoltage * current_mA / 3600;
}

Вам также может понравиться