Introduction: The Cat Has Left the Building - ESP8266 PIR Monitor

Picture of The Cat Has Left the Building - ESP8266 PIR Monitor

ESP8266 PIR Motion Detector to IFTTT

Or The Cat has left the Building!


UPDATED 10/2017 -Instructions for new IFTTT procedures. Corrected for changes to the IFTTT website.


Also: There is another version of this project to use ESP8266 Deep Sleep mode to significantly save battery power. BUT requires extra hardware, so more complex. See https://github.com/rgrokett/ESP8266_PIRv2

Overview

We just installed a Cat Door in our garage and I wanted to see how many times per day (actually night) our cat went in and out the door. We could tell the cat was using the door as we would find it outside sometimes and inside sometimes. For this project, I skipped the camera that other projects used and decided to try IFTTT (If This Then That) free service to send emails when detecting motion.

So, breaking out my parts box, I found a PIR motion sensor, an ESP8266 and a 5V battery.

I used the Adafruit HUZZAH ESP8266 as it has 5V regulator for powering the 3.3v ESP as well as good tutorials for initially getting it set up. I also used the Arduino IDE with the ESP8266 library, since I was already quite familiar with using it with the Huzzah ESP8266.

I decided to interface this to IFTTT (www.ifttt.com) to trigger any number of types of events. Initially, just an email each time motion was detected.

Note that IFTTT requires HTTPS SSL encryption. Thus this project includes code for that.

Step 1: PARTS LIST

Parts List

Important Initial Setup of ESP8266

Before beginning the project, you should become familiar with the Adafruit HUZZAH board and programming it using the Arduino IDE. The best way is to use the excellent Adafruit tutorial:

https://learn.adafruit.com/adafruit-huzzah-esp8266...

You must be able to program your ESP8266 and connect wirelessly to it via browser as described in their tutorial. Once completed, THEN continue below to next step...

Step 2: IFTTT Setup

Picture of IFTTT Setup

1. Go to www.ifttt.com

2. Log in. If you don’t have an account, you can sign up. It’s free.

3. Once logged in, click on My Applets

4. Click on New Applet

5. Click the “this” of ifthisthenthat…

6. Type “Webhooks” into the Search services box

7. Click on the Webhooks icon

8. For Choose a Trigger, there is only one big gray box with “Receive a web Request”. Click it

9. For Complete Trigger Fields, enter “pirtrigger” and click Create. This is the event name used in the ESP8266 .ino software. They must match.

10. Click the “that” of ifthisthenthat…

11. Type “email” into the Search Channels box. You could change this to do other things, like send SMS messages, etc. But let’s stick to email. You can always edit later.

14. For Choose an Action, click the big gray box with “Send me an email”.

15. Click the big blue box with “Send me an email”.

16. You can edit the text or just leave it as is. Some of the fields aren’t used, but they just show up blank in your email.

17. Click Create Action. You will see a screen that just describes what this recipe will do. You can edit the Recipe Title, or just leave as is. Click FINISH.

18. NOTE that it uses the email address you entered when you signed up for IFTTT.

19. You now have an IFTTT Recipe.

Any IFTTT Recipe that uses Maker channel can be used, as long as it is called “pirtrigger”. (You can change the trigger name in the ESP8266_PIR.ino program below, if desired.)


IFTTT Maker URL

You will need Maker URL assigned by IFTTT in order to send from ESP8266 to IFTTT.

1. Click on your name in the upper right corner of the screen and select Services.

2. Again, click on the Webhooks icon.

3. Click on Settings button.

4. On the Maker Settings screen, you need to copy the api key portion of the “URL:” field.

Example: https://maker.ifttt.com/use/aBc1fakekey2ab3cBA

It is needed for the .ino program later on.

Step 3: Software

You should program and test the ESP8266 before adding its hardware wiring.

1. STOP! Be sure you have already completed the Adafruit tutorial software setup of the Arduino IDE and tested the ESP8266 with your WiFi network as described in the Initial Setup section above!

