#ifndef __OTA_CLASSIC_H__ #define __OTA_CLASSIC_H__ #include "TaskSchedulerDeclarations.h" #include "ArduinoOTA.h" #include "MeshNet.h" #include "Plugin.h" using namespace std; using namespace std::placeholders; struct OtaConfig { int port; const char* password; }; class OtaTcpPlugin : public Plugin { private: OtaConfig config; Task otaTask; MeshNet* net; public: OtaTcpPlugin(OtaConfig cfg){ config = cfg; } void connectUpdateNetwork() { Serial.println("OTA connect to update-network"); net->connectStation(); } void enable() { Serial.println("OTA enable"); ArduinoOTA.begin(); otaTask.enable(); } void onMessage(SprocketMessage msg) { if(msg.type == SprocketMessage::OTA){ Serial.println("OTA msg received"); WiFi.disconnect(); net->mesh.stop(); connectUpdateNetwork(); enable(); //net->mesh.sendBroadcast("my ip:" + WiFi.localIP().toString(), true); //net->mesh.sendBroadcast("gw ip:" + WiFi.gatewayIP().toString(), true); WiFi.gatewayIP().printTo(Serial); } } void activate(Scheduler* userScheduler, Network* network){ // connect done in network class? //connectUpdateNetwork(network); net = static_cast(network); // setup task // TOOD check if we can increase the time OTA needs to be handled otaTask.set(TASK_MILLISECOND * 1000, TASK_FOREVER, [](){ ArduinoOTA.handle(); }); userScheduler->addTask(otaTask); // configure OTA ArduinoOTA.setPort(config.port); //ArduinoOTA.setHostname(HOSTNAME); if(strlen(config.password) > 0){ ArduinoOTA.setPassword(config.password); } // setup callbacks ArduinoOTA.onStart([]() { Serial.println("OTA: Start"); }); ArduinoOTA.onEnd([]() { Serial.println("OTA: End"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("OTA: Progress: %u%%\r", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("OTA: Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("OTA: Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("OTA: Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("OTA: Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("OTA: Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("OTA: End Failed"); }); enable(); } void disable(){ otaTask.disable(); } }; #endif