# Security Monitoring System ## Tutorial on how to a monitoring system using a motion sensor and camera Title: Security Monitoring System Name: Tamim Jabr, tj222kg Overview: detecting the motion in the room, and be able to take pictures and upload them to the cloud using the mobile app. Time needed to do: 20h ### Objective I chose this project because I am very interested in monitoring my own office. My personal zone is a priority for me. Being able to detect if someone enters the room and catching them red-handed sounds amusing. ### Material 1. Pycom LoPy4: a device that is a little device programmed by MicroPython and has several bands of connectivity (including wifi that I use in my project). The device has many digital and analog input and outputs and is well suited for an IoT project. ![](https://i.imgur.com/tBGu9HP.png) 2. The expansion board : makes it easier to connect things to LoPy4, as well as connecting my computer via micro USB, enabling me to plug the usb cable and start programming my LoPy4 ![](https://i.imgur.com/ewD9LOk.png) 3. Breadboard : Allowing me to connect my sensors to the breadboard and connect the breadboard to the LoPy4, making things easier when connecting several sensors. ![](https://i.imgur.com/xNkACHB.png) 4. Jumper wires male-male: easy to convert to female-male using a pair of nippers ![](https://i.imgur.com/2FkInal.png) 5. Push button: to start and end the program ![](https://i.imgur.com/exJxL6t.png) 6. Motion sensor : detecting the motion in the room ![](https://i.imgur.com/vYS2W35.png) 7. ESP32 camera: that supports wifi and is used to take pictures with different resolutions. ![](https://i.imgur.com/KTXJTrt.png) The material from 1 to 4 was included in IoT – LoPy4 and sensors bundle that I purchased from Electrokit and the price was 949kr including many other things like resistors, temperature sensor... The push button price was 19kr The motion sensor price was 49kr The ESP32 camera price was 164kr ### Computer setup #### Pycom 1. When it comes to flashing the firmware, so everything worked well for me without doing that. But if you need to do that watch this video on pycom official youtube channel [How To Flash Your Pycom Devices With The Latest Firmware](https://www.youtube.com/watch?v=TEfiMnkvyyM) 2. I use Visual Studio Code for uploading code to my pycom device because I am familiar with it. 3. Make sure to install pymakr extension for VSC to be able to upload and download your code to and from your pycom device, as well as running your code. ![](https://i.imgur.com/ncdZXm1.png) ![](https://i.imgur.com/az6UFt2.png) 4. Edit the gloabl settings for your pymakr extension and make sure to enable auto connect for easier connection for your LoPy4 ![](https://i.imgur.com/rHW2H1e.png) 5. Download Nodejs if you don't already have it on your computer because PyMakr uses Nodejs. [Nodejs-download](https://nodejs.org/en/download/) #### ESP32 1. I use the Arduino IDE for uploading code to my ESP32 camera 2. You will need to add Arduino-ESP32 support by following this simple guide [Add Arduino-ESP32 support](https://heltec-automation-docs.readthedocs.io/en/latest/esp32+arduino/quick_start.html) ### Putting everything together ![](https://i.imgur.com/X07O12i.png) The figure shows how I connect the devices to each other. The white wires are connected to GND (ground), the red wire for the button is connected to the 3v3, but on the motion sensor to 5v. The green wires show that the button is connected to pin 10 and the motion sensor to pin 4 on the expansion board. This set up is intended for development, but not production because it is easy to disconnect wires and devices when moving them. For production, it is a good idea to use 3D printer to create some kind of box where those devices can be fastened. When it comes to the ESP32 camera, so it was plug and play (no wiring is required). ### Platform I use [Pybytes](https://pybytes.pycom.io/) as platform to manage my pycom device, because it gives me those features that I need ex. locating the device, saving signals sent from the device for one month without setting up a database, enabling me to save notifications (ex. when the battery level is low or when sending a specific amount of data), as well as sending data to my API using webhooks when the motion sensor detects something. You can read more about how to add your pycom device here [how to connect your Pycom devices to Pybytes](https://docs.pycom.io/pybytes/connect/#app) The installation is cloud based because signals and notifications are sent to Pybytes platform and then data is saved both in Pybytes and InfluxDB.I am not planning to use a paid subscription because I only need to add my motion sensor and it works fine with the free plan. For ESP32 camera, I use a local installation because I don't want to make the camera publicly accessible, but only on my local network. In case I want to scale my application, I can send data from the motion sensor to my own server removing the dependency on Pybytes. ### The code My code is divided into four different repositories on GitHub: 1. The code for the ESP32 camera can be found here: [ESP32-CAMERA](https://github.com/Tamimjabr/esp32-camera). All it does is connecting the camera to wifi and start a server with three different endpoints for different resolutions: "/cam-low.jpg", "/cam-high.jpg" and "/cam-medium.jpg" 2. The code for the pycom device with the motion sensor and button can be found here: [Pycom-motion-sensor](https://github.com/Tamimjabr/sensors-python). It enable the user to start the motion sensor by holding the button until the green led flashes and flashing red led when a motion is detected and send data to Pybytes via wifi. To stop detection, hold the button until the blue led flashes. Don't forget to add your wifi name and password to "pybytes_config.json" to be able to connect to Pybytes 3. The API is a simple Nodejs Express app that receives a webhook from Pybytes when a motion is detected and saves the data in the time-series database InfluxDB. In addition, it is used to retrieve data from the database to be able to visualize it in the React Native app. GitHub repository: [sensordata-API](https://github.com/Tamimjabr/sensordata-API-influxDB) 4. React Native application to visualize the data from motion sensor and be able to capture images using the mobile app. GitHub repository: [sensordata-React-Native](https://github.com/Tamimjabr/IoT-sensordata-react-native) ### Data flow / Connectivity ![](https://i.imgur.com/PO6utvH.png) Data is transmitted from the Pycom device with the motion sensor to the internet using wifi and sent on every detected motion. The used transport protocol is MQTT because Pybytes runs over an open MQTT broker. The server receives data from Pybytes on every detected motion using a registered webhook and saves the data in the time-series database InfluxDB. ESP32 camera is accessible on a local server and accessed by React Native app using wifi. React Native app is used to see the history of detected motions and capture images that can be uploaded directly to my Cloudinary account. ### Presenting the data ![](https://i.imgur.com/odHArsN.png) The dashboard is built in the React Native app and consists of the following: 1. A bar chart that shows motions from the last 7 days 2. History that shows all detected motions for the last 30 days Because I am using the free plan for InfluxDB, data is saved only for 30 days, and that is enough for the project goal. Pybytes provides also a dashboard for the data sent by the motion sensor, and it is saved for 30 days for the same reason. ![](https://i.imgur.com/dqXFQpJ.png) ### Finalizing the design The final result is a device that detects if someone enters my office room. As well as, a mobile application connected to a camera that can capture images and send them to the image cloud service Cloudinary. It works fine but there are some points that could be improved: 1. Use 3D printer for making the device more user-friendly 2. Send e-mail or sms notifications for instantly notification when motion is detected 3. Add face detection ➡️ [Here is a presentation video on Youtube](https://www.youtube.com/watch?v=pNUyrNwqWgU&ab_channel=tamimjabr) ![](https://i.imgur.com/OvXM58C.jpg) ## Source list for pictures: 1. Pycom.io, pictures of Lopy4 and expansion board 2. Electrokit.com, pictures of breadboard, wires, button and motion sensors 3. Amazon.se for ESP32 camera