I am trying to make a simple project where the esp8266 sends an sms to my phone using IFTTT. I have tested my IFTTT applet/recipe and it works fine. I thought it might be a connection issue with my wifi, i have found that to not be the problem too. So I checked for a connection issue with the host and that seems to be the problem. It prints connection failed according to this piece of code:
if (.client,connect(host, httpsPort)) {
Serial.println("connection failed");
return;
Can someone please help me with this problem. Thank you
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = "ssid";
const char* password = "pass";
const char* host = "maker.ifttt.com";
const int httpsPort = 443;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
WiFiClientSecure client;
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
String url = "/trigger/ESP8266/json/with/key/xxxxxxxxxx";
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
}
void loop() {
}```
[1]: https://i.stack.imgur.com/RkQqL.png
You must wait for the board to connect to your wifi network before you can initiate an http call.
Here is an example for connecting to the wifi:
#define WIFI_TIMEOUT 5000
#define WIFI_DEBUG true
/*
This function will wait for a wifi to connect.
If the connection takes more then WIFI_TIMEOUT it will return
false, otherwise returns true.
*/
boolean connectWifi(){
WiFi.begin(ssid, password);
long wifiConnStartMS = millis();
#if WIFI_DEBUG
Serial.printf("\nConnecting to: %s...",ssid);
#endif
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(200);
if( millis() - wifiConnStartMS >= WIFI_TIMEOUT ){
#if WIFI_DEBUG
Serial.println("\nTimeout on wifi connection. Wrong credentials?");
#endif
return false;
}
}
#if WIFI_DEBUG
Serial.printf("\nWiFi Connected to: %s\n",ssid);
#endif
return true;
}
void setup(){
Serial.begin(115200);
if( connectWifi() ){
// Begin the call after that...
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.