FastLED 3.9.15
Loading...
Searching...
No Matches

◆ setup()

void setup ( )

Definition at line 121 of file OTA.ino.

121 {
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}
#define NUM_LEDS
fl::CRGB leds[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:119
static const char * OTA_PASS
Definition OTA.ino:108
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
const char * c_str() const FL_NOEXCEPT
Definition strstream.h:44
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
@ 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_NOEXCEPT
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:510
#define Serial
Definition serial.h:304

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: