The RF network that will be used for the sensor nodes is provided by the RF24Network library (https://github.com/TMRh20/RF24Network). This allows RN nodes to be networked in a tree structure in which each node can have up to five child nodes, since each individual node can listen to up to six other nodes at once.
As such, the addresses for the nodes are octal and follow the structure, as shown in the following diagram, where 000 is always the base node, 001 is a child of the base node, 021 is a child node of 001, and so on:
This allows a message to be passed to any node on the network by first transmitting it upwards through the tree until it reaches the first node that is a common path for both the sending and receiving node.
To set up the Pi as the base node for the RF network, we must first connect the RF module to the GPIO port and install the drivers that will allow us to receive messages send to it by using a Python script. To do this, perform the following steps:
sudo apt-get install libboost-python-dev git git clone https://github.com/TMRh20/RF24.git cd RF24 sudo make install cd RPi/pyRF24 sudo python setup.py install
cd
git clone https://github.com/TMRh20/RF24Network.git RFNetwork2 mv RFNetwork2/RPi/RFNetwork ~ cd RFNetwork sudo make install cd RF24Network2/RPi/pyRF24Network sudo python setup.py install
BaseNode_RPi.py
script from the rf_network
directory in the files for this project to the home directory on the Pi.sudo python BaseNode_RPi.py localhost 1338
This should give an output similar to the following. If you see a repeating pattern in the configuration data or all the bits are set to the same value, then you may have an issue with the wiring between the RF module and the Pi.
Assuming that the output of the script was similar to the previous screenshot (such that the values are not all 0x00 or 0xFF), we can go ahead and configure the base node script to start when the Pi boots. To do so, perform the following steps:
rc.local
file in nano
:sudo nano /etc/rc.local
exit 0
line:python /home/pi/BaseNode_RPi.py localhost 1883
This is shown in the following screenshot:
Once this is complete, reboot the Pi to run the base node and web application on boot. This completes the configuration that needs to be done on the Pi.
It's now time to program each of the RF nodes that will be used in the sensor network. We can program these using the code in the rf_network/SensorNode_Arduino
directory in the files for this project and the Arduino IDE.
You should already have the Arduino IDE installed from when it was used in a previous chapter. However, we first need to download some additional libraries for the RF module:
sketchbook/libraries
directory. The sketchbook directory is where the Arduino IDE stores saved code files by default and where it searches for third-party libraries. By default, this folder is in your home directory.When doing this, ensure that the program used to unzip the archives does not create an additional directory (this is known to happen when you unzip them with Windows Explorer). The directory structure should be similar to that shown in the following screenshot:
Next, using the following steps, program each Arduino by using the sensor node code based on the system structure diagram given earlier in this chapter:
SensorNode_Arduino.ino
code in the Arduino IDE.THIS_NODE_ADDR
variable to the address of the node you have connected. Note that having 0
at the start of the address is required, that is, if the address is 23, you must use 023
as the value.NUM_SENSORS
and sensors
variables as described in the Interfacing sensors section.Once all the sensor nodes are programmed and tested, they can be installed in their intended positions. I found that Blu-Tack is a good temporary fix for the nodes that are to be placed above the doors.
3.146.107.89