FastLED 3.10.8
Loading...
Searching...
No Matches

◆ setup()

void setup ( )

Definition at line 119 of file OTA.ino.

119 {
120 Serial.begin(115200);
121 delay(100);
122
123 Serial.println("FastLED OTA Example");
124 Serial.println("===================");
125
126 // Initialize FastLED
127 FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
128 FastLED.setBrightness(50);
129
130 // Optional: Enable AP fallback when Wi-Fi STA fails (must be called before beginWiFi)
131 // ota.enableApFallback("MyDevice-Setup", "setup-only");
132
133#if USE_WIFI
134 // Start Wi-Fi (async mode - returns immediately)
135 Serial.println("Starting Wi-Fi (async mode)...");
137 Serial.println("Wi-Fi connecting in background...");
138 Serial.println("LEDs will show connection status:");
139 Serial.println(" - Pulsing blue: Connecting...");
140 Serial.println(" - Solid green: Connected!");
141
142 // Check for service initialization failures
143 uint8_t failed = ota.getFailedServices();
144 if (failed != 0) {
145 Serial.println("\nWARNING: Some OTA services failed to initialize:");
147 Serial.println(" - mDNS failed: Device won't be discoverable at hostname.local");
148 Serial.println(" Try accessing via IP address instead");
149 }
151 Serial.println(" - HTTP server failed: Web OTA unavailable");
152 Serial.println(" You can still use Arduino IDE OTA (port 3232)");
153 }
155 Serial.println(" - ArduinoOTA failed: IDE OTA unavailable");
156 Serial.println(" You can still use Web OTA interface");
157 }
158 }
159#else
160 // If your networking is already up (Wi-Fi or ETH), just start OTA services:
161 // For Ethernet users: Call ETH.begin() first, then ota.begin()
162 Serial.println("Starting OTA services (network assumed configured)...");
163 if (ota.begin(HOSTNAME, OTA_PASS)) {
164 Serial.println("OTA services started successfully");
165 } else {
166 Serial.println("OTA services failed to start");
167 }
168
169 // Check for service initialization failures
170 uint8_t failed = ota.getFailedServices();
171 if (failed != 0) {
172 Serial.println("\nWARNING: Some OTA services failed to initialize:");
174 Serial.println(" - mDNS failed: Device won't be discoverable at hostname.local");
175 Serial.println(" Try accessing via IP address instead");
176 }
178 Serial.println(" - HTTP server failed: Web OTA unavailable");
179 Serial.println(" You can still use Arduino IDE OTA (port 3232)");
180 }
182 Serial.println(" - ArduinoOTA failed: IDE OTA unavailable");
183 Serial.println(" You can still use Web OTA interface");
184 }
185 }
186#endif
187
188 // Optional callbacks for monitoring OTA progress:
189 ota.onProgress([](size_t written, size_t total){
190 //Serial.printf("OTA Progress: %u/%u (%d%%)\n", (unsigned)written, (unsigned)total, (int)((written * 100) / total));
191 fl::sstream msg;
192 msg << "OTA Progress: " << written << "/" << total << " (" << (int)((written * 100) / total) << "%)";
193 Serial.println(msg.c_str());
194 });
195 ota.onError([](const char* error_msg){
196 // Serial.printf("OTA error: %s\n", error_msg);
197 fl::sstream msg;
198 msg << "OTA error: " << error_msg;
199 Serial.println(msg.c_str());
200 });
201 ota.onState([](uint8_t state){
202 //Serial.printf("OTA state: %u\n", state);
203 fl::sstream msg;
204 msg << "OTA state: " << (int)state;
205 Serial.println(msg.c_str());
206 });
207
208 // Callback invoked before device reboots after successful OTA update
209 // Use this to save state, turn off LEDs, or perform cleanup
210 ota.onBeforeReboot([](){
211 Serial.println("OTA complete! Preparing to reboot...");
212 Serial.println("Saving state and turning off LEDs...");
213
214 // Turn off all LEDs before reboot
216 FastLED.show();
217
218 // In a real application, you might:
219 // - Save LED configuration to EEPROM/preferences
220 // - Save animation state
221 // - Close file handles
222 // - Notify other systems of impending reboot
223 });
224
225 Serial.println("Setup complete. Running LED animation...");
226}
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:117
static const char * OTA_PASS
Definition OTA.ino:106
static const char * WIFI_SSID
Definition OTA.ino:109
static const char * WIFI_PASS
Definition OTA.ino:110
static const char * HOSTNAME
Definition OTA.ino:105
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 stdint.h:208
@ Black
<div style='background:#000000;width:4em;height:4em;'></div>
Definition crgb.h:517
#define Serial
Definition serial.h:309

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: