Introduction: Room Monitoring
We will see how to create a system that allows the monitoring of a room by counting the people going in and out of this room. This system might be used in a building in order to increase security by ensuring there is nobody left in it, or by schools in order to know how many pupils are in a specific classroom...
Note : This project has been held during the 5th year of Embedded System engineering at Polytech Paris-UPMC.
Step 1: Material Needed
In order to create this system, you will need the following components :
Electrical part :
- 2 pairs of infrared shooting sensors (at least)
- 4 1.2V AA batteries
- 1 4xAA battery case
- 1 3.3V step-up/step-down voltage regulator S7V8F3
- 1 10kOhm resistor (at least)
- 1 breadboard
If you have a soldering iron, you can also weld the components on a perfboard.
Computing part :
- 1 computer running on Linux or Windows
- Arduino software (tested with Arduino 1.6.5)
- 1 ESP8266 Node-MCU module (at least)
- 1 USB to Micro-USB cable
- IBM Bluemix account
Useful links :
Here are some links if you want to purchase the components mentioned above :
Step 2: Electrical Board Design
Provided that the sensors are the components which give us the information of someone entering in a room, the first step is to make them work.
A sensor consists in a pair : an emitter and a receiver. The receiver emits an infrared beam and the receiver delivers an output voltage, depending on the reception of this beam :
- If it receives the infrared beam, it provides its supply voltage, corresponding to a high logical level.
- If the beam is cut, the sensor provides a null voltage, corresponding to a low logical level.
The data returned by the sensors will be given to an ESP8266 which can handle an input voltage up to 3.3V.
Therefore, we will supply the sensors with 3.3V for them to provide an output of the same voltage when the beam between the pair of sensor is not cut and 0V when it is cut.
Step 3: Node-MCU Programming
The ESP8266 is a Node-MCU module that embeds a micro-controller and supports Wi-Fi for a very low-cost.
It is very useful for this type of IoT application because it allows an easy communication between sensors and external services such as Bluemix.
Connecting the sensors
Now that we have operating sensors, we have to connect them to the ESP8266 module, for it to read the data and then send it to Bluemix.
You have to connect the data wire (the white wire of the receiver) to one of the digital pins of the ESP8266 whose name starts with a "D" (D0, for example) and connect the ground to the GND pin of the module.
Before programming your Node-MCU module, you have to add the required packages in Arduino.
- Go to File > Preferences and add the following URL as an Additional Board Manager URL : http://arduino.esp8266.com/stable/package_esp8266...
- Go to Tools > Board > Board Manager and install the esp8266 package.
- Go to Tools > Board and select NodeMCU 1.0 (ESP-12E module) among the list of boards.
Reading the sensor data
In order to read the value given by a sensor on a specific digital input, you have to configure this pin as an input using the function pinMode() and read it data using the function digitalRead().
Send the data to Bluemix
Once the data is read and a passing is detected, we want the ESP8266 module to send the information to Bluemix. First, we have to connect to a Wi-Fi network, then we can connect to Bluemix and send data to it. Some useful functions are described in the header file ESP8266WiFi.h.
You can download the code below in order to read the data from the sensors and to send it to Bluemix. remind to change the Wi-Fi configuration by changing the network and its password.
Step 4: Bluemix Node-RED Application (1/2)
Now that we are able to send data to Bluemix, we want to process it in order to display the number of person in one or several rooms. In Bluemix, we will use a Node-RED application which will extract the useful data, update the counter, sort it in a database and, finally, send it to a HTML webpage.
Create a Bluemix account
First of all, we have to create a Bluemix account by clicking "Sign Up" and following the instructions at this URL : https://console.ng.bluemix.net
Add ESP8266 device and Node-RED app
Once you have a Bluemix account follow these steps in order to start a Node-RED application :
- Access your Dashboard, where all your applications and services will be listed.
- Click "Create a Service" to select the type of service you want to use.
- Select "Internet of Things Platform" and create the service.
- Once this services is created, click launchthe Watson IoT Platform.
- In the Watson IoT Platform, click "Devices" and then "Create a Device".
- Select "Create device type" and follow the instructions.
- You can now create a device of the type you just created, click "Next".
- When Device ID is asked, type the MAC address of your ESP8266. You can run the code given below to know this MAC address.
- Click " Next" until the last step and save the Identification token as you will need it later.
- Back to your Dashboard, click "Create an App" to select the type of application you want to add.
- Select "Node-RED Starter".
- Complete the fields with the App Name and the Host Name you want, then click "Create".
- You now have a connection between your ESP8266 and Bluemix, plus a Node-RED application which needs to be developed.
Step 5: Bluemix Node-RED Application (2/2)
Node-RED provides a flow editor that makes it easy to wire together devices, APIs, and online services.
Sensor to Database Flow
The sensor's value is treated so we send +1 or -1 to the upcoming dataflow. with it, we send the name of the esp8266 and the name of the database it has to insert the values in. Depending from the pin the sensors are connected to, it will send a different database name. Depending of the database name, the next step is to go into the chosen database and extract the value of the number of people inside the room attached to the database. We will indeed use as many noSQL databases as we have room to monitor. After we have this value, we add it with the upcoming value (+1 or -1)
Database to Webpage Flow
The data displayed on the web page are coming from the database.
There are two urls serving http requests – https://YourAppName.mybluemix.net/web and https://YourAppName.mybluemix.net/web When you go to /web it serves up a simple page with a placeholder div and a js script. The script sets a function to run every 2 seconds. The function sends a request to /data and replaces the text in the div with the response, i.e. the latest sensor data.
When you write your own Node-Red flow for this, you have to be careful to replace the variable theUrl Auto Update Script with the link of your application.
You can import the following files as a clipboard into your Node-RED application in order to automatically build the application that receives the data, sends it to a database (sensor_to_db.txt) before it sends it to a webpage via HTTP (db_to_http.txt).
Step 6: Web Page
As said before, once all the data are gathered, a web page is created to be browsed through the HTTP request.
First we create a add a style element to the payload in the message in the flow. We add it in the CSS node where w choose the position of our elements, their color, their fonts.
This element is then called in the HTML node where we really build our page and use the data collected before that we can find in the data object (data.count1, data.room1...).
All these elements can be rebuild with the flow bdd_to_http presented earlier.
Step 7: Running the System
Now, you are ready to run your system! Here is a video to summarize how to execute it.