ota.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. Ticker tkOTA; // periodic check if OTA available
  2. int OTA_check_ms = 100;
  3. void loopOTA(){
  4. ArduinoOTA.handle();
  5. //Serial.println("OTA check");
  6. }
  7. void detachOTA(){
  8. tkOTA.detach();
  9. }
  10. void setupOTA(){
  11. ArduinoOTA.onStart([]() {
  12. Serial.println("Starting firmware update");
  13. redLedState(-1,100); // fast red blink
  14. });
  15. ArduinoOTA.onEnd([]() {
  16. Serial.println("update done !");
  17. redLedState(0, 500);
  18. blueLedState(-1, 100); // 5 fast blue blink on end
  19. delay(500);
  20. blueLedState(0,100);
  21. //ledBlink(Blue_Led, 3, 100);
  22. });
  23. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  24. Serial.printf("Progress: %u%%\n", (progress / (total / 100)));
  25. blueLedState(-1,500); //slow blue blink during progress
  26. });
  27. ArduinoOTA.onError([](ota_error_t error) {
  28. Serial.printf("Error[%u]: ", error);
  29. if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  30. else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  31. else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  32. else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  33. else if (error == OTA_END_ERROR) Serial.println("End Failed");
  34. redLedState(-1, 250);
  35. blueLedState(-1,250);
  36. });
  37. ArduinoOTA.begin();
  38. Serial.println("OTA started");
  39. tkOTA.attach_ms(OTA_check_ms, loopOTA);
  40. }