AutoConnectWithStaticIP.ino 2.4 KB

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