2. Ok, download the ESP8266_PIR software from GitHub (https://github.com/rgrokett/ESP8266_PIR/)

3. Copy the ESP8266_PIR subdirectory into your Arduino IDE development directory.

This folder has the 3 software files needed.

<p>ESP8266_PIR.ino<br>HTTPSRedirect.h
HTTPSRedirect.cpp</p>

4. Double-click the ESP8266_PIR.ino program to load it into your Arduino IDE.

5. Using Arduino IDE, edit the ESP8266_PIR.ino and insert your WiFi SSID and PASSWORD into the appropriate places.

6. Update the API_KEY with your IFTTT API KEY copied previously. You can look on Channels -> Search Channels -> Maker in IFTTT for it, if needed.

7. You can also change a few variables, described here:

<p>const char* ssid     = "{YOUR_WIFI_SSID}"; // Your WiFi SSID<br>const char* password = "{YOUR_WIFI_PWD}";  // Your WiFi Password
const char* api_key  = "aBc1fakekey2ab3cBA";   // Your API KEY from <a href="https://ifttt.com/maker" rel="nofollow"> https://ifttt.com/maker
</a>
const char* event    = "pirtrigger";       // Your IFTTT Event Name
bool verifyCert = false; // Select true if you want SSL certificate validation
int PIRpin = 14;      // GPIO 14 (PIR Sensor)
int MOTION_DELAY = 15; // Delay in seconds between events to keep from flooding IFTTT & emails</p>

IFTTT requires HTTPS SSL and HTTPS 302 Redirection. The ESP8266 Library (installed in the Adafruit Tutorial) contains the HTTPS SSL functions and an extension of that library was developed by https://github.com/electronicsguy/ESP8266/tree/ma... to handle the HTTPS 302 Redirection.

Since this code wasn’t in the ESP8266 Library, I have included a copy or you can get the latest version from the URL above and add the .cpp and .h files to the ESP8266_PIR folder.

The IFTTT server initially returns a 302 Redirect message back, so the “HTTPSRedirect.cpp” software invisibly handles resending the request to the new host.

OPTIONAL

The “WiFiClientSecure” provides SSL encryption, so the messages are always sent encrypted, but by default the verification of IFTTT’s SSL Certificate is turned off. You can turn it on by changing verifyCert = true;

Doing this requires the SHA1 Fingerprint of the IFTTT server to be used to verify the certificate.

<p>const char* SHA1Fingerprint="A9 81 E1 35 B3 7F 81 B9 87 9D 11 DD 48 55 43 2C 8F C3 EC 87";</p>

This fingerprint is initially retrieved from the IFTTT server using the Linux command:

<p>$ openssl s_client -servername maker.ifttt.com -connect maker.ifttt.com:443 | openssl x509 -fingerprint -noout</p>

Replace colons with spaces in result and update the ESP8266_PIR.ino as needed.

NOTE: You should NOT have to change this unless IFTTT changes their SSL Certificate.

Again, you can bypass all of this check by leaving verifyCert = false; The data is still encrypted.

CONTINUE HERE

8. Compile and Upload the program using the FTDI or USB console cable just like shown in the Adafruit tutorial. Remember, you have to press the tiny GPIO0 and RESET buttons on the HUZZAH ESP8266 (aka bootload mode) to allow the upload to occur.

9. When the program finishes loading, open a Serial Monitor, set to 115,200 baud, and press the ESP8266 RESET button to restart the program running.

10. It should display the IP address in the Serial Monitor once connected to your Wifi.

Also, the onboard Red LED should blink 4 times signifying it’s successfully connected. If you don't get a connection, be sure your SSID/PASSWD in the ESP8266_PIR.ino has been updated and uploaded.

11. Time for wiring the PIR and power…

Step 4: Hardware - PIR

Picture of Hardware - PIR

1. Temporarily unplug the FTDI/USB cable from the PC to power off the ESP8266.

2. Wire the PIR sensor as follows. Note that the PIR is powered by 5V, but its I/O line is 3.3v which makes it directly compatible with the ESP8266’s 3.3v GPIO pins.

(See Pinout diagram)

3. With the PIR now wired in, reconnect the FTDI/USB cable to the PC.

4. Again, start the Serial Monitor from the Arduino IDE.

5. Reset the ESP8266 and you should see the LED blink 4 times and the IP address displayed again.

6. If you move in front of the PIR, the Serial Monitor should register the event and send off to IFTTT. If IFTT trigger is successful, you should see a 200 OK HTTP response message and text and receive an email.

<p>< HTTP/1.1 200 OK<br>< Server: Cowboy
< Connection: keep-alive
< X-Powered-By: Sad Unicorns
< X-Top-Secrettt: VG9vIGVhc3k/IElmIHlvdSBFK3.../NlY3JldEBlIHdnQgTWFrZXJzLg==
< Content-Type: text/html; charset=utf-8
< Content-Length: 50
< Etag: W/"32-44d0098f"
< Date: Wed, 29 Jun 2016 21:25:32 GMT
< Via: 1.1 vegur
< 
* Connection #0 to host maker.ifttt.com left intact
* Closing connection #0
* SSLv3, TLS alert, Client hello (1):
Congratulations! You've fired the pirtrigger event</p>

Step 5: Hardware - Power

Picture of Hardware - Power

1. Since the Adafruit HUZZAH ESP8266 supports operating on 5V, a 5V USB battery or USB power supply can be used. Since we tied the PIR sensor directly to one of the V+ pins, the battery or power supply is going to have to connect to the V+ and GND that the FTDI/USB cable connected to. So if you need to reprogram the ESP8266, you will need to disconnect these.

(See Cable Pinout diagrams)

You may need to build a USB-to-ESP cable by taking an unused USB cable and cutting it as shown in the cable diagram above.

2. Once ready, plug in the USB connector to your power supply or battery.

3. After a few seconds, you should see the red LED blink four times to indicate it connected to your WiFi.

4. Do some motion and after a minute or so, an email should arrive!

Place everything into a box. The PIR sensor will work thru clear plastic, but is more sensitive if mounted outside. Put the box near the cat door (or anywhere you want to detect motion) and you are ready to go.

Have Fun!

Comments

emaillj (author)2017-11-08

Great job, and excellent write up. I'm wondering if you can suggest if this would be possible to make using a 18650 battery and 6v solar panel?

I imagine I could use the same transistor, but I'd need to change resistor sizes perhaps? Sorry very new at this.

I have also found this: https://www.instructables.com/id/Convert-a-5v-PIR-Motion-Sensor-to-33v-for-ESP8266/ which might be of use to both of us?

rgrokett (author)emaillj2017-11-09

I would strongly suggest switching to the deep sleep version https://github.com/rgrokett/ESP8266_PIRv2 though it takes a bit more hardware wiring. Using that, a set of AA batteries lasts about 6 months w/o recharging. If space is available, a 6v lantern battery or D batteries would last 1 or 2 years or more.

Otherwise, you would need a charging converter for the solar panel so that the batteries could charge and also run the ESP/PIR at the same time. I haven't attempted that, since the deep-sleep works reliably.

If you use the Adafruit HUZZAH ESP8266, you do not need to convert the power, as it includes a 3-6v regulator and has a 3.3v output for the PIR along with UART stuff. Its worth the price for the extra ease of implementation.

emaillj (author)rgrokett2017-11-10

Thank you for the reply - I am in fact using v2, deep sleep was a requirement. What I'm actually doing is I'm making a motion sensor that can be hidden on our driveway - I've got an old solar light panel & stake, and I've gutted it and reprinted a top for it to fit my 6v solar panel and hidden the PIR underneath in a 3d printed enclosure.

A Tp4056 is being used to charge the 18650 battery, and I've modded the PIR to run on 3.3v (bypassed regulator through 'H' pad as per the previous post). I tried a variation of a HUZZAH that I had however it wouldn't put out 3.3v when the input on the 5v pin dropped to 4v. That said, I tried an ESP8266 dev board I had lying around, and despite the same regulator, it holds at 3.3v (even though I think there's supposed to be a 1v drop out).

I will have another try tonight but I'm struggling to get the transistor part working succesfully having adjusted resistors for 3.3v (or what I think they should be.... this is my first project with transistor use).

emaillj (author)emaillj2017-11-11

Further to that, my issue appears to be that the RST is not dropping low enough - measuring the output of the transistor shows the drop from 2.8v to 0v, however once connected to RST, voltage only drops from 3.3v to 3.0v. I've tried with and without D16 bridged or with a resistor, but no change. Tried replacing the base 1k R with a 4.7k as per my possibly bad calculations, but again no change. If I don't use a resistor at all on base it drops to 1.5v. Unfortunately, I'm well out of my depth.... probably have been for a little while, but eager to beat it.

emaillj (author)emaillj2017-11-11

Finally, it works... 1 faulty transistor later, and another one that was CBE, not EBC (P2N2222A).... rebuilt the circuit and it's working so far! Thanks again for the great guide and base to start me off - hopefully the cat never makes it that far from the house where me sensor is though...

mike14 (author)2017-10-10

It appears that ifttt has renamed "maker" in step 2 number 6 to webhooks.

Also Arduino IDE was having problems finding some of the functions defined. This was fixed by moving them to above where they are used.

Thanks for sharing this it saved a bunch of time and is more energy efficient than what I was going to setup.

rgrokett (author)mike142017-10-11

I updated the docs for the IFTTT Webhooks change. I wasn't able to reproduce the functions error in the .ino, but I went ahead and relocated the functions anyway. The .ino compiled for me either way. I am using Arduino IDE v1.8.5

Just FYI, I have been able to get about 6 months use from 4AA batteries on the PIRv2 version with hundreds of IFTTT message sent over that time. (They only lasted a week with the previous v1 version). Deep Sleep makes BIG difference!.

Thanks!

About This Instructable

2,201views

13favorites

License:

More by rgrokett:PiEyeR Enhanced Thermal CameraRaspberry Pi Talking Barcode ReaderOne Transistor Audio for Pi Zero W
Add instructable to: