MySQL for the Internet of Things

(Steven Felgate) #1

// Record data since master is dead.
recordData();
}
#else
recordData(); // read and save data
Serial.print("Sending heartbeat...");
sendHeartbeat();
Serial.println("done.");
#endif
delay(3000); // wait 3 seconds
}


Don’t be too overwhelmed with this code. I know it is rather complex and perhaps more complex since
it uses conditional compilation, but it saves us from having two projects instead of one. And given that the
master and slave need the same code to talk with a MySQL server and sensor(s), we can write it once and
save some headaches of debugging and maintaining duplicate code.
Take some time to read through this code before you try to run it yourself. It is also a good idea to
double-check and triple-check your wiring before moving on to running the code, which can be a bit tricky,
so read the next section carefully.


Test the Sketch


Testing the sketch can be a bit intimidating, so let’s go over what we need to do. First, we must compile the
sketch and upload it to the master. We simply comment out the #define, select the correct board and port,
and then upload the sketch. Next, we remove the comment from the #define, select the correct board and
port for the slave, and then compile and upload.
What I did (and I highly recommend this) is to have both the master and slave connected to your
computer via two USB connections. This will make setting up the experiment much easier. Indeed, you can
switch between the two simply by setting the port in the Arduino IDE and starting the serial monitor. Note,
however, that the Arduino will force the Arduino to reset when the serial monitor connects. This is fine for
testing, but just be aware it means it may take a few seconds to see the master/slave code working.
When connected to the master via the serial monitor, you should see output similar to that shown in
Figure 8-5. While it is not interesting, it can help you determine whether your master is sending the message.
Don’t forget we also wired in an LED, so you can watch the LED, and it should blink along with each
message feedback you see.


ChapTEr 8 ■ DEmonsTraTion of high availabiliTy TEChniquEs

Free download pdf