With advances in microcontrollers and Bluetooth® technology, creating a network of remote sensor devices that report their data to a control station has become a simple matter. This article will show how to make a small network of 2 sensors and a central device that presents collected data on a screen. It will include step by step instructions to setup this project, as well as notes on expanding and changing it to better fit your needs.
Project overview
As can be seen from the diagram, this project consists of 3 separate modules:
-
The master module consists of a Mikromedia board and an RN4871 click that is configured to act as a master device. After clicking the corresponding button on the Mikromedia touchscreen, request for data is sent to a remote sensor, and after a response is received, data is printed on the screen.
-
Sensor 1 module consists of two click boards mounted on a clicker 2. First click is a Thermo K click – a temperature sensor, that acquires data when polled. The second click is an RN4870 Bluetooth click that is configured as a slave device. It will wait for requests from master module, ask Thermo K for data upon receiving such request, and then send the data back to the master.
-
Sensor 2 module works exactly the same as sensor 1, but uses RN4678 as Bluetooth click, and Earthquake click as a sensor. Try shaking the device gently to get a simulation of an earthquake and some data, or try any other sensor click you like instead!
Hardware setup
-
Connect Mikromedia+ for PIC32MX7 to corresponding Mikromedia Shield. Put RN4871 click into slot 1 on Mikromedia Shield.
-
Put RN4870 into slot 2 of one of the clicker 2 boards. Put Thermo K click into slot 1. This is sensor 1 module.
-
Put RN4678 into slot 2 of the other clicker 2 board. Put Earthquake click into slot 1. This is sensor 2 module.
Now that the hardware is in place, it is time to load up the code. Let's see what's inside...
Explore the code
-
Sbtp Parser
We will have a look at the sbtp parser first, as it is the simplest part of the code. To send data safely over Bluetooth, and prevent corruption, as well as make sure that the receiver knows what kind of data it is receiving, and from which sensor, it is necessary to create some kind of a protocol. This project uses custom-made Simple Bluetooth Protocol (SBTP), which is explained in detail in the header file of the parser. The parser deals with recognizing if the sentence passed to it is correct, and then putting the data into matching fields of a data structure. Such data can then be retrieved by the other layers of the application.
-
Hardware layer
Hardware layer contains all the functions that send commands to the Bluetooth click. It contains Bluetooth initialization function, a function for connecting and sending data to other Bluetooth clicks, and function for reading from the data stream, and passing the sentence to parser after it is complete.
-
Upper layer (Sensor and Master modules)
While the previous two layers are compatible with any platform and purpose, this layer deals with things more specific. It sets identifiers and addresses for clicks used in the project, initializes services that are required (such as UART or I2C), sets interrupt and pin settings, and calls functions from other modules with parameters specific for a particular purpose.
Listening for messages from other modules is done in the following way: Interrupt function will send all received data to the hardware layer. Hardware layer then performs only basic checks to see if received data belongs to a valid sentence, to avoid parsing just anything that comes via the data stream. If the hardware layer detects that a sentence has been received (marked by the '#' character at the beginning, and 'rn' sequence at the end), it will set the flag that the sentence is ready. Another function, called from the main loop, will periodically check the flag, and if the sentence is ready, it will be sent to the parser for detailed checking and processing.
If the module in question is a master module, upper layer also deals with triggers (such as button press) that will start the communication, and send a request for data to sensor modules.
Flexibility and interchangeability
This example is specifically designed to be easily expanded or modified in a number of different ways:
-
Different MCU or architecture – Most of the code in the project is independent of the platform used. Libstock already contains examples for several other architectures, and all that is required to change to any platform is to change interrupt, pin, and uart settings.
-
Different sensor clicks – Instead of clicks used in this example, it is possible to collect data from any of the sensor clicks available. This project contains an example of implementing sensor clicks that have a complete library and can be implemented with a simple function call, such as the Earthquake click. Another option is modifying existing code of the clicks that only have examples, and creating a seperate driver file for such click, like Thermo K used here.
-
Adding more sensors or masters – It is possible to add a large number of additional sensor or master devices – protocol allows for 255 unique Ids for Bluetooth devices, and can be easily expanded to allow for even more. All that is required for adding additional modules into the project is to copy existing code, and change address values to something new (as well as to change the interface).
-
RN clicks interchangeability – Although Bluetooth clicks used in this project have different characteristics, their functioning is similar, and they are therefore often interchangeable. All that is required from you in order to use different RN click than the default one is to change the parameter of initialization function (and set required pins if the click in question is RN4678).
Summary
With the ever-growing number of click boards and development systems, a clever engineer can think of an unlimited number of ways to utilize them. This project demonstrates how to make a simple wireless sensor network, which can then be easily expanded and modified, to serve any purpose.