[SOLVED]esp8266 AND ifttt

Hey guys I am pretty much a noob with Arduino, I just started. I found a Tutorial about making a door sensor that would send me a notification to my phone when my door opens. It uses an ESP8266, a magnetic sensor the IFTTT app and some coding. The probelm is that it is not working. I am able to flash it on my ESP8266 with no problem. But I guess something is rong because when I open the sesnor I don't receive any notification.

Here is the code

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#define REED_SWITCH 5 //D1


int status = WL_IDLE_STATUS; 

const char* ssid = "******";
const char* password = "**********";
int doorClosed = 1;

void setup() {
  pinMode(REED_SWITCH, INPUT_PULLUP);

  Serial.begin(9600);

   WiFi.mode(WIFI_STA);
   status = WiFi.begin(ssid, password);
   Serial.print("Attempting to connect to SSID: ");
   Serial.println(ssid);

   while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");
    }
    Serial.println("Connected to wifi");
    get_http();

}

void loop() {

  while ((digitalRead(REED_SWITCH) == HIGH) && (doorClosed == 1))
    {
      Serial.println("DOOR OPEN!!");
      while (get_http() != 0);
      doorClosed = 0;
      break;
    }

}

 
int get_http()
{
   HTTPClient http;
   int ret = 0;
   Serial.print("[HTTP] begin...\n");

    http.begin("HERE IS MY IFTTT KEY************"); //HTTP

    Serial.print("[HTTP] begin...\n");

    int httpCode = http.GET();

    if(httpCode > 0) {

    Serial.printf("[HTTP] GET code: %d\n", httpCode);

      if(httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        Serial.println(payload);
      }
    } else {
        ret = -1;
        Serial.printf("[HTTP] GET failed, error: %s\n", http.errorToString(httpCode).c_str());
    }

    http.end();
    return ret;
}

Any help or advise is appreciated

Puzzled by the break; here

while ((digitalRead(REED_SWITCH) == HIGH) && (doorClosed == 1))
    {
      Serial.println("DOOR OPEN!!");
      while (get_http() != 0);
      doorClosed = 0;
      break;
    }

the main while loop is exited anyway, but on another note, what is the output of Serial ? does the switch work ?
and why are you not resetting variable doorClosed, like this it will only send a notification once.

Hey Deva_Rishi, thanks for helping, I really am new to this and I don't exactly understand what you mean by "Puzzled by the break; here " and "the main while loop is exited anyway"

Yes, I tested the switch and it works perfectly

MaxRobot:
Hey Deva_Rishi, thanks for helping, I really am new to this and I don't exactly understand what you mean by "Puzzled by the break; here "

well what is supposed to do ?

MaxRobot:
"the main while loop is exited anyway"

well 1 of the conditions is not longer met when doorClosed=0;

MaxRobot:
Yes, I tested the switch and it works perfectly

So what is the output on the Serial port/monitor ?

The main objective is that when the reed switch is opened it tells the ESP8266 that the door opened and then the ESP8266 uses IFTTT to send a web request and I also have the IFTTT app on my phone so it notifies me.

well 1 of the conditions is not longer met when doorClosed=0;

what am I supposed to do to fix it

So what is the output on the Serial port/monitor ?

Here is a photo of what happens on the Serial Port. When I close the reed switch it stops

MaxRobot:
The main objective is that when the reed switch is opened it tells the ESP8266 that the door opened and then the ESP8266 uses IFTTT to send a web request and I also have the IFTTT app on my phone so it notifies me.
what am I supposed to do to fix it

But you only send a notification once, OK, you program keeps trying until there is a succesful attempt, but if you then close the door and open it again, no new notification is sent, because doorClosed stays '0'

MaxRobot:
Capture hosted at ImgBB — ImgBB
Here is a photo of what happens on the Serial Port. When I close the reed switch it stops

