Skip to content

Commit

Permalink
📡 HUGE OTA Update: server-update & reliable uploading
Browse files Browse the repository at this point in the history
- Added a http-update! Send 'update=<URL>' cmd and it'll go!
- Figured out how the watchdog was killing both OTA methods
  • Loading branch information
t413 committed Jun 25, 2020
1 parent b204d2f commit d68677b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
44 changes: 39 additions & 5 deletions lib/MPPTLib/solar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <WiFiClient.h>
#include <ESPmDNS.h>
#include <Update.h>
#include <esp_task_wdt.h>
#include <HTTPUpdate.h>

WiFiClient espClient;

Expand All @@ -24,6 +26,7 @@ uint32_t lastPSUpdate_ = 0, lastPSUSuccess_ = 0;
uint32_t nextAutoSweep_ = 0, lastAutoSweep_ = 0;
double newDesiredCurr_ = 0;
extern const String updateIndex;
String doOTAUpdate_ = "";

void Solar::setup() {
Serial.begin(115200);
Expand Down Expand Up @@ -71,6 +74,7 @@ void Solar::setup() {
pub_.add("clear",[=](String s){ pub_.clearPrefs(); return "cleared"; }).hide();
pub_.add("debug",[=](String s){ psu_.debug_ = !(s == "off"); return String(psu_.debug_); }).hide();
pub_.add("version",[=](String){ log("Version " GIT_VERSION); return GIT_VERSION; }).hide();
pub_.add("update",[=](String s){ doOTAUpdate_ = s; return "OK, will try "+s; }).hide();
pub_.add("uptime",[=](String){ String ret = "Uptime " + timeAgo(millis()/1000); log(ret); return ret; }).hide();

server_.on("/", HTTP_ANY, [=]() {
Expand All @@ -95,8 +99,9 @@ void Solar::setup() {
HTTPUpload& upload = server_.upload();
if (upload.status == UPLOAD_FILE_START){
log(str("Update: %s\n", upload.filename.c_str()));
printPeriod_ = measperiod_ = 10000000;
nextPSUpdate_ = nextSolarAdjust_ = nextAutoSweep_ = millis() + 100000000;
doOTAUpdate_ = " "; //stops tasks
db_.client.disconnect(); //helps reliability
esp_task_wdt_init(120, true); //slows watchdog
if (!Update.begin(UPDATE_SIZE_UNKNOWN))//start with max available size
Update.printError(Serial);
} else if (upload.status == UPLOAD_FILE_WRITE){
Expand Down Expand Up @@ -266,6 +271,9 @@ int Solar::getCollapses() const { return collapses_.size(); }

void Solar::loop() {
uint32_t now = millis();
if (doOTAUpdate_.length())
return delay(100);

if ((now - lastV) >= ((state_ == States::sweeping)? measperiod_ * 4 : measperiod_)) {
int analogval = analogRead(pinInvolt_);
inVolt_ = analogval * 3.3 * (vadjust_ / 3.3) / 4096.0;
Expand Down Expand Up @@ -382,6 +390,12 @@ void Solar::loop() {
}
}

void Solar::sendOutgoingLogs() {
String s;
while (db_.client.connected() && pub_.popLog(&s))
db_.client.publish((db_.feed + "/log").c_str(), s.c_str(), false);
}

void Solar::publishTask() {
doConnect();
db_.client.loop();
Expand All @@ -407,6 +421,12 @@ void Solar::publishTask() {
while (true) {
uint32_t now = millis();
if ((now - lastpub) >= (psu_.outEn_? db_.period : db_.period * 4)) { //slow-down when not enabled
while (doOTAUpdate_ == " ") //stops this task while an upload-OTA is running
delay(1000);
if (doOTAUpdate_.length()) {
doUpdate(doOTAUpdate_);
doOTAUpdate_ = "";
}
if (db_.client.connected()) {
int wins = 0;
auto pubs = pub_.items(true);
Expand All @@ -420,12 +440,10 @@ void Solar::publishTask() {
pub_.logNote("[pub disconnected]");
doConnect();
}
sendOutgoingLogs();
heap_caps_check_integrity_all(true);
lastpub = now;
}
String s;
while (db_.client.connected() && pub_.popLog(&s))
db_.client.publish((db_.feed + "/log").c_str(), s.c_str(), false);
db_.client.loop();
pub_.poll(&Serial);
server_.handleClient();
Expand Down Expand Up @@ -469,6 +487,22 @@ String DBConnection::getEndpoint() const {
return (sep >= 0)? serv.substring(0, sep) : serv;
}

void Solar::doUpdate(String url) {
log("[OTA] running from " + url);
sendOutgoingLogs(); //send any outstanding log() messages
db_.client.disconnect(); //helps to disconnect everything
esp_task_wdt_init(120, true); //way longer watchdog timeout
t_httpUpdate_return ret = httpUpdate.update(espClient, url, GIT_VERSION);
if (ret == HTTP_UPDATE_FAILED) {
log(str("[OTA] Error (%d):", httpUpdate.getLastError()) + httpUpdate.getLastErrorString());
} else if (ret == HTTP_UPDATE_NO_UPDATES) {
log("[OTA] no updates");
} else if (ret == HTTP_UPDATE_OK) {
log("[OTA] SUCCESS!!! restarting");
delay(100);
ESP.restart();
}
}

//page styling
const String style =
Expand Down
2 changes: 2 additions & 0 deletions lib/MPPTLib/solar.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Solar {
Solar();
void setup();
void loop();
void sendOutgoingLogs();
void publishTask();
void doConnect();
void applyAdjustment();
Expand All @@ -32,6 +33,7 @@ class Solar {
void doSweepStep();
bool hasCollapsed() const;
int getCollapses() const;
void doUpdate(String url);

void backoff(String reason);
void setState(const String state, String reason="");
Expand Down

0 comments on commit d68677b

Please sign in to comment.