Skip to content

Commit

Permalink
💥 HUGE: new low-voltage disconnect, psu pref, inPin
Browse files Browse the repository at this point in the history
- new low-voltage cutoff preference
 * use to cut off your load with a relay!
 * sweet new configuration option for it
- now supports the 'psu' command to set up which one
 * right now only supports 'drok' or 'DPS' options
- adds inPin pref to set that analog pin!
  • Loading branch information
t413 committed Jul 2, 2020
1 parent 80a457f commit fd3b72d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 12 deletions.
91 changes: 81 additions & 10 deletions lib/MPPTLib/solar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#include <esp_task_wdt.h>
#include <HTTPUpdate.h>

using namespace std::placeholders;

WiFiClient espClient;

Solar::~Solar() { }

Solar::Solar(String version) :
version_(version),
state_(States::off),
psu_(NULL),
server_(80),
pub_() {
db_.client.setClient(espClient);
Expand Down Expand Up @@ -53,13 +56,15 @@ void Solar::setup() {
pub_.add("mqttUser", db_.user).hide().pref();
pub_.add("mqttPass", db_.pass).hide().pref();
pub_.add("mqttFeed", db_.feed).hide().pref();
pub_.add("psu", bind(&Solar::setPSU, this)).pref();
pub_.add("inPin", pinInvolt_).pref();
pub_.add("lvProtect", std::bind(&Solar::setLVProtect, this, _1)).pref();
pub_.add("psu", std::bind(&Solar::setPSU, this, _1)).pref();
pub_.add("outputEN",[=](String s){ if (s.length()) psu_->enableOutput(s == "on"); return String(psu_->outEn_); });
pub_.add("outvolt", [=](String s){ if (s.length()) psu_->setVoltage(s.toFloat()); return String(psu_->outVolt_); });
pub_.add("outcurr", [=](String s){ if (s.length()) psu_->setCurrent(s.toFloat()); return String(psu_->outCurr_); });
pub_.add("outpower",[=](String){ return String(psu_->outVolt_ * psu_->outCurr_); });
pub_.add("currFilt",[=](String){ return String(psu_->currFilt_); });
pub_.add("state", state_ );
pub_.add("currFilt", psu_->currFilt_ );
pub_.add("pgain", pgain_ ).pref();
pub_.add("ramplimit", ramplimit_ ).pref();
pub_.add("setpoint", setpoint_ ).pref();
Expand All @@ -71,7 +76,7 @@ void Solar::setup() {
pub_.add("autosweep", autoSweep_ ).pref();
pub_.add("currentcap", currentCap_ ).pref();
pub_.add("involt", inVolt_);
pub_.add("wh", psu_->wh_ );
pub_.add("wh", [=](String s) { if (s.length()) psu_->wh_ = s.toFloat(); return String(psu_->wh_); });
pub_.add("collapses", [=](String) { return String(getCollapses()); });
pub_.add("sweep",[=](String){ startSweep(); return "starting sweep"; }).hide();
pub_.add("connect",[=](String s){ doConnect(); return "connected"; }).hide();
Expand Down Expand Up @@ -129,10 +134,6 @@ void Solar::setup() {
pub_.loadPrefs();
// wifi & mqtt is connected by pubsubConnect below

// psu_ = new (Serial2);
// Serial2.begin(4800, SERIAL_8N1, -1, -1, false, 1000);
//TODO

//fn, name, stack size, parameter, priority, handle
xTaskCreate(runPubt, "publish", 10000, this, 1, NULL);

Expand All @@ -145,6 +146,32 @@ void Solar::setup() {
log("OSPController Version " + version_);
}

String Solar::setLVProtect(String s) {
if (s.length()) {
lvProtect_.reset(new LowVoltageProtect(s)); //may throw!
lvProtect_->init();
return "new " + lvProtect_->toString() + " ok";
} else return lvProtect_? lvProtect_->toString() : "";
}

String Solar::setPSU(String s) {
log("setPSU " + s);
if (s.length() || !psu_) {
//TODO Parse softserial pins, bluetooth comms, and moar.
s.toUpperCase();
if (s.startsWith("DP")) {
psu_.reset(new DPS(&Serial2));
Serial2.begin(9600, SERIAL_8N1, -1, -1, false, 1000);
} else { //default
psu_.reset(new Drok(&Serial2));
Serial2.begin(4800, SERIAL_8N1, -1, -1, false, 1000);
}
psu_->begin();
return "created PSU=" + psu_->getType();
}
return psu_->getType();
}

void Solar::doConnect() {
if (! WiFi.isConnected()) {
if (wifiap.length() && wifipass.length()) {
Expand Down Expand Up @@ -290,8 +317,10 @@ void Solar::loop() {
return delay(100);

if (now > nextVmeas_) {
int analogval = analogRead(pinInvolt_);
inVolt_ = analogval * 3.3 * (vadjust_ / 3.3) / 4096.0;
if (!psu_->getInputVolt(&inVolt_)) {
int analogval = analogRead(pinInvolt_);
inVolt_ = analogval * 3.3 * (vadjust_ / 3.3) / 4096.0;
}
pub_.setDirtyAddr(&inVolt_);
if (state_ == States::sweeping) {
doSweepStep();
Expand Down Expand Up @@ -391,6 +420,19 @@ void Solar::loop() {
nextPSUpdate_ = now + min(getBackoff(5000), 100000); //100s
}

if (lvProtect_ && now > lvProtect_->nextCheck_) {
if (psu_->outVolt_ < lvProtect_->threshold_) {
log(str("LOW VOLTAGE PROTECT TRIGGERED (now at %0.2fV)", psu_->outVolt_));
sendOutgoingLogs(); //send logs, tripping this relay may power us down
delay(200);
lvProtect_->trigger();
lvProtect_->nextCheck_ = now + 20 * 1000;
} else if (psu_->outVolt_ > lvProtect_->threshRecovery_) {
lvProtect_->trigger(false);
lvProtect_->nextCheck_ = now + 10000;
}
}

if (getCollapses() > 2)
nextAutoSweep_ = lastAutoSweep_ + autoSweep_ / 3.0 * 1000;

Expand Down Expand Up @@ -517,6 +559,35 @@ void Solar::doUpdate(String url) {
}
}


String LowVoltageProtect::toString() const {
return str("%d:%0.2f:%0.2f", pin_, threshold_, threshRecovery_);
}
LowVoltageProtect::LowVoltageProtect(String config) {
StringPair sp1 = split(config, ":");
if (sp1.first.length()) pin_ = sp1.first.toInt();
log("DEBUG sp1 " + sp1.first + " /// " + sp1.second);
if (sp1.second.length()) {
StringPair sp2 = split(sp1.second, ":");
log("DEBUG sp2 " + sp2.first + " /// " + sp2.second);
threshold_ = sp2.first.toFloat();
if (sp2.second.length())
threshRecovery_ = sp2.second.toFloat();
else threshRecovery_ = threshold_ * 1.08;
}
log("created lvProtect="+toString());
}

void LowVoltageProtect::init() {
trigger(false);
log("low-voltage cutoff enabled: " + toString());
}

void LowVoltageProtect::trigger(bool trigger) {
pinMode(pin_, trigger? OUTPUT : INPUT_PULLUP);
digitalWrite(pin_, !trigger);
}

//page styling
const String style =
"<style>#file-input,input{width:100%;height:44px;border-radius:4px;margin:10px auto;font-size:15px}"
Expand Down
22 changes: 20 additions & 2 deletions lib/MPPTLib/solar.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <WebServer.h>

class PowerSupply;
struct LowVoltageProtect;

struct DBConnection {
String serv, user, pass, feed;
Expand All @@ -23,7 +24,11 @@ struct SPoint {
class Solar {
public:
Solar(String version);
~Solar();
void setup();
String setLVProtect(String);
String setPSU(String);

void loop();
void sendOutgoingLogs();
void publishTask();
Expand All @@ -41,7 +46,7 @@ class Solar {

const String version_;
String state_;
uint8_t pinInvolt_ = 32;
int pinInvolt_ = 32;
float inVolt_ = 0;
double setpoint_ = 0, pgain_ = 0.005, ramplimit_ = 2;
double currentCap_ = 8.5;
Expand All @@ -53,8 +58,9 @@ class Solar {
String wifiap, wifipass;
uint32_t ignoreSubsUntil_ = 0;
int8_t backoffLevel_ = 0;
std::unique_ptr<LowVoltageProtect> lvProtect_;

PowerSupply* psu_;
std::unique_ptr<PowerSupply> psu_;
WebServer server_;
Publishable pub_;
DBConnection db_;
Expand All @@ -71,3 +77,15 @@ struct States {
STATE(capped);
STATE(collapsemode);
};


struct LowVoltageProtect {
uint8_t pin_ = 22;
float threshold_ = 12.0;
float threshRecovery_ = 13.0;
uint32_t nextCheck_ = 0;
String toString() const;
LowVoltageProtect(String configuration);
void init();
void trigger(bool trigger=true);
};

0 comments on commit fd3b72d

Please sign in to comment.