MySQL for the Internet of Things

(Steven Felgate) #1

Chapter 4 ■ Data transformation


The third is another example of a derived value where we want to store the difference of the value read
from the last value read. This represents a host of annotations involving averages, running totals, and so on,
involving numeric data.
While all of these are technically changing or adding new data, we keep the original values to ensure we
can recover from any changes in the derivation or calculations. Also, we store these values to make reading
and processing the data easier.
Listing 4-8 shows an excerpt from an Arduino sketch that simulates and implements reading of three
sensors: a blood oxygen sensor that requires calibration,^8 a weight sensor measuring weight of several
objects, and a voltage sensor that we use to keep the delta since the last value read.


Listing 4-8. Derived and Calculated Annotations (Arduino)


/**
Example of derived or calculated columns annotation.


This project demonstrates how to save data to a
microSD card as a log file with additional column 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
float blood_oxygen;


// 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 94.512313 + random(100)/100.00; // sensor #2
} else if (sensor_num == 3) {
return 45.6675 + random(100)/100.00; // sensor #3


(^8) In this case, I use a simple calibration of shifting the value by a fixed delta. Calibrations of this nature are seldom.
Calibrations may be based on a linear scale (the inaccuracy gets worse or less as values increase) or could require a more
complex formula to correct the values.

Free download pdf