MySQL for the Internet of Things

(Steven Felgate) #1

But wait! Why are we using an Arduino Mega? This is one of those times that we see just how resource-
intensive fault detection and recovery can be. Recall the Arduino Uno and similar small boards have a finite
amount of memory. We are adding a host of libraries: the Connector/Arduino for connecting to the MySQL
server, the SD card for caching the data, and the RTC module to read the date and time. Recall also the
Connector/Arduino requires several other libraries. This, combined with the code to detect the fault and
recover from it, we will easily exceed the memory of an Uno board. Thus, we use the Mega board, which has
more than sufficient memory. As you will discover in your own projects (if not now, soon), writing complex
code for the Arduino often requires stepping up to the larger boards.
You may also be wondering about the RTC module. Recall our database has a timestamp column for
storing the data and time the sample was stored. Recall also that the database server will fill this value in for
you. However, if we detect a fault and it is some time (perhaps hours) later before we can reconnect to the
database server, those values for the date stored will be incorrect—the database server will use the date and
time of the write to the database, not when the sample was taken. Thus, we need the RTC module to get the
current date and time to store in the cache along with the value stored. As you can see, caching the data is
not just a simple write and read later event!
Let’s go over the connections since there are many. First, the soil moisture sensor is wired with 5V and
ground coming from the Arduino. I also connect the signal (data) line from the sensor to analog pin 0 on the
Arduino. So far, we’re good. However, the RTC is not wired the same as it was in the earlier example. This is
because the pins for SDA and SCL are in a different place on the Mega. In this case, they’re on pins 20 and
21 respectfully. So, we wire the corresponding RTC module SDA and SCL connections to pins 20 and 21 and
then get 5V and ground from the Arduino. I use a breadboard in the drawing to make the power and ground
connections easier.


WhY NOt USe a LOW-COSt COMpUter BOarD?


one of the goals of presenting this project is to demonstrate how to achieve some rather sophisticated
features from a small microcontroller. While the arduino mega is a bit more expensive than a raspberry
pi, learning how to implement fault detection and recovery at the lowest level will help you implement
similar and even more sophisticated mechanisms in more powerful boards. if fact, you can implement
this project in python using Connector/python on a raspberry pi and achieve the same level of
redundancy. i encourage you to explore this as a challenge.

Now let’s look at the code needed for our sketch.

Write the Sketch


There are several parts to the sketch (code). We know we will write a data collector to read samples from a
sensor and write the data to the database. Also, should the connection not be viable, we will save data to a
local file. When the database connection is reestablished, we must read the data written to the file and insert
it into the database. Before we get into the code, we should consider the design goals and determine the key
requirements. I’ve found it helps to write these down even if they are clear in your mind. Indeed, I’ve found
listing the requirements can help you plan and implement the code in more organized manner. I list the
major requirements here:



  • All data collected should be saved to the database.

  • The database connection should be tested to ensure the server is connected.

  • In the event the database cannot be connected, data should be stored to a local file.


ChapTEr 8 ■ DEmonsTraTion of high availabiliTy TEChniquEs

Free download pdf