Intro: NodeMCU Motion Activated Security System
A while back I wrote an Instructable and uploaded a couple of YouTube videos describing how to use a PIR motion sensor with an ESP8266 or NodeMCU, I am not pleased with how these projects turned out.
Adafruit and IFTTT are excruciatingly slow! Whenever motion was detected by the sensor the NodeMCU would quickly send the information to Adafruit via MQTT; however, somewhere between Adafruit and my cell phone communication was breaking down. Sometimes I would receive alerts minutes or an hour after motion was detected. Other times I would receive motion alerts in bunches, maybe ten or more at a time. This is not acceptable!
You want the motion alarm system to work! It must not give false signals, and it must notify you in a timely manner when motion is detected. Hopefully this Instructable will fulfill these requirements. Instead of using Adafruit we will be using ThingSpeak and Twitter to notify us when motion is sensed. I have tested this setup for awhile now and the reaction time is from 3 to 30 Seconds with few false signals or lost notifications.
What are we going to do in this Instructable?
- Connect our Motion Sensor to a NodeMCU
- Create a ThingSpeak Account and create a Channel
- Create a ThingSpeak React and Link it to your Twitter Account
- Download the Arduino Code and make a few changes to it
- Install the ThingSpeak Library in the Arduino IDE
- Upload the code
If you missed the YouTube videos I made, here they are; although, I would suggest skipping the parts about wiring them up. There is some good information in the videos, just not the wiring! We'll correct that in the Wiring it up Step. There is a YouTube video at the end of this Instructable describing how to setup the Nodemcu and PIR motion sensor.
Step 1: What We Will Need
Things we will need -
1. NodeMCU or "ESP8266"
2. PIR motion sensor HC-sr501
3. Jumper wires
4. A breadboard - if needed
5. ThingSpeak and Twitter account - if you want to be notified (tweeted)
6. Project enclosure box and power USB cord if you plan to actually use it
The whole project should cost less than $15
The Nodemcu is $5 to $8
The motion sensor is $2 to $4
Pretty cheap alarm system!
Step 2: Wiring It Up
Let's wire up this baby! In the YouTube videos I showed you how to wire it up using a different method, by soldering a wire on to the voltage regulator. This is NOT necessary if you are using the NodeMCU. We will only need three wires for this project: VCC, Ground, and a Signal wire.
There is a terminal named "VIN" (Voltage In) located near the USB connector, this is where we will be powering our motion sensor. On some NodeMCU models it may be labeled simply "5V". Connect a wire from here to the terminal labeled "VCC" on the motion sensor. Our motion sensor requires 5 volts of power in order to run properly without falsing. Its the Red wire in my example. If you are unclear as to which terminals are which on the motion sensor, you can remove the white plastic lens and reveal the labels the Chinese manufacturer so cleverly covered up!
Next to the "VIN" on the NodeMCU is a terminal labeled "GND", connect a wire from here to the terminal labeled "GND" on the motion sensor. It's the Green wire in my example. You could connect it to any of the "GND" terminals on the NodeMCU.
The signal wire (the middle terminal on the motion sensor) outputs a 3.3 volt signal. The signal wire is what our NodeMCU will use to determine when motion was detected, and send the information to ThingSpeak and Twitter. We need to connect a wire from pin "D6" of the NodeMCU to the middle terminal of the motion sensor. This is the Blue wire in my example.
That's It, Easy right!
Step 3: Create a ThingSpeak Channel & Link to Twitter
There are several steps you will need to complete here.
1. Create a ThingSpeak account if you don't already have one, it's easy and free! I won't go into detail here, it should be self explanatory and procedures and screens will certainly change over time.
2. Create a Channel - This is where you data will be sent to ThingSpeak. You can have many channels, so you could have multiple motion sensors setup around your house, each Tweeting you when they detect motion.
First Click, Create Channel and a new screen will come up asking you several questions. When creating the Channel you can name it whatever you like. You can also give it whatever description you want. In field1 you can give it whatever name you prefer.
ThingSpeak will assign a unique Channel ID to your channel; write this number down, you will need this ID later when editing the code.
Click Save Channel, congratulations you just created a ThingSpeak Channel!
- Before completing the next step you will need to have a Twitter account, I will assume you have one or can easily get one.
Next you will need to Link your Twitter account to ThingSpeak.
Under Apps click on ThingTweet.
Click on Link Twitter Account, you will need to give ThingSpeak authorization to link the account and give ThingSpeak your Twitter information.
Step 4: Create a ThingSpeak React
Under Apps go down to React and click Create React
From here you can Name the React whatever you like.
Condition Type should be Numeric
Test Frequency should be On Data Insertion
Condition (If Channel) should be whatever you named your Channel
Field should be whatever you named your Field in the previous step
Next field set it to Equal to
Next field set it to 1
Action set to ThingTweet
under Then Tweet type%%datetime%% Motion Detected
This is very important!!!!
When ThingSpeak posts a tweet to your Twitter account it Must Be Unique otherwise it will only post once
that is why I chose to post the current time and date to Twitter followed by the text "Motion Detected". Feel free to change the text but you should probably leave the time and date stamp alone
Using Twitter Account should be your Twitter account name
That wasn't too bad, was it?
Step 5: Download the Software and Make Necessary Changes to the Code
First you will need to get the software. It is located at GitHub. If you are not familiar with GitHub it is a repository where programmers can share their code with each other and easily download it and even make improvements. You do not need an account to download the file. You can also simply copy and paste it into your Arduino IDE. Just look for the ".INO" file. Here is the link. ThingSpeak Motion or you can download the Zip file.
https://github.com/mkconer/thingspeakmotion here is the link again.
You will need to make some changes to the code
char ssid = "yourwifi"; // Enter your WiFi SSID (router name)
char pass = "yourpassword"; // Enter your WiFi router password
unsigned long myChannelNumber = 111111; // Enter your Thingspeak Channel Number
const char * myWriteAPIKey = "api-key"; // Enter your ThingSpeak API Key
- You will need to enter your wifi router name and password here.
- You will also need to enter your Channel Number you created in a previous step
- Last you will need to enter your API key for this channel
You can find the API key Information under Channels, My Channels and the Name of your Channel
Then Click on API Keys, copy and paste the Write API Key into the code where it says API-key
Save the Sketch program.
Step 6: Install the ThingSpeak Library
Get the ThingSpeak Library
Install the Thingspeak Library into your Arduino IDE by clicking Sketch, Include Library, andManage Libraries.
Then search for ThingSpeak in the filter. Choose the latest version and install. You may need to close and restart the Arduino IDE for changes to take effect.
Step 7: Upload the Code and Helpful Hints
Next, Upload the code to the NodeMCU. I assume you know how to do this. If not there are a lot of good videos on YouTube about setting up the NodeMCU esp8266 with the Arduino IDE. Here is a link to an Instructable for first time users.
I comment a lot in my code, this code is no exception! It should be self explanatory however there are a few things I should tell you. First, the code is intentionally short and sweet, nothing fancy! That is so you can easily understand it, make changes and customize it!
There is a variable named interval in the code. It is currently set to 90,000 milliseconds or 90 seconds. This time delay is to prevent false signals from being sent to ThingSpeak when setting up the device; for instance, when you initially set the device on a counter you will have 90 seconds to plug it in and move away before it triggers.
Also, it has the added advantage of preventing the WDT Watch Dog Timer from causing the NodeMCU from triggering a false signal should the device Reset itself. As the code is written now I have not had it reset itself, but it is not unusual for the esp8266 to do this. You might also notice a Yield() statement in the code, this is very important! Do not remove the yield statement, doing so could cause the esp8266 to reset itself often. The Yield allows the esp8266 to perform necessary wifi procedures and cleaning.
When setting up the Motion Sensor it is important to not adjust the sensitivity too high, doing so could cause false signals. All sorts of things could cause the motion sensor to trigger so placement is important. Test it out in a controlled environment for 24 hours first.
Also, the time delay potentiometer is very important as well. Do Not set it too low, it should be at least 15 seconds. This is how long the signal wire will be at 3.3 volts. I prefer this to be close to 45 seconds to a minute or about the 10 O'clock position. IMPORTANT!! The motion sensor time delay (potentiometer) should not be longer than the time interval variable mentioned above. This will also prevent several motion triggers from being sent to ThingSpeak back to back. ThingSpeak only want you to upload data a maximum of once every 15 seconds, so keep that in mind.
A word about Twitter. You will see on your twitter account posts your NodeMCU makes when motion is detected; however, you will probably not get Notifications. After all, Twitter assumes you know what you just posted to your own account, so you won't get a "Da Ding" saying you just posted something. If you want to get an Audible notification from Twitter when something was posted you will probably want to create two Twitter accounts, at this time you cannot "Follow Yourself" on Twitter. If you already have a Twitter account it may be best to setup a second one just for the NodeMCU/ ThingSpeak account and then subscribe or "Follow" yourself that way.
Have Fun, and be sure to reply/post if you do this Instructable!!
Step 8: YouTube Video
Here is a YouTube video describing how it works, Thanks for watching!