Skip to content

Commit

Permalink
ota feature
Browse files Browse the repository at this point in the history
  • Loading branch information
say-paul committed Mar 13, 2024
1 parent b794021 commit 898bea1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/blite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ void Blite::setup(){
digitalWrite(LED_BUILTIN, HIGH);
this->defineM12(true);
this->defineM34(true);
String newHostname = "buildybee";
WiFi.hostname(newHostname.c_str());
WiFi.disconnect();
WiFi.mode(WIFI_OFF);

Expand Down Expand Up @@ -133,15 +135,17 @@ int Blite::readADC(){
}

void Blite::setupServer(String HTML_CONTENT) {
this->webServer.on("/", HTTP_GET, [&]() {
this->webServer.on("/", HTTP_GET, [=]() {
this->webServer.send(200, "text/html", HTML_CONTENT);
});
this->webServer.begin();
this->serverSetupDone = true;
this->otaSetup();
}

void Blite::renderServer() {
this->webServer.handleClient();
this->otaLoop();
}

void Blite::smartRenderServer(String HTML_CONTENT){
Expand All @@ -150,3 +154,43 @@ void Blite::smartRenderServer(String HTML_CONTENT){
}
this->renderServer();
}

void Blite::otaSetup(){

ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}

// NOTE: if updating FS this would be the place to unmount FS using FS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
}

void Blite::otaLoop(){
ArduinoOTA.handle();
}
6 changes: 6 additions & 0 deletions src/blite.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ArduinoOTA.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>

class Blite {
public:
Expand All @@ -46,6 +49,9 @@ void setupServer(String HTML_CONTENT);
void renderServer();
void smartRenderServer(String HTML_CONTENT);

void otaSetup();
void otaLoop();

private:
int m1,m2,m3,m4,speed;
ESP8266WebServer webServer = ESP8266WebServer(80);
Expand Down

0 comments on commit 898bea1

Please sign in to comment.