------------------------------------------------------------------------------------------------
Section 1 Overview of project
------------------------------------------------------------------------------------------------
A. Where we have uploaded our tutorial
Paste the link here of your Youtube and tutorial document here
B. What is the application about?
The Motley Crew’s project is designed to simulate a home surveillance system. With our setup, it consists of 2 main areas with the first being the front door and the second being the kitchen.
The front door section will let the user be updated on any motion detected at the door as well as require a permitted access card to be able to“enter” the house. The light sensor will be used to automatically control a led acting as a light system which is designed to light up if it gets dark so that our PiCam is able to take a picture regardless of time. The user is also able to control the state of the door via the telegram bot.
The kitchen section will constantly note the amount of gas and infrared inside the kitchen and a buzzer will sound, acting as an alarm if the value gets out of the safe range. The user will also be sent a message warning them of any problems that might have occurred.
C. How does the final RPI set-up look like?
D. How does the web or mobile application look like?
Web interface:
Telegram bot (Door Control) :
Telegram bot (Front door notification):
E. System architecture of our system
------------------------------------------------------------------------------------------------
Section 2 Hardware requirements
------------------------------------------------------------------------------------------------
As our application has multiple functions such as detecting flame,gas and motion, here are the hardware needed and what they are used for. There will be a total of 2 areas covered for our project.
Outside the House1. PIR MotionSensor
A PIR Sensor is able to tell when something nearby moves.
The sensor detects the pattern of infrared energy in its surroundings. If the pattern changes, the sensor outputs a high value.
The sensor has 3 pins.
o VCC – Connect this to power (5V or 3.3V)
o VOUT – Connect this to a GPIO pin to read its value
o GND – Connect this to ground
2. 3 LED ( 2 Green, 1 Red)
To connect the LEDs, ensure that the longer leg is connected to the positive supply of the circuit(power) while the shorter one is to the negative side of the circuit(ground). In this case, for the status of the door, we will be using 2 LEDs (1 red, 1 green) to represent whether the door is locked or unlocked as well as the other LED ( 1 green) to represent if the lights are turned on outside the front door.
3. 2 LCD Screen
For this application, we will use the LCD Screen for the kitchen and the door to show information such as the current concentration of gas and infrared in the kitchen and the safety level as well as whether the key card used is granted access to open the door.
4. 7 Resistors(6 x 330 Ω Resistors, 1 x 10k Ω Resistor)
a) Resistors help ensure that small current will flow and that the Raspberry Pi will not be damaged. As this application requires multiple LEDs, we will use 1 330 Ω resistor for each LED used in the setup.
b) As our application requires the use of abutton, we will be using a 10k Ω resistor to help moderate the flow of current.
5.1 RFID / NFC MFRC522 Card Reader Module
We will be using a RFID reader for the door. This RFID will scan to check if a key card is used to open the door. This will be used to check if the key card has been authorised to be able to open the door.
6. 1 Gravity: Digital Push Button
We will be using a digital push button as a doorbell to send input to our Raspberry Pi and trigger the buzzer.
The button acts as a form of input and to know whether a push-button is pressed or unpressed, we can detect its HIGH or LOW state, which are passed through the 'legs'
7. 3 Buzzers
We will be using a buzzer for each of our Raspberry Pi which will trigger upon meeting a certain requirement such as the concentration of gas going above a certain range.
A buzzer is an audio signaling device which is commonly found in circuits to create a buzzing or beeping noise.
An active buzzer can be connected just like an LED but they are even easier to use because a resistor is not needed.
A buzzer typically has 2 pins
o VOUT – Connect this to a GPIO pin to control its value
o GND – Connect this to ground
Kitchen8. Grove – Gas Sensor(MQ2)
This sensor is used to display the amount of flammable gas in its surrounding. This will be deployed in the kitchen, and with the help of this component and a “if” statement, the Arduino will be able to know if the amount of flammable gas is too high in the room, signalling that whether a gas leak or a fire is happening.
The gas sensor typically has 4 pins
o VCC – Connect this to the 5Vpower pin
o GND – Connect this to ground
o A0 – Connect this to any Analog output pin. Produces analog output
o D0 – Connect this to any Digital output pin. Produces digital output
For the case of this setup, only the A0 will be used
9. Flame Sensor
This flame sensor is used to detect fire as the sensor detects the amount of infrared rays shone into the sensor. This will be deployed away from sunlight and the stove, to ensure that a dangerous fire is truly occurring when the sensor shows a high reading. If both this sensor and the gas sensor show high readings, this means that a fire is happening in the kitchen.
The flame sensor used for the setup has 4pins
o VCC – Connect this to the 5V power pin
o GND – Connect this to ground
o A0 – Connect this to any Analog output pin. Produces analog output
o D0 – Connect this to any Digital output pin. Produces digital output
For the case of this setup, only the A0 will be used
10. RGB LED
A RGB LED is a LED with red, green and blue diodes inside the bulb. For this setup, the green light will be active to display that the Arduino setup is active, and that all readings are normal, and red will be used when either a fire or gas leak has been detected. This RGB LED will work alongside the buzzer and LCD display to ensure the user near the Arduino knows about the detected event, by having the LCD display the error,the buzzer emitting a differing sound between a gas leak and a fire, and the RGB LED shining red when the buzzer triggers.
The RGB LED for this case has 4 pins, 3 belonging to the red, green and blue diodes in the component, labelled R, G and B respectively. These pins will each be connected to a digital pin on the Arduino.The 4th pin is the cathode pin, meant to be connected to the ground pin.
------------------------------------------------------------------------------------------------
Section 3 Hardware setup
------------------------------------------------------------------------------------------------
In this section, we will connect the necessary components described in Section 2.
RPI 1 – Kitchen
RPI 2 – Front door part 1
RPi 3 - Front door part 2
------------------------------------------------------------------------------------------------
Section 4 Software Requirements
------------------------------------------------------------------------------------------------
In this section, we will cover the software required for our project.
· AWS DynamoDB
· AWS IoT
· Phpmyadmin
· Telegram bot
· Twilio
· Arduino IDE
· Boto3
· MQTT
------------------------------------------------------------------------------------------------
Section 5 Create a “Thing”
------------------------------------------------------------------------------------------------
A “thing” in this case is a Raspberry Pi registered in the AWS service to allow the Raspberry Pi to connect to AWS services. For the CA2 setup, only the Raspberry Pi used for the kitchen section of the application has to be used for this section.
Step 1) In the AWS console website, click on the “Services” tab in the main page, and find the link to “IOT Core”
Step 2) In the main page on the side navigation bar, click Manage > Things, and click on“Register a thing”.
Step 3) Choose “Create a single thing”
Step 4) Enter a name for the device that is about to be registered, and leave the other fields with their default values,then click Next
Step 5) Choose “Create certificate”
Step 6) When the page below shows, download all certificates, keys and the root CA showing below. Download Amazon Root CA1.
Step 7) Save all downloaded documents in a folder, move them into a folder in the registered Raspberry Pi and rename the files according to their types, like so:
Step 8) Click “Activate”, and then “Attach a Policy” on the page with the downloadable documents
Step 9) Click on “Register Thing”
Step 10) Here, we add a policy. At the main page of the IOT Core of the AWS console website at the side navigation bar,click on “Secure” > “Policies” and select “Create a Policy”
Step 11) Add in any name for the security policy, fill in the fields under “Add statements” as shown below, and click “Create”.
Step 12) Select the created certificate,and click on “Actions”, then “Attach policy” on the top right hand side of the page.
Step 13) Select the device registered as a Thing in the AWSservice, and click “Attach”
Step 14) At the IOT Core main page, click on “Manage” > “Things”, then double click on the registered Thing and select“Interact” on the new navigation sidebar to reach the page shown in the second image. Copy the link in the HTTPS section and save it in the same folder the downloaded certificates in Step 6 are located for ease of navigation.
Step 15) Start up the Raspberry Pi, and in the console,execute this lines of code:
------------------------------------------------------------------------------------------------
Section 6 DynamoDB Setup
------------------------------------------------------------------------------------------------
For this segment, a role will be created to direct data sent via MQTT to DynamoDB database, followed by the DynamoDB database that will store data sent using a specified topic, and lastly an IAM role, to allow access to the DynamoDB data via code.
Step 1) In the AWS console website, click on the “Services” tab in the main page, and find the link to “IAM”. Click on the “Roles” tab in the side navigation pane and click “Create role”
Step 2) Select “AWS service”, then “IOT”
Step 3) Select “IOT” for use case, then click “Next: Permissions”
Step 4) Click “Next: Tags”
Step 5) Click “Next: Review”
Step 6) Put in a role name, and complete the process.
Step 7) Click “Services” > “DynamoDB”and the click “Create table”
Step 8) Fill in the form fields as shown below, then click“Create”
Step 9) Click the “Services” button in the main page of the AWS console and then click “IOT Core”. Then, click “Act” on the navigation bar. Once the page shown below is reached, click “Create a rule”
Step 10) Fill in the form fields as shown below
Step 11) In the “Set one or more actions”section, click “Add action”
Step 12) Select the action shown below, and click “Configure action”
Step 13) For “Table name” choose the DynamoDB table that was set up in Step 8, and “Create rule”
Step 14) In the main page of the AWS console, click “Services” > “IAM” > “Users” and click “Add user”
Step 15) Fill in a User name , and choose“Programmatic access”. Once done, click “Next: Permissions”
Step 16) Select “Attach existing policies directly”, and click the textbox for the policy granting full access to AWS DynamoDB. The click “Next: Review”
Step 17) Click “Create User”
Step 18) Here, one can either download the.csv file which will contain the access key and secret access key or copy them when they are displayed in the webpage shown below (censored to hide the keys).Both keys will be used to access DynamoDB for retrieval of data via a python script (server.py in the case of this setup)
------------------------------------------------------------------------------------------------
Section 7 AWS EC2 Hosting of Web Application
------------------------------------------------------------------------------------------------
In this section, an EC2 instance will be created. At its base an EC2 instance is a virtual computer that AWS users can make use of for a variety of applications. In this case, we will use it to host a Flask web server, so that connection to the web server can be established globally.
Step 1) In the main page of the AWS console, click “Services”, “EC2” and then “Launch Instance”
Step 2) Select “Amazon Linux AMI”
Step 3) Click next until the “Configure Security Group” is dispayed,then choose “HTTP” in an empty dropdown field on the left of the screen, andensure that the chosen port range is 80. Click “Review and Launch”
Step 4) Click “Launch”, the choose “Createa new key pair” on the dropdown box in the popup shown below. Add a name forthe key pair, then click “Download key pair” Save the downloaded file, andclick “Launch Instances”
Step 5) Click “View Instances to be broughtto the page displaying all active EC2 instances. The EC2 will take time to comeonline. When the EC2 comes fully online, its Instance State will say “running”.
The following steps will involve establishing a connection to the EC2 through SSH so that commands can be sent to the EC2 instance, and a FTP connection to the EC2 instance to allow web application-related files to be placed in the EC2 instance to be run or used.
Step 6) Download and install PuTTY andWinSCP.
PuTTY: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
WinSCP: https://winscp.net/eng/download.php
Step 7) After installation, open up PuTTYgen.
Step 8) At the bottom, ensure that “Type of key to generate:”is RSA. Press Load and select the .pem file that you had downloaded previously from the key pair. Press Generate when .pem file is selected and press Ok when dialog box shows up.
Step 9) Press Save private key and Yes to the warning. Proceed to name it and place it some where you will remember.
Step 10) Head back to the EC2 Dashboard and note down the Public DNS for your instance. In this case, it is“ec2-3-112-152-125.ap-northeast-1.compute.amazonaws.com”.
Step 11) The default username for our instance is ec2-user.Using PuTTY, enter the host name as “ec2-user@ec2-3-112-152-125.ap-northeast-1.compute.amazonaws.com”(Defaultname@Public_DNS)
Step 12) Proceed to navigate to Auth which is under SSH from the left menu and click Browse to use your .ppk file that you had created using PuTTYgen.
Step 13) Click Open at the bottom right and press yes on the security alert. From there you are now accessing EC2 through SSH.
Step 14) Launch WinSCP. Enter in the Public DNS under hostname and ec2-user as User name.
Step 15) After inputting, click on Advanced and navigate to Authentication under SSH. Under private key, select the .ppk file that you had used for PuTTY previously.
Step 16) Click Ok and Save. Press Login and Yes when the warning pops up. You now can transfer files to the instance. (Example has files in it already, do not be confused.)
Step 17) Create a new directory called Home Security. Access it and transfer the necessary files for the web application and the rootca.pem,public.pem.key, private.pem.key and certificate.pem.crt that was downloaded previously.
Web Application Startup
Step 18) Open PuTTY and install tmux. Using tmux will allow the web application to continue running even is the connection to the SSH has been lost. Enter “y” when asked.
sudo yum install tmux
Step 19) Continue to install the following:
sudo pip install numpy
sudo pip install gevent
sudo pip install flask
sudo pip install AWSIoTPythonSDK
sudo pip install boto3
Step 20) On PuTTY, navigate to /HomeSecurity directory and launch tmux.
cd HomeSecurity/
tmux
Step 21) Run the following code:
sudo python server.py &
tmux detach
Step 22) You can close the PuTTY window if you want to. Check your instance in the EC2 Dashboard and take note of the IPv4 address provided and key that into your browser and your web application should be there.
------------------------------------------------------------------------------------------------
Section 8 Reading RFID/NFC tags setup
------------------------------------------------------------------------------------------------
Enable SPI and prepare the MFRC522 libraries
If your raspberry pi is not configured with the MFRC522 libraries, you can follow the following instructions to set it up.
<< Enable SPI via raspi-config >>
Step 1) Run raspi-config, choose menu item “5 Interfacing Options” and enable SPI.
sudo raspi-config
Step 2) Modify the /boot/config.txt to enable SPI
sudo nano /boot/config.txt
Step 3) Ensure these lines are included in config.txt
device_tree_param=spi=on
dtoverlay=spi-bcm2835
Step 4)Install the python development libraries
sudo apt-get install python-dev
Step 5)Install the SPI-Py Library
Step 6) Clone the MFRC522-pythonlibrary to your home folder as follows:-
cd~
git clone https://github.com/pimylifeup/MFRC522-python.git
cd ~/MFRC522-python
sudo python setup.py install
Step 7) *Copy the MFRC522.py into the folder which you create your ReadF.py*
sudo cp ~/MFRC522-python/mfrc522/*.py ~/labs/IOTCA2Files
------------------------------------------------------------------------------------------------
Section 9 Arduino Setup
------------------------------------------------------------------------------------------------
Before making use of the Arduino, one must first ensure theRaspberry Pi can run Arduino code
Step 1) Enter these commands into the Raspberry Pi command window.
Step 2) Once done, open Arduino IDE by clicking on the Raspbian logo on the top-left hand corner , the click“Programming”
Step 3) On top of the window, click on“Tools”, and ensure the Board has ‘Arduino Uno” selected. Since in this case only 1 Arduino will be connected to the Raspberry Pi, the serial port will only have1 selection. Select it.
Arduino code will be run using this IDE. To load the code, click “File” > “Open” then double click on the .ino file in the zip file containing the source code.
------------------------------------------------------------------------------------------------
Section 10 Program Setup
------------------------------------------------------------------------------------------------
1. kitchensensor.ino (RPi 1 - Kitchen)
This .ino file will be sent to the Arduino.Arduino code works in such a way, where after the library import commands have been run, the code will run through the “setup” method once, then repeatedly run the “loop” method repeatedly forever, until another code is uploaded to the Arduino, or the power has been reset, where the code will run from the import commands to the “loop” method once again
For this .ino file, the “setup” method will let the MQ2 gas sensor warm up for 20s while displaying “Calibrating” on the LCD display, then add a “Done” message and shine a green light using the RGB LED for 3s.
In the infinitely repeating “loop” method, every1 second the gas sensor and flame sensor will have their readings printed on the console of the Arduino IDE. The printed values in the console is not meant for the user to see, but for the subsequent python script to receive the data.
kitchensensor.ino also includes code that will trigger a buzzer, and change the RGB LED to red when the readings of the sensors show abnormal levels. For instance, if the gas sensor shows that there is a large amount of flammable gas but the flame sensor does not show a high reading, the LCD will indicate that there is a gas leak and the buzzer and RGB LED will make a beeping sound and flash respectively, and if both the gas sensor and flame sensor show high readings, the LCD will indicate a fire has been detected, the buzzer will make a drawn out sound, and the RGB LED will show red for the entire duration the buzzer sounds.
2.kitchen_db.py (RPi 1 - Kitchen)
In this python file, the code will first connect to the Arduino console and the MQTT topic used to send data from the Arduino, then wait for text to be printed in the Arduino console. Thus, each time the Arduino sends any data (completes the “loop method” stated above),kitchen_db.py will receive the printed data and send it to the MQTT topic.
At the same time, this python script also contains the same checks that will trigger when a gas leak or fire is detected.In the case of this python script, when either event is triggered, a message will be sent to the user via the Twilio messaging service. When a fire is detected, a sprinkler, represented in this setup as a LED light, will trigger to help in quelling the ongoing fire.
3. Doorbelltb.py (RPi 3 - Front door 2)
In this python script, the main code will trigger only when the button connected to the RPi is pressed. When it is pressed, the picamera will be used to take a photo, intended to be a photo taken of whoever is in the front door. A notification will also be sent to the user via Twilio and the telegram bot, and in the telegram bot the user can also send commands to the Telegram bot to control the lock on the door. The photo taken by the picamera will be sent to the user through Twilio alongside the notification of someone being outside the door,where the photo will be compressed and hosted on Imgur, using the python library called Pyimgur.
4. Telegrambot.py (RPI 2 – Front door 1)
In this python script, we have defined 3 functions, UnlockDoor,LockDoor and respondtoMsg. The first 2 functions will allow us to control the state of the door when we send a message “Lock” or“Unlock” to the bot. As we are making use of LEDs to represent the state of door,the green LED and red LED will light up respectively when “Unlock” and “Lock”are sent to the Telegram bot.
5.motionpicturetb.py (RPI 2 – Front door 1)
In this python script, we will be making use of the the motion sensor, buzzer and the picamera. After running the script, when the motion sensor detects any motion, it will trigger the method TakePictureAndSendwhere a picture is taken by the Picam and sent to the user via twilio alongside a image url. It will then proceed to send a telegram method via the method DetectandNotify with the message “Someone is outside” and sound the buzzer.
6.AddAccess.py (RPI 2 – Front door 1)
In this python script, we will be making use of a local database mysql as well as the rfid scanner to allow a new card to be added to our system. Upon running the script, our raspberry pi will connect to the mysql database, after scanning for an nfc card, the script will take the scanned uid and remove all the “,” between the string of numbers and store it as a varchar value inside the database.
7.DelAccess.py (RPI 2 – Front door 1)
In this python script, similar to AddAccess.py, we will simply be checking for any values that match the scanned uid inside the database and delete any matching uids.
8.ReadNFC.py(RPI 2 – Front door 1)
In this python script, after scanning for an nfc card and getting the uid, we will take the scanned uid and compare it to the values stored in the local database. If there is a match between the uid and any of the values stored, it will execute the methods ledOn and Open,whereas if it does not match any of the stored values, it will run the methods ledOn2, Close and bz.on before it is set to sleep.
------------------------------------------------------------------------------------------------
Section 11 Web Interface Setup
------------------------------------------------------------------------------------------------
We used WinSCP to move files to and from the EC2 that hosts our server. The following files are used for the web interface.
------------------------------------------------------------------------------------------------
Section 12 Expected Outcome
------------------------------------------------------------------------------------------------
Once the hardware setup for RPi 1: Kitchen is complete, create a directory in the RPi using a FTP client, and that the line of code has been executed in that RPi:
Simply open up kitchensensor.ino file using the downloaded Arduino IDE, and upload the code using the right-pointing arrow icon.
Then, open up the command line window of the Raspberry Pi, and run kitchen_db.py by typing in these commands (replace the first line with your own directory for your files)
With these done, the Arduino data will besent to the DynamoDB databse without any control required.
If all steps mentioned before in the tutorial are done correctly, the application should like something like this: https://www.youtube.com/watch?v=T5O9mbO8ww8&feature=youtu.be
The Arduino would be able to detect gases and flame and a buzzer would sound off if any of both sensors detect something wrong. The RGB LED would also turn red otherwise, the LED will remain green and will be as soon as the program runs.
The Front door would be able to send messages to the user's phone number and telegram when the motion sensor detector if someone pressses the doorbell. When someone presses the doorbell, the message will also include an URL to a picture that is taken from the PiCam as soon as the button is pressed. A buzzer will also sound off if the doors are open via RFID card reader when it reads a authenticated card or when the doorbell is pressed. When an authenticated card has been read, a green LED will be lit up while a red LED will always be lit up to simulate the status of the door.
The website will show a live line graph of gases and a history log of the previous 10 values will also be shown along with the date and time of when it was taken. At the side of the web page, there will be a live clock and the fire level. The fire level will update its color if it changes. E.g. White for "No Fire", yellow for "Low" and red for "High".
-- End of CA2 Step-by-step tutorial --
Comments