Guest User

https://www.instructables.com/id/ESP8266-and-ESP32-With-WiFi

a guest
Jun 26th, 2020
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #if defined(ESP8266)
  2. #include <ESP8266WiFi.h> //ESP8266 Core WiFi Library
  3. #else
  4. #include <WiFi.h> //ESP32 Core WiFi Library
  5. #endif
  6.  
  7.  
  8. #if defined(ESP8266)
  9. #include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
  10. #else
  11. #include <WebServer.h> //Local DNS Server used for redirecting all requests to the configuration portal ( https://github.com/zhouhan0126/DNSServer---esp32 )
  12. #endif
  13.  
  14. #include <DNSServer.h> //Local WebServer used to serve the configuration portal ( https://github.com/zhouhan0126/DNSServer---esp32 )
  15. #include <WiFiManager.h> // WiFi Configuration Magic ( https://github.com/zhouhan0126/DNSServer---esp32 ) >> https://github.com/zhouhan0126/DNSServer---esp32 (ORIGINAL)
  16.  
  17. const int PIN_AP = 2;
  18. void setup() {
  19. Serial.begin(9600);
  20. pinMode(PIN_AP, INPUT);
  21. //declaração do objeto wifiManager
  22. WiFiManager wifiManager;
  23.  
  24. //utilizando esse comando, as configurações são apagadas da memória
  25. //caso tiver salvo alguma rede para conectar automaticamente, ela é apagada.
  26. // wifiManager.resetSettings();
  27.  
  28. //callback para quando entra em modo de configuração AP
  29. wifiManager.setAPCallback(configModeCallback);
  30. //callback para quando se conecta em uma rede, ou seja, quando passa a trabalhar em modo estação
  31. wifiManager.setSaveConfigCallback(saveConfigCallback);
  32.  
  33. //cria uma rede de nome ESP_AP com senha 12345678
  34. wifiManager.autoConnect("ESP_AP", "12345678");
  35. }
  36.  
  37. void loop() {
  38.  
  39. WiFiManager wifiManager;
  40. //se o botão foi pressionado
  41. if ( digitalRead(PIN_AP) == HIGH ) {
  42. Serial.println("resetar"); //tenta abrir o portal
  43. if(!wifiManager.startConfigPortal("ESP_AP", "12345678") ){
  44. Serial.println("Falha ao conectar");
  45. delay(2000);
  46. ESP.restart();
  47. delay(1000);
  48. }
  49. Serial.println("Conectou ESP_AP!!!");
  50. }
  51. }
  52.  
  53. //callback que indica que o ESP entrou no modo AP
  54. void configModeCallback (WiFiManager *myWiFiManager) {
  55. // Serial.println("Entered config mode");
  56. Serial.println("Entrou no modo de configuração");
  57. Serial.println(WiFi.softAPIP()); //imprime o IP do AP
  58. Serial.println(myWiFiManager->getConfigPortalSSID());
  59. }
  60.  
  61. void saveConfigCallback () {
  62. // Serial.println("Should save config");
  63. Serial.println("Configuração salva");
  64. Serial.println(WiFi.softAPIP());
  65. }
Add Comment
Please, Sign In to add comment