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.

104 lines
2.6 KiB

  1. /*
  2. ARDUINO OTA MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if OTA_ARDUINOOTA_SUPPORT
  6. // TODO: allocate ArduinoOTAClass on-demand, stop using global instance
  7. void _arduinoOtaConfigure() {
  8. ArduinoOTA.setPort(OTA_PORT);
  9. ArduinoOTA.setHostname(getSetting("hostname").c_str());
  10. #if USE_PASSWORD
  11. ArduinoOTA.setPassword(getAdminPass().c_str());
  12. #endif
  13. ArduinoOTA.begin();
  14. }
  15. void _arduinoOtaLoop() {
  16. ArduinoOTA.handle();
  17. }
  18. void _arduinoOtaOnStart() {
  19. // Disabling EEPROM rotation to prevent writing to EEPROM after the upgrade
  20. eepromRotate(false);
  21. // Because ArduinoOTA is synchronous, force backup right now instead of waiting for the next loop()
  22. eepromBackup(0);
  23. DEBUG_MSG_P(PSTR("[OTA] Start\n"));
  24. #if WEB_SUPPORT
  25. wsSend_P(PSTR("{\"message\": 2}"));
  26. #endif
  27. }
  28. void _arduinoOtaOnEnd() {
  29. DEBUG_MSG_P(PSTR("\n"));
  30. DEBUG_MSG_P(PSTR("[OTA] Done, restarting...\n"));
  31. #if WEB_SUPPORT
  32. wsSend_P(PSTR("{\"action\": \"reload\"}"));
  33. #endif
  34. // Note: ArduinoOTA will reset the board after this callback returns.
  35. customResetReason(CUSTOM_RESET_OTA);
  36. nice_delay(100);
  37. }
  38. void _arduinoOtaOnProgress(unsigned int progress, unsigned int total) {
  39. // Removed to avoid websocket ping back during upgrade (see #1574)
  40. // TODO: implement as separate from debugging message
  41. #if WEB_SUPPORT
  42. if (wsConnected()) return;
  43. #endif
  44. static unsigned int _progOld;
  45. unsigned int _prog = (progress / (total / 100));
  46. if (_prog != _progOld) {
  47. DEBUG_MSG_P(PSTR("[OTA] Progress: %u%%\r"), _prog);
  48. _progOld = _prog;
  49. }
  50. }
  51. void _arduinoOtaOnError(ota_error_t error) {
  52. #if DEBUG_SUPPORT
  53. DEBUG_MSG_P(PSTR("\n[OTA] Error #%u: "), error);
  54. if (error == OTA_AUTH_ERROR) DEBUG_MSG_P(PSTR("Auth Failed\n"));
  55. else if (error == OTA_BEGIN_ERROR) DEBUG_MSG_P(PSTR("Begin Failed\n"));
  56. else if (error == OTA_CONNECT_ERROR) DEBUG_MSG_P(PSTR("Connect Failed\n"));
  57. else if (error == OTA_RECEIVE_ERROR) DEBUG_MSG_P(PSTR("Receive Failed\n"));
  58. else if (error == OTA_END_ERROR) DEBUG_MSG_P(PSTR("End Failed\n"));
  59. #endif
  60. eepromRotate(true);
  61. }
  62. void arduinoOtaSetup() {
  63. espurnaRegisterLoop(_arduinoOtaLoop);
  64. espurnaRegisterReload(_arduinoOtaConfigure);
  65. ArduinoOTA.onStart(_arduinoOtaOnStart);
  66. ArduinoOTA.onEnd(_arduinoOtaOnEnd);
  67. ArduinoOTA.onError(_arduinoOtaOnError);
  68. ArduinoOTA.onProgress(_arduinoOtaOnProgress);
  69. _arduinoOtaConfigure();
  70. }
  71. #endif // OTA_ARDUINOOTA_SUPPORT