MySQL for the Internet of Things

(Steven Felgate) #1
Chapter 4 ■ Data transformation

Data from Multiple Nodes


Closely related to data from multiple sensors is data collected from several nodes. Rather than reading
sensors directly, the node is getting data from other devices. For example, you may have an Arduino
connected to several modules wirelessly using XBee modules. Since the XBee modules can only send data,
you need the Arduino node to gather the data and store it—either in a file locally or in a database.
The same considerations apply as we discovered with data from multiple sensors. It is most likely each
XBee module is sending data at regular intervals and may include multiple pieces of data (multiple sensor
data). Thus, how to save the data has the same implications—storing the data together as a single row or
storing the data separately.
So far, nothing is different in principle from the last form. However, collecting data from multiple nodes
has another capability that can be exploited—saving data in bulk. Recall from a previous example, the
MySQL INSERT SQL statement permits storing data for several rows in a single command. By collecting data
from multiple nodes, we can build such statements easily.
Listing 4-12 shows an excerpt from an Arduino sketch that gets data from multiple XBee modules and
saves the data in a MySQL database.^10 In this example, I keep things rather simple where we are reading
sensor data generated from multiple data nodes represented as an XBee module with a temperature sensor.
The code is designed to bulk insert data into the database. I have set the number of rows included at three,
but you can easily expand this code to as many rows as you have memory to store the data.
So how do we know which XBee was read? We save the address of the module—all XBee modules have
a unique address, and thus we know the origin of the temperature data. We also see a case of performing
calculations on the data node where we store the temperature in its raw sensor form as well as in Celsius and
Fahrenheit. I should also note that with multiple XBee modules connected, it is possible to gather data from
the same module twice depending on the timing of when the XBee modules send data.


Listing 4-12. Getting Data from Multiple Nodes (Arduino)


String get_data(ZBRxIoSampleResponse *ioSample) {
// Received data from address of data node
int address = (ioSample->getRemoteAddress64().getMsb() << 8) +
ioSample->getRemoteAddress64().getLsb();


// Get and calculate the temperature in C and F
float temp_raw = ioSample->getAnalog(3);
float temp_c = ((temp_raw 1200.0 / 1024.0) - 500.0) / 10.0;
float temp_f = ((temp_c
9.0)/5.0) + 32.0;
String strRow = String("(");
strRow += String(address);
strRow += ",";
strRow += String(temp_raw);
strRow += ",";
strRow += String(temp_c);
strRow += ",";
strRow += String(temp_f);
strRow += ")";


return strRow;
}


...


(^10) Yes, you can do that with an Arduino!

Free download pdf