2

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
  • 1
    You need to establish a physical connection (i.e. connect to WiFi) before you establish a TCP client. WiFi connection could take up as long as 6-10s, so you need to loop and check `WiFi.status()`. – hcheung Aug 21 '22 at 10:59
  • hi, first of all thank you for you help but i am new to esp and don't quite get what you mean, do you mind briefing about it a little? what is meant by physical connection and there is no such function as Wifi.status() in libraries which i am using, if it is from some other library please can you name it? Thanks again – Nuceo Is Bored Aug 21 '22 at 11:30
  • Look at [examples showing how to use WiFi](https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino). You're doing it incorrectly, as @hcheung pointed out - it takes time to connect to wifi, your code isn't waiting for a successful connection. If `WiFi.status()` doesn't exist then something is very broken. Capitalization matters, it's `WiFi`, not `Wifi`. – romkey Aug 21 '22 at 19:35

1 Answers1

0

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...
    }
}
Dr.Random
  • 430
  • 3
  • 16