First Commit
This commit is contained in:
@@ -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);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user