ESP32 connect to mysql database

tullaMann
Posts: 2
Joined: Sun Mar 11, 2018 8:43 pm

ESP32 connect to mysql database

Postby tullaMann » Sun Mar 11, 2018 8:57 pm

Hallo,

i'm new her. I can`t find a solution for my problem. I use a esp8266 with the esp8266wifi.h and the mysql_connection.h and mysql_coursor.h in my scripts.
And now i want make the same with the esp32, but donsn`t work. I can`t include the wifi.h and the two classes from mysql_ .
He set a exception
Arduino: 1.8.5 (Windows 10), Board: "ESP32 Dev Module, QIO, 80MHz, 4MB (32Mb), 921600, None"

Arduino\libraries\MySQL_Connector_Arduino-master\src\old\MySQL_Connection.cpp: In member function 'boolean MySQL_Connection::connect(IPAddress, int, char*, char*)':

\Arduino\libraries\MySQL_Connector_Arduino-master\src\old\MySQL_Connection.cpp:74:25: error: 'check_ok_packet' was not declared in this scope

if (check_ok_packet() != 0) {

^
the sketch for this:

Code: Select all

// LOAD CLASSES
#include "time.h"
#include <TimeAlarms.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <WiFi.h>


//LOAD CONFIGRATION
#include "default.h"
#include "setup.h"
#include "db.h"

WiFiServer  server(80);


void setup(){
  //DEFINE IO's
  
  pinMode(ACTIV_LED, OUTPUT);
  pinMode(ACTION_LINE1, INPUT);
  pinMode(ACTION_LINE2, INPUT);
  pinMode(ACTION_LINE3, INPUT);
  pinMode(ACTION_LINE4, INPUT);
  
  pinMode(ACTIVELINE1, OUTPUT);
  pinMode(ACTIVELINE2, OUTPUT);
  pinMode(ACTIVELINE3, OUTPUT);
  pinMode(ACTIVELINE4, OUTPUT);
  pinMode(ALARM_ACTIV, OUTPUT);


  //ACTIVATE SERIAL MONITORING
  Serial.begin(115200);
  delay(10);
  Serial.println("setup - ok");
  Serial.println("connect to the network... ");

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  delay(2000);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
   Serial.println(" connection established !");

   Serial.println("\n read timestamp from ntpServer ...");
   configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
   printLocalTime();

   getConfiguration();
}



void loop(){
  
}
Anyone can help me?

ikram.benabd
Posts: 1
Joined: Tue Mar 13, 2018 4:16 pm

Re: ESP32 connect to mysql database

Postby ikram.benabd » Wed Mar 14, 2018 11:30 am

did you find a solution ???

tullaMann
Posts: 2
Joined: Sun Mar 11, 2018 8:43 pm

Re: ESP32 connect to mysql database

Postby tullaMann » Thu Mar 15, 2018 5:11 pm

No, sorry!

I hav a another version with php an json.
I read the data from database per php und print it as json argument in the html.
The esp32 read this file and get the data for declare the variable..

if you are interest it, i write the code her. but`s not really the result for this conent.

phscar_skuldtec
Posts: 1
Joined: Mon Feb 10, 2020 1:04 pm

Re: ESP32 connect to mysql database

Postby phscar_skuldtec » Mon Feb 10, 2020 1:16 pm

I found a solution that worked in my case.

I use the following code

Code: Select all

#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <WiFi.h>

const char* ssid = "your wifi name";
const char* password = "your wifi password";

char user[] = "root"; // MySQL user login username
char passwordSQL[] = ""; // MySQL user login password

char INSERT_SQL[] = "INSERT INTO dbi.PARAMETRO(CANTEIRO, TIPO, HORA_INICIO, HORA_FIM, UMIDADE_INICIO, UMIDADE_FIM) VALUES ('CENTRAL ESP32', 'UMIDADE', '11:00:00', '12:30:00', '50', '70');";

IPAddress server_addr(your MySQL ip like (8, 8, 8, 8); // IP of the MySQL server here


WiFiServer  server(80);
void setup() {
  //Código de configuração aqui
  Serial.begin(115200);

  connectToNetwork();
  mySqlLoop();
}

void loop() {
}
void connectToNetwork() {
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Establishing connection to WiFi..");
  }
 
  Serial.println("Connected to network");
 
}
void mySqlLoop(){
  WiFiClient client;
  MySQL_Connection conn((Client *)&client);
  if (conn.connect(server_addr, 3306, user, passwordSQL)) {
    Serial.println("Database connected.");
  }
  else{
    Serial.println("Connection failed.");
    Serial.println("Recording data.");
  }
  // Initiate the query class instance
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  // Execute the query
  cur_mem->execute(INSERT_SQL);
  // Note: since there are no results, we do not need to read any data
  // Deleting the cursor also frees up memory used
  delete cur_mem;
  Serial.println("closing connection\n");
  //client.stop();
}
Work with MariaDB in xampp, and use direct connection without php neither json, just esp32 with arduino IDE

References: https://github.com/ChuckBell/MySQL_Conn ... issues/114
https://techtutorialsx.com/2017/06/29/e ... with-wifi/

Who is online

Users browsing this forum: No registered users and 40 guests