ota.h 1.6 KB

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