Advanced.ino 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * WiFiManager advanced demo, contains advanced configurartion options
  3. * Implements TRIGGEN_PIN button press, press for ondemand configportal, hold for 3 seconds for reset settings.
  4. */
  5. #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
  6. #define TRIGGER_PIN 0
  7. WiFiManager wm; // global wm instance
  8. WiFiManagerParameter custom_field; // global param ( for non blocking w params )
  9. void setup() {
  10. WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
  11. Serial.begin(115200);
  12. Serial.setDebugOutput(true);
  13. delay(3000);
  14. Serial.println("\n Starting");
  15. pinMode(TRIGGER_PIN, INPUT);
  16. // wm.resetSettings(); // wipe settings
  17. // add a custom input field
  18. int customFieldLength = 40;
  19. // new (&custom_field) WiFiManagerParameter("customfieldid", "Custom Field Label", "Custom Field Value", customFieldLength,"placeholder=\"Custom Field Placeholder\"");
  20. // test custom html input type(checkbox)
  21. // new (&custom_field) WiFiManagerParameter("customfieldid", "Custom Field Label", "Custom Field Value", customFieldLength,"placeholder=\"Custom Field Placeholder\" type=\"checkbox\""); // custom html type
  22. // test custom html(radio)
  23. const char* custom_radio_str = "<br/><label for='customfieldid'>Custom Field Label</label><input type='radio' name='customfieldid' value='1' checked> One<br><input type='radio' name='customfieldid' value='2'> Two<br><input type='radio' name='customfieldid' value='3'> Three";
  24. new (&custom_field) WiFiManagerParameter(custom_radio_str); // custom html input
  25. wm.addParameter(&custom_field);
  26. wm.setSaveParamsCallback(saveParamCallback);
  27. // custom menu via array or vector
  28. //
  29. // menu tokens, "wifi","wifinoscan","info","param","close","sep","erase","restart","exit" (sep is seperator) (if param is in menu, params will not show up in wifi page!)
  30. // const char* menu[] = {"wifi","info","param","sep","restart","exit"};
  31. // wm.setMenu(menu,6);
  32. std::vector<const char *> menu = {"wifi","info","param","sep","restart","exit"};
  33. wm.setMenu(menu);
  34. // set dark theme
  35. wm.setClass("invert");
  36. //set static ip
  37. // wm.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0)); // set static ip,gw,sn
  38. // wm.setShowStaticFields(true); // force show static ip fields
  39. // wm.setShowDnsFields(true); // force show dns field always
  40. // wm.setConnectTimeout(20); // how long to try to connect for before continuing
  41. wm.setConfigPortalTimeout(30); // auto close configportal after n seconds
  42. // wm.setCaptivePortalEnable(false); // disable captive portal redirection
  43. // wm.setAPClientCheck(true); // avoid timeout if client connected to softap
  44. // wifi scan settings
  45. // wm.setRemoveDuplicateAPs(false); // do not remove duplicate ap names (true)
  46. // wm.setMinimumSignalQuality(20); // set min RSSI (percentage) to show in scans, null = 8%
  47. // wm.setShowInfoErase(false); // do not show erase button on info page
  48. // wm.setScanDispPerc(true); // show RSSI as percentage not graph icons
  49. // wm.setBreakAfterConfig(true); // always exit configportal even if wifi save fails
  50. bool res;
  51. // res = wm.autoConnect(); // auto generated AP name from chipid
  52. // res = wm.autoConnect("AutoConnectAP"); // anonymous ap
  53. res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
  54. if(!res) {
  55. Serial.println("Failed to connect or hit timeout");
  56. // ESP.restart();
  57. }
  58. else {
  59. //if you get here you have connected to the WiFi
  60. Serial.println("connected...yeey :)");
  61. }
  62. }
  63. void checkButton(){
  64. // check for button press
  65. if ( digitalRead(TRIGGER_PIN) == LOW ) {
  66. // poor mans debounce/press-hold, code not ideal for production
  67. delay(50);
  68. if( digitalRead(TRIGGER_PIN) == LOW ){
  69. Serial.println("Button Pressed");
  70. // still holding button for 3000 ms, reset settings, code not ideaa for production
  71. delay(3000); // reset delay hold
  72. if( digitalRead(TRIGGER_PIN) == LOW ){
  73. Serial.println("Button Held");
  74. Serial.println("Erasing Config, restarting");
  75. wm.resetSettings();
  76. ESP.restart();
  77. }
  78. // start portal w delay
  79. Serial.println("Starting config portal");
  80. wm.setConfigPortalTimeout(120);
  81. if (!wm.startConfigPortal("OnDemandAP","password")) {
  82. Serial.println("failed to connect or hit timeout");
  83. delay(3000);
  84. // ESP.restart();
  85. } else {
  86. //if you get here you have connected to the WiFi
  87. Serial.println("connected...yeey :)");
  88. }
  89. }
  90. }
  91. }
  92. String getParam(String name){
  93. //read parameter from server, for customhmtl input
  94. String value;
  95. if(wm.server->hasArg(name)) {
  96. value = wm.server->arg(name);
  97. }
  98. return value;
  99. }
  100. void saveParamCallback(){
  101. Serial.println("[CALLBACK] saveParamCallback fired");
  102. Serial.println("PARAM customfieldid = " + getParam("customfieldid"));
  103. }
  104. void loop() {
  105. checkButton();
  106. // put your main code here, to run repeatedly:
  107. }