2023 Б АПОТ КІУКІ 19 8 Довгаль В Р Додатки

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 11

49

ДОДАТОК А
Графічна частина проєкту
50
51
52
53
54
55
56

ДОДАТОК Б
Код серверної частини (ServerPage)

const char index_html[] PROGMEM = R"rawliteral(


<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://use.fontawesome.com/releases/v5.7.2/css/all.css"
integrity="sha384-
fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr"
crossorigin="anonymous">
<style>
html {
font-family: Arial;
display: inline-block;
margin: 0px auto;
text-align: center;
}
h2 { font-size: 3.0rem; }
p { font-size: 3.0rem; }
.units { font-size: 1.2rem; }
.ds-labels{
font-size: 1.5rem;
vertical-align:middle;
padding-bottom: 15px;
}
</style>
</head>
<body>
<h2>PB Capacity Server</h2>
<p>
<i class="fa-solid fa-battery-full" style="color:#059e8a;"></i>
<span id="quantity" class="ds-labels">Battery Capacity</span>
<span id="capacity">%CAPACITY%</span>
<b id="value" > mAh </b>
</p>
<p>
<strong> %STATE%</strong>
</p>
</body>
<script>
setInterval(function ( )
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200) {
document.getElementById("capacity").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/capacity", true);
xhttp.send();
}, 10000) ;

</script>
</html>)rawliteral";
57

ДОДАТОК В

Код пристрою управління (ControlDevice)

/* Import required libraries*/


#include <Wire.h>
#include <Adafruit_INA219.h>

//The main library for creating the Wi-Fi connection


//
#include <ESP8266WiFi.h>

//A fully asynchronous TCP library, aimed at enabling trouble-free,


//multi-connection network environment for Espressif's ESP8266 MCUs
//
#include <ESPAsyncTCP.h>

//The library for easy way building an asynchronous web server


//
#include <ESPAsyncWebServer.h>

//define the "default" capacity of the PowerBank.


//set after the accurated value first full charging and discharging
#define DEFAULT_CAPACITY 10000

//Value for measuring 1 hour of device working


#define MIN_TO_HOUR 60

//Instance of current module


Adafruit_INA219 ina219;

// Create AsyncWebServer object on port 80


//
AsyncWebServer server(80);

// Replace with your network credentials


//
static const char* ssid = "YOUR_WIFI_NAME";
static const char* password = "YOUR_WIFI_PASSWORD";

extern const char index_html[] PROGMEM; //HTML page code (server page)

//Set the update timer for all measures and data transfers in msec
//
static const unsigned timerDelay = 60000;

//Variables for storing the necessary values to algorithm


//
static unsigned short capacity = DEFAULT_CAPACITY;
static unsigned char measureCount = 0;
static unsigned char hoursOfCharging = 0;

//Asynchronous function for displaying the capacity


// on the server page
//
static String readBatteryCapacity()
{
//Return and print the real value of capacity
{
58

Serial.print("Capacity: ");
Serial.print(capacity);
Serial.print("%\n");
}

return String(capacity);
}

//Main handle for calling the capacity calculation


//
static String processor(const String& var)
{
//A test function to inform user to disconnect the device
//to store the battery for future or next devices
if(var == "STATE")
{
//Set 1 hour for demo
if(hoursOfCharging >= 1){
return "Disconnect to save the capacity for more";
}
else{
return " ";
}

//read the capacity after the async request


if(var == "CAPACITY")
{
return readBatteryCapacity();
}

return String();
}

void setup(void)
{
Serial.begin(115200);

while (!Serial) {
// will pause Zero, Leonardo, etc until serial console opens
delay(1);
}

Serial.println("Serial connected succesfully!");

// Initialize the INA219.


// By default the initialization will use the largest range (32V, 2A).
However
// you can call a setCalibration function to change this range (see
comments).
if (! ina219.begin()) {
Serial.println("Failed to find INA219 chip");
while (1) { delay(10); }
}

//Initiate the WiFi connection


//
WiFi.begin(ssid, password);

Serial.print("Connecting");
//Connecting the device via WiFi
//
59

while (WiFi.status() != WL_CONNECTED) {


Serial.print(".");
}

// Print ESP Local IP Address


//
Serial.println();
Serial.println(WiFi.localIP());

//Main request for initiating the communication with web server


//
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});

server.on("/capacity", HTTP_GET, [](AsyncWebServerRequest *request){


request->send_P(200, "text/plain", readBatteryCapacity().c_str());
});

// Start server
//
server.begin();
}

void loop(void)
{
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float power_mW = 0;

//count each timerdelay for measuring, printing the results and


if ( 0 == ( millis() % timerDelay ) ) {
//Count how many times the values are measured, printed and transfered to
Web
measureCount++;

shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
power_mW = ina219.getPower_mW();
loadvoltage = busvoltage + (shuntvoltage / 1000);

Serial.print("Bus Voltage: "); Serial.print(busvoltage);


Serial.println(" V");
Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage);
Serial.println(" mV");
Serial.print("Load Voltage: "); Serial.print(loadvoltage);
Serial.println(" V");
Serial.print("Current: "); Serial.print(current_mA);
Serial.println(" mA");
Serial.print("Power: "); Serial.print(power_mW); Serial.println("
mW");
Serial.println("");

//One hour working for demo signals to recount the charging time
//and substract the capacity from all.
if (measureCount == MIN_TO_HOUR){
measureCount = 0;
hoursOfCharging++;
capacity -= current_mA;
}

You might also like