Rodrigo_Moraes

Untitled

May 30th, 2017
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.86 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.   Modificado por : Rodrigo M. Moraes
  28. */
  29.  
  30. #include <SPI.h>
  31.  
  32. #include <Dhcp.h>
  33. #include <Dns.h>
  34. #include <Ethernet.h>
  35. #include <EthernetClient.h>
  36. #include <EthernetServer.h>
  37. #include <EthernetUdp.h>
  38.  
  39. #include <MySQL_Connection.h>
  40. #include <MySQL_Cursor.h>
  41. #include <MySQL_Encrypt_Sha1.h>
  42. #include <MySQL_Packet.h>
  43.  
  44. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  45. IPAddress ip(10, 1, 1, 10);       //Define o endereco IP
  46. //IPAddress gateway(10, 1, 1, 1);  //Define o gateway
  47. //IPAddress subnet(255, 0, 0, 0); //Define a máscara de rede
  48.  
  49.  
  50. IPAddress server_addr(10, 1, 1, 3); //Define o endereço IP do servidor
  51. char user[] = "ARDUINO10"; //usuario
  52. char password[] = "100nha"; //senha
  53. char INSERT_DATA[] = "USE `test_arduino`; INSERT INTO `autorizacao` (`Nome`, `ID`, `acesso`) VALUES ('%s', '%s', %i)";
  54. char SELECT_DATA[] = "SELECT acesso FROM test_arduino.autorizacao WHERE ID LIKE '%%s%'";
  55. char query[2048];
  56. //char query2[2048];
  57. //String oq = " Id ";
  58. EthernetClient client;
  59. MySQL_Connection conn((Client *)&client);
  60. MySQL_Cursor cur = MySQL_Cursor(&conn);
  61. bool in = true;
  62. String nome  = "";
  63. char id[15] = "";
  64. String ID = "";
  65. String card = "";
  66. char card_id[20] = "";
  67. String acesso;
  68. char nomee[100] = "";
  69. bool inserir_dados = false;
  70.  
  71. void setup() {
  72.   Ethernet.begin(mac, ip);
  73.   Serial.begin(115200);
  74.   pinMode (4, 1);
  75.   pinMode (5, 1);
  76.   while (!Serial);
  77.   delay(1000);
  78.   Serial.println("Connecting...");
  79.   if ((conn.connect(server_addr, 3390, user, password))) {
  80.     delay(1000);
  81.  
  82.     Serial.println("Success!");
  83.   }
  84.   else {
  85.     Serial.println("Connection failed.");
  86.     conn.close();
  87.   }
  88.   Serial.println("OK");
  89.  
  90. }
  91.  
  92.  
  93. void loop() {
  94.   card = "123456";
  95.   //INSERT DATA
  96.   if (digitalRead(4)) { // Entrada do Botao para inserir dados no banco de dados
  97.     Serial.println("Insira o Nome");// Quando pressionado o botao ira pedir para digitar o nome na Serial
  98.     while (Serial.available() <= 0) ;// Quando serial for igual a 0 nao acontece nada
  99.     nome = Serial.readString();
  100.     Serial.println(nome);
  101.     Serial.flush();
  102.     delay(100);
  103.     Serial.println("Insira o ID");
  104.     while (Serial.available() <= 0);
  105.     ID = Serial.readString();
  106.     Serial.println(ID);
  107.     Serial.flush();
  108.     delay(100);
  109.     Serial.println("Insira o nivel de acesso");
  110.     while (Serial.available() <= 0);
  111.     acesso = Serial.readString();
  112.     Serial.println(acesso);
  113.     Serial.flush();
  114.     delay(100);
  115.  
  116.     int acessoo = acesso.toInt(); // converte o nivel de acesso digitado no Serial para um valor inteiro
  117.     nome.toCharArray(nomee, 100); //converte o nome digitado no Serial para um conjunto de caracteres
  118.     ID.toCharArray(id, 15); //converte o ID digitado no Serial para um conjunto de caracteres
  119.     delay(1000);
  120.     MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  121.     sprintf(query, INSERT_DATA, nomee, id, acessoo);
  122.     cur_mem->execute(query);
  123.     delete cur_mem;
  124.     Serial.println("Dado enserido com sucesso.");
  125.     inserir_dados = false;
  126.     Serial.flush();
  127.   }
  128.   //READ DATA
  129.   if (digitalRead(5)) { //vefiricar id do cartao
  130.     card.toCharArray(card_id, 20);
  131.     row_values *row = NULL;
  132.     long cartao;
  133.     MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  134.     sprintf(query, SELECT_DATA, card_id);
  135.     Serial.println(query);
  136.     // Execute the query
  137.     cur_mem->execute(query);
  138.     // Fetch the columns (required) but we don't use them.
  139.     column_names *columns = cur_mem->get_columns();
  140.  
  141.     // Read the row (we are only expecting the one)
  142.     do {
  143.       row = cur_mem->get_next_row();
  144.       if (row != NULL) {
  145.         cartao = atol(row->values[0]);
  146.       }
  147.     } while (row != NULL);
  148.     // Deleting the cursor also frees up memory used
  149.     delete cur_mem;
  150.  
  151.     // Show the result
  152.     Serial.println(cartao);
  153.  
  154.     delay(500);
  155.   }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment