ota.h 1.4 KB

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