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.

66 lines
1.8 KiB

7 years ago
  1. /*
  2. OTA MODULE
  3. Copyright (C) 2016-2017 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. ArduinoOTA.setPassword(getSetting("adminPass", ADMIN_PASS).c_str());
  13. }
  14. // -----------------------------------------------------------------------------
  15. void otaSetup() {
  16. _otaConfigure();
  17. #if WEB_SUPPORT
  18. wsOnAfterParseRegister(_otaConfigure);
  19. #endif
  20. ArduinoOTA.onStart([]() {
  21. DEBUG_MSG_P(PSTR("[OTA] Start\n"));
  22. #if WEB_SUPPORT
  23. wsSend_P(PSTR("{\"message\": 2}"));
  24. #endif
  25. });
  26. ArduinoOTA.onEnd([]() {
  27. DEBUG_MSG_P(PSTR("\n[OTA] End\n"));
  28. #if WEB_SUPPORT
  29. wsSend_P(PSTR("{\"action\": \"reload\"}"));
  30. #endif
  31. deferredReset(100, CUSTOM_RESET_OTA);
  32. });
  33. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  34. DEBUG_MSG_P(PSTR("[OTA] Progress: %u%%%%\r"), (progress / (total / 100)));
  35. });
  36. ArduinoOTA.onError([](ota_error_t error) {
  37. #if DEBUG_SUPPORT
  38. DEBUG_MSG_P(PSTR("\n[OTA] Error #%u: "), error);
  39. if (error == OTA_AUTH_ERROR) DEBUG_MSG_P(PSTR("Auth Failed\n"));
  40. else if (error == OTA_BEGIN_ERROR) DEBUG_MSG_P(PSTR("Begin Failed\n"));
  41. else if (error == OTA_CONNECT_ERROR) DEBUG_MSG_P(PSTR("Connect Failed\n"));
  42. else if (error == OTA_RECEIVE_ERROR) DEBUG_MSG_P(PSTR("Receive Failed\n"));
  43. else if (error == OTA_END_ERROR) DEBUG_MSG_P(PSTR("End Failed\n"));
  44. #endif
  45. });
  46. ArduinoOTA.begin();
  47. }
  48. void otaLoop() {
  49. ArduinoOTA.handle();
  50. }