MySQL for the Internet of Things

(Steven Felgate) #1

You should also consider uploading the master sketch and leaving it running while you upload the slave
sketch and then connect to the serial monitor output for the slave. I found this sometimes helps keep things
straight. After all, the majority of the tricky code is on the slave.
Finally, make sure there are no power issues or similar power incompatibilities between the master and
slave boards. I tested using a Leonardo and a much older board and everything worked fine. However, I have
seen where some boards, particularly clone boards, do not always work correctly with some I2C devices, and
since we’re using the slave as an I2C device, that could be a problem source.


■Tip be sure you have the #define commented out to compile the sketch for the master.


Once you start seeing the correct output, celebrate that it worked! This sketch—however small—
implements a sophisticated heartbeat and automatic failover, which is something other solutions with more
powerful hardware may not be capable of achieving. However, as you can see, with a little ingenuity you can
do amazing things with simple hardware like a microcontroller such as the Arduino.
Now the real test begins. Unplug the master and slave from your laptop and power them up. I would
power on the master first and then the slave. Does the LED on the master blink followed by the LED on
the slave? As long as these LEDs are flashing, the master heartbeat is being sent and received. Now you see
why I added LEDs—to test the solution without a computer. If you do not see this happen, try resetting or
powering off the boards and then back on again. I have found sometimes the I2C bus doesn’t get going quite
right and rebooting seems to help.
Once you are satisfied everything is working, you can add the code to read the sensor and write the data
to the MySQL database and then deploy your redundant data collector with failover in your IOT solution.
Remember that smaller boards may not have the memory needed to support multiple, large libraries, so you
may need to use a larger Arduino board for your own solution. Just remember to find the SDA and SCL pins
and wire them correctly.
Now let’s see how to handle faults when writing data to the database.


Example: Fault-Tolerant Data Collector


Achieving fault tolerance with MySQL is not overly difficult and, as you have seen, can be accomplished
using MySQL replication for redundancy, recovery, scalability, and ultimately high availability for your
database component. However, achieving fault tolerance on a typical microcontroller-based node that
writes data to the database can be a bit more difficult.
In this section, I present a project you can build yourself. The project is a simple plant soil moisture data
collector with fault detection and recovery for writing data to the database. You already saw such a project
earlier in the book, but this time we’re adding the fault detection and recovery mechanisms, which, as you
will see, adds quite a bit more complexity to the code and even requires additional hardware!
If you consider a data collector node in its most basic functionality, it should read data from one or
more sensors and then pass that data on to a data node or database, or even store the data locally to a file. So,
what possible faults can we detect? Clearly, writing code to detect that the node itself is kaput is not feasible
(at least not without a redundant node). So, we consider only those faults that a data collector could recover
from: failure reading the sensor(s) and failure to store data.
For this project, we focus on only one of those faults and specifically the connection to the database
server. In this case, we are concerned about the node becoming isolated on the network, the database server
going down, or any other form of interruption to communication from the data collector to the database.
I chose this particular fault to demonstrate the added complexity needed. As you will see, it is not trivial, but
it isn’t overly complicated either—it just takes a bit of work.
Let’s begin with a brief description of the design and then a list of components (hardware) needed.


ChapTEr 8 ■ DEmonsTraTion of high availabiliTy TEChniquEs

Free download pdf