Introduction: MQTT Based Fire Detection System
In recent days, automatic fire detection and control is becoming very essential to reduce the fire in a building or an industry. It provides real-time surveillance, monitoring and automatic alarm. A key aspect of such systems is to identify a developing fire emergency in a timely manner, and to alert the building's occupants and nearby fire stations.
In this Instructable, we will learn how to create a low cost fire detection system in which different subsystems communicates with each other over the Internet. This project is a combination of electronic circuits, programming and networking mechanisms working together to detect the presence of fire and provides an alert through a visual medium. Furthermore, the usage of light weight MQTT protocol aids in (almost) real-time execution and parsing of the critical data.
In this project, we consider an example of an organization which has one headquarters(HQ) and four sister institutions. All five firms of the organization has a control room each to monitor the fire status of the sister institutions. All control rooms receive the fire status of the four sister institutions and visually alerts the control room personnel by the glowing of respective LED (for eg., if there is a fire emergency in Institution 1, then LED 1 in all the control rooms glows). At the sister institutions, fire is detected using infrared sensors (Note: Hot objects emit Infrared Rays). The communication between the control rooms are performed using the MQTT protocol. The control room at the sister institutions are based on NodeMCU and acts as a node in the network. On the other hand, the control room at the HQ is equipped with a Raspberry Pi and acts as a broker. To display the fire status, another instance of a node is created at the Raspberry Pi.
This project brushes over the following topics:
- SSH Raspberry Pi
- Raspberry Pi as an MQTT broker
- Port Forwarding
- NodeMCU as publisher and subscriber
- Raspberry Pi as a subscriber
- File transfer over IP address (SFTP)
Step 1: Materials Required
- LEDs x 20
- Resistor x 5
- NodeMCU x 4
- Raspberry PI 2 x 1
- Ethernet Cable x 1
- Network Router with Internet Connectivity x 1
- Breadboards x 9
- Infrared Sensors x 4
- Micro USB Cable x 5
Wires (Male to Male, Male to Female)
Step 2: Enable SSH on Raspberry Pi
Communicating with Pi using SSH has the following advantages:
- Connecting peripherals like keyboard and mouse on Pi can be avoided.
- We can work on Pi remotely.
By default, SSH service on Raspberry Pi is turned off. To enable this service, the following steps are done:
1. Type the following command on the terminal
$ sudo raspi-config
(Note: You may enter $ sudo -i to enable root access for the user)
2. Select 'Interfacing Options' (#5) on the window that pops up.
3. Select 'SSH' (#2)
4. Click 'Yes' to enable the service.
5. Click 'OK'
6. Click 'Finish'
Step 3: Access Pi Through Putty
Putty is a terminal emulator.
1. Connect Raspberry Pi to a network router via an Ethernet cable
2. Enter the following command in the Pi's terminal to know the local IP address of the Raspberry Pi.
3. Note down the IP address that is written after "inet addr:"
4. Download and Install Putty on your computer from www.putty.org (Make sure that your computer is connected to the same network as the Raspberry Pi)
5. Open Putty, Select 'SSH' and type in the IP address that was noted down earlier.
6. Enter the log in credentials of Raspberry Pi in the terminal window that opens up.
Step 4: Understanding MQTT
MQTT is a light weight messaging protocol that stands for MQ Telemetry Transport. In an MQTT system, several clients or nodes may be connected to a broker(/server). The clients publish certain data as a message under a specific topic. Any other client which is interested in receiving that data, subscribes to the respective topic. All the data transfer is performed via the broker.
The following link would provide a deeper insight to the theory of MQTT: http://internetofthingsagenda.techtarget.com/defin...
Step 5: Installing Mosquitto on Raspberry Pi
Eclipse Mosquitto is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 3.1 and 3.1.1.
Type the following codes one by one:
$ sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
$ sudo apt-key add mosquitto-repo.gpg.key
$ cd /etc/apt/sources.list.d/
$ sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
$ sudo apt-get update
$ sudo apt-get install mosquitto
Step 6: Install MQTT Client
$ sudo apt-get install mosquitto-clients
Step 7: Testing of MQTT
1. Open two instances of Putty
2. Log in to Pi
3. In 1st instance of Putty, type the command with the topic name:
$ mosquitto_sub -d -t testmsg_mqtt
2. In the second instance putty2 window type the following command with the same topic i.e. :
$ mosquitto_pub -d -t testmsg_mqtt -m “message”
3. Now come back to the 1st instance of putty; here you will be able to see the published message
This concludes that you are running the Mosquitto broker successfully.
Note: If 'Error: Connection Refused' pops up, while executing the above commands, just write:
$ mosquitto -d
Step 8: Get Your Network's External IP
Almost all network routers that are connected to internet have two distinct IP addresses. One is known as the Gateway IP address and the other is known as the External IP address.
Gateway IP address is used in a local network (eg. 192.168.xxx.1), while the external IP address is used for the Wide Area Network (WAN). (eg. 167.165.xxx.45)
Note down your external IP from :https://myexternalip.com/ (Log on to this website from your Raspberry Pi or from any device that is connected to the same network as that of Raspberry Pi)
Step 9: Port Forwarding
PORT FORWARDING is performed so that external devices (NodeMCUs) can interact with the MQTT BROKER (Raspberry Pi) placed in a private network.
We need to change the port settings in the router to redirect messages/data sent to the router by publisher/subscriber to the Pi.
Step 10: Open the Homepage of the Network Router
The homepage of the network router can be opened by entering the Gateway IP address on your browser.
Step 11: Enter the Login Credentials
Note: The following screenshots and methods of performing the port forwarding is solely w.r.t. D-Link routers. The selection procedure for other routers may vary slightly.
Step 12: Select the Port Forwarding Option
Open the 'Advanced' tab and select 'Port forwarding' option.
Step 13: Click on Add to Create a New Rule
Step 14: Creating the Rule
1. In the “Select a Service” option dropdown, select Secured Shell (SSH)
2. You can use a port of your choice, to connect the publisher/subscriber to the broker. In the “External Port Start” section and the “External Port End” section, type the port through which the Publisher and Subscriber are connecting to the broker (Note – keep the External Port Start and External Port End same). Say 48500. Type the port no. 1883 (i.e default for SSH) in the “Internal Port Start” section
3. In the “Server IP Address (Host Name) :” section, type the local IP to which the Broker (i.e. Pi) is connected, eg 188.8.131.52.
4. Click on the apply button
Step 15: Understanding the Rule
- External Port: 48500
- Internal Port: 1883
- Server IP- 192.168.1.22
Understanding the parameters
The rule states that all the data coming on port # 48500 of the network router from any device( connected to any network with Internet connection) will be re-directed to port # 1883 of the local server at 192.168.1.22 (In this project, Raspberry Pi is the local server).
Step 16: Circuit Connections at the Sister Institution
The Control Room at each sister institution has the following circuit.
- Place the LEDs, resistor, IR Sensor and the NodeMCU on the breadboard as shown in the figure.
- Connect the anode of the LEDs to the Digital output pins D1, D2, D3, and D4 of the NodeMCU respectively.(The colour coding of the connecting wires may be maintained to avoid confusions upon the order of the institutes . For example:In the given circuit the wires are used in such a way that purple stands for Institute 1, White stands for Institute 2, Blue stands for Institute 3 and Red stands for Institute 4.)
- Connect the cathode of the LEDs to one pin of the resistor.
- Connect the other pin of the resistor to the ground pin(GND) of the NodeMCU.
- Connect the Vcc pin of the infrared sensor to 3V pin of the NodeMCU.
- Connect the ground pin of the infrared sensor to the GND pin of the NodeMCU.
- Connect the output pin to of the infrared sensor to the D7 (digital output) of the NodeMCU.
- The whole setup is powered via a micro USB Cable.
Step 17: Download PubSubClient Library
- Open Arduino IDE.
- Click on 'Sketch', go to 'Include Library' and select 'Manage Library'.
- In the Search bar, type 'PubSubClient'
- Select 'PubSubClient' library and click 'Install'.
- After installation, click 'Close'.
Step 18: Download and Execute the Sketch
Note: "institute_1_Sankalp.ino" has been commented for reference.
Step 19: Circuit Connections at Headquarters
The Control Room at the HQ has the following circuit.
- Place the LEDs and resistor on the breadboard as shown in the figure.
- Connect the anode of the LEDs to the GPIO pins 4, 17, 27, and 22 of the Raspberry Pi respectively.(The colour coding of the connecting wires may be maintained to avoid confusions upon the order of the institutes . For example:In the given circuit the wires are used in such a way that purple stands for Institute 1, White stands for Institute 2, Blue stands for Institute 3 and Red stands for Institute 4.)
- Connect the cathode of the LEDs to one pin of the resistor.
- Connect the other pin of the resistor to the ground pin(GND) of the Raspberry Pi.
Step 20: Python Script for Raspberry Pi As a Subscriber
A python script is executed on Raspberry Pi so as to receive data from the sister institutions. Firstly, a python code shall be created in the text editor which will subscribe to the topics of the 4 sister institutions.
The python script is initially written on Notepad++ software on computer and the file is saved with a '.py' extension.
1. Import MQTT and GPIO libraries (General purpose of input output)
2. Subscribe to the topics of sister institutions 1, 2, 3&4
3. Convert the incoming message/data into integer format so as to facilitate easy comparison.
4. According to the incoming messages for various topics set LOW or HIGH for the LED’s connected at various pins on the Pi.
5. Put the entire process inside a loop so that the process keeps continuing for the stream of incoming messages.
Step 21: Transporting the Python Code to the Pi(SFTP)
We shall use FileZilla for this process. The following steps need to be followed for the same:
1. Open Filezilla
2. Enter the PI’s host name (IP Address)
3. Next enter the username (default = “pi”), password (default = “raspberry”), set the port to 22 (sftp) and click “Quick connect”.
FileZilla will now display your local files on the left hand side and the files of your Pi on the right, navigate your local file system on the left to find the file that we just saved. Navigate the files of your Pi to where you want to save your code too then simply drag and drop the file across.
Step 22: Access Raspberry Pi Once Again Through Putty
- Run the Putty terminal emulator,
- Enter the IP address or hostname of the Raspberry Pi into the Hostname box
- Select SSH option
- Choose port 22
- Click open.
We now have access to terminal on your Pi and can execute the code by entering appropriate directory and the python file name.
Before executing the program, we need to perform Port Forwarding.
Step 23: Execute the Python Script
1. Enter the username and password
2. Navigate to the directory where you have transferred the python script by entering the following command:
$ cd /home/pi
3. Execute the program by entering the following command:
$ python3 test2.py
Step 24: Power Up All the Circuits
Screenshot of the Serial Monitor Output of Institution 1, Institution 4 and the HQ are shown for reference.