FastLED 3.9.15
Loading...
Searching...
No Matches
OTA.ino
Go to the documentation of this file.
1
97
98#include <Arduino.h>
99#include <FastLED.h>
100#include "fl/net/ota.h"
101#include "fl/stl/sstream.h"
102
103// ====== Configure your transport here ======
104#define USE_WIFI 1
105
106// ====== Your identifiers ======
107static const char* HOSTNAME = "my-device";
108static const char* OTA_PASS = "supersecret"; // used for both ArduinoIDE OTA (hashed) and Web UI Basic Auth
109
110// ====== Wi-Fi creds (only used if USE_WIFI=1 and you call beginWiFi) ======
111static const char* WIFI_SSID = "MySSID";
112static const char* WIFI_PASS = "MyPass";
113
114// ====== LED Configuration ======
115#define NUM_LEDS 60
116#define DATA_PIN 2
118
120
121void setup() {
122 Serial.begin(115200);
123 delay(100);
124
125 Serial.println("FastLED OTA Example");
126 Serial.println("===================");
127
128 // Initialize FastLED
129 FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
130 FastLED.setBrightness(50);
131
132 // Optional: Enable AP fallback when Wi-Fi STA fails (must be called before beginWiFi)
133 // ota.enableApFallback("MyDevice-Setup", "setup-only");
134
135#if USE_WIFI
136 // Start Wi-Fi (async mode - returns immediately)
137 Serial.println("Starting Wi-Fi (async mode)...");
139 Serial.println("Wi-Fi connecting in background...");
140 Serial.println("LEDs will show connection status:");
141 Serial.println(" - Pulsing blue: Connecting...");
142 Serial.println(" - Solid green: Connected!");
143
144 // Check for service initialization failures
145 uint8_t failed = ota.getFailedServices();
146 if (failed != 0) {
147 Serial.println("\nWARNING: Some OTA services failed to initialize:");
148 if (failed & (uint8_t)fl::net::ota::Service::MDNS_FAILED) {
149 Serial.println(" - mDNS failed: Device won't be discoverable at hostname.local");
150 Serial.println(" Try accessing via IP address instead");
151 }
152 if (failed & (uint8_t)fl::net::ota::Service::HTTP_FAILED) {
153 Serial.println(" - HTTP server failed: Web OTA unavailable");
154 Serial.println(" You can still use Arduino IDE OTA (port 3232)");
155 }
156 if (failed & (uint8_t)fl::net::ota::Service::ARDUINO_OTA_FAILED) {
157 Serial.println(" - ArduinoOTA failed: IDE OTA unavailable");
158 Serial.println(" You can still use Web OTA interface");
159 }
160 }
161#else
162 // If your networking is already up (Wi-Fi or ETH), just start OTA services:
163 // For Ethernet users: Call ETH.begin() first, then ota.begin()
164 Serial.println("Starting OTA services (network assumed configured)...");
165 if (ota.begin(HOSTNAME, OTA_PASS)) {
166 Serial.println("OTA services started successfully");
167 } else {
168 Serial.println("OTA services failed to start");
169 }
170
171 // Check for service initialization failures
172 uint8_t failed = ota.getFailedServices();
173 if (failed != 0) {
174 Serial.println("\nWARNING: Some OTA services failed to initialize:");
175 if (failed & (uint8_t)fl::net::ota::Service::MDNS_FAILED) {
176 Serial.println(" - mDNS failed: Device won't be discoverable at hostname.local");
177 Serial.println(" Try accessing via IP address instead");
178 }
179 if (failed & (uint8_t)fl::net::ota::Service::HTTP_FAILED) {
180 Serial.println(" - HTTP server failed: Web OTA unavailable");
181 Serial.println(" You can still use Arduino IDE OTA (port 3232)");
182 }
183 if (failed & (uint8_t)fl::net::ota::Service::ARDUINO_OTA_FAILED) {
184 Serial.println(" - ArduinoOTA failed: IDE OTA unavailable");
185 Serial.println(" You can still use Web OTA interface");
186 }
187 }
188#endif
189
190 // Optional callbacks for monitoring OTA progress:
191 ota.onProgress([](size_t written, size_t total){
192 //Serial.printf("OTA Progress: %u/%u (%d%%)\n", (unsigned)written, (unsigned)total, (int)((written * 100) / total));
193 fl::sstream msg;
194 msg << "OTA Progress: " << written << "/" << total << " (" << (int)((written * 100) / total) << "%)";
195 Serial.println(msg.c_str());
196 });
197 ota.onError([](const char* error_msg){
198 // Serial.printf("OTA error: %s\n", error_msg);
199 fl::sstream msg;
200 msg << "OTA error: " << error_msg;
201 Serial.println(msg.c_str());
202 });
203 ota.onState([](uint8_t state){
204 //Serial.printf("OTA state: %u\n", state);
205 fl::sstream msg;
206 msg << "OTA state: " << (int)state;
207 Serial.println(msg.c_str());
208 });
209
210 // Callback invoked before device reboots after successful OTA update
211 // Use this to save state, turn off LEDs, or perform cleanup
212 ota.onBeforeReboot([](){
213 Serial.println("OTA complete! Preparing to reboot...");
214 Serial.println("Saving state and turning off LEDs...");
215
216 // Turn off all LEDs before reboot
218 FastLED.show();
219
220 // In a real application, you might:
221 // - Save LED configuration to EEPROM/preferences
222 // - Save animation state
223 // - Close file handles
224 // - Notify other systems of impending reboot
225 });
226
227 Serial.println("Setup complete. Running LED animation...");
228}
229
230uint8_t hue = 0;
232
233void loop() {
234 // Service OTA (low overhead: ~73µs when idle)
235 ota.poll();
236
237#if USE_WIFI
238 // Async WiFi mode: Show connection status via LEDs
239 if (!ota.isConnected()) {
240 // Connecting: Show pulsing blue animation
241 uint8_t brightness = beatsin8(60, 30, 255); // Pulse at 60 BPM
243 FastLED.setBrightness(brightness);
244 FastLED.show();
245 delay(20);
246 return; // Skip normal animation while connecting
247 } else if (!wifi_connected_notified) {
248 // Just connected: Show success and print info
250 Serial.println("\nWi-Fi connected!");
251 Serial.print("Access web interface at: http://");
252 Serial.print(HOSTNAME);
253 Serial.println(".local/");
254
255 // Brief celebration: Green flash
257 FastLED.setBrightness(255);
258 FastLED.show();
259 delay(500);
260
261 // Restore normal brightness for animation
262 FastLED.setBrightness(50);
263 }
264#endif
265
266 // Normal LED animation (rainbow)
268 FastLED.show();
269
270 hue++;
271
272 // Small delay for animation speed
273 delay(20);
274}
#define NUM_LEDS
fl::CRGB leds[NUM_LEDS]
fl::UISlider brightness("Brightness", BRIGHTNESS, 0, 255)
#define DATA_PIN
Definition ClientReal.h:82
TestState state
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
fl::net::OTA ota
Definition OTA.ino:119
static const char * OTA_PASS
Definition OTA.ino:108
void setup()
Definition OTA.ino:121
static const char * WIFI_SSID
Definition OTA.ino:111
static const char * WIFI_PASS
Definition OTA.ino:112
static const char * HOSTNAME
Definition OTA.ino:107
bool wifi_connected_notified
Definition OTA.ino:231
void loop()
Definition OTA.ino:233
uint8_t hue
Definition advanced.h:94
OTA (Over-The-Air) update manager for ESP32 platforms.
Definition ota.h:124
const char * c_str() const FL_NOEXCEPT
Definition strstream.h:44
void fill_rainbow(CRGB *targetArray, int numToFill, fl::u8 initialhue, fl::u8 deltahue=5) FL_NOEXCEPT
Fill a range of LEDs with a rainbow of colors.
Definition fill.cpp.hpp:29
void fill_solid(CRGB *targetArray, int numToFill, const CRGB &color) FL_NOEXCEPT
Fill a range of LEDs with a solid color.
Definition fill.cpp.hpp:9
constexpr EOrder GRB
Definition eorder.h:19
fl::CRGB CRGB
Definition crgb.h:25
LIB8STATIC u8 beatsin8(accum88 beats_per_minute, u8 lowest=0, u8 highest=255, u32 timebase=0, u8 phase_offset=0) FL_NOEXCEPT
Generates an 8-bit sine wave at a given BPM that oscillates within a given range.
Definition beat.h:105
@ HTTP_FAILED
HTTP server failed to start (Web OTA unavailable)
Definition ota.h:112
@ MDNS_FAILED
mDNS initialization failed (device not discoverable at hostname.local)
Definition ota.h:111
@ ARDUINO_OTA_FAILED
ArduinoOTA initialization failed (IDE OTA unavailable)
Definition ota.h:113
Minimal, batteries-included OTA (Over-The-Air) update system for ESP32.
@ Green
<div style='background:#008000;width:4em;height:4em;'></div>
Definition crgb.h:558
@ Blue
<div style='background:#0000FF;width:4em;height:4em;'></div>
Definition crgb.h:512
@ Black
<div style='background:#000000;width:4em;height:4em;'></div>
Definition crgb.h:510
#define Serial
Definition serial.h:304