Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.7 KiB

  1. /*
  2. ESPurna
  3. OTA MODULE
  4. Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
  5. */
  6. #include <ArduinoJson.h>
  7. #include "ArduinoOTA.h"
  8. // -----------------------------------------------------------------------------
  9. // OTA
  10. // -----------------------------------------------------------------------------
  11. void otaSetup() {
  12. ArduinoOTA.setPort(OTA_PORT);
  13. ArduinoOTA.setHostname(config.hostname.c_str());
  14. ArduinoOTA.setPassword((const char *) OTA_PASS);
  15. ArduinoOTA.onStart([]() {
  16. #if ENABLE_RF
  17. rfEnable(false);
  18. #endif
  19. #if DEBUG
  20. Serial.println(F("[OTA] Start"));
  21. #endif
  22. });
  23. ArduinoOTA.onEnd([]() {
  24. #if DEBUG
  25. Serial.println(F("[OTA] End"));
  26. #endif
  27. #if ENABLE_RF
  28. rfEnable(true);
  29. #endif
  30. });
  31. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  32. #if DEBUG
  33. Serial.printf("[OTA] Progress: %u%%\r", (progress / (total / 100)));
  34. #endif
  35. });
  36. ArduinoOTA.onError([](ota_error_t error) {
  37. #if DEBUG
  38. Serial.printf("[OTA] Error[%u]: ", error);
  39. if (error == OTA_AUTH_ERROR) Serial.println(F("[OTA] Auth Failed"));
  40. else if (error == OTA_BEGIN_ERROR) Serial.println(F("[OTA] Begin Failed"));
  41. else if (error == OTA_CONNECT_ERROR) Serial.println(F("[OTA] Connect Failed"));
  42. else if (error == OTA_RECEIVE_ERROR) Serial.println(F("[OTA] Receive Failed"));
  43. else if (error == OTA_END_ERROR) Serial.println(F("[OTA] End Failed"));
  44. #endif
  45. });
  46. ArduinoOTA.begin();
  47. }
  48. void otaLoop() {
  49. ArduinoOTA.handle();
  50. }