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