Intro: Home Security System Using ESP8266, TWITTER and Thingspeak.
In this tutorial we will see how to make a home security system which can post alerts to TWITTER. We will use the ESP866 Wi-Fi chip to communicate to the internet. For the purpose of this tutorial i have included two sensors that are the PIR motion sensor as well as a GAS sensor for LPG gas leak detection. So lets get started.
Step 1: Parts Required
- Atmega328 microcontroller. You may use any arduino based microcontroller.
- ESP8266. Any version of this chip will work fine. You may also use the nodemcu. I have made my own board so i will not be using the nodemcu.
- MQ-6 Gas Sensor
- PIR sensor.
- Power supply.
- Passive components(capacitors and resistors)
Breadboard and Jumper cables.
- THINGSPEAK account.
- ARDUINO IDE.
Step 2: THE WORKING AND SCHEMATICS
The main goal behind this project is to collect the data from an array of sensors and to send it over to TWITTER in particular. Hence to acquire the data from the sensors the ATmega microcontroller was used. Next part of the project is to send the acquired data over to the THINGSPEAK account(which u should have. Its a great tool for IOT based projects.) and for doing that we use an ESP8266. Before the ESP8266 days we had the the Ethernet shield which was rather expensive. The ESP8266 is really cheap(usually below $3) and gets the work done. It is just a little bit tricky to set it up and get things going.
The Schematic -
PIN 13 is connected to the OUTPUT of the PIR sensor.
PIN A0 is connected to the ANALOG output of the gas sensor.
PIN 7 is connected to the ESP8266 TX(use of a 5v-3.3v level shifter is advised)
PIN 8 is connected to the ESP8266 RX(use of a 5v- 3.3v level shifter is advised)
CAUTION- ESP8266 is not 5v tolerant. if you give 5v to any of the pins the ESP8266 might stop working.
The rest of the connections are VCC and GND.
I have made Two boards one for an an ATmega328p based Arduino board and another one for the ESP8266 breakout board.
Step 3: SOFTWARE PART
1. ARDUINO -
There is not much to it when it comes to the Arduino code. You just have to monitor the sensors and if they trigger you have to communicate with the ESP8266 telling it what happened and what to do.
The monitoring part is simple. But for communicating with the ESP8266 we need the UART interface. ATmega328p only has one hardware UART which is connected to the PC and is used for debugging (you can use this to communicate with ESP). So we use the SOFTWARE SERIAL library which can simulate the UART interface on any pins we wish to use. I have used pin 7 and 8 as RX and TX which are connected to the TX and RX of the ESP8266.
2. ESP8266 -
Before you do anything with the ESP8266 make sure that you have updated it with the nodemcu firmware. This will allow us to use the ESP8266 with the ARDUINO IDE. You will also have to add ESP8266 support to the ARDUINO IDE. The how-to is easily available on the internet.
After you have done the above things make sure you have an account on the Thingspeak website.
Once you log in to the account go to the apps menu and scroll down till you find 'THING TWEET'. Here you will be asked by the twitter to give permission to the THINGSPEAK service to post tweets on your behalf. Once you give the permission you will be able given an API KEY which is used by our application code to access our twitter account.
The other thing that is required is to post time with the tweet. Instead of using a RTC module i have used the 'THING HTTP' app to acquire time from a website and return it to my ESP8266. What it does is it essentially read a webpage and return back the String from the webpage which we have instructed it to do so.
Let's see how to do it -
LOG in to your Thingspeak account. Go To apps > THING HTTP. Create a new new THING HTTP app.
NEXT go to the website from where you want to acquire the current time.
When you see the time right click on it and select INSPECT.
It will take you to the source code of the webpage. Right click on the line which has the code to display the time and copy its Xpath.
Now go back to you THING HTTP page.
Enter the website URL in the URL tab. Select the http version 1.0.
Scroll down to the bottom where it says Parse String and paste the Xpath there. Save it.
You will be given the API key. Go to a new tab and open this url -
https: //api.thingspeak.com/apps/thinghttp/send_request?api_key='your api key'
Here you will see the time. if you don't see the time please make sure you have selected the correct Xpath for the acquisition of the time from the website.
We will bee needing these two API keys in our code.
UPLOAD the code to the esp8266 and Arduino board. Make the connections and see the results.
I HAVE ATTACHED ALL THE SCREENSHOTS ABOVE. PLEASE MAKE SURE YOU CLICK ON THEM AND SCROLL THROUGH THEM.
Step 4: RESULTS
The final board and the result on the twitter.