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.

134 lines
3.4 KiB

7 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. #include <ESP8266httpUpdate.h>
  7. // -----------------------------------------------------------------------------
  8. // OTA
  9. // -----------------------------------------------------------------------------
  10. void _otaConfigure() {
  11. ArduinoOTA.setPort(OTA_PORT);
  12. ArduinoOTA.setHostname(getSetting("hostname").c_str());
  13. #if USE_PASSWORD
  14. ArduinoOTA.setPassword(getSetting("adminPass", ADMIN_PASS).c_str());
  15. #endif
  16. }
  17. #if TERMINAL_SUPPORT
  18. void _otaFrom(const char * url) {
  19. DEBUG_MSG_P(PSTR("[OTA] Downloading from '%s'\n"), url);
  20. #if WEB_SUPPORT
  21. wsSend_P(PSTR("{\"message\": 2}"));
  22. #endif
  23. ESPhttpUpdate.rebootOnUpdate(false);
  24. t_httpUpdate_return ret = ESPhttpUpdate.update(url);
  25. switch(ret) {
  26. case HTTP_UPDATE_FAILED:
  27. DEBUG_MSG_P(
  28. PSTR("[OTA] Error (%d): %s\n"),
  29. ESPhttpUpdate.getLastError(),
  30. ESPhttpUpdate.getLastErrorString().c_str()
  31. );
  32. break;
  33. case HTTP_UPDATE_NO_UPDATES:
  34. DEBUG_MSG_P(PSTR("[OTA] No updates available\n"));
  35. break;
  36. case HTTP_UPDATE_OK:
  37. DEBUG_MSG_P(PSTR("[OTA] Done, restarting...\n"));
  38. #if WEB_SUPPORT
  39. wsSend_P(PSTR("{\"action\": \"reload\"}"));
  40. #endif
  41. deferredReset(100, CUSTOM_RESET_OTA);
  42. break;
  43. }
  44. }
  45. void _otaInitCommands() {
  46. settingsRegisterCommand(F("OTA"), [](Embedis* e) {
  47. if (e->argc < 2) {
  48. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  49. } else {
  50. DEBUG_MSG_P(PSTR("+OK\n"));
  51. String url = String(e->argv[1]);
  52. _otaFrom(url.c_str());
  53. }
  54. });
  55. }
  56. #endif // TERMINAL_SUPPORT
  57. void _otaLoop() {
  58. ArduinoOTA.handle();
  59. }
  60. // -----------------------------------------------------------------------------
  61. void otaSetup() {
  62. _otaConfigure();
  63. #if WEB_SUPPORT
  64. wsOnAfterParseRegister(_otaConfigure);
  65. #endif
  66. #if TERMINAL_SUPPORT
  67. _otaInitCommands();
  68. #endif
  69. // Register loop
  70. espurnaRegisterLoop(_otaLoop);
  71. // -------------------------------------------------------------------------
  72. ArduinoOTA.onStart([]() {
  73. DEBUG_MSG_P(PSTR("[OTA] Start\n"));
  74. #if WEB_SUPPORT
  75. wsSend_P(PSTR("{\"message\": 2}"));
  76. #endif
  77. });
  78. ArduinoOTA.onEnd([]() {
  79. DEBUG_MSG_P(PSTR("\n"));
  80. DEBUG_MSG_P(PSTR("[OTA] Done, restarting...\n"));
  81. #if WEB_SUPPORT
  82. wsSend_P(PSTR("{\"action\": \"reload\"}"));
  83. #endif
  84. deferredReset(100, CUSTOM_RESET_OTA);
  85. });
  86. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  87. DEBUG_MSG_P(PSTR("[OTA] Progress: %u%%\r"), (progress / (total / 100)));
  88. });
  89. ArduinoOTA.onError([](ota_error_t error) {
  90. #if DEBUG_SUPPORT
  91. DEBUG_MSG_P(PSTR("\n[OTA] Error #%u: "), error);
  92. if (error == OTA_AUTH_ERROR) DEBUG_MSG_P(PSTR("Auth Failed\n"));
  93. else if (error == OTA_BEGIN_ERROR) DEBUG_MSG_P(PSTR("Begin Failed\n"));
  94. else if (error == OTA_CONNECT_ERROR) DEBUG_MSG_P(PSTR("Connect Failed\n"));
  95. else if (error == OTA_RECEIVE_ERROR) DEBUG_MSG_P(PSTR("Receive Failed\n"));
  96. else if (error == OTA_END_ERROR) DEBUG_MSG_P(PSTR("End Failed\n"));
  97. #endif
  98. });
  99. ArduinoOTA.begin();
  100. }