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.

141 lines
3.7 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;
  25. if (strncmp(url, "https", 5) == 0) {
  26. String fp = getSetting("otafp", OTA_GITHUB_FP);
  27. DEBUG_MSG_P(PSTR("[OTA] Using fingerprint: '%s'\n"), fp.c_str());
  28. ret = ESPhttpUpdate.update(url, APP_VERSION, fp.c_str());
  29. } else {
  30. ret = ESPhttpUpdate.update(url, APP_VERSION);
  31. }
  32. switch(ret) {
  33. case HTTP_UPDATE_FAILED:
  34. DEBUG_MSG_P(
  35. PSTR("[OTA] Error (%d): %s\n"),
  36. ESPhttpUpdate.getLastError(),
  37. ESPhttpUpdate.getLastErrorString().c_str()
  38. );
  39. break;
  40. case HTTP_UPDATE_NO_UPDATES:
  41. DEBUG_MSG_P(PSTR("[OTA] No updates available\n"));
  42. break;
  43. case HTTP_UPDATE_OK:
  44. DEBUG_MSG_P(PSTR("[OTA] Done, restarting...\n"));
  45. #if WEB_SUPPORT
  46. wsSend_P(PSTR("{\"action\": \"reload\"}"));
  47. #endif
  48. deferredReset(100, CUSTOM_RESET_OTA);
  49. break;
  50. }
  51. }
  52. void _otaInitCommands() {
  53. settingsRegisterCommand(F("OTA"), [](Embedis* e) {
  54. if (e->argc < 2) {
  55. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  56. } else {
  57. DEBUG_MSG_P(PSTR("+OK\n"));
  58. String url = String(e->argv[1]);
  59. _otaFrom(url.c_str());
  60. }
  61. });
  62. }
  63. #endif // TERMINAL_SUPPORT
  64. void _otaLoop() {
  65. ArduinoOTA.handle();
  66. }
  67. // -----------------------------------------------------------------------------
  68. void otaSetup() {
  69. _otaConfigure();
  70. #if WEB_SUPPORT
  71. wsOnAfterParseRegister(_otaConfigure);
  72. #endif
  73. #if TERMINAL_SUPPORT
  74. _otaInitCommands();
  75. #endif
  76. // Register loop
  77. espurnaRegisterLoop(_otaLoop);
  78. // -------------------------------------------------------------------------
  79. ArduinoOTA.onStart([]() {
  80. DEBUG_MSG_P(PSTR("[OTA] Start\n"));
  81. #if WEB_SUPPORT
  82. wsSend_P(PSTR("{\"message\": 2}"));
  83. #endif
  84. });
  85. ArduinoOTA.onEnd([]() {
  86. DEBUG_MSG_P(PSTR("\n"));
  87. DEBUG_MSG_P(PSTR("[OTA] Done, restarting...\n"));
  88. #if WEB_SUPPORT
  89. wsSend_P(PSTR("{\"action\": \"reload\"}"));
  90. #endif
  91. deferredReset(100, CUSTOM_RESET_OTA);
  92. });
  93. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  94. DEBUG_MSG_P(PSTR("[OTA] Progress: %u%%\r"), (progress / (total / 100)));
  95. });
  96. ArduinoOTA.onError([](ota_error_t error) {
  97. #if DEBUG_SUPPORT
  98. DEBUG_MSG_P(PSTR("\n[OTA] Error #%u: "), error);
  99. if (error == OTA_AUTH_ERROR) DEBUG_MSG_P(PSTR("Auth Failed\n"));
  100. else if (error == OTA_BEGIN_ERROR) DEBUG_MSG_P(PSTR("Begin Failed\n"));
  101. else if (error == OTA_CONNECT_ERROR) DEBUG_MSG_P(PSTR("Connect Failed\n"));
  102. else if (error == OTA_RECEIVE_ERROR) DEBUG_MSG_P(PSTR("Receive Failed\n"));
  103. else if (error == OTA_END_ERROR) DEBUG_MSG_P(PSTR("End Failed\n"));
  104. #endif
  105. });
  106. ArduinoOTA.begin();
  107. }