add base MeshSprocket with OTA enabled

This commit is contained in:
2018-08-17 14:29:18 +02:00
parent a94aaa6804
commit c127346394
8 changed files with 85 additions and 49 deletions

View File

@@ -0,0 +1,73 @@
#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;
public:
OtaTcpPlugin(OtaConfig cfg){
config = cfg;
}
void enable(Scheduler* userScheduler, Network* network){
scheduler = userScheduler;
// connect to network
if(!network->isConnected()){
// TODO when config is refactored, cast is not necessary anymore
static_cast<MeshNet*>(network)->config.stationMode = 1;
network->connectStation();
// TODO set service message to mesh to announce station IP
}
// setup task
otaTask.set(TASK_MILLISECOND * 100, TASK_FOREVER, [](){
ArduinoOTA.handle();
});
scheduler->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");
});
ArduinoOTA.begin();
otaTask.enable();
}
void disable(){
otaTask.disable();
}
};
#endif