send the array value to the mysql database

0

I would like to be able to send the array data in the convert.toCharArray parameter to the mysql database? My question is what value do I put in the INSERT line in the VALUES field? I can connect to the base but nothing appears in the table created (TESTES), my code is as follows:

   /*
  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
  thereof. See the documentation located in the /docs folder for more
  details.
  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>

// WiFi card example
char ssid[] = "----------";         // your SSID
char pass[] = "----------";     // your SSID Password

IPAddress server_addr(192,168,43,175);  // IP of the MySQL *server* here
char user[] = "root";              // MySQL user login username
char password[] = "admin";        // MySQL user login password
float tempC;// Define variável tempC convertendo-a para o tipo de dado float.
int reading;// Define variável reading convertendo-a para o tipo de dado int.
float referenceVoltage; // Define variável referenceVoltage convertendo-a para o tipo de dado float.
int tempPin = A0; //Pino analógico onde se encontra conetado o LM35
String convert; // Inicialização da variável convert como String 
char array[6]; // Inicialização do Array de char com 6 posições


char INSERT_SQL[] = "INSERT INTO MQTT.TESTES (Temperatura) VALUES (convert)";
char query[128];


WiFiClient client;                 // Use this for WiFi instead of EthernetClient
MySQL_Connection conn(&client);
MySQL_Cursor* cursor;

void setup()
{
  Serial.begin(115200);
  referenceVoltage = 1;// Tensão de referência do ESP82666
  // 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.println("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()

{
   reading = 0;
  for(int i = 0; i < 10; i++) { // Média de 10 leituras para que a leitura seja mais precisa
     reading += analogRead(tempPin); 
     //Serial.println(reading);// Mostra os valores das 10 leituras
     delay(20);// Tempo de leitura do pino A0 a cada 20 milisegundos
  }
   tempC =  (referenceVoltage * reading * 10) / 1023; // Fórmula de conversão da tensão analógica em valor de temperatura em graus Celsius
   Serial.println(tempC);
   convert += tempC; // Converte o valor da variável tempC em string
   convert.toCharArray(array, 6);// converte a string num array de char com 6 posições
   //Serial.println(array);
  Serial.print(query);
  if (conn.connected())
  sprintf(query, INSERT_SQL, array);
  cursor->execute(query);

  delay(5000);
}
    
asked by anonymous 01.10.2018 / 15:15

0 answers