MySQL for the Internet of Things

(Steven Felgate) #1
ChapTEr 8 ■ DEmonsTraTion of high availabiliTy TEChniquEs

Test the Sketch


Now let’s get the code compiled and uploaded to the Arduino Mega. One thing you will notice when you
compile the code is that it consumes more than 36KB of program storage. While the compiler may show
something similar to the following and indicate you have plenty of space available, if you were to attempt to
compile this for a smaller Arduino, you will get an error stating the program is too large.


Sketch uses 36,370 bytes (14%) of program storage space. Maximum is 253,952 bytes.
Global variables use 2,219 bytes (27%) of dynamic memory, leaving 5,973 bytes for local
variables. Maximum is 8,192 bytes.


Once the code is compiling properly and you have set up the sample database, go ahead and run it for
some time. After a while, you can simulate a database failure by disconnecting the Ethernet cable. Let’s see
what happens.
If you open the serial monitor, you will see the output of the information messages I sprinkled
throughout the code. Listing 8-8 shows an excerpt of a run. Notice that the sample values are the same.
Recall I set the delay to only two seconds, so there will not be much variance in the sample values. In a
real-world implementation, you would probably want to check the soil moisture once per hour or even once
every couple of hours for plants in a controlled climate.


Listing 8-8. Debug Statements from Fault-Tolerant Data Collector


Connected to server version 5.7.8-rc-log
Writing data
INSERT INTO plant_moisture.plants VALUES (NULL,456,NULL)
Disconnected.
Connected to server version 5.7.8-rc-log
Writing data
INSERT INTO plant_moisture.plants VALUES (NULL,456,NULL)
Disconnected.
Data cached to log file.
Data cached to log file.
Data cached to log file.
Connected to server version 5.7.8-rc-log
INSERT INTO plant_moisture.plants VALUES (NULL,456,'2015-11-27 15:9:8')
INSERT INTO plant_moisture.plants VALUES (NULL,457,'2015-11-27 15:9:21')
INSERT INTO plant_moisture.plants VALUES (NULL,455,'2015-11-27 15:9:34')
Cache emptied. File removed.
Writing data
INSERT INTO plant_moisture.plants VALUES (NULL,456,NULL)
Disconnected.


Notice that when I disconnected the Ethernet cable, the code saved the data to the cache file. But later
when I reconnected the cable, the data was read from the cache and inserted in the database. Notice those
three statements again. Do you see that the date and time values were preserved? Recall this data was saved
to the file. But notice the other INSERT statements use NULL to signal the database to use the current date and
time of when the data was saved. That’s pretty neat, yes?

Free download pdf