AutoConnectWithStaticIP.ino 2.3 KB

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