Wireless Communications With ChibiArduino 03: Intro to Wireless Sensors

Intro

Wireless sensors is an amazing field that enables a lot of applications that normally wouldn’t be able to happen. You can monitor environmental parameters in each room of the house and have that information sent to a dashboard in the main area of the house. Or sense environmental parameters on a volcano and have the data sent to the internet for remote monitoring half a world away. Those are the kinds of things we can do with this technology if we understand how to use it properly. For this tutorial, we’re going to monitor a simple sensor and transmit the data to a receiver connected to a computer. This can easily be a raspberry pi or a similar low cost, low power computing device as well. We’ll just be printing the data out, although we can take things further and have that data sent to the internet as well.

Hardware

In this tutorial, we’re going to use the same hardware which is the FreakUSB 900 and Freakduino 900 combo. It’s quite versatile and useful for a variety of applications. That way, it’s also possible to go through the whole battery of tutorials just using one set of equipment without having to buy a lot of extra equipment. These tutorials can also be done using the Freakduino and FreakUSB 2.4 GHz versions.

We’ll also be using the DHT11 humidity and temperature sensor in this tutorial. This is a low cost sensor that can provide temperature and humidity data using a special one wire protocol. The accuracy of the sensor is +/- 1 degree Celsius for tempertaure and +/- 5% for the relative humidity. There are already many Arduino libraries designed to communicate with these devices. We’ll be using the Adafruit DHT11 library which is quite stable and performs well.

We’re going to need to interface the DHT11 to the Freakduino which is quite straightforward. The interface only consists of three wires. We will need to connect two wires up to +5V and GND. These are located on the Freakduino at the 5V pin and the GND pins. Then we connect the wire up to any pin that we’ll specify. In our case, we’ll use pin 2 which is the default pin in the Adafruit software. Once that’s done, we should be ready to go with the temperature/humidity sensor hardware.

Tx Code

Most of the new work for this tutorial will be done on the transmit side of the communications chain. In this case, the transmitter will be the Freakduino which will be transmitting the sensor data to the FreakUSB receiver which is attacehd to the PC. Once the sensor is interfaced to the Freakduino, we’re ready to start on the coding side of things.

In the first part of the code, we’re going to bring in our libraries. Along with the chibiArduino library, we’re also going to bring in the DHT library from Adafruit. This is one of the benefits of using the Arduino platform. We have access to a huge array of software libraries for parts which will drastically cut down on our coding time. This allows us to focus on working on applications rather than writing low level drivers for each part that we’re going to use. For the chibiArduino stack, we can bring it in using #include “chibi.h”. For the DHT library, we bring it in using #include “DHT.h”.

There are two new defines we’re also introducing too. To use the DHT11 sensor, we have to specify the DHT sensor type and the pin that the DHT is connected to. In this case, we’re going to specify the DHT11 sensor and the pin will be connected to pin 2 which is default for this library. We can use any other pin as well, but let’s just keep things default.

Once that’s configured, we can instantiate the DHT object which means creating the DHT sensor. The DHT uses a special protocol which only requires a single wire. All of the details of the protocol are kept inside the DHT library file so we just need to create the DHT object and then we can start using it. In creating the object, we need to configure things properly which means we need to specify the DHT sensor type and the DHT pin. Here we just use the defines we specified a few lines up to create the object. Using the #defines or macros as they are commonly called, makes our code much easier to read and we can keep all our critical parameters in a single place.

Finallly, we create our byte buffer which uses the standard payload size. We won’t use the full payload size of the byte buffer, but its allocated and there if we need it.

In the setup, along with the chibiInit() to initialize the chibiArduino stack, we need to initialize the DHT sensor. To do this, we call the dht.begin() method which will start up the sensor and get it ready to start sensing.

