AutoConnectWithStaticIP.ino 2.2 KB

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