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