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.

58 lines
1.6 KiB

8 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. void otaSetup() {
  15. otaConfigure();
  16. ArduinoOTA.onStart([]() {
  17. DEBUG_MSG_P(PSTR("[OTA] Start\n"));
  18. wsSend("{\"message\": \"OTA update started\"}");
  19. });
  20. ArduinoOTA.onEnd([]() {
  21. customReset(CUSTOM_RESET_OTA);
  22. DEBUG_MSG_P(PSTR("\n[OTA] End\n"));
  23. wsSend("{\"action\": \"reload\"}");
  24. delay(100);
  25. });
  26. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  27. DEBUG_MSG_P(PSTR("[OTA] Progress: %u%%%%\r"), (progress / (total / 100)));
  28. });
  29. ArduinoOTA.onError([](ota_error_t error) {
  30. #if DEBUG_PORT
  31. DEBUG_MSG_P(PSTR("\n[OTA] Error[%u]: "), error);
  32. if (error == OTA_AUTH_ERROR) DEBUG_MSG_P(PSTR("Auth Failed\n"));
  33. else if (error == OTA_BEGIN_ERROR) DEBUG_MSG_P(PSTR("Begin Failed\n"));
  34. else if (error == OTA_CONNECT_ERROR) DEBUG_MSG_P(PSTR("Connect Failed\n"));
  35. else if (error == OTA_RECEIVE_ERROR) DEBUG_MSG_P(PSTR("Receive Failed\n"));
  36. else if (error == OTA_END_ERROR) DEBUG_MSG_P(PSTR("End Failed\n"));
  37. #endif
  38. });
  39. ArduinoOTA.begin();
  40. }
  41. void otaLoop() {
  42. ArduinoOTA.handle();
  43. }