_otaServer.ino 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // 1-channel LoRa Gateway for ESP8266
  2. // Copyright (c) 2016, 2017, 2018, 2019 Maarten Westenberg version for ESP8266
  3. //
  4. //
  5. // All rights reserved. This program and the accompanying materials
  6. // are made available under the terms of the MIT License
  7. // which accompanies this distribution, and is available at
  8. // https://opensource.org/licenses/mit-license.php
  9. //
  10. // NO WARRANTY OF ANY KIND IS PROVIDED
  11. //
  12. // Author: Maarten Westenberg (mw12554@hotmail.com)
  13. //
  14. // This file contains the ota code for the ESP Single Channel Gateway.
  15. // Provide OTA server funcionality so the 1ch gateway can be updated
  16. // over the air.
  17. // This code uses the ESPhttpServer functions to update the gateway.
  18. #if A_OTA==1
  19. //extern ArduinoOTAClass ArduinoOTA;
  20. // Make sure that webserver is running before continuing
  21. // ----------------------------------------------------------------------------
  22. // setupOta
  23. // Function to run in the setup() function to initialise the update function
  24. // ----------------------------------------------------------------------------
  25. void setupOta(char *hostname) {
  26. ArduinoOTA.begin();
  27. # if _MONITOR>=1
  28. mPrint("setupOta:: Started");
  29. # endif //_MONITOR
  30. // Hostname defaults to esp8266-[ChipID] for ESP8266 nodes
  31. ArduinoOTA.setHostname(hostname);
  32. ArduinoOTA.onStart([]() {
  33. String type;
  34. // XXX version mismatch of platform.io and ArduinoOtaa
  35. // see https://github.com/esp8266/Arduino/issues/3020
  36. //if (ArduinoOTA.getCommand() == U_FLASH)
  37. type = "sketch";
  38. //else // U_SPIFFS
  39. // type = "filesystem";
  40. // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
  41. Serial.println("Start updating " + type);
  42. });
  43. ArduinoOTA.onEnd([]() {
  44. Serial.println("\nEnd");
  45. });
  46. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  47. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  48. });
  49. ArduinoOTA.onError([](ota_error_t error) {
  50. Serial.printf("Error[%u]: ", error);
  51. if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  52. else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  53. else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  54. else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  55. else if (error == OTA_END_ERROR) Serial.println("End Failed");
  56. });
  57. # if _MONITOR>=1
  58. if (debug>=1) {
  59. mPrint("Ready IP address: " + String(WiFi.localIP().toString()));
  60. }
  61. # endif //_MONITOR
  62. // Only if the Webserver is active also
  63. #if A_SERVER==2 // Displayed for the moment
  64. ESPhttpUpdate.rebootOnUpdate(false);
  65. server.on("/esp", HTTP_POST, [&](){
  66. HTTPUpdateResult ret = ESPhttpUpdate.update(server.arg("firmware"), "1.0.0");
  67. switch(ret) {
  68. case HTTP_UPDATE_FAILED:
  69. //PREi::sendJSON(500, "Update failed.");
  70. Serial.println(F("Update failed"));
  71. break;
  72. case HTTP_UPDATE_NO_UPDATES:
  73. //PREi::sendJSON(304, "Update not necessary.");
  74. Serial.println(F("Update not necessary"));
  75. break;
  76. case HTTP_UPDATE_OK:
  77. //PREi::sendJSON(200, "Update started.");
  78. Serial.println(F("Update started"));
  79. ESP.restart();
  80. break;
  81. default:
  82. Serial.println(F("setupOta:: Unknown ret="));
  83. }
  84. });
  85. #endif
  86. }
  87. // ----------------------------------------------------------------------------
  88. // updateOtaa()
  89. //
  90. // ----------------------------------------------------------------------------
  91. void updateOtaa() {
  92. String response="";
  93. Serial.print(F("updateOtaa:: <unimplemented> IP="));
  94. printIP((IPAddress)WiFi.localIP(),'.',response);
  95. Serial.println(response);
  96. // ESPhttpUpdate.update(response, 80, "/arduino.bin");
  97. }
  98. #endif