MySQL for the Internet of Things

(Steven Felgate) #1
Chapter 4 ■ Data transformation

Recording the Sensor Name or Adding Notes


The simplest form of annotation is adding a short string to the data stored. You may want to do this to ensure
the values stored are understood such as what event is being observed or which data collector (sensor)
generated the data. You may also want to add any notes to the data stored such as user input or maybe
subjective observations such as “cat slept in the planter.” The added data could be helpful when reading the
data at a later date, especially if you are writing the data to a file as either a log or data storage.
However, you must use this technique with some care. Adding too much text could make the data
harder to interpret or perhaps harder to parse should you need to do so in the future. Since adding strings is
easy, I will present some examples with a short explanation.


Code Implementation


Adding text to data stored in a file is not difficult. Listing 4-1 shows how to add text to a row of data in an
Arduino sketch. In this case, I add the data from two (simulated) sensor readings along with an explanation
of each. I also add statements for debugging (the Serial.print statements).


Listing 4-1. Simple Annotation (Arduino)


/**
Example of simple annotation.


This project demonstrates how to save data to a
microSD card as a log file with sting annotation.
*/
#include <SPI.h>
#include <SD.h>
#include <String.h>


// Pin assignment for Arduino Ethernet shield
//#define SD_PIN 4
// Pin assignment for Sparkfun microSD shield
#define SD_PIN 8
// Pin assignment for Adafruit Data Logging shield
//#define SD_PIN 10


File log_file; // file handle
String strData; // storage for string


// Simulated sensor reading method
float read_sensor(int sensor_num) {
if (sensor_num == 1) {
return 90.125 + random(20)/10.00; // sensor #1
} else if (sensor_num == 2) {
return 14.512313 + random(100)/100.00; // sensor #2
} else {
if (random(25) >= 5) {
return (float)random(14)/10.00;
} else {
return -(float)random(14)/10.00;
}
}
}

Free download pdf