Intro: $10 Arduino WiFi Motion Detector (PIR) That Writes to SQL Via Cheap ESP8266 Module
This is my first instructable so if anything is unclear, please let me know and I'll try to explain it!
My scenario was as follows: I have an external location of which I want to know if somebody's there. The easiest way to do this is using a motion detector (PIR) and an Arduino. There are a lot of examples of how to get this working using an Ethernet shield, but in my case I wanted to use the cheap (±$3) and new ESP8266 WiFi module. Since this module is fairly new, not much tutorials can be found, so it was quite a struggle to figure things out.
So my goal in one sentence: Make an arduino post information from the PIR motion detector via PHP to an SQL database using the ESP8266 WiFi module.
Total cost: Less than 10 dollars.
Step 1: List of Materials
For this tutorial I used:
- Fake Arduino Uno
- PIR motion detector
- The cheapest possible ESP8266 WiFi module
- Some jumper wires
- A breadboard
Step 2: Wiring Things Up
Note: From the documentation of the ESP8266, I found that this modules requires more power than the Uno's 3v3 port can give, but since I didn't have any alternative 3.3v power source I tried powering it from the 3v3 port and it just seemed to work.
In my case I used the V090 of the ESP8266 module (see image attached). Since this module isn't really breadboard friendly, using the breadboard is only necessary because you need to power 2 pins of the ESP8266 chip with 3.3v.
The PIR sensor is wired just as you would do in the default PIR example from the Arduino library.
Arduino | ESP8266
RX (D0) | TX
TX (D1) | RX
3v3 | VCC and CH_PD
GND | GND
Arduino | PIR
D3 | OUT
5v | VCC
GND | GND
Note how the PIR is powered with 5v, while the EPS8266 only takes 3.3v! Don't connect the EPS8266 to 5v, because that will fry the board.
Step 3: PHP
Because this tutorial is not about making SQL database etc. I assume that you're able to make a database containing the right tables yourself.
In my example, I will use 3 pages: dbconnect.php, add_data.php and review_data.php and I have a database called 'motion', containing a table 'motion' (as well) which has the colums 'id', 'event' and 'motion', where id and event (timestamp) are automatically created with every insert of motion.
dbconnect.php --> In this file we make the connection to the database. You can find the code for this page here: Github: dbconnect.php
add_data.php --> In this file the values are actually added to the database. In fact, when just browsing to add_data.php, nothing will be added, but you should enter add_data.php?motionornot=1 instead, with 1 in case of motion, or 0 in case of no motion (defined by the arduino code later on). The code can be found here: Github: add_data.php
data_review.php --> This is the page that shows a table with values 1 or 0, depending on if there was motion or not. This table is our final result. The code for this page can be found here: Github: data_review.php
Step 4: Arduino Code
Now to detect the motion and write therefore a 1 (in case of motion) or a 0 (no motion) to the database, we have to program the Arduino. I made the code so that when there's motion, the Arduino won't check for further motion for the next 5 minutes. Then, the Arduino will check for another 5 minutes if there's motion without writing a 0 to the database yet. Then, when there's no motion after all, a 0 will be written to the database. For more info about this particular part, check out this link.
My Arduino code can be found here:
Github: Arduino code (I'm sorry that it's not as efficient as possible yet, will make it better in the future)
Now, some changes in this code must be made to get this working:
- On lines 6 and 7 you enter your Wifi settings
- On line 8 you enter the host where the previously made .php files are hosted (eg: example.com) No http, no slahes etc.
- On line 22 you define the number of seconds that you want between measurements.
- On line 98 you define the path to the add_data.php file. If it's located at "example.com/arduino/add_data.php", you fill in here: "/arduino/add_data.php"
Now upload the code to your Arduino.
Step 5: Result
Now if you visit the page at example.com/arduino/data_review.php, you should get a nice table with detections of motion!
I hope this instructable will get you on the road since there aren't too many instructables containing an Arduino and an ESP8266 WiFi module yet.
Have fun :)
P.S. I read a LOT of pages and used some code of many people, but unfortunately I'm unable to trace all my sources back. If you recognise any of your code used in mine, please let me know and I'll credit you. :)