MySQL for the Internet of Things

(Steven Felgate) #1
ChapTEr 8 ■ DEmonsTraTion of high availabiliTy TEChniquEs

#define LOGFILE "cache.txt" // name of log file for caching data


int sensorPin = 0; // Pin for reading the sensor


RTC_DS1307 rtc; // real time clock


byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server_addr(10,0,1,8); // Supply the IP of the MySQL server here


char user[] = "root"; // can be anything but the user must have
char password[] = "secret"; // access rights to connect (host must permit it)


EthernetClient client;
MySQL_Connection conn((Client *)&client);


// Get the date and time from the RTC
String get_datetime() {
DateTime now = rtc.now();
String dateStr = String(now.year());
dateStr += "-";
dateStr += now.month();
dateStr += "-";
dateStr += now.day();
dateStr += " ";
dateStr += String(now.hour());
dateStr += ":";
dateStr += String(now.minute());
dateStr += ":";
dateStr += String(now.second());
return dateStr;
}


// Save the data to the SD card for later processing
// Note: What happens when the disk is full?
bool cache_data(int value) {
File log_file;


String row = String(value);
log_file = SD.open(LOGFILE, FILE_WRITE);
if (log_file) {
// Get datetime
row += ",";
row += get_datetime();
// Save data to the file
log_file.println(row);
log_file.close();
Serial.println("Data cached to log file.");
return true;
}

Free download pdf