Skip to content

Commit

Permalink
declaration CWebOTA class (#6)
Browse files Browse the repository at this point in the history
Change delay functions to prevent a 42 day delay in case of wrap around
  • Loading branch information
SurfGargano committed Nov 7, 2021
1 parent 2246413 commit 1d77cb5
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 34 deletions.
58 changes: 33 additions & 25 deletions software_esp8266/src/WebOTA.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
Changelog :
5.11.2021 (Surfgargano)
declaration CWebOTA class
*/



// Arduino build process info: https://github.com/arduino/Arduino/wiki/Build-Process

#define WEBOTA_VERSION "0.1.5"
Expand All @@ -23,44 +31,44 @@ WebServer OTAServer(9999);
ESP8266WebServer OTAServer(9999);
#endif

WebOTA webota;
CWebOTA webota;

////////////////////////////////////////////////////////////////////////////

int WebOTA::init(const unsigned int port, const char *path) {
this->port = port;
this->path = path;
int CWebOTA::init(const unsigned int thisPort, const char *thisPath) {
port = thisPort;
path = thisPath;

// Only run this once
if (this->init_has_run) {
if (init_has_run) {
return 0;
}

add_http_routes(&OTAServer, path);
add_http_routes(&OTAServer, thisPath);
OTAServer.begin(port);

Serial.printf("WebOTA url : http://%s.local:%d%s\r\n\r\n", this->mdns.c_str(), port, path);
Serial.printf("WebOTA url : http://%s.local:%d%s\r\n\r\n", mdns.c_str(), thisPort, thisPath);

// Store that init has already run
this->init_has_run = true;
init_has_run = true;

return 1;
}

// One param
int WebOTA::init(const unsigned int port) {
return WebOTA::init(port, "/webota");
int CWebOTA::init(const unsigned int thisPort) {
return init(port, "/webota");
}

// No params
int WebOTA::init() {
return WebOTA::init(8080, "/webota");
int CWebOTA::init() {
return init(8080, "/webota");
}

int WebOTA::handle() {
int CWebOTA::handle() {
// If we haven't run the init yet run it
if (!this->init_has_run) {
WebOTA::init();
if (!init_has_run) {
init();
}

OTAServer.handleClient();
Expand All @@ -71,7 +79,7 @@ int WebOTA::handle() {
return 1;
}

long WebOTA::max_sketch_size() {
long CWebOTA::max_sketch_size() {
long ret = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;

return ret;
Expand Down Expand Up @@ -139,10 +147,10 @@ domReady(function() {


#ifdef ESP8266
int WebOTA::add_http_routes(ESP8266WebServer *server, const char *path) {
int CWebOTA::add_http_routes(ESP8266WebServer *server, const char *path) {
#endif
#ifdef ESP32
int WebOTA::add_http_routes(WebServer *server, const char *path) {
int CWebOTA::add_http_routes(WebServer *server, const char *path) {
#endif
// Index page
server->on("/", HTTP_GET, [server]() {
Expand All @@ -154,8 +162,8 @@ int WebOTA::add_http_routes(WebServer *server, const char *path) {
server->on(path, HTTP_GET, [server,this]() {
Serial.println("ota page!");
String html = "";
if (this->custom_html != NULL) {
html += this->custom_html;
if (custom_html != NULL) {
html += custom_html;
} else {
html += ota_version_html;
}
Expand All @@ -177,7 +185,7 @@ int WebOTA::add_http_routes(WebServer *server, const char *path) {
Serial.printf("Firmware update initiated: %s\r\n", upload.filename.c_str());

//uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
uint32_t maxSketchSpace = this->max_sketch_size();
uint32_t maxSketchSpace = max_sketch_size();

if (!Update.begin(maxSketchSpace)) { //start with max available size
Update.printError(Serial);
Expand Down Expand Up @@ -213,18 +221,18 @@ int WebOTA::add_http_routes(WebServer *server, const char *path) {

// If the MCU is in a delay() it cannot respond to HTTP OTA requests
// We do a "fake" looping delay and listen for incoming HTTP requests while waiting
void WebOTA::delay(unsigned int ms) {
void CWebOTA::delay(unsigned int ms) {
// Borrowed from mshoe007 @ https://github.com/scottchiefbaker/ESP-WebOTA/issues/8
decltype(millis()) last = millis();

while ((millis() - last) < ms) {
OTAServer.handleClient();
::delay(5);
delay(5);
}
}

void WebOTA::set_custom_html(char const * const html) {
this->custom_html = html;
void CWebOTA::set_custom_html(char const * const html) {
custom_html = html;
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
Expand Down
15 changes: 11 additions & 4 deletions software_esp8266/src/WebOTA.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
Changelog :
5.11.2021 (Surfgargano)
declaration CWebOTA class
*/


#include <Arduino.h>

#ifdef ESP8266
Expand All @@ -7,14 +14,14 @@
#include <WebServer.h>
#endif

class WebOTA {
class CWebOTA {
public:
unsigned int port;
String path = "";
String mdns = "";

int init(const unsigned int port, const char *path);
int init(const unsigned int port);
int init(const unsigned int thisPort, const char *thisPath);
int init(const unsigned int thisPort);
int init();
void delay(unsigned int ms);

Expand All @@ -38,4 +45,4 @@ class WebOTA {

int init_wifi(const char *ssid, const char *password, const char *mdns_hostname);

extern WebOTA webota;
extern CWebOTA webota;
15 changes: 13 additions & 2 deletions software_esp8266/src/app.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/*
Changelog :
5.11.2021 (Surfgargano)
change this
if (millis() > (uint32_t) 100 + timer) {
to
if ((millis()- timer) > (uint32_t) 100) {
to prevent a 42 day delay in case of wrap around
*/

#include "main.h"
#include <ArduinoJson.h>
#include <Arduino.h>
Expand Down Expand Up @@ -85,7 +96,7 @@ void app_loop() {


// 100 ms task
if (millis() > (uint32_t) 100 + timer) {
if ((millis()- timer) > (uint32_t) 100) {
timer = millis();

app_comm_machine();
Expand All @@ -94,7 +105,7 @@ void app_loop() {


// 1000 ms task
if (millis() > (uint32_t) 1000 + timer100ms) {
if ((millis()-timer100ms) > (uint32_t) 1000 ) {
timer100ms = millis();

//for (unsigned int x = 0;x<ACTUATOR_COUNT;x++) {
Expand Down
14 changes: 12 additions & 2 deletions software_esp8266/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*
Changelog :
5.11.2021 (Surfgargano)
change this
if (millis() > (uint32_t) 100 + timer) {
to
if ((millis()- timer) > (uint32_t) 100) {
to prevent a 42 day delay in case of wrap around
*/

#include <Arduino.h>
#include <WebOTA.h>
#include "mqtt.h"
Expand Down Expand Up @@ -41,7 +51,7 @@ void loop() {
static uint32_t timer10ms = 0;

// LED / heartbeat
if (millis() > (uint32_t) 1000 + timer) {
if ((millis()-timer) > (uint32_t) 1000) {
timer = millis();
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
//Serial.println("heartbeat");
Expand All @@ -52,7 +62,7 @@ void loop() {
webota.handle();


if (millis() > (uint32_t) 1000 + timer10ms) {
if ((millis()-timer10ms) > (uint32_t) 1000 ) {
timer10ms = millis();

telnet_loop();
Expand Down
12 changes: 11 additions & 1 deletion software_esp8266/src/mqtt.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*
Changelog :
5.11.2021 (Surfgargano)
change this
if (millis() > (uint32_t) 100 + timer) {
to
if ((millis()- timer) > (uint32_t) 100) {
to prevent a 42 day delay in case of wrap around
*/

#include <Arduino.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
Expand Down Expand Up @@ -32,7 +42,7 @@ void mqtt_loop() {
}
mqtt_client.loop();

if (millis() > (uint32_t) 2000 + timer) {
if ((millis()-timer) > (uint32_t) 2000 ) {
timer = millis();

publish_valves ();
Expand Down

0 comments on commit 1d77cb5

Please sign in to comment.