联网成功后可以通过IDE进行OTA升级,代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <ArduinoOTA.h>
void arduino_8266_OTA() { ArduinoOTA.begin(); Serial.println("OTA ready"); ArduinoOTA.onStart([]() { String type; if (ArduinoOTA.getCommand() == U_FLASH) { type = "sketch"; } else { type = "filesystem"; } Serial.println("Start updating " + type); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
strip.setPixelColor(((progress / (total / 100))) - 1, (progress / (total / 100)) / 100 * 255 * brightness / 100, (255 - (progress / (total / 100)) / 100 * 255) * brightness / 100, (255 - (progress / (total / 100)) / 100 * 255) * brightness / 100); strip.show(); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("OTA更新发生错误[%u]: ", error); if (error == OTA_AUTH_ERROR) { Serial.println("认证失败"); } else if (error == OTA_BEGIN_ERROR) { Serial.println("开始失败"); } else if (error == OTA_CONNECT_ERROR) { Serial.println("连接失败"); } else if (error == OTA_RECEIVE_ERROR) { Serial.println("接收失败"); } else if (error == OTA_END_ERROR) { Serial.println("结束失败"); } ESP.restart(); }); ArduinoOTA.onEnd([]() { Serial.println("OTA更新结束"); strip.clear(); }); }
|
以上代码中brightness
是led的亮度,范围是1
~100
,setPixelColor可以参考点亮ws2812的文章