Since I am doing a project that I want to use a physical button to connect the NodeMCU when it is connecting the internet. So when I press the physical button, it will call the ifttt trigger to do sending mail etc.
But now I facing a problem. When avoiding the hard code of ssid and wifi password for connect the internet, it dose not successful.
When I am try to do the hard code of ssid and the password for the whole action, it runs as well (using the below function connectToWifi()). But when I am trying to use wifiManager, It seems that not work.
At the below have some capture screen.
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h> //https://github.com/kentaylor/WiFiManager
#include <IFTTTWebhook.h>
// Onboard LED I/O pin on NodeMCU board
const int PIN_LED = 2; // D4 on NodeMCU and WeMos. Controls the onboard LED.
int wakePin = 16;
//const char* ssid="";
//const char* password="";
const char* resource = "/trigger/ xxxxx";
const char* iftttserver = "maker.ifttt.com";
void setup() {
// put your setup code here, to run once:
// initialize the LED digital pin as an output.
pinMode(PIN_LED, OUTPUT);
Serial.begin(115200);
while (!Serial) {
}
Serial.println(" ");// print an empty line before and after Button Press
Serial.println("Button Pressed");
Serial.println(" ");
//connectToWifi();
Serial.println("\n Starting");
unsigned long startedAt = millis();
WiFi.printDiag(Serial); //Remove this line if you do not want to see WiFi password printed
Serial.println("Opening configuration portal");
digitalWrite(PIN_LED, LOW); // turn the LED on by making the voltage LOW to tell us we are in configuration mode.
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//sets timeout in seconds until configuration portal gets turned off.
//If not specified device will remain in configuration mode until
//switched off via webserver.
if (WiFi.SSID()!="") wifiManager.setConfigPortalTimeout(60); //If no access point name has been previously entered disable timeout.
//it starts an access point
//and goes into a blocking loop awaiting configuration
if (!wifiManager.startConfigPortal("ESP8266","password")) //Delete these two parameters if you do not want a WiFi password on your configuration access point
{
Serial.println("Not connected to WiFi but continuing anyway.");
}
else
{
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
digitalWrite(PIN_LED, HIGH); // Turn led off as we are not in configuration mode.
// For some unknown reason webserver can only be started once per boot up
// so webserver can not be used again in the sketch.
Serial.print("After waiting ");
int connRes = WiFi.waitForConnectResult();
float waited = (millis()- startedAt);
Serial.print(waited/1000);
Serial.print(" secs in setup() connection result is ");
Serial.println(connRes);
if (WiFi.status()!=WL_CONNECTED)
{
Serial.println("failed to connect, finishing setup anyway");
}
else
{
Serial.print("local ip: ");
Serial.println(WiFi.localIP());
}
makeIFTTTRequest();
ESP.deepSleep(wakePin);
}
/*
void connectToWifi() {
Serial.print("Connecting to: NAME "); //uncomment next line to show SSID name
//Serial.print(ssid);
WiFi.begin(ssid, password);
Serial.println(" ");
Serial.print("Attempting to connect: ");
//try to connect for 10 seconds
int i = 10;
while (WiFi.status() != WL_CONNECTED && i >= 0) {
delay(1000);
Serial.print(i);
Serial.print(", ");
i--;
}
Serial.println(" ");
//print connection result
if (WiFi.status() == WL_CONNECTED) {
Serial.print("Connected.");
Serial.println(" ");
Serial.print("NodeMCU ip address: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("Connection failed - check your credentials or connection");
}
}
*/
void makeIFTTTRequest() {
Serial.print("Connecting to ");
Serial.print(iftttserver);
WiFiClient client;
int retries = 5;
while (!!!client.connect(iftttserver, 80) && (retries-- > 0)) {
Serial.print(".");
}
Serial.println();
if (!!!client.connected()) {
Serial.println("Failed to connect, going back to sleep");
}
Serial.print("Request resource: ");
Serial.println(resource);
client.print(String("GET ") + resource +
" HTTP/1.1\r\n" +
"Host: " + iftttserver + "\r\n" +
"Connection: close\r\n\r\n");
int timeout = 5 * 10; // 5 seconds
while (!!!client.available() && (timeout-- > 0)) {
delay(100);
}
if (!!!client.available()) {
Serial.println("No response, going back to sleep");
}
while (client.available()) {
Serial.write(client.read());
}
Serial.println("\nclosing connection");
client.stop();
}
void loop() {
delay(10000);
// put your main code here, to run repeatedly:
}