Introduction: Sensor Stations Network for Lighting and Security Control

About: IT Teacher (Andalucía, Spain)

With this sensor stations network configured in a master/slave mode, you will be able to do lighting and security control in your home. These sensor stations (Node01, Node02 in this project) are connected to a master station (Node00) connected to your local wifi network. I have installed the Node01 in my storage room and the Node02 in my garage controlling lights and motions. The master station is installed in our dinig room connected to our wifi router. I have installed the bell in the garage and a little buzzer in the master station to sound when a motion is detected by some sensor station in the network.

The main features of the network are:

  • It is possible to configure a network with more than two sensor stations (slave stations) (Node01, Node02, Node03, ....)
  • Because the use of wireless transceivers with antenna, the network is able to cover a wide area
  • You can control the whole network from a mobile phone
  • Send a email (Gmail account) when a motion is detected and the alarm mode is enable. So if you enable the Gmail push notifications in your mobile you will know when a movement is detected in your network

for domotic purposes:

  • Switch on the lights when a motiont is detected / switch always on the lights (for the whole network or for each slave station)
  • Change the time in minutes the lights is on after a motion has been detected for each slave station

for security purposes:

  • Enable and disable the alarm mode
  • Each slave station is able to send a signal to a master station (Node00) when the alarm mode is enable and a motion is detected
  • The master station is able to activate a bell and send a email when one of these signal is received from any slave station and the alarm mode is enable. The master station (Node00) is connected to internet using a wifi connection to control the whole system using a mobile phone. It is configured like a WifiWebServer

Step 1: List of Material

Step 2: How to Connect and Program Node01 and Node02 (Slave Stations)

To program the sketch "SlaveSation.ino" I heve needed the RF24 library and the RF24 Network library.

In the flow chart above you can see the logic of the sensor station and the messages exchanged between a slave sensor station and the master one.

Before you load the sketch you have to configure the address of the slave node in octal format

const uint16_t this_node = 01; // Address of our node in octal format (slave): 01, 02, 03 ...

The meaning of the leds in each slave stations is the following:

  • Blue led. It will be on while the PIR sensor pin is high.
  • Green led. It will be on when the alarm is connected.

Step 3: How to Connect and Program Node00 (Master Station)

In the Node00 station we can find two different boards:

  • MEGA 2560 R3 board. This device is used to receive messages from slaves station when a motion is detected using wireless communicaction. It will activate a bell when a movement is detected and the alarm is connected. To stop the bell just only disconnect the alarm mode. Moreover it sends to the slaves station messages from NodeMCU to enable or disable motion detection, to turn always on the lights, to turn always off the lights, ...
  • NodeMCU Lua Amica V2 ESP8266 board. It works like a Wifi Web Server to transmit and receive commands from a remote place using a Web page in a mobile phone. Moreover sends e-mail messages when a motion is detected and the alarm mode is enabled.

Both devices are programmed to communicate with one another in a Master(NodeMCU)/Slave(MEGA) configuration via the I2C synchronous serial protocol using the Wire Library. The I2C protocol involves using two lines to send and receive data: a serial clock pin (SCL) and a serial data pin (SDA) over which data is sent between the two devices.

As you can see in drawing, the two pins for the I2C communication in the MEGA 2560 R3 board are:

  1. SDA -> pin 20 SDA
  2. SCL -> pin 21 SCL

and the other ones in the NodemMCU are:

  1. SDA -> digital pin 1
  2. SCL -> digital pin 2

Before you load the sketch for the ESP8266 you have to install the ESP8266 board. To do that you have to enter "http://arduino.esp8266.com/stable/package_esp8266com_index.json" into the “Additional Board Manager URLs” in the preferences window in the Arduino IDE. After that you have to open boards manager and install "esp8266"

I use a external power supply adapter (9V , 1A) for the MEGA 2560 R3 board (DC power jack, 2,1mm x 5,5 mm) and I connect the NodeMCU board to the 3V3 pin in the MEGA board.

Step 4: WifiWebServer (NodeMCU in Master Station)

The sketch loaded into the NodeMCU (Node00 or master station) is configured like a WebServer connected to your home wifi network. It is possible to access remotely if you configure port forwarding in your router.

