FastLED 3.10.9
Loading...
Searching...
No Matches

◆ setup()

void setup ( )

Definition at line 120 of file OTA.ino.

120 {
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}
CRGB leds[1]
#define NUM_LEDS
#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
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
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
constexpr EOrder GRB
Definition eorder.h:19
@ 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
void delay(u32 ms, bool run_async=true) FL_NO_EXCEPT
Public delay wrapper that keeps bare Arduino delay() preferred after using fl::delay; while still all...
Definition delay.h:98
unsigned char uint8_t
Definition s16x16x4.h:209
@ Black
<div style='background:#000000;width:4em;height:4em;'></div>
Definition crgb.h:517
#define Serial
Definition serial.h:314

References fl::net::ota::ARDUINO_OTA_FAILED, fl::CRGB::Black, fl::sstream::c_str(), DATA_PIN, FastLED, fill_solid(), GRB, HOSTNAME, fl::net::ota::HTTP_FAILED, leds, fl::net::ota::MDNS_FAILED, NUM_LEDS, ota, OTA_PASS, Serial, state, WIFI_PASS, and WIFI_SSID.

+ Here is the call graph for this function: