munet Network Library for muwerk Scheduler
A muwerk network library supporting WiFi, NTP, OTA and MQTT for ESP8266 and ESP32 and serial links for all platforms
Loading...
Searching...
No Matches
ota.h
1// ota.h
2#pragma once
3
4// #if defined(__ESP__)
5
6#include <functional>
7
8#include "ustd_platform.h"
9#include "ustd_array.h"
10#include "ustd_map.h"
11
12#include "scheduler.h"
13#include "filesystem.h"
14
15#include <ArduinoOTA.h>
16#include <Arduino_JSON.h>
17
18namespace ustd {
19
57class Ota {
58 private:
59 // muwerk task management
60 Scheduler *pSched;
61 int tID;
62
63 // runtime control - state management
64 bool bNetUp = false;
65 bool bCheckOTA = false;
66 bool bOTAUpdateActive = false;
67
68 public:
69 Ota() {
71 }
72
73 ~Ota() {
74 }
75
76 void begin(Scheduler *_pSched) {
85 // init scheduler
86 pSched = _pSched;
87 tID = pSched->add([this]() { this->loop(); }, "ota", 25000L); // check for ota every 25ms
88
89 // subscribe to all messages
90 pSched->subscribe(tID, "#", [this](String topic, String msg, String originator) {
91 this->subsMsg(topic, msg, originator);
92 });
93
94 pSched->publish("net/network/get");
95 }
96
97 private:
98 void loop() {
99 if (bCheckOTA) {
100 ArduinoOTA.handle();
101 }
102 }
103
104 void subsMsg(String topic, String msg, String originator) {
105 JSONVar mqttJsonMsg = JSON.parse(msg);
106
107 if (JSON.typeof(mqttJsonMsg) == "undefined") {
108 return;
109 }
110
111 if (topic == "net/network") {
112 String state = (const char *)mqttJsonMsg["state"]; // root["state"];
113 if (state == "connected") {
114 if (!bNetUp) {
115 bNetUp = true;
116 OTAsetup();
117 bCheckOTA = true;
118 }
119 } else {
120 bNetUp = false;
121 bCheckOTA = false;
122 }
123 }
124 }
125
126 void OTAsetup() {
127
128#if defined(__ESP32__)
129 ArduinoOTA.setHostname(WiFi.getHostname());
130#else
131 ArduinoOTA.setHostname(WiFi.hostname().c_str());
132#endif
133
134 // TODO: No authentication by default
135 // ArduinoOTA.setPassword("secret");
136
137 // Password can be set with it's md5 value as well
138 // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
139 // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
140
141 ArduinoOTA.onStart([&]() {
142 String type;
143 if (ArduinoOTA.getCommand() == U_FLASH)
144 type = "sketch";
145 else // U_SPIFFS
146 type = "filesystem";
147
148 DBG("Start updating " + type);
149 bOTAUpdateActive = true;
150 pSched->singleTaskMode(tID);
151 fsEnd();
152 });
153 ArduinoOTA.onEnd([&]() {
154 DBG("\nEnd of update");
155 pSched->singleTaskMode(-1);
156 bOTAUpdateActive = false;
157 });
158 ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
159 DBGF("Progress: %u%%\r", (progress / (total / 100)));
160 });
161 ArduinoOTA.onError([&](ota_error_t error) {
162#ifdef USE_SERIAL_DBG
163 Serial.printf("Error[%u]: ", error);
164 if (error == OTA_AUTH_ERROR)
165 Serial.println("Auth Failed");
166 else if (error == OTA_BEGIN_ERROR)
167 Serial.println("Begin Failed");
168 else if (error == OTA_CONNECT_ERROR)
169 Serial.println("Connect Failed");
170 else if (error == OTA_RECEIVE_ERROR)
171 Serial.println("Receive Failed");
172 else if (error == OTA_END_ERROR)
173 Serial.println("End Failed");
174#endif
175 });
176 ArduinoOTA.begin();
177 }
178};
179
180} // namespace ustd
181
182// #endif // defined(__ESP__)
munet OTA Class
Definition: ota.h:57
void begin(Scheduler *_pSched)
Definition: ota.h:76
Ota()
Definition: ota.h:69
The muwerk namespace.
Definition: mqtt.h:21