The loop code is where the real work begins. We’re going to read the sensor once per second and transmit it to the receiver. First, when we read the data from the receiver, it will return the humidity and temperature data as float values which require 4 bytes. These float values are decimal values but we don’t really need that kind of precision, especially because the sensor isn’t super accurate. In this case, I’d rather simplify things and round the values to an 8-bit value (between 0 and 255) that we can store inside our byte array. Because of that, I’m rounding the value of the humidity and temperature to the nearest integer and then storing it in a single byte in our byte array. This makes it easier for us because we just need to read out a single byte from the byte array for the temperature and a single byte for the humidity.

Once we have our values stored in the byte array, we can transmit it. We call the chibiTx function with the destination address, byte array, and a value of 2 bytes for our parameters. The reason why we’re only using 2 bytes for our length parameter is because we were able to store our temperature as a single byte value and our humidity as a single byte value. This is because tho

Rx Code

The receive side is something that we’ve seen quite often. We’re basically checking to see if data was received. If data has come into the device, then we retrieve the data and process it.

In the first line, we bring in the chibiArduino library by using #include “chibi.h”. This includes the chibiArduino library and gives us access to its functions.

The #define macros are something we’ve seen before where we set the address of this device and also the maximum payload size. We’re not going to use it, but by now, you can see that its something that is a standard template for our wireless code. Finally, we create the byte buffer that we’re going to use to retrieve the data.

In the setup, we initialize the serial port with a speed of 57600 bps which is the standard speed I normally use for my serial communications. You can use whatever speed you want, but I recommend standardizing on 57600 which is reliable and fairly fast.

After initializing the serial port, then we initialize the chibiArduino software and set the short address of the receiver. Setting the address ensures that we will receive the packets that are sent to us.

In the loop code, we will be going through our standard receive procedure. The first thing we do is check to see if any data is received via the chibiDataRcvd() function. If data has been received, ie: the function returns true, then we retrieve the data using the chibiGetData() function. That function also returns the length of the data received. We want to make sure the length is nonzero indicating valid data has been received. In this case, if data has been received, we assume it’s from our wireless humidity and temperature sensor. That means the first byte will be the humidity and the second byte will be the temperature. Since we know the format, we can print out the labels and values to the screen. In actual production code, we’d at least check to make sure the data length is what we expect so we don’t try to parse values that might not be from our sensor.

Application

To demonstrate how this can be used in a real application, we have a specialized application where we are measuring the temperature and humidity of a seedling tray inside an aquarium. These are seedlings that we are growing indoors in an automated way so it doesn’t require too much intervention. We’re constantly monitoring the temperature and humidity so we have a record of the conditions each batch of seedlings grow in. That way, we can check quantitatively if we have a particularly good or bad batch of seedlings. We timestamp the data as it comes into the receiver and then store it into a single file along with a backup of the file at a different location. The data format is CSV or comma-separated values and is a standard format for many programs including Microsoft Excel. If we just need a simple chart, we can visualize this data in an Excel spreadsheet. If we need something more sophisticated, we can bring it into many different programs or simply write our own to process and visualize the data using a built-in CSV parser library, commonly found in languages like python, C++, and java. Timestamped data like this, also known as time-series data, is an essential part of data collection for engineering or scientific research. Although this might seem like a trivial example, you’d be surprised on how many insights you can get when you start collecting and visualizing data.

Now that we can receive values from our wireless sensor on our PC, there are quite a few things we can do with it. We can timestamp it and then either send it into a local or remote database on the internet. That way, we can view graphs of our humidity and temperature as they vary with time from a web browser. We can also take the humidity and temperature data and act on it. It could be something as simple as turning on a fan when the temperature or humidity gets above a certain threshold to something as complex as modulating a heating or cooling element to maintain temperature to +/- 1 degree Celsius set point.

These are just a few ideas. Once you start remotely monitoring sensors, many new possibilities for applications will open up.

Support FreakLabs

If you enjoyed this tutorial and are curious to learn more about wireless, please show your support by checking out the FreakLabs shop. All the boards used in this tutorial can be purchased here . Thank so much!