MySQL for the Internet of Things

(Steven Felgate) #1

Chapter 6 ■ Building low-Cost MysQl data nodes


MySQL Clients: How to Connect and Save Data


You have already seen how to connect to the MySQL server with the MySQL client. That tool is an interactive
tool where we can execute queries, but it isn’t helpful for saving data from our sensors or data nodes. What
we need is something called a connector. A connector is a programming module designed to permit our
sketches (from an Arduino) or scripts or programs to send data to the database server. Connectors also allow
us to query the database server to get data from the server.
I will cover two primary connectors you are likely to encounter when developing your own IOT
solutions. I present each as a tutorial that you can use to follow along with your own hardware. I begin with
a connector for use with the Arduino (Connector/Arduino) and then present a connector for use in writing
Python scripts (Connector/Python).


DataBaSe CONNeCtOrS FOr MYSQL


there are many database connectors for MysQl. oracle supplies a number of database connectors for
a variety of languages. the following are the current database connectors available for download from
http://dev.mysql.com/downloads/connector/:


  • Connector/ODBC: standard odBC compliant

  • Connector/Net: windows .net platforms

  • Connector/J: Java applications

  • Connector/Python: python applications

  • Connector/C++: standardized C++ applications

  • Connector/C (libmysql): C applications

  • MySQL native driver for PHP (mysqlnd): php 5.3 or newer connector

  • Connector/Arduino: arduino sketches


as you can see, there is a connector for just about any programming language you are likely to
encounter—and now there is even one for the arduino!

Introducing Connector/Arduino


With a new database connector made specifically for the Arduino, you can connect your Arduino project
directly to a MySQL server without using an intermediate computer or a web-based service. Having direct
access to a database server means you can store data acquired from your project in a database. You can also
check values stored in tables on the server. The connector allows you to keep your IOT solution local to your
facility—it can even be disconnected from the Internet or any other external network.
Saving your data in a database not only preserves the data for analysis at a later time but also means
your project can feed data to more complex applications. Better still, if you have projects that use large data
volumes for calculations or lookups, you can store the data on the server and retrieve only the data you need
for the calculation or operation—all without taking up large blocks of memory on your Arduino. Clearly, this
opens a whole new avenue of Arduino projects!

Free download pdf