add
This commit is contained in:
9
Makefile
9
Makefile
@@ -1,8 +1,9 @@
|
||||
datasdir := $(HOME)/.make/
|
||||
pio := $(HOME)/.platformio/penv/bin/pio
|
||||
ttydown := /dev/ttyUSB0
|
||||
webdev := http://192.168.2.200/update
|
||||
#webdev := http://ESP8266-UPS_2E3ED3.lan/update
|
||||
#webdev := http://192.168.2.200/
|
||||
#webdev := http://ESP8266-UPS_2E3ED3.lan/
|
||||
webdev := http://ESP8266-UPS.lan/
|
||||
|
||||
all : $(datasdir) $(datasdir)PlatformIO/esp8266 .pio/libdeps
|
||||
$(pio) run
|
||||
@@ -10,7 +11,9 @@ all : $(datasdir) $(datasdir)PlatformIO/esp8266 .pio/libdeps
|
||||
cp -rf lib/ .pio/
|
||||
update : $(datasdir) $(datasdir)PlatformIO/esp8266 .pio/libdeps
|
||||
$(pio) run
|
||||
curl -#lF 'firmware=@./.pio/build/esp12e/firmware.bin' $(webdev) | tee /dev/null
|
||||
curl $(webdev)rebootload || echo 你干嘛
|
||||
sleep 2
|
||||
curl -#lF 'firmware=@./.pio/build/esp12e/firmware.bin' $(webdev)update | tee /dev/null
|
||||
install : $(datasdir) $(datasdir)PlatformIO/esp8266 .pio/libdeps
|
||||
$(pio) run --target upload --upload-port $(ttydown)
|
||||
clean : $(datasdir) $(datasdir)PlatformIO/esp8266
|
||||
|
||||
377
main.cpp
Normal file
377
main.cpp
Normal file
@@ -0,0 +1,377 @@
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include "ritos.h"
|
||||
|
||||
#include "user.h"
|
||||
|
||||
#include "WiFiManager.h"
|
||||
#include "adc.h"
|
||||
|
||||
unsigned long tick; // current read time - last read
|
||||
unsigned long lastread = 0; // used to calculate Ah
|
||||
unsigned long offtime = 0; // used to calculate Ah
|
||||
int power_S = 0;
|
||||
|
||||
#if defined USE_SPIFFS
|
||||
#include <FS.h>
|
||||
#define fileSystem SPIFFS
|
||||
SPIFFSConfig fileSystemConfig = SPIFFSConfig();
|
||||
#elif defined USE_LITTLEFS
|
||||
#include <LittleFS.h>
|
||||
#define fileSystem LittleFS
|
||||
LittleFSConfig fileSystemConfig = LittleFSConfig();
|
||||
#elif defined USE_SDFS
|
||||
#include <SDFS.h>
|
||||
#define fileSystem SDFS
|
||||
SDFSConfig fileSystemConfig = SDFSConfig();
|
||||
// fileSystemConfig.setCSPin(chipSelectPin);
|
||||
#else
|
||||
#error Please select a filesystem first by uncommenting one of the "#define USE_xxx" lines at the beginning of the sketch.
|
||||
#endif
|
||||
|
||||
int bucken = 0;
|
||||
|
||||
ESP8266WebServer httpServer(80);
|
||||
|
||||
//int int0 = 0;//未设置数据
|
||||
|
||||
static const char webupdateResponse[] PROGMEM =
|
||||
"<META http-equiv=\"refresh\" content=\"15;URL=/\">Update Success! Rebooting...";
|
||||
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200); //设置串口波特率
|
||||
Serial.println("");
|
||||
Serial.println("Boot...");
|
||||
|
||||
uint32_t realSize = ESP.getFlashChipRealSize();
|
||||
uint32_t ideSize = ESP.getFlashChipSize();
|
||||
FlashMode_t ideMode = ESP.getFlashChipMode();
|
||||
|
||||
Serial.printf("Flash real id: %08X\r\n", ESP.getFlashChipId());
|
||||
Serial.printf("Flash real size: %u bytes\r\n\r\n", realSize);
|
||||
|
||||
Serial.printf("Flash ide size: %u bytes\r\n", ideSize);
|
||||
Serial.printf("Flash ide speed: %u Hz\r\n", ESP.getFlashChipSpeed());
|
||||
Serial.printf("Flash ide mode: %s\r\n", (ideMode == FM_QIO ? "QIO" : ideMode == FM_QOUT ? "QOUT"
|
||||
: ideMode == FM_DIO ? "DIO"
|
||||
: ideMode == FM_DOUT ? "DOUT"
|
||||
: "UNKNOWN"));
|
||||
|
||||
if (ideSize != realSize) {
|
||||
Serial.println("Flash Chip configuration wrong!\r\n");
|
||||
delay(5000);
|
||||
delay(5000);
|
||||
delay(5000);
|
||||
delay(5000);
|
||||
} else {
|
||||
Serial.println("Flash Chip configuration ok.\r\n");
|
||||
}
|
||||
|
||||
Serial.print("> FS init ");
|
||||
fileSystemConfig.setAutoFormat(false);
|
||||
fileSystem.setConfig(fileSystemConfig);
|
||||
if (!fileSystem.begin()) {
|
||||
Serial.println("ERROR");
|
||||
}else{
|
||||
Serial.println("OK");
|
||||
user_.catdata();
|
||||
}
|
||||
Serial.println("> GPIO init ");
|
||||
bool battery_is;
|
||||
bool bypass_is;
|
||||
pinMode(charge_MOSFET, OUTPUT);
|
||||
battery_is = digitalRead(battery_MOSFET);
|
||||
bypass_is = digitalRead(bypass_MOSFET);
|
||||
pinMode(battery_MOSFET, OUTPUT);
|
||||
pinMode(bypass_MOSFET, OUTPUT);
|
||||
pinMode(15, OUTPUT);
|
||||
digitalWrite(battery_MOSFET, battery_is); //恢复状态
|
||||
digitalWrite(bypass_MOSFET, bypass_is); //恢复状态
|
||||
digitalWrite(2, !charge_on); //充电关闭
|
||||
analogWriteRange(pwmResolution);
|
||||
analogWriteFreq(pwmFrequency);
|
||||
PWM = 1023;
|
||||
analogWrite(buck_pwm, PWM);
|
||||
|
||||
a_adc.setup();
|
||||
|
||||
if(iicok){
|
||||
wifima.setup();
|
||||
}else{
|
||||
wifima.setup_bootloader();
|
||||
}
|
||||
}
|
||||
|
||||
void ups_main(){
|
||||
if (fdAh != 0){//存在电池容量
|
||||
if(Ah > 0){//实时容量大于0
|
||||
if(INV){
|
||||
upsshenyufenzhong = (fdAh * 600) / (currentOutput * 10);//计算估计电池充电耗尽的时间 分钟
|
||||
}else{
|
||||
upsshenyufenzhong = (fdAh * 600) / (currentbat * 10);//计算估计电池充电耗尽的时间 分钟
|
||||
}
|
||||
upsbaifengbi = (Ah / (fdAh / 100));//计算电池电量
|
||||
if (upsbaifengbi < 20){//电池低于20%
|
||||
upsronliang = 4;//电池耗尽
|
||||
}else if(upsbaifengbi < 50){//电池低于50%
|
||||
upsronliang = 3;//电量不足
|
||||
}else{
|
||||
upsronliang = 2;//电量正常
|
||||
}
|
||||
if(upsbaifengbi > 100){
|
||||
upsbaifengbi = 100;
|
||||
}
|
||||
}else{
|
||||
upsbaifengbi = 0;//电量0
|
||||
upsronliang = 4;//电池耗尽
|
||||
}
|
||||
}else{
|
||||
upsronliang = 1;//不支持
|
||||
}
|
||||
|
||||
if(ups_mode == 12){//关机
|
||||
if(offtime <= millis()){//1秒执行一次
|
||||
offtime = millis() + 1000;//更新时间
|
||||
power_S--;
|
||||
shutdown = power_S;
|
||||
|
||||
}
|
||||
if(INV){//电源接通
|
||||
Serial.println("> IN OK ");
|
||||
digitalWrite(battery_MOSFET, !battery_on); //关闭电池
|
||||
digitalWrite(charge_MOSFET, charge_on); //打开充电
|
||||
digitalWrite(bypass_MOSFET, bypass_on); //打开旁路
|
||||
pwmChannel = 1;
|
||||
upsoutmode = 3; //UPS 正常模式 输出
|
||||
ups_mode = 10;//正常模式
|
||||
shutdown = -1;//停止关机
|
||||
if (testdata == 4){ups_mode = 0;upsoutmode = 1;Serial.println("> RES TEST ");}//重新开始电池测试
|
||||
}
|
||||
}else if(ups_mode == 11){//电池供电模式
|
||||
if(voltagebat < voltageBatteryMin){//电池电压低于最低电压
|
||||
Serial.println("> PWEROFF ");
|
||||
//digitalWrite(battery_MOSFET, !battery_on); //关闭电池
|
||||
//digitalWrite(charge_MOSFET, !charge_on); //关闭充电
|
||||
//digitalWrite(bypass_MOSFET, bypass_on); //打开旁路
|
||||
ups_mode = 12;//进入关机模式
|
||||
power_S = 120;//120秒关机
|
||||
}
|
||||
if(fdAh > 0){//存在电池容量
|
||||
if(upsbaifengbi < 15){//电量低于15
|
||||
ups_mode = 12;//进入关机模式
|
||||
power_S = 120;//120秒关机
|
||||
}
|
||||
}
|
||||
if(INV){//电源接通
|
||||
Serial.println("> IN OK ");
|
||||
if(batmode == 1){//市电关闭电池输出
|
||||
digitalWrite(battery_MOSFET, !battery_on); //关闭电池
|
||||
}else{//市电打开电池输出
|
||||
digitalWrite(battery_MOSFET, battery_on); //打开电池
|
||||
}
|
||||
digitalWrite(charge_MOSFET, charge_on); //打开充电
|
||||
digitalWrite(bypass_MOSFET, bypass_on); //打开旁路
|
||||
pwmChannel = 1;
|
||||
upsoutmode = 3;//UPS 正常模式 输出
|
||||
ups_mode = 10;//正常模式
|
||||
if (testdata == 4){ups_mode = 0;upsoutmode = 1;Serial.println("> RES TEST ");}//重新开始电池测试
|
||||
}
|
||||
}else if(ups_mode == 10){//正常模式
|
||||
if(upsbaifengbi < 90){//低于90 打开充电
|
||||
//digitalWrite(battery_MOSFET, !battery_on); //打开 电池
|
||||
digitalWrite(charge_MOSFET, charge_on); //打开 充电
|
||||
//digitalWrite(bypass_MOSFET, bypass_on); //打开 旁路
|
||||
pwmChannel = 1;
|
||||
}else if((currentbat < 0.1)&&(voltagebat >= voltageBatteryMax)){
|
||||
//充电电流小于0.1电压大于等于最高电池电压 电池充满了
|
||||
digitalWrite(charge_MOSFET, !charge_on); //关闭 充电
|
||||
pwmChannel = 0;
|
||||
}
|
||||
if(!INV){//电源断开
|
||||
Serial.println("> IN NO ");
|
||||
digitalWrite(battery_MOSFET, battery_on); //打开 电池
|
||||
digitalWrite(charge_MOSFET, !charge_on); //关闭 充电
|
||||
digitalWrite(bypass_MOSFET, !bypass_on); //关闭 旁路
|
||||
pwmChannel = 0;
|
||||
upsoutmode = 5; //UPS 电池 输出
|
||||
ups_mode = 11; //电池供电模式
|
||||
}
|
||||
}else if(ups_mode == 9){//恢复模式
|
||||
Batin_Wh = 0;
|
||||
BatOut_Wh = 0;
|
||||
//没写,不知道怎么获取实时容量,重新测试电池把,flash是有寿命的
|
||||
Ah = user_.cat("/Ah","0").toFloat();
|
||||
if(Ah == 0){
|
||||
ups_mode = 0;//重新初始化
|
||||
fdAh = 0;//清理电池容量信息
|
||||
}else{
|
||||
ups_mode = 10;
|
||||
user_.serve("/Ah","0");
|
||||
}
|
||||
}else if(ups_mode == 5){//测试电池 放电
|
||||
Batin_Wh = 0;
|
||||
BatOut_Wh = 0;
|
||||
if(voltagebat < voltageBatteryMin){
|
||||
//电池电压小于电池最低电压
|
||||
Serial.println("> BAT OUT OK ");
|
||||
fdAh = 0 - Ah;//电池容量等于实时容量,±切换
|
||||
user_.serve("/fdAh",String(fdAh));//存储电池容量
|
||||
Ah = 0;//清空 实时 电池 容量
|
||||
Wh = 0;
|
||||
if(batmode == 1){//市电关闭电池输出
|
||||
digitalWrite(battery_MOSFET, !battery_on); //关闭电池
|
||||
}else{//市电打开电池输出
|
||||
digitalWrite(battery_MOSFET, battery_on); //打开电池
|
||||
}
|
||||
digitalWrite(charge_MOSFET, charge_on); //打开 充电
|
||||
digitalWrite(bypass_MOSFET, bypass_on); //打开 旁路
|
||||
pwmChannel = 1;
|
||||
ups_mode = 10;//正常模式
|
||||
testdata = 1; //电池测试成功
|
||||
upsoutmode = 3;//UPS 正常模式 输出
|
||||
}if(!INV){//电源断开
|
||||
digitalWrite(battery_MOSFET, battery_on); //关闭电池
|
||||
digitalWrite(charge_MOSFET, !charge_on); //关闭充电
|
||||
digitalWrite(bypass_MOSFET, !bypass_on); //打开旁路
|
||||
pwmChannel = 0;
|
||||
upsoutmode = 5;//UPS 电池 输出
|
||||
ups_mode = 11; //电池供电模式
|
||||
upsronliang = 4;//直接报告 电池耗尽
|
||||
testdata = 4; //电池测试 意外终止
|
||||
}
|
||||
}else if(ups_mode == 4){//测试电池 充电
|
||||
Batin_Wh = 0;
|
||||
BatOut_Wh = 0;
|
||||
if((currentbat < 0.1)&&(voltagebat >= voltageBatteryMax)){
|
||||
//充电电流小于0.1电压大于等于最高电池电压 电池充满了
|
||||
Serial.println("BAT IN OK ");
|
||||
Serial.println(Ah);//打印电池实时容量
|
||||
cdAh = Ah; //记录上次充电容量
|
||||
Ah = 0; //清空 实时 电池 容量
|
||||
Wh = 0;
|
||||
digitalWrite(battery_MOSFET, battery_on); //打开电池
|
||||
digitalWrite(charge_MOSFET, !charge_on); //关闭充电
|
||||
digitalWrite(bypass_MOSFET, !bypass_on); //关闭旁路
|
||||
pwmChannel = 0;
|
||||
ups_mode = 5;//测试电池 放电
|
||||
Serial.println("BAT OUT ");
|
||||
}
|
||||
if(!INV){//电源断开
|
||||
digitalWrite(battery_MOSFET, battery_on); //关闭电池
|
||||
digitalWrite(charge_MOSFET, !charge_on); //关闭充电
|
||||
digitalWrite(bypass_MOSFET, !bypass_on); //打开旁路
|
||||
pwmChannel = 0;
|
||||
upsoutmode = 5; //UPS 电池 输出
|
||||
ups_mode = 11; // 电池供电模式
|
||||
upsronliang = 4;//直接报告 电池耗尽
|
||||
testdata = 4; //电池测试 意外终止
|
||||
}
|
||||
}else if(ups_mode == 3){//测试电池
|
||||
Batin_Wh = 0;
|
||||
BatOut_Wh = 0;
|
||||
Serial.println("> BAT In ");
|
||||
upsoutmode = 1; //UPS 其他(不支持) 输出
|
||||
testdata = 5; // 正在测试 电池
|
||||
Ah = 0; //清空 实时 电池 容量
|
||||
Wh = 0;
|
||||
ups_mode = 4; //测试电池 充电
|
||||
if(batmode == 0){digitalWrite(battery_MOSFET, !battery_on);} //关闭 电池
|
||||
digitalWrite(charge_MOSFET, charge_on); //打开 充电
|
||||
digitalWrite(bypass_MOSFET, bypass_on); //打开 旁路
|
||||
pwmChannel = 1;
|
||||
}else if(ups_mode == 2){//找不到电池
|
||||
Batin_Wh = 0;
|
||||
BatOut_Wh = 0;
|
||||
if(INV&!BNC){//电源存在电池不存在
|
||||
//digitalWrite(battery_MOSFET, !battery_on); //干嘛 电池
|
||||
digitalWrite(charge_MOSFET, charge_on); //打开 充电
|
||||
pwmChannel = 1;
|
||||
//digitalWrite(bypass_MOSFET, !bypass_on); //小黑子 旁路
|
||||
//电池保护板激活代码,没写
|
||||
testdata = 3;
|
||||
if(INV&BNC){//电源存在电池存在
|
||||
if (fdAh == 0){// 判断电池容量是否存在
|
||||
testdata = 5;// 正在测试
|
||||
ups_mode = 0;// 初始化
|
||||
}else{
|
||||
testdata = 1;// 测试ok
|
||||
ups_mode = 9;//恢复模式
|
||||
}
|
||||
}
|
||||
}else{
|
||||
ups_mode = 3;//不知道,什么都不知道
|
||||
}
|
||||
}else if(ups_mode == 0){//初始化
|
||||
Batin_Wh = 0;
|
||||
BatOut_Wh = 0;
|
||||
if(INV&!BNC){//电源存在电池不存在
|
||||
Serial.println("> BAT No ");
|
||||
digitalWrite(battery_MOSFET, !battery_on); //关闭 电池
|
||||
digitalWrite(charge_MOSFET, charge_on); //打开 充电
|
||||
//digitalWrite(bypass_MOSFET, bypass_on); //打开 旁路
|
||||
pwmChannel = 1;//开始充电
|
||||
testdata = 3; //测试错误
|
||||
ups_mode = 2;
|
||||
}else if(INV&BNC){//电源存在电池存在
|
||||
if (fdAh == 0){// 判断电池容量是否存在
|
||||
testdata = 5;// 正在测试
|
||||
ups_mode = 3;// 开始 测试电池
|
||||
}else{
|
||||
testdata = 1;// 测试ok
|
||||
ups_mode = 9;//恢复模式
|
||||
}
|
||||
}else if(!INV){//不存在输入
|
||||
if (fdAh == 0){// 判断电池容量是否存在
|
||||
digitalWrite(battery_MOSFET, battery_on); //打开 电池
|
||||
digitalWrite(charge_MOSFET, !charge_on); //打开 充电
|
||||
digitalWrite(bypass_MOSFET, !bypass_on); //关闭 旁路
|
||||
upsoutmode = 5; //UPS 电池 输出
|
||||
ups_mode = 11; // 电池供电模式
|
||||
upsronliang = 4;//直接报告 电池耗尽
|
||||
testdata = 4; // 电池测试 意外终止
|
||||
}else{
|
||||
testdata = 1;// 测试ok
|
||||
ups_mode = 9;//恢复模式
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void buck(){
|
||||
if(pwmChannel == 1){
|
||||
if(currentbat>currentCharging) {PWM++;} //电流高于 → 降低占空比
|
||||
else if(voltagebat>voltageBatteryMax){PWM++;} //电压高于 → 降低占空比
|
||||
else if(voltagebat<voltageBatteryMax){if(PWM != 0){PWM--;}} //当输出低于充电电压时增加占空比(仅用于 CC-CV 模式)
|
||||
else{}
|
||||
if(pwmMax < PWM) {PWM = pwmMax;} //当达到设定的输出电压时什么都不做
|
||||
analogWrite(buck_pwm, PWM);
|
||||
}else{
|
||||
PWM = pwmMax;
|
||||
}
|
||||
}
|
||||
int ruun = 0;
|
||||
void loop() {
|
||||
unsigned long newtime;
|
||||
//容量算法
|
||||
newtime = millis();
|
||||
tick = newtime - lastread;
|
||||
lastread = newtime;
|
||||
Ah += (currentbat * tick) / 3600000.0;
|
||||
//digitalWrite(15, !digitalRead(15)); //打开旁路
|
||||
//Serial.print(tick);
|
||||
//Serial.print(" \r");
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
a_adc.loop();
|
||||
if(iicok){
|
||||
wifima.loop();
|
||||
}else{
|
||||
wifima.loop_bootloader();
|
||||
}
|
||||
ups_main();
|
||||
buck();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user