Building a Wireless Sensor Network in Your Home
This tutorial will teach you how to create a network of cost effective, wireless sensor modules. Many of the ready-made wireless modules can be too expensive for use in multiple sensor units. While Wi-Fi and Zigbee modules are popular and easy to use, their cost can make their use in an array of sensors impractical. There are, however, low cost RF modules without all of the expensive features that will work just fine these purposes. The Arduino platform can use low end radio modules to communicate easily and reliably.
Once the hardware is assembled, you will leverage the Raspberry Pi platform to post the data to Xively’s Internet of Things service where you can track the values over time and trigger on threshold conditions.
Gather the Components and Tools
This project will include three parts, two transmitter units and one receiver unit. The transmitters are made up of an Arduino board, sensor, and RF transmitter. The receiver unit is composed of an Arduino board, an RF receiver, a logic level converter, and a Raspberry Pi.
There is a wide range of Arduino compatible boards that can be used for this project. The minimum requirement for the board for this project is two digital GPIO pins and one analog pin. Select an Arduino compatible board that matches the cost and simplicity needs for this project.
In this tutorial I’ve used a set of Arduino Nano boards that fit well in a breadboard and are simple to program, However, the 5V versions of the Arduino pro mini or Trinket will also work well and at a much lower price point. These however require a little more experience to program and use. Choose wisely.
- Raspberry Pi—Model B with power supply, case, and SD card—Ensure the GPIO pins are accessible with the case you choose
- Pi compatible Wi-Fi adapter
- Breadboard jumper wires
- Male to female jumper Wires
- Logic level converter
- 434 MHz RF receiver
- Half breadboard
Transmitters (two units)
- 2 x Arduino
- 2 x Temperature / humidity sensors
- 2 x 434 MHz RF transmitter
- 2 x Half breadboard
- 2 x Breadboard-friendly 2.1mm DC barrel jack
- 2 x 9V power adapter with 2.1mm x 5
- Breadboard jumper wires
- PC/Mac with Arduino programming IDE installed
- USB cable for programming Arduino boards
- USB Keyboard and Mouse. Either combined or with a hub so both can be used with a single USB port.
- The source package associated with this tutorial on your programming workstation
The transmitters themselves are relatively simple circuits. Only one pin is used to retrieve the information from the temperature and humidity sensor and one pin is used to send that data to the RF transmitter. The breadboard diagram is shown below.
The 9V power supply will attach to the barrel connector making the bottom rails 9V. The power regulator in the Arduino will produce 5V that is safe to use for the radio and sensors, the top power rail of the diagram.
The sensor comes with a 10k ohm resistor that connects the data pin to power as a pull up resistor while another wire connects it to GPIO D3.
Be careful to follow the setup below and make sure you double check the datasheet for the sensor and RF module to ensure that the components are positioned in the breadboard properly and the power, ground, and signal pins are connected to the right pins. The fritzing diagram is included in the source package for more detail.
The antenna is an important part of the board because the RF module does not have a built in antenna. I used a 6-inch female to male jumper wire plugged into the breadboard and it worked well enough to allow reception from all parts of my home and a little bit outside. As noted in the diagram, 6.5-inch is optimal for this antenna if you need additional range.
A note about RF use. There are different laws and rules about use of frequencies in different countries. Please ensure you are in compliance with these rules before broadcasting. That being said, the signals from these modules are barely powerful enough to pass outside of your home. In perfect conditions, though, these modules can broadcast up to 500 feet.
Download Libraries for Components
The transmitter uses two libraries that are not bundled with the Arduino IDE. Download the libraries as described below and uncompressed them into your sketch directory in a subdirectory named Libraries.
- Download the VirtualWire source package for this tutorial and unzip the wirelesstransmitter sketch folder into your Arduino sketch folder
- In the wirelesstransmitter folder create a folder named Libraries
- Download the latest version of the VirtualWire code, 1.23 as of this writing, from the project page
- Extract the VirtualWire folder into the wirelesstransmitter/Libraries/ folder so you have another subfolder named VirtualWire
- Download the DHT sensor library from its project github page
- Extract the DHT folder into the Libraries folder as well. You should now have the two required library folders DHT and VirtualWire in your wirelesstransmitter/Libraries folder.
Program the Arduino Board
This tutorial assumes you have some experience with Arduino and how to program them using the Arduino IDE. If you do not, there are very good instructions at the official Arduino site.
- Open the wirelesstransmitter sketch from the source archive in the Arduino IDE and save a copy locally
- Ensure the Arduino is NOT connect to power via the barrel connector
- Connect the board to your programming workstaiton station with an appropriate USB cable
- Set the board type to your selected Arduino board under the Tools > Board menu
- Set the serial port to the port detected when you connected the Arduino board under the Tools > Port menu
- Ensure that the MYID define is set to 1 and the TRANSPIN and DHTPIN are properly set to the pins connected to the RF transmitter module and DHT sensor respectively. If you built your board per the diagram above this should all be set already. See the code example below.
- Ensure that the UNIT is set properly for your preference Fahrenheit or Celsius.
The MYID define is a numeric ID that the transmitter uses to uniquely identify itself. Because you will have multiple transmitters in different locations it is important to have a unique ID for each one. This number will be used again when you setup the receiver script.
- Verify the code by pressing Control-R to ensure the libraries are included and compiled properly.
- Push the code to the board by clicking the Upload button on the tool bar.
- Open the Serial Monitor windows by pressing Control-Shift-M
The Serial Monitor window resets the Arduino so you should see a line of code on the screen that looks something like:
The message is composed of Name:Value pairs that the receiver will handle. The transmitter will read and broadcast its signal on a long random interval. The sensors don’t change very much or frequently, so broadcasting more often than once every minute adds no value. The random wait time is to allow multiple sensors to coexist.
Even if there is doubling and the signal from both transmitters is lost, the random interval will ensure that their next broadcasts will not overlap. The random seed for this interval is set from an analogRead on an unused analog port which will return random values to ensure no two transmitters are on the same pattern.
The example code generating the output above is set to use Fahrenheit. You can see the TF:60.79 identifier in the message string indicating that my lab is indeed just a hair under 61 degrees. However the relative humidity RH:44.00 is a comfortable 44%. One might infer from the cool damp environment that my lab is in my basement. One might be right.
The transmitters are set to wait 2 to 5 minutes between broadcasts by default. If you wish to speed this up for debugging purposes then modify the delay() value at the end of the sketch to be more like 5000 (ms). It is highly recommended that you change this back and re-upload the code to your transmitters when you are ready for full time use.
- Build the second transmitter board
- Modify the transmitter sketch so that the MYID define is set to 2
- Upload the code to the second board
- Open the Serial Monitor windows by pressing Control-Shift-M and verify that the output looks like the first transmitter board with the exception that the transmitted message starts with ID:2
Build the Receiver Board
The receiver board will be responsible for receiving the broadcast message on its RF Receiver component and sending that message over serial wires to the Raspberry Pi. The Arduino board is used to receive the signal for a couple very important reasons. The VirtualWire code uses the real time nature of the Arduino to manage modulating and demodulating the signal.
This means that the receiving unit needs to be operating at the same frequency. In addition, there is little room for jitter on the receiving processor, to which the Raspberry Pi is prone, due to its preemptive, non real-time operating system. Comparing the costs of an Arduino Pro Mini plus the RF receiver module to that of a Zigbee module that could talk directly to the Raspberry Pi revealed that using an external Arduino was still quite economical.
At this point do NOT attach the 5V and ground leads from the Pi to the breadboard. Keep the jumper wires handy, but you don’t want to power the Arduino from both the USB port and the Raspberry Pi.
Note that the logic level converter in the materials list above is not exactly the same as the one in the Fritzing library, but the pin outs are well labeled, just in different places. Please make certain that the correct wires are connected to the correct pins on the actual logic level converter.
This component is required in order to convert the 5V Arduino serial signal to a 3.3V Raspberry Pi serial signal and not damage the Pi. See the image below for additional help.
For more detail: Building a Wireless Sensor Network in Your Home