Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- 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[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
- IPAddress ip(10, 1, 1, 10); //Define o endereco IP
- //IPAddress gateway(10, 1, 1, 1); //Define o gateway
- //IPAddress subnet(255, 0, 0, 0); //Define a máscara de rede
- IPAddress server_addr(10, 1, 1, 3); //Define o endereço IP do servidor
- char user[] = "ARDUINO10"; //usuario
- char password[] = "100nha"; //senha
- // Sample query
- char query[] = "USE `test_arduino`; SELECT acesso FROM autorizacao WHERE ID = '%DE 3B 9A 32%';";
- 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, ip);
- 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 *cols = 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment