This example uses Arduino Leonardo ETH and MySQL to demonstrate how to store data in a table in MySQL from 4 button connected to pins 2, 3, 4, 5. I need to monitor 4 machine and time when it on. We will create a special database and table for testing.
The following are the SQL commands you will need to run in order to setup your database for running this sketch.
CREATE DATABASE machine; CREATE TABLE machine.state ( num integer primary key auto_increment, message char(40), recorded timestamp );
Here we see one database and a table with three fields; a primary key that is an auto_increment, a string, and a timestamp. This will demonstrate how to save the date and time of when the row was inserted, which can help you determine when data was recorded or updated.
1) Create the database and table as shown above.
2) Change the address of the server to the IP address of your MySQL server in my case my IP is 184.108.40.206
3) Change the user and password to a valid MySQL user and password in my case it is
4) Install MySQL library in your IDE by going to sketch > include library > manage library, and in the filter use search type MySQL, then install it.
5) Arduino Leonardo uses Ethernet2 library, so you need to install it in your IDE. Go to sketch > include library > manage library, and in the filter use search type ethernet2, then install it.
6) Also, you need to modify file MySQL_Packet.h, the original created by Dr.Charles. Change
, otherwise it will give you an error message and not compile.
7) Connect a USB cable to your Arduino.
8) Select the Arduino Leonardo ETH board and port.
9) Compile and upload the sketch to your Arduino.
10) Connect 4 push button between ground and pins 2, 3, 4, 5 through 10k Ohm resistor.
11) After the sketch has run for some time, press some push buttons and open a MySQL client and issue the command:
SELECT * FROM machine.state;
to see the data recorded. Note the field values and how the database handles both the auto_increment and timestamp fields for us.
- Use the MAC on your board in back; if not, you can put anything but make sure it is unique in your network.
- Your MySQLl default port address is 3306, but this will conflict with Skype so you can change it to 3307 in both your sketch and your MySQL. And make sure this port is open in your server firewall.
Created by: Samir Mohamad tawfik 28-1-2017