MySQL for the Internet of Things

(Steven Felgate) #1

ChApTEr 3 ■ how IoT DATA Is sTorED


// Begin writing rows to the file


log_file = SD.open("log.txt", FILE_WRITE);
if (log_file) {
for (int i=0; i < 10; i++) {
text_string = String(i);
text_string += ", Example row: ";
text_string += String(i+1);
log_file.println(text_string);
}
log_file.close();
} else {
Serial.println("Cannot open file for writing.");
}


// Begin reading rows from the file


log_file = SD.open("log.txt");
if (log_file) {
// Read one row at a time.
while (log_file.available()) {
text_string = String("");


// Read first column
i = 0;
while ((c != ',') && (i < 4)) {
c = log_file.read();
text_string += c;
if (c != ',') {
number[i] = c;
}
i++;
}
number[i] = '\0';
value = atoi(number);


// Read second column
c = ' ';
while (c != '\n') {
c = log_file.read();
text_string += c;
}
// If value > 5, print the row
if (value > 5) {
Serial.print("> ");
Serial.print(text_string);
}
}

Free download pdf