Rodrigo_Moraes

SELECT arduino

May 8th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. /*
  2.   MySQL Connector/Arduino Example : complex select
  3.  
  4.   This example demonstrates how to issue a SELECT query with parameters that
  5.   we provide from code. Thus, it demonstrates how to build query parameters
  6.   dynamically.
  7.  
  8.   Notice also the sketch demonstrates how to read columns and rows from
  9.   the result set. Study this example until you are familiar with how to
  10.   do this before writing your own sketch to read and consume query results.
  11.  
  12.   NOTICE: You must download and install the World sample database to run
  13.           this sketch unaltered. See http://dev.mysql.com/doc/index-other.html.
  14.  
  15.   INSTRUCTIONS FOR USE
  16.  
  17.   1) Change the address of the server to the IP address of the MySQL server
  18.   2) Change the user and password to a valid MySQL user and password
  19.   3) Connect a USB cable to your Arduino
  20.   4) Select the correct board and port
  21.   5) Compile and upload the sketch to your Arduino
  22.   6) Once uploaded, open Serial Monitor (use 115200 speed) and observe
  23.  
  24.   Note: The MAC address can be anything so long as it is unique on your network.
  25.  
  26.   Created by: Dr. Charles A. Bell
  27. */
  28. #include <Ethernet.h>
  29. #include <MySQL_Connection.h>
  30. #include <MySQL_Cursor.h>
  31.  
  32. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  33. IPAddress ip(10, 1, 1, 10);       //Define o endereco IP
  34. //IPAddress gateway(10, 1, 1, 1);  //Define o gateway
  35. //IPAddress subnet(255, 0, 0, 0); //Define a máscara de rede
  36.  
  37.  
  38. IPAddress server_addr(10, 1, 1, 3); //Define o endereço IP do servidor
  39. char user[] = "ARDUINO10"; //usuario
  40. char password[] = "100nha"; //senha
  41. // Sample query
  42.  
  43. char query[] = "USE `test_arduino`; SELECT acesso FROM autorizacao WHERE ID = '%DE 3B 9A 32%';";
  44.  
  45.  
  46. EthernetClient client;
  47. MySQL_Connection conn((Client *)&client);
  48. MySQL_Cursor cur = MySQL_Cursor(&conn);
  49.  
  50. void setup() {
  51.   Serial.begin(115200);
  52.   while (!Serial); // wait for serial port to connect
  53.   Ethernet.begin(mac, ip);
  54.   Serial.println("Connecting...");
  55.   if (conn.connect(server_addr, 3306, user, password)) {
  56.     delay(1000);
  57.   }
  58.   else
  59.     Serial.println("Connection failed.");
  60. }
  61.  
  62.  
  63. void loop() {
  64.   row_values *row = NULL;
  65.   long head_count = 0;
  66.  
  67.   delay(1000);
  68.  
  69.   //Serial.println("1) Demonstrating using a cursor dynamically allocated.");
  70.   // Initiate the query class instance
  71.   MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  72.   // Execute the query
  73.   cur_mem->execute(query);
  74.   // Fetch the columns (required) but we don't use them.
  75.   column_names *cols = cur_mem->get_columns();
  76.   // Read the row (we are only expecting the one)
  77.   do {
  78.     row = cur_mem->get_next_row();
  79.     if (row != NULL) {
  80.       head_count = atol(row->values[0]);
  81.     }
  82.   } while (row != NULL);
  83.   // Deleting the cursor also frees up memory used
  84.   delete cur_mem;
  85.  
  86.   // Show the result
  87.   //Serial.print("  NYC pop = ");
  88.   Serial.println(head_count);
  89.  
  90.   delay(500);
  91.  
  92.   //  Serial.println("2) Demonstrating using a local, global cursor.");
  93.   //  // Execute the query
  94.   //  cur.execute(query);
  95.   //  // Fetch the columns (required) but we don't use them.
  96.   //  cur.get_columns();
  97.   //  // Read the row (we are only expecting the one)
  98.   //  do {
  99.   //    row = cur.get_next_row();
  100.   //    if (row != NULL) {
  101.   //      head_count = atol(row->values[0]);
  102.   //    }
  103.   //  } while (row != NULL);
  104.   //  // Now we close the cursor to free any memory
  105.   //  cur.close();
  106.   //
  107.   //  // Show the result but this time do some math on it
  108.   //  //Serial.print("  NYC pop = ");
  109.   //  Serial.println(head_count);
  110. }
Advertisement
Add Comment
Please, Sign In to add comment