Before you load the WifiWebServerV3.ino sketch to the NodeMCU you have to do the following:

  1. Update the port you are going to configure in your router (port forwarding) to access remotely to the NodeMCU
  2. Update the user and password to access to your home wifi network
  3. Update the login and password of your Gmail account to send e-mails when a motion is detected and the alarm is connected
  4. Update the e-mail address where you will receive the e-mails

To update the user, password and the port the server will use to listen to, you have to search for the following code in the WifiWebServerV3.ino sketch and update it:

const char* ssid = "*** your ssid ***";
const char* password = "*** your password ***";
WiFiServer server(80);

To update the e-mail address where you will receive the e-mails you have to search for the following code and update it:

const char* to_email = "*** e-mail address where you will receive the message ***";

To update the user and password of your Gmail account to receive e-mails you have to search for the following code in the file "Gsender.h" and update it:

const char* EMAILBASE64_LOGIN = "*** your e-mail address encode in BASE64 ***";
const char* EMAILBASE64_PASSWORD = "*** your password encode in BASE64 ***";
const char* FROM = "*** your e-mail address ***";

To encode or decode data in BASE64 I have used www.base64encode.org

If you need more information about to send and receive e-mails with the ESP8266 there is an "Instructable" about it here

Once you can do it, you can test the connection in your router. If the device is connected you have to reserve the assigned IP address to this device. Moreover it is a good idea to talk with your Internet provider to set a static IP address on your router.

To access the web page near your wifi router, just only type the local IP address assigned by your router following the port you are using in a web navigator. For example: 192.168.40.15:800. If you try to access remotely you have to type the IP address of your router following the port you are using like above.

The web page I show you in the images is programmed to control a network with two slave stations.

The system is initially configured to control 5 slave stations maximum, but you can modify it. Just only search for the following code in WifiWebServerV3.ino and SlaveStation.ino

#define number_max_slaves 5

The web page initially is divided in 4 section:

  1. Section "State" where it shows you if the alarm is connected or not
  2. Section "Alarms counter" where it shows you the number of motion detection in each slave station
  3. Section "Alarm" where you can connect or disconnect (CONNECT ALARM/DISCONNECT ALARM) the alarm and reset the alarms counter (RESET COUNTERS)
  4. Section "Lighting" where you can control the lights of each slave station. You can control the following:
    • Switch on the lights in all slave stations / switch on the lights only when a motion is detected in all slave stations (NETWORK ALWAYS ON/NETWORK WHEN MOVEMENT)
    • Change the time in minutes the lights is on for each slave station (NODExx ON 1 MINUTE/NODEXX ON 2 MINUTE)
    • Switch on the lights for each slave station (NODExx ALWAYS ON)
    • Switch on the lights for each slave station only when a motion is detected (NODExx_MOV)

You don´t have to change anything beacause it will work whatever the name of slaves stations you configure. Moreover it will automatically detect the number of slave stations connected in the network.

Of course, you can download the sketch and easily to modify the code necessary to show your own web control page.

Step 5: From the Beginning ...

Here you are the steps you have to follow to build your network:

  1. Buy all the material following the list I give you in the "Step 1"
  2. Connect the components following the drawing you can download in the "Step 2" and "Step 3"
  3. Build boxes for each slave station and the master station. I have builded methacrylate boxes.
  4. Configurate each slave station:
    • Download and load the sketch "SlaveStation.ino" in one MEGA 2560 R3 board. You only have to edit the sketch and update the address of the slave station as you can see in the "Step 2"
  5. Configurate the master station:
    • Download and load the sketch "SlaveStation.ino" in one MEGA 2560 R3. You don´t have to change anything whatever the number of slaves nodes of your network. Initially the sketch is configured for a maximum of 5 slave stations.
    • Download and load the sketch "WifiServerV3.ino" in the NodeMCU. You have to update the sketch following the instrucction in the "Step 4"
  6. Configure your router to access remotely to the NodeMCU (port forwarding, static IP ...)

That´s all folks. I hope you enjoy it.

Safe and Secure Challenge

Participated in the
Safe and Secure Challenge