MySQL for the Internet of Things

(Steven Felgate) #1
Chapter 6 ■ Building low-Cost MysQl data nodes

mysql> GRANT ALL ON . to 'root'@'%' IDENTIFIED BY 'secret';


mysql> INSERT INTO hello VALUES ('From Laptop', NULL);
Query OK, 1 row affected (0.00 sec)


mysql> SELECT * FROM hello;
+-------------+---------------------+
| source | event_date |
+-------------+---------------------+
| From Laptop | 2013-02-16 20:40:12 |
+-------------+---------------------+
1 row in set (0.00 sec)


mysql>


Starting a New Sketch


It is time to start writing your sketch. Open your Arduino environment, and create a new sketch named
hello_mysql. The following sections detail the parts of a typical MySQL database-enabled sketch. You begin
with the required include files.


Include Files


To use the Connector/Arduino library, recall that it requires an Ethernet shield and therefore the Ethernet
library. The Connector/Arduino library requires the MySQL_Connection library for connecting and the
MySQL_Cursor library for running queries. Thus, you must include each of these in order. The following
shows all the library header files you need to include at a bare minimum for a MySQL database-enabled
sketch. Go ahead and enter these now.


#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>


Preliminary Setup


With the include files set up, you next must take care of some preliminary declarations. These include
declarations for the Ethernet library and Connector/Arduino.
The Ethernet library requires you to set up a MAC address and the IP address of the server. The MAC
address is a string of hexadecimal digits and need not be anything special, but it should be unique among
the machines on your network. It uses Dynamic Host Control Protocol (DHCP) to get an IP address, DNS,
and gateway information. The IP address of the server is defined using the IPAddress class (which stores the
value as an array of four integers, just as you would expect).
On the other hand, the Ethernet class also permits you to supply an IP address for the Arduino. If you
assign an IP address for the Arduino, it must be unique for the network segment to which it is attached. Be
sure to use an IP scanner to make sure your choice of IP address isn’t already in use.
The following shows what these statements would look like for a node on a 10.0.1.X network:


/ Setup for Ethernet Library /
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server_addr(10, 0, 1, 23);

Free download pdf