MySQL for the Internet of Things

(Steven Felgate) #1

Chapter 6 ■ Building low-Cost MysQl data nodes


Next, you need to set up some variables for Connector/Arduino. You need to define a reference to the
connection class. We dynamically allocate the cursor class later in order to manage memory better. The
connection class requires a parameter that is an instance of the Ethernet.Client class. If you are using an
Arduino Ethernet Shield, you can simply use the EthernetClient class. If you are using another library or a
WiFi shield, you would use the appropriate client. For example, use WiFiClient for the Arduino WiFi shield.
You also need some strings to use for the data you use in the sketch. At a minimum, these include a
string for the user ID, another for the password, and one for the query you use. This last string is optional
because you can just use the literal string directly in the query call, but it is good practice to make strings for
the query statements. It is also the best way to make queries parameterized for reuse.
The following is an example of the statements needed to complete the declarations for your sketch:


/ Setup for the Connector/Arduino /
EthernetClient client;
MySQL_Connection conn((Client *)&client);


char user[] = "root";
char password[] = "secret";
char INSERT_SQL[] = "INSERT INTO test_arduino.hello VALUES ('Hello from Arduino!', NULL)";


Notice the INSERT statement. You include a string to indicate that you are running the query from your
Arduino. You also include the NULL value so that the server will create the timestamp for the row as shown in
the manual execution previously.


Connecting to a MySQL Server


That concludes the preliminaries; let’s get some code written! Next, you change the setup() method. This is
where the code for connecting to the MySQL server should be placed. Recall that this method is called only
once each time the Arduino is booted. The following shows the code needed:


void setup() {
Ethernet.begin(mac_addr);
Serial.begin(115200);
while (!Serial);
delay(1000);
Serial.println("Connecting...");
if (conn.connect(server_addr, 3306, user, password))
delay(500);
else
Serial.println("Connection failed.");
}


The code begins with a call to the Ethernet library to initialize the network connection. Recall that when
you use the Ethernet.begin() method, passing only the MAC address as shown in the example, it causes
the Ethernet library to use DHCP to obtain an IP address. If you want to assign an IP address manually, see
the Ethernet.begin() method documentation at http://arduino.cc/en/Reference/EthernetBegin.
Next is a call to serial monitor. Although not completely necessary, it is a good idea to include it so you
can see the messages written by Connector/Arduino. If you have problems with connecting or running
queries, be sure to use the serial monitor so you can see the messages sent by the library.
Now comes a call to the delay() method. You issue this wait of one second to ensure that you have time
to start the serial monitor and not miss the debug statements. Feel free to experiment with changing this
value if you need more time to start the serial monitor.

Free download pdf