MySQL for the Internet of Things

(Steven Felgate) #1

All that is left now are the details of communicating with the serial monitor, timer expiration, and
blinking the LED—all of which should be familiar. In any case, Listing 8-6 shows the completed code for the
redundant data collector with automatic failover.


Listing 8-6. Redundant Data Collector with Failover


/**
Example Arduino redundant data collector


This project demonstrates how to create a data
collector master/slave for redundancy. If the master
fails, the slave will take over.


The sketch has both the master and slave code with the
default the slave role. To use the sketch as a master,
comment out the #define SLAVE.
*/
#include <Wire.h>


// TODO: Add MySQL Connector/Arduino include, variables here


#define SLAVE


int address = 8; // Address on I2C bus


// Blink the LED on pin 13
void blink() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
}


// Record data to database
// Stubbed method for writing data
void recordData() {
// TODO: Complete this code for your sensor read and the write to MySQL.
}


#if defined SLAVE
unsigned long startTime; // start of simple timer
unsigned long elapsedTime; // number of milliseconds elapsed
unsigned long maxTime = 10000; // timeout value (10 seconds)


// Get the heartbeat message from the master
void getHeartbeat(int chars) {
char ch;
while (Wire.available()) {
ch = Wire.read();
}


ChapTEr 8 ■ DEmonsTraTion of high availabiliTy TEChniquEs

Free download pdf