First Commit
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
MySQL Connector/Arduino Example : basic insert
|
||||
|
||||
This example demonstrates how to issue an INSERT query to store data 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_arduino (
|
||||
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 a date and time of when the row was inserted, which can help you
|
||||
determine when data was recorded or updated.
|
||||
|
||||
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_arduino" 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_arduino".
|
||||
|
||||
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_SQL[] = "INSERT INTO test_arduino.hello_arduino (message) VALUES ('Hello, Arduino!')";
|
||||
|
||||
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);
|
||||
}
|
||||
else
|
||||
Serial.println("Connection failed.");
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
delay(2000);
|
||||
|
||||
Serial.println("Recording data.");
|
||||
|
||||
// Initiate the query class instance
|
||||
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
|
||||
// Execute the query
|
||||
cur_mem->execute(INSERT_SQL);
|
||||
// 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;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
MySQL Connector/Arduino Example : connect by wifi
|
||||
|
||||
This example demonstrates how to connect to a MySQL server from an
|
||||
Arduino using an Arduino-compatible Wifi shield. Note that "compatible"
|
||||
means it must conform to the Ethernet class library or be a derivative
|
||||
with the same classes and methods.
|
||||
|
||||
For more information and documentation, visit the wiki:
|
||||
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
|
||||
|
||||
INSTRUCTIONS FOR USE
|
||||
|
||||
1) Change the address of the server to the IP address of the MySQL server
|
||||
2) Change the user and password to a valid MySQL user and password
|
||||
3) Change the SSID and pass to match your WiFi network
|
||||
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
|
||||
|
||||
If you do not see messages indicating you have a connection, refer to the
|
||||
manual for troubleshooting tips. The most common issues are the server is
|
||||
not accessible from the network or the user name and password is incorrect.
|
||||
|
||||
Created by: Dr. Charles A. Bell
|
||||
*/
|
||||
#include <ESP8266WiFi.h> // Use this for WiFi instead of Ethernet.h
|
||||
#include <MySQL_Connection.h>
|
||||
#include <MySQL_Cursor.h>
|
||||
|
||||
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_SQL[] = "INSERT INTO test_arduino.hello_arduino (message) VALUES ('Hello, Arduino!')";
|
||||
|
||||
// WiFi card example
|
||||
char ssid[] = "your-ssid"; // your SSID
|
||||
char pass[] = "ssid-password"; // your SSID Password
|
||||
|
||||
WiFiClient client; // Use this for WiFi instead of EthernetClient
|
||||
MySQL_Connection conn(&client);
|
||||
MySQL_Cursor* cursor;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while (!Serial); // wait for serial port to connect. Needed for Leonardo only
|
||||
|
||||
// Begin WiFi section
|
||||
Serial.printf("\nConnecting to %s", ssid);
|
||||
WiFi.begin(ssid, pass);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
// print out info about the connection:
|
||||
Serial.println("\nConnected to network");
|
||||
Serial.print("My IP address is: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
Serial.print("Connecting to SQL... ");
|
||||
if (conn.connect(server_addr, 3306, user, password))
|
||||
Serial.println("OK.");
|
||||
else
|
||||
Serial.println("FAILED.");
|
||||
|
||||
// create MySQL cursor object
|
||||
cursor = new MySQL_Cursor(&conn);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (conn.connected())
|
||||
cursor->execute(INSERT_SQL);
|
||||
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
MySQL Connector/Arduino Example : basic select
|
||||
|
||||
This example demonstrates how to issue a SELECT query with no parameters
|
||||
and use the data returned. For this, we use the Cursor class to execute
|
||||
the query and get the results.
|
||||
|
||||
It demonstrates who methods for running queries. The first allows you to
|
||||
allocate memory for the cursor and later reclaim it, the second shows how
|
||||
to use a single instance of the cursor use throughout a sketch.
|
||||
|
||||
NOTICE: You must download and install the World sample database to run
|
||||
this sketch unaltered. See http://dev.mysql.com/doc/index-other.html.
|
||||
|
||||
CAUTION: Don't mix and match the examples. Use one or the other in your
|
||||
own sketch -- you'll get compilation errors at the least.
|
||||
|
||||
For more information and documentation, visit the wiki:
|
||||
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
|
||||
|
||||
INSTRUCTIONS FOR USE
|
||||
|
||||
1) Change the address of the server to the IP address of the MySQL server
|
||||
2) Change the user and password to a valid MySQL user and password
|
||||
3) Connect a USB cable to your Arduino
|
||||
4) Select the correct board and port
|
||||
5) Compile and upload the sketch to your Arduino
|
||||
6) Once uploaded, open Serial Monitor (use 115200 speed) and observe
|
||||
|
||||
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 query[] = "SELECT population FROM world.city WHERE name = 'New York'";
|
||||
|
||||
EthernetClient client;
|
||||
MySQL_Connection conn((Client *)&client);
|
||||
// Create an instance of the cursor passing in the connection
|
||||
MySQL_Cursor cur = MySQL_Cursor(&conn);
|
||||
|
||||
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);
|
||||
}
|
||||
else
|
||||
Serial.println("Connection failed.");
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
row_values *row = NULL;
|
||||
long head_count = 0;
|
||||
|
||||
delay(1000);
|
||||
|
||||
Serial.println("1) Demonstrating using a cursor dynamically allocated.");
|
||||
// Initiate the query class instance
|
||||
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
|
||||
// Execute the query
|
||||
cur_mem->execute(query);
|
||||
// Fetch the columns (required) but we don't use them.
|
||||
column_names *columns = cur_mem->get_columns();
|
||||
|
||||
// Read the row (we are only expecting the one)
|
||||
do {
|
||||
row = cur_mem->get_next_row();
|
||||
if (row != NULL) {
|
||||
head_count = atol(row->values[0]);
|
||||
}
|
||||
} while (row != NULL);
|
||||
// Deleting the cursor also frees up memory used
|
||||
delete cur_mem;
|
||||
|
||||
// Show the result
|
||||
Serial.print(" NYC pop = ");
|
||||
Serial.println(head_count);
|
||||
|
||||
delay(500);
|
||||
|
||||
Serial.println("2) Demonstrating using a local, global cursor.");
|
||||
// Execute the query
|
||||
cur.execute(query);
|
||||
// Fetch the columns (required) but we don't use them.
|
||||
cur.get_columns();
|
||||
// Read the row (we are only expecting the one)
|
||||
do {
|
||||
row = cur.get_next_row();
|
||||
if (row != NULL) {
|
||||
head_count = atol(row->values[0]);
|
||||
}
|
||||
} while (row != NULL);
|
||||
// Now we close the cursor to free any memory
|
||||
cur.close();
|
||||
|
||||
// Show the result but this time do some math on it
|
||||
Serial.print(" NYC pop = ");
|
||||
Serial.println(head_count);
|
||||
Serial.print(" NYC pop increased by 12 = ");
|
||||
Serial.println(head_count+12);
|
||||
|
||||
}
|
||||
@@ -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() {
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
MySQL Connector/Arduino Example : complex select
|
||||
|
||||
This example demonstrates how to issue a SELECT query with parameters that
|
||||
we provide from code. Thus, it demonstrates how to build query parameters
|
||||
dynamically.
|
||||
|
||||
Notice also the sketch demonstrates how to read columns and rows from
|
||||
the result set. Study this example until you are familiar with how to
|
||||
do this before writing your own sketch to read and consume query results.
|
||||
|
||||
For more information and documentation, visit the wiki:
|
||||
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
|
||||
|
||||
NOTICE: You must download and install the World sample database to run
|
||||
this sketch unaltered. See http://dev.mysql.com/doc/index-other.html.
|
||||
|
||||
INSTRUCTIONS FOR USE
|
||||
|
||||
1) Change the address of the server to the IP address of the MySQL server
|
||||
2) Change the user and password to a valid MySQL user and password
|
||||
3) Connect a USB cable to your Arduino
|
||||
4) Select the correct board and port
|
||||
5) Compile and upload the sketch to your Arduino
|
||||
6) Once uploaded, open Serial Monitor (use 115200 speed) and observe
|
||||
|
||||
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
|
||||
//
|
||||
// Notice the "%lu" - that's a placeholder for the parameter we will
|
||||
// supply. See sprintf() documentation for more formatting specifier
|
||||
// options
|
||||
const char QUERY_POP[] = "SELECT name, population FROM world.city WHERE population > %lu ORDER BY population DESC;";
|
||||
char query[128];
|
||||
|
||||
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);
|
||||
}
|
||||
else
|
||||
Serial.println("Connection failed.");
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
delay(1000);
|
||||
|
||||
Serial.println("> Running SELECT with dynamically supplied parameter");
|
||||
|
||||
// Initiate the query class instance
|
||||
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
|
||||
// Supply the parameter for the query
|
||||
// Here we use the QUERY_POP as the format string and query as the
|
||||
// destination. This uses twice the memory so another option would be
|
||||
// to allocate one buffer for all formatted queries or allocate the
|
||||
// memory as needed (just make sure you allocate enough memory and
|
||||
// free it when you're done!).
|
||||
sprintf(query, QUERY_POP, 9000000);
|
||||
// Execute the query
|
||||
cur_mem->execute(query);
|
||||
// Fetch the columns and print them
|
||||
column_names *cols = cur_mem->get_columns();
|
||||
for (int f = 0; f < cols->num_fields; f++) {
|
||||
Serial.print(cols->fields[f]->name);
|
||||
if (f < cols->num_fields-1) {
|
||||
Serial.print(',');
|
||||
}
|
||||
}
|
||||
Serial.println();
|
||||
// Read the rows and print them
|
||||
row_values *row = NULL;
|
||||
do {
|
||||
row = cur_mem->get_next_row();
|
||||
if (row != NULL) {
|
||||
for (int f = 0; f < cols->num_fields; f++) {
|
||||
Serial.print(row->values[f]);
|
||||
if (f < cols->num_fields-1) {
|
||||
Serial.print(',');
|
||||
}
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
} while (row != NULL);
|
||||
// Deleting the cursor also frees up memory used
|
||||
delete cur_mem;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
MySQL Connector/Arduino Example : connect
|
||||
|
||||
This example demonstrates how to connect to a MySQL server from an
|
||||
Arduino using an Arduino-compatible Ethernet shield. Note that "compatible"
|
||||
means it must conform to the Ethernet class library or be a derivative
|
||||
with the same classes and methods.
|
||||
|
||||
For more information and documentation, visit the wiki:
|
||||
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
|
||||
|
||||
INSTRUCTIONS FOR USE
|
||||
|
||||
1) Change the address of the server to the IP address of the MySQL server
|
||||
2) Change the user and password to a valid MySQL user and password
|
||||
3) Connect a USB cable to your Arduino
|
||||
4) Select the correct board and port
|
||||
5) Compile and upload the sketch to your Arduino
|
||||
6) Once uploaded, open Serial Monitor (use 115200 speed) and observe
|
||||
|
||||
If you do not see messages indicating you have a connection, refer to the
|
||||
manual for troubleshooting tips. The most common issues are the server is
|
||||
not accessible from the network or the user name and password is incorrect.
|
||||
|
||||
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>
|
||||
|
||||
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
|
||||
|
||||
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);
|
||||
// You would add your code here to run a query once on startup.
|
||||
}
|
||||
else
|
||||
Serial.println("Connection failed.");
|
||||
conn.close();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
MySQL Connector/Arduino Example : connect by hostname
|
||||
|
||||
This example demonstrates how to connect to a MySQL server resolving the
|
||||
hostname for cases when you do not know the IP address of the server or
|
||||
it changes because it is in the cloud.
|
||||
|
||||
For more information and documentation, visit the wiki:
|
||||
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
|
||||
|
||||
INSTRUCTIONS FOR USE
|
||||
|
||||
1) Change the hostname variable to the hostname of the MySQL server
|
||||
2) Change the user and password to a valid MySQL user and password
|
||||
3) Connect a USB cable to your Arduino
|
||||
4) Select the correct board and port
|
||||
5) Compile and upload the sketch to your Arduino
|
||||
6) Once uploaded, open Serial Monitor (use 115200 speed) and observe
|
||||
|
||||
If you do not see messages indicating you have a connection, refer to the
|
||||
manual for troubleshooting tips. The most common issues are the server is
|
||||
not accessible from the network or the user name and password is incorrect.
|
||||
|
||||
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 <Dns.h>
|
||||
|
||||
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
||||
|
||||
char hostname[] = "www.google.com"; // change to your server's hostname/URL
|
||||
char user[] = "root"; // MySQL user login username
|
||||
char password[] = "secret"; // MySQL user login password
|
||||
|
||||
IPAddress server_ip;
|
||||
EthernetClient client;
|
||||
MySQL_Connection conn((Client *)&client);
|
||||
DNSClient dns_client; // DNS instance
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial); // wait for serial port to connect
|
||||
Ethernet.begin(mac_addr);
|
||||
// Begin DNS lookup
|
||||
dns_client.begin(Ethernet.dnsServerIP());
|
||||
dns_client.getHostByName(hostname, server_ip);
|
||||
Serial.println(server_ip);
|
||||
// End DNS lookup
|
||||
Serial.println("Connecting...");
|
||||
if (conn.connect(server_ip, 3306, user, password)) {
|
||||
delay(1000);
|
||||
// You would add your code here to run a query once on startup.
|
||||
}
|
||||
else
|
||||
Serial.println("Connection failed.");
|
||||
conn.close();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
MySQL Connector/Arduino Example : connect with default database
|
||||
|
||||
This example demonstrates how to connect to a MySQL server and specifying
|
||||
the default database when connecting.
|
||||
|
||||
For more information and documentation, visit the wiki:
|
||||
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
|
||||
|
||||
INSTRUCTIONS FOR USE
|
||||
|
||||
1) Change the address of the server to the IP address of the MySQL server
|
||||
2) Change the user and password to a valid MySQL user and password
|
||||
3) Connect a USB cable to your Arduino
|
||||
4) Select the correct board and port
|
||||
5) Compile and upload the sketch to your Arduino
|
||||
6) Once uploaded, open Serial Monitor (use 115200 speed) and observe
|
||||
|
||||
If you do not see messages indicating you have a connection, refer to the
|
||||
manual for troubleshooting tips. The most common issues are the server is
|
||||
not accessible from the network or the user name and password is incorrect.
|
||||
|
||||
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>
|
||||
|
||||
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
|
||||
char default_db = "test_arduino;
|
||||
|
||||
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, default_db)) {
|
||||
delay(1000);
|
||||
// You would add your code here to run a query once on startup.
|
||||
}
|
||||
else
|
||||
Serial.println("Connection failed.");
|
||||
conn.close();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
MySQL Connector/Arduino Example : connect and disconnect (close)
|
||||
|
||||
This example demonstrates how to use the connection to open at the start
|
||||
of a loop, perform some query, then close the connection. Use this technique
|
||||
for solutions that must sleep for a long period or otherwise require
|
||||
additional processing or delays. The connect/close pair allow you to
|
||||
control how long the connection is open and thus reduce the amount of
|
||||
time a connection is held open. It also helps for lossy connections.
|
||||
|
||||
This example demonstrates how to connect to a MySQL server and specifying
|
||||
the default database when connecting.
|
||||
|
||||
For more information and documentation, visit the wiki:
|
||||
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
|
||||
|
||||
INSTRUCTIONS FOR USE
|
||||
|
||||
1) Change the address of the server to the IP address of the MySQL server
|
||||
2) Change the user and password to a valid MySQL user and password
|
||||
3) Connect a USB cable to your Arduino
|
||||
4) Select the correct board and port
|
||||
5) Compile and upload the sketch to your Arduino
|
||||
6) Once uploaded, open Serial Monitor (use 115200 speed) and observe
|
||||
|
||||
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
|
||||
|
||||
EthernetClient client;
|
||||
MySQL_Connection conn((Client *)&client);
|
||||
MySQL_Cursor cur = MySQL_Cursor(&conn);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial); // wait for serial port to connect
|
||||
Ethernet.begin(mac_addr);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("Sleeping...");
|
||||
delay(2000);
|
||||
Serial.println("Connecting...");
|
||||
if (conn.connect(server_addr, 3306, user, password)) {
|
||||
delay(500);
|
||||
Serial.println("Running a query");
|
||||
cur.execute("SHOW DATABASES"); // execute a query
|
||||
cur.show_results(); // show the results
|
||||
cur.close(); // close the cursor
|
||||
conn.close(); // close the connection
|
||||
} else {
|
||||
Serial.println("Connect failed. Trying again on next iteration.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
MySQL Connector/Arduino Example : connect by wifi
|
||||
|
||||
This example demonstrates how to connect to a MySQL server from an
|
||||
Arduino using an Arduino-compatible Wifi shield. Note that "compatible"
|
||||
means it must conform to the Ethernet class library or be a derivative
|
||||
with the same classes and methods.
|
||||
|
||||
For more information and documentation, visit the wiki:
|
||||
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
|
||||
|
||||
INSTRUCTIONS FOR USE
|
||||
|
||||
1) Change the address of the server to the IP address of the MySQL server
|
||||
2) Change the user and password to a valid MySQL user and password
|
||||
3) Change the SSID and pass to match your WiFi network
|
||||
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
|
||||
|
||||
If you do not see messages indicating you have a connection, refer to the
|
||||
manual for troubleshooting tips. The most common issues are the server is
|
||||
not accessible from the network or the user name and password is incorrect.
|
||||
|
||||
Note: The MAC address can be anything so long as it is unique on your network.
|
||||
|
||||
Created by: Dr. Charles A. Bell
|
||||
*/
|
||||
#include <WiFi.h> // Use this for WiFi instead of 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
|
||||
|
||||
// WiFi card example
|
||||
char ssid[] = "horse_pen"; // your SSID
|
||||
char pass[] = "noname"; // your SSID Password
|
||||
|
||||
WiFiClient client; // Use this for WiFi instead of EthernetClient
|
||||
MySQL_Connection conn((Client *)&client);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial); // wait for serial port to connect. Needed for Leonardo only
|
||||
|
||||
// Begin WiFi section
|
||||
int status = WiFi.begin(ssid, pass);
|
||||
if ( status != WL_CONNECTED) {
|
||||
Serial.println("Couldn't get a wifi connection");
|
||||
while(true);
|
||||
}
|
||||
// print out info about the connection:
|
||||
else {
|
||||
Serial.println("Connected to network");
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("My IP address is: ");
|
||||
Serial.println(ip);
|
||||
}
|
||||
// End WiFi section
|
||||
|
||||
Serial.println("Connecting...");
|
||||
if (conn.connect(server_addr, 3306, user, password)) {
|
||||
delay(1000);
|
||||
}
|
||||
else
|
||||
Serial.println("Connection failed.");
|
||||
conn.close();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
MySQL Connector/Arduino Example : connect by wifi using WiFi 101 shield
|
||||
|
||||
This example demonstrates how to connect to a MySQL server from an
|
||||
Arduino using using the new WiFi Shield 101 from arduino.cc.
|
||||
|
||||
For more information and documentation, visit the wiki:
|
||||
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
|
||||
|
||||
INSTRUCTIONS FOR USE
|
||||
|
||||
1) Change the address of the server to the IP address of the MySQL server
|
||||
2) Change the user and password to a valid MySQL user and password
|
||||
3) Change the SSID and pass to match your WiFi network
|
||||
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
|
||||
|
||||
If you do not see messages indicating you have a connection, refer to the
|
||||
manual for troubleshooting tips. The most common issues are the server is
|
||||
not accessible from the network or the user name and password is incorrect.
|
||||
|
||||
Note: The MAC address can be anything so long as it is unique on your network.
|
||||
|
||||
Created by: Dr. Charles A. Bell
|
||||
*/
|
||||
#include <SPI.h>
|
||||
#include <WiFi101.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
|
||||
|
||||
// WiFi card example
|
||||
char ssid[] = "horse_pen"; // your SSID
|
||||
char pass[] = "noname"; // your SSID Password
|
||||
|
||||
WiFiClient client;
|
||||
MySQL_Connection conn((Client *)&client);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial); // wait for serial port to connect
|
||||
|
||||
// Begin WiFi section
|
||||
int status = WiFi.begin(ssid, pass);
|
||||
if ( status != WL_CONNECTED) {
|
||||
Serial.println("Couldn't get a wifi connection");
|
||||
while(true);
|
||||
}
|
||||
// print out info about the connection:
|
||||
else {
|
||||
Serial.println("Connected to network");
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("My IP address is: ");
|
||||
Serial.println(ip);
|
||||
}
|
||||
// End WiFi section
|
||||
|
||||
Serial.println("Connecting...");
|
||||
if (conn.connect(server_addr, 3306, user, password)) {
|
||||
delay(1000);
|
||||
}
|
||||
else
|
||||
Serial.println("Connection failed.");
|
||||
conn.close();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
MySQL Connector/Arduino Example : query with PROGMEM strings
|
||||
|
||||
This example demonstrates how to issue queries using strings stored in
|
||||
PROGMEM. As you will see, you need only add a parameter to the execute()
|
||||
method in the cursor class, const and PROGMEM to the string declaration
|
||||
and add the #include <avr/pgmspace.h> directive.
|
||||
|
||||
For more information and documentation, visit the wiki:
|
||||
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
|
||||
|
||||
NOTICE: You must download and install the World sample database to run
|
||||
this sketch unaltered. See http://dev.mysql.com/doc/index-other.html.
|
||||
|
||||
INSTRUCTIONS FOR USE
|
||||
|
||||
1) Change the address of the server to the IP address of the MySQL server
|
||||
2) Change the user and password to a valid MySQL user and password
|
||||
3) Connect a USB cable to your Arduino
|
||||
4) Select the correct board and port
|
||||
5) Compile and upload the sketch to your Arduino
|
||||
6) Once uploaded, open Serial Monitor (use 115200 speed) and observe
|
||||
|
||||
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>
|
||||
#include <avr/pgmspace.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
|
||||
const char PROGMEM query[] = "SELECT * FROM world.city LIMIT 12";
|
||||
|
||||
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);
|
||||
}
|
||||
else
|
||||
Serial.println("Connection failed.");
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
delay(2000);
|
||||
|
||||
Serial.println("\nRunning SELECT from PROGMEM and printing results\n");
|
||||
|
||||
// Initiate the query class instance
|
||||
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
|
||||
// Execute the query with the PROGMEM option
|
||||
cur_mem->execute(query, true);
|
||||
// Show the results
|
||||
cur_mem->show_results();
|
||||
// Deleting the cursor also frees up memory used
|
||||
delete cur_mem;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
MySQL Connector/Arduino Example : query results
|
||||
|
||||
This example demonstrates how to issue a SELECT query and how to read columns
|
||||
and rows from the result set. Study this example until you are familiar with how to
|
||||
do this before writing your own sketch to read and consume query results.
|
||||
|
||||
For more information and documentation, visit the wiki:
|
||||
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
|
||||
|
||||
NOTICE: You must download and install the World sample database to run
|
||||
this sketch unaltered. See http://dev.mysql.com/doc/index-other.html.
|
||||
|
||||
INSTRUCTIONS FOR USE
|
||||
|
||||
1) Change the address of the server to the IP address of the MySQL server
|
||||
2) Change the user and password to a valid MySQL user and password
|
||||
3) Connect a USB cable to your Arduino
|
||||
4) Select the correct board and port
|
||||
5) Compile and upload the sketch to your Arduino
|
||||
6) Once uploaded, open Serial Monitor (use 115200 speed) and observe
|
||||
|
||||
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 query[] = "SELECT * FROM world.city LIMIT 12";
|
||||
|
||||
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);
|
||||
}
|
||||
else
|
||||
Serial.println("Connection failed.");
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
delay(2000);
|
||||
|
||||
Serial.println("\nRunning SELECT and printing results\n");
|
||||
|
||||
// Initiate the query class instance
|
||||
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
|
||||
// Execute the query
|
||||
cur_mem->execute(query);
|
||||
// Fetch the columns and print them
|
||||
column_names *cols = cur_mem->get_columns();
|
||||
for (int f = 0; f < cols->num_fields; f++) {
|
||||
Serial.print(cols->fields[f]->name);
|
||||
if (f < cols->num_fields-1) {
|
||||
Serial.print(", ");
|
||||
}
|
||||
}
|
||||
Serial.println();
|
||||
// Read the rows and print them
|
||||
row_values *row = NULL;
|
||||
do {
|
||||
row = cur_mem->get_next_row();
|
||||
if (row != NULL) {
|
||||
for (int f = 0; f < cols->num_fields; f++) {
|
||||
Serial.print(row->values[f]);
|
||||
if (f < cols->num_fields-1) {
|
||||
Serial.print(", ");
|
||||
}
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
} while (row != NULL);
|
||||
// Deleting the cursor also frees up memory used
|
||||
delete cur_mem;
|
||||
}
|
||||
82
libraries/MySQL_Connector_Arduino/examples/reboot/reboot.ino
Normal file
82
libraries/MySQL_Connector_Arduino/examples/reboot/reboot.ino
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
MySQL Connector/Arduino Example : reboot if connection lost
|
||||
|
||||
This example demonstrates how to reboot an Arduino if connection to the
|
||||
server is lost for a period of time.
|
||||
|
||||
For more information and documentation, visit the wiki:
|
||||
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
|
||||
|
||||
INSTRUCTIONS FOR USE
|
||||
|
||||
1) Change the address of the server to the IP address of the MySQL server
|
||||
2) Change the user and password to a valid MySQL user and password
|
||||
3) Connect a USB cable to your Arduino
|
||||
4) Select the correct board and port
|
||||
5) Compile and upload the sketch to your Arduino
|
||||
6) Once uploaded, open Serial Monitor (use 115200 speed) and observe
|
||||
|
||||
To test the reboot, unplug your Ethernet cable once you see "disconnected"
|
||||
then wait for the timeout. Once the Arduino reboots, plug the cable in again
|
||||
and you should see the query processing resume.
|
||||
|
||||
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
|
||||
|
||||
EthernetClient client;
|
||||
MySQL_Connection conn((Client *)&client);
|
||||
MySQL_Cursor cur = MySQL_Cursor(&conn);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial); // wait for serial port to connect
|
||||
Ethernet.begin(mac_addr);
|
||||
}
|
||||
|
||||
// Begin reboot code
|
||||
int num_fails; // variable for number of failure attempts
|
||||
#define MAX_FAILED_CONNECTS 5 // maximum number of failed connects to MySQL
|
||||
|
||||
void soft_reset() {
|
||||
asm volatile("jmp 0");
|
||||
}
|
||||
// End reboot code
|
||||
|
||||
void loop() {
|
||||
Serial.println("Sleeping...");
|
||||
delay(1000);
|
||||
if (conn.connected()) {
|
||||
Serial.println("Running a query");
|
||||
cur.execute("SHOW DATABASES"); // execute a query
|
||||
cur.show_results(); // show the results
|
||||
cur.close(); // close the cursor
|
||||
conn.close(); // close the connection
|
||||
num_fails = 0; // reset failures
|
||||
delay(3000);
|
||||
} else {
|
||||
Serial.println("Connecting...");
|
||||
if (conn.connect(server_addr, 3306, user, password)) {
|
||||
delay(500);
|
||||
} else {
|
||||
num_fails++;
|
||||
Serial.println("Connect failed!");
|
||||
if (num_fails == MAX_FAILED_CONNECTS) {
|
||||
Serial.println("Ok, that's it. I'm outta here. Rebooting...");
|
||||
delay(2000);
|
||||
// Here we tell the Arduino to reboot by redirecting the instruction
|
||||
// pointer to the "top" or position 0. This is a soft reset and may
|
||||
// not solve all hardware-related lockups.
|
||||
soft_reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user