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.

72 lines
2.0 KiB

7 years ago
6 years ago
  1. /*
  2. OTA MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include "ArduinoOTA.h"
  6. // -----------------------------------------------------------------------------
  7. // OTA
  8. // -----------------------------------------------------------------------------
  9. void _otaConfigure() {
  10. ArduinoOTA.setPort(OTA_PORT);
  11. ArduinoOTA.setHostname(getSetting("hostname").c_str());
  12. #if USE_PASSWORD
  13. ArduinoOTA.setPassword(getSetting("adminPass", ADMIN_PASS).c_str());
  14. #endif
  15. }
  16. // -----------------------------------------------------------------------------
  17. void otaSetup() {
  18. _otaConfigure();
  19. #if WEB_SUPPORT
  20. wsOnAfterParseRegister(_otaConfigure);
  21. #endif
  22. ArduinoOTA.onStart([]() {
  23. DEBUG_MSG_P(PSTR("[OTA] Start\n"));
  24. #if WEB_SUPPORT
  25. wsSend_P(PSTR("{\"message\": 2}"));
  26. #endif
  27. });
  28. ArduinoOTA.onEnd([]() {
  29. DEBUG_MSG_P(PSTR("\n"));
  30. DEBUG_MSG_P(PSTR("[OTA] End\n"));
  31. #if WEB_SUPPORT
  32. wsSend_P(PSTR("{\"action\": \"reload\"}"));
  33. #endif
  34. deferredReset(100, CUSTOM_RESET_OTA);
  35. });
  36. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  37. DEBUG_MSG_P(PSTR("[OTA] Progress: %u%%\r"), (progress / (total / 100)));
  38. });
  39. ArduinoOTA.onError([](ota_error_t error) {
  40. #if DEBUG_SUPPORT
  41. DEBUG_MSG_P(PSTR("\n[OTA] Error #%u: "), error);
  42. if (error == OTA_AUTH_ERROR) DEBUG_MSG_P(PSTR("Auth Failed\n"));
  43. else if (error == OTA_BEGIN_ERROR) DEBUG_MSG_P(PSTR("Begin Failed\n"));
  44. else if (error == OTA_CONNECT_ERROR) DEBUG_MSG_P(PSTR("Connect Failed\n"));
  45. else if (error == OTA_RECEIVE_ERROR) DEBUG_MSG_P(PSTR("Receive Failed\n"));
  46. else if (error == OTA_END_ERROR) DEBUG_MSG_P(PSTR("End Failed\n"));
  47. #endif
  48. });
  49. ArduinoOTA.begin();
  50. // Register loop
  51. espurnaRegisterLoop(otaLoop);
  52. }
  53. void otaLoop() {
  54. ArduinoOTA.handle();
  55. }