MySQL for the Internet of Things

(Steven Felgate) #1

Serial.println("ERROR: Cannot open file for writing.");
return false;
}


// Save the data to the database
bool write_data(int val, const char *dateStr) {
String queryStr = String("INSERT INTO plant_moisture.plants VALUES (NULL,");


queryStr += String(val);
queryStr += ",";
if (dateStr != "NULL") queryStr += "'"; // quote string for values
queryStr += dateStr;
if (dateStr != "NULL") queryStr += "'"; // quote string for values
queryStr += ")";
// write the data here
Serial.println(queryStr);
// Create an instance of the cursor passing in the connection
MySQL_Cursor *cur = new MySQL_Cursor(&conn);
cur->execute(queryStr.c_str());
delete cur;
}


// Read the cache file on disk and write rows found to the database
bool dump_cache() {
File log_file;
char char_read;
char buffer[128];
int i = 0;
int comma_pos = 0;
int val = 0;


// if no file, no cache!
if (!SD.exists(LOGFILE)) return true;
log_file = SD.open(LOGFILE);
if (log_file) {
// Read one row at a time.
while (log_file.available()) {
char_read = log_file.read();
if (char_read != '\n') {
// look for fist value
if (char_read == ',') {
buffer[i] = '\0';
val = atoi(buffer);
i = 0;
} else {
buffer[i] = char_read;
i++;
}
} else {
buffer[i] = '\0';
i = 0;


ChapTEr 8 ■ DEmonsTraTion of high availabiliTy TEChniquEs

Free download pdf