AutoConnectWithStaticIP.ino 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <FS.h> //this needs to be first, or it all crashes and burns...
  2. #if defined(ESP8266)
  3. #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
  4. #else
  5. #include <WiFi.h>
  6. #endif
  7. //needed for library
  8. #include <ESPAsyncWebServer.h>
  9. #include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager
  10. AsyncWebServer server(80);
  11. DNSServer dns;
  12. /**************************************************************************************
  13. * this example shows how to set a static IP configuration for the ESP
  14. * although the IP shows in the config portal, the changes will revert
  15. * to the IP set in the source file.
  16. * if you want the ability to configure and persist the new IP configuration
  17. * look at the FS examples, which save the config to file
  18. *************************************************************************************/
  19. //default custom static IP
  20. //char static_ip[16] = "10.0.1.59";
  21. //char static_gw[16] = "10.0.1.1";
  22. //char static_sn[16] = "255.255.255.0";
  23. void setup() {
  24. // put your setup code here, to run once:
  25. Serial.begin(115200);
  26. Serial.println();
  27. //WiFiManager
  28. //Local intialization. Once its business is done, there is no need to keep it around
  29. AsyncWiFiManager wifiManager(&server,&dns);
  30. //reset settings - for testing
  31. //wifiManager.resetSettings();
  32. //set static ip
  33. //the commented bit only works for ESP8266 core 2.1.0 or newer
  34. /*IPAddress _ip,_gw,_sn;
  35. _ip.fromString(static_ip);
  36. _gw.fromString(static_gw);
  37. _sn.fromString(static_sn);
  38. */
  39. IPAddress _ip = IPAddress(10, 0, 1, 78);
  40. IPAddress _gw = IPAddress(10, 0, 1, 1);
  41. IPAddress _sn = IPAddress(255, 255, 255, 0);
  42. wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn);
  43. //tries to connect to last known settings
  44. //if it does not connect it starts an access point with the specified name
  45. //here "AutoConnectAP" with password "password"
  46. //and goes into a blocking loop awaiting configuration
  47. if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
  48. Serial.println("failed to connect, we should reset as see if it connects");
  49. delay(3000);
  50. ESP.reset();
  51. delay(5000);
  52. }
  53. //if you get here you have connected to the WiFi
  54. Serial.println("connected...yeey :)");
  55. Serial.println("local ip");
  56. Serial.println(WiFi.localIP());
  57. }
  58. void loop() {
  59. // put your main code here, to run repeatedly:
  60. }