AutoConnectWithStaticIP.ino 2.2 KB

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