Please attach the photograph to a post (then if you copy the link address you can add it as an image after.

Oh now i see what happens, it crashes . hmm it doesn't say the wdt reboots but i suppose that could be an issue as well (you are not resetting it during the attempts to send) actually code 2 is WDT reset so since ESP requires a call to yeild every 2.5s a long (possibly endless) loop should include a yield(); or delay(); (which includes yeild() ) long parts of code benefit from yeild() as well since it takes care of scheduled task and background processes. anyway. if you change while (get_http() != 0); towhile (get_http() != 0) yield(); you will probably be ok already then then function gets called within the condition and the call yield();

Ok so I just made the change

if you change
Code: [Select]

while (get_http() != 0);

to
Code: [Select]

while (get_http() != 0) yield();

you will probably be ok already then then function gets called within the condition and the call yield();

and now it does not work at all. Nothing even appears on the on the Serial Port/monitor.

and what is the baud rate on the monitor ?
'cause something should have shown up already anyway

Serial.begin(9600);

   WiFi.mode(WIFI_STA);
   status = WiFi.begin(ssid, password);
   Serial.print("Attempting to connect to SSID: ");
   Serial.println(ssid);

before loop() even starts.

The baud rate on the monitor is 115200 and nothing is showing up. Before, even though I didn't receive a notification, the light on the ESp starts blinking blue and something is hapening on the monitor. But when I change what you told me to change and I open the switch, the light doesn't blink and the monitor is blank.

I really apprectiate the fact that you really are trying to hep me
Thank you

MaxRobot:
The baud rate on the monitor is 115200 and nothing is showing up.

But in your sketch it is set to 9600, so either change the one or the other (115200 is the BAUD rate the the error terminal uses)

MaxRobot:
Before, even though I didn't receive a notification, the light on the ESp starts blinking blue

The led blinks every time the ESP is reset.

MaxRobot:
and something is hapening on the monitor.

It si spitting out error messages !

MaxRobot:
But when I change what you told me to change and I open the switch, the light doesn't blink and the monitor is blank.

Great the program is running and not rebooting or spitting out error messages, so change Serial.begin(9600);in your sketch to Serial.begin(115200); and messages should start to show again.
Then we will find out what is happening within the program, is there a successful http_get request (probably not) are you connected etc.

Allright so I just changed Serial.begin(9600); to Serial.begin(115200);
and here is what I get on the monitor

DOOR OPEN!!
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...

and it does not stop

We are making progress I guess :smiley:

MaxRobot:
We are making progress I guess :smiley:

Yep.. so maybe there is an error in the code here ?http.begin("HERE IS MY IFTTT KEY************"); //HTTPis it not a "https' ? or your code is incorrect (but i thought that wouldn't be it) or you are not connected to the internet. Anyway, what do you type in your browser to send a notification ?
Then we can worry about the next part of the code, somehow it gets stuck in trying to send the request, which i guess is fine but after a few tries it may as well give up, wait for the door to close and try again when it opens again. But let's focus on the sending of the notification. Btw i suppose it connected to your ssid ok ?

ep.. so maybe there is an error in the code here ?
Code: [Select]

http.begin("HERE IS MY IFTTT KEY************"); //HTTP

I have an actual link in here I just did not share it on the forum which is why I put the stars.

Here is the actual video that I used to do what I did so far

You should watch it it's 4min long and maybe that would help . I am not using exactly the code he shared in the description but I uses the one that he actually showed in his video.

Anyway, what do you type in your browser to send a notification ?

You'll see in the video how it works.

Tell me if it helps

MaxRobot:
I have an actual link in here I just did not share it on the forum which is why I put the stars.

I figured that much.

MaxRobot:
Here is the actual video that I used to do what I did so far
You should watch it it's 4min long and maybe that would help . I am not using exactly the code he shared in the description but I uses the one that he actually showed in his video.

Well i skimmed through it but i can't read the code so assuming that you copied it correctly and you are using the correct key, i'm stumped.

i'm stumped

Yes me too.
But there must be an error somewhere since the monitor shows

DOOR OPEN!!
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...
[HTTP] GET failed, error: connection refused
[HTTP] begin...
[HTTP] begin...

The error must be here :

http.begin("HERE IS MY IFTTT KEY************"); //HTTP

Just to make sure, it should be

http.begin("http://theaddress.pag/IFTTT-KEY"); //HTTP

(from Basic HTTPClient example)
or

    http.begin("http://user:password@192.168.1.12/test.html");

    /*
      // or
      http.begin("http://192.168.1.12/test.html");
      http.setAuthorization("user", "password");

      // or
      http.begin("http://192.168.1.12/test.html");
      http.setAuthorization("dXNlcjpwYXN3b3Jk");
    */

(from HTTPClient Authorization example )
Just to clarify it, what is in http.begin() must be the same as what you can type into browser as an address to send a notification to your phone, and the authorization is what could show up in the login pop-up window (if that exists)

I tried and when I type into my browser what I have in between http.begin() it send me a notification http.begin("https://maker.ifttt.com/trigger/door_opened/with/key/dPr3nmKyZ6e5_wbC0-xWgh"); //HTTP

Here is exactly what I have in between

http.begin()

"https://maker.ifttt.com/trigger/door_opened/with/key/dPr3nmKyZ6e5_wbC0-xWgh"

It's https ! i asked you before.
Then you'll need to get the fingerprint from the site and add it as an argument to the .begin() call

http.begin("https://maker.ifttt.com/trigger/door_opened/with/key/dPr3nmKyZ6e5_wbC0-xWgh","FINGERPRINT");

to get the fingerprint ,
go to iftt.com,
click on the little lock next to the IP-address in your browser,
click on certificate and then
on details, then
click on the thumbprint and copy those numbers (and spaces) and insert them where it says FINGERPRINT,
A fingerprint is subject to change for a site and that is a method for keeping it secure, there is a way to obtain the latest serial nr programmaticaly and you can find that here

Great! I am glad you have a solution. I’ll try when I get home and keep you updated. Hopefully it will work