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.

87 lines
2.3 KiB

3 years ago
  1. #include <WiFi.h>
  2. #include <ESPmDNS.h>
  3. #include <WiFiUdp.h>
  4. #include <ArduinoOTA.h>
  5. #include "config.h"
  6. void APConnect(){
  7. Serial.println("Configuring access point...");
  8. //WiFi.mode(WIFI_AP);
  9. WiFi.softAP(ssid, pass);
  10. Serial.println("Wait 100 ms for AP_START...");
  11. delay(100);
  12. Serial.println("Set softAPConfig");
  13. IPAddress Ip(192, 168, 10, 23);
  14. IPAddress NMask(255, 255, 255, 0);
  15. WiFi.softAPConfig(Ip, Ip, NMask);
  16. IPAddress myIP = WiFi.softAPIP();
  17. Serial.print("AP IP address: ");
  18. Serial.println(myIP);
  19. }
  20. void WiFiConnect(){
  21. Serial.println("Booting");
  22. WiFi.mode(WIFI_STA);
  23. WiFi.begin(ssidW, passwordW);
  24. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  25. Serial.println("Connection Failed! Rebooting...");
  26. delay(5000);
  27. ESP.restart();
  28. }
  29. }
  30. void setupOTA()
  31. {
  32. Serial.begin(115200);
  33. // Port defaults to 3232
  34. ArduinoOTA.setPort(3232);
  35. // Hostname defaults to esp3232-[MAC]
  36. ArduinoOTA.setHostname("ENCSYS");
  37. // No authentication by default
  38. // ArduinoOTA.setPassword("admin");
  39. // Password can be set with it's md5 value as well
  40. // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  41. // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
  42. ArduinoOTA
  43. .onStart([]() {
  44. String type;
  45. if (ArduinoOTA.getCommand() == U_FLASH)
  46. type = "sketch";
  47. else // U_SPIFFS
  48. type = "filesystem";
  49. // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
  50. Serial.println("Start updating " + type);
  51. })
  52. .onEnd([]() {
  53. Serial.println("\nEnd");
  54. })
  55. .onProgress([](unsigned int progress, unsigned int total) {
  56. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  57. })
  58. .onError([](ota_error_t error) {
  59. Serial.printf("Error[%u]: ", error);
  60. if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  61. else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  62. else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  63. else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  64. else if (error == OTA_END_ERROR) Serial.println("End Failed");
  65. });
  66. ArduinoOTA.begin();
  67. Serial.println("Ready");
  68. Serial.print("IP address: ");
  69. Serial.println(WiFi.localIP());
  70. }