First Commit
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
MySQL Connector/Arduino Example : complex insert
|
||||
|
||||
This example demonstrates how to issue an INSERT query to store data in a
|
||||
table using data from variables in our sketch. In this case, we supply the
|
||||
values dynamically.
|
||||
|
||||
This sketch simulates storing data from a sensor in a table.
|
||||
|
||||
For this, 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 test_arduino;
|
||||
CREATE TABLE test_arduino.hello_sensor (
|
||||
num integer primary key auto_increment,
|
||||
message char(40),
|
||||
sensor_num integer,
|
||||
value float,
|
||||
recorded timestamp
|
||||
);
|
||||
|
||||
Here we have a table that contains an auto_increment primary key, a text
|
||||
field, a field to identify the sensor, the value read, and timestamp of
|
||||
the recorded data.
|
||||
|
||||
Note: Since this sketch uses test data, we place the INSERT in the setup()
|
||||
method so that it runs only once. Typically, you would have the
|
||||
INSERT in the loop() method after your code to read from the sensor.
|
||||
|
||||
For more information and documentation, visit the wiki:
|
||||
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
|
||||
|
||||
INSTRUCTIONS FOR USE
|
||||
|
||||
1) Create the database and table as shown above.
|
||||
2) Change the address of the server to the IP address of the MySQL server
|
||||
3) Change the user and password to a valid MySQL user and password
|
||||
4) Connect a USB cable to your Arduino
|
||||
5) Select the correct board and port
|
||||
6) Compile and upload the sketch to your Arduino
|
||||
7) Once uploaded, open Serial Monitor (use 115200 speed) and observe
|
||||
8) After the sketch has run for some time, open a mysql client and issue
|
||||
the command: "SELECT * FROM test_arduino.hello_sensor" to see the data
|
||||
recorded. Note the field values and how the database handles both the
|
||||
auto_increment and timestamp fields for us. You can clear the data with
|
||||
"DELETE FROM test_arduino.hello_sensor".
|
||||
|
||||
Note: The MAC address can be anything so long as it is unique on your network.
|
||||
|
||||
Created by: Dr. Charles A. Bell
|
||||
*/
|
||||
#include <Ethernet.h>
|
||||
#include <MySQL_Connection.h>
|
||||
#include <MySQL_Cursor.h>
|
||||
|
||||
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
||||
|
||||
IPAddress server_addr(10,0,1,35); // IP of the MySQL *server* here
|
||||
char user[] = "root"; // MySQL user login username
|
||||
char password[] = "secret"; // MySQL user login password
|
||||
|
||||
// Sample query
|
||||
char INSERT_DATA[] = "INSERT INTO test_arduino.hello_sensor (message, sensor_num, value) VALUES ('%s',%d,%s)";
|
||||
char query[128];
|
||||
char temperature[10];
|
||||
|
||||
EthernetClient client;
|
||||
MySQL_Connection conn((Client *)&client);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial); // wait for serial port to connect
|
||||
Ethernet.begin(mac_addr);
|
||||
Serial.println("Connecting...");
|
||||
if (conn.connect(server_addr, 3306, user, password)) {
|
||||
delay(1000);
|
||||
// Initiate the query class instance
|
||||
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
|
||||
// Save
|
||||
dtostrf(50.125, 1, 1, temperature);
|
||||
sprintf(query, INSERT_DATA, "test sensor", 24, temperature);
|
||||
// Execute the query
|
||||
cur_mem->execute(query);
|
||||
// Note: since there are no results, we do not need to read any data
|
||||
// Deleting the cursor also frees up memory used
|
||||
delete cur_mem;
|
||||
Serial.println("Data recorded.");
|
||||
}
|
||||
else
|
||||
Serial.println("Connection failed.");
|
||||
conn.close();
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
}
|
||||
Reference in New Issue
Block a user