AutoConnectWithStaticIP.ino 2.5 KB

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