MySQL_Connection library with ESP32

Hello everyone,

I am trying to connect ESP32-CAM to MySQL database, first I did it successfully when the MySQL sever is running on my laptop and both ESP32-CAM and my laptop are connecting to the same WiFi network.

When I tried to connect ESP32-CAM to MySQL server existed in another laptop existed in another city, the connection was failed.

Does MySQL library have the ability to allow remote connection to MySQL? I just need to make sure the whole idea can be done to start figuring out the problem of connection - whether they are some settings on MySQL server, firewall or etc.

here is the code,

#include <WiFi.h>           // Use this for WiFi instead of Ethernet.h
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>

IPAddress server_addr(192,168,***,**);  // IP of the MySQL *server* here
char user[] = "****";              // MySQL user login username
char password[] = "****";             // MySQL user login password

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

const char QUERY_POP[] = "SELECT platenumber FROM users.cars";
char query[128];

WiFiClient client;
MySQL_Connection conn((Client *)&client);

void setup() {
 Serial.begin(115200);
  while (!Serial); // wait for serial port to connect. Needed for Leonardo only

  // 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.print("Connecting to SQL...  ");
  if (conn.connect(server_addr, 3306, user, password))
    Serial.println("OK.");
  else
    Serial.println("FAILED.");
  
}


void loop() {
  delay(10000);
  Serial.println("> Running SELECT with dynamically supplied parameter");

  // Initiate the query class instance
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  // Supply the parameter for the query
  // Here we use the QUERY_POP as the format string and query as the
  // destination. This uses twice the memory so another option would be
  // to allocate one buffer for all formatted queries or allocate the
  // memory as needed (just make sure you allocate enough memory and
  // free it when you're done!).
  sprintf(query, QUERY_POP, 9000000);
  // Execute the query
  cur_mem->execute(query);
  // Fetch the columns and print them
  column_names *cols = cur_mem->get_columns();
  for (int f = 0; f < cols->num_fields; f++) {
    Serial.print(cols->fields[f]->name);
    if (f < cols->num_fields-1) {
      Serial.print(',');
    }
  }
  Serial.println();
  // Read the rows and print them
  row_values *row = NULL;
  do {
    row = cur_mem->get_next_row();
    if (row != NULL) {
      for (int f = 0; f < cols->num_fields; f++) {
        Serial.print(row->values[f]);
        if (f < cols->num_fields-1) {
          Serial.print(',');
        }
      }
      Serial.println();
    }
  } while (row != NULL);
  // Deleting the cursor also frees up memory used
  delete cur_mem;
}
1 Like

Has the remote dB been configured to accept remote connections?

Also, the SQL logs of the server are a great source of information on why the connection may have been refused.

Oh, Yes MySQL does allow remote connections.

Bonsoir moi je suis un débutant en arduino et je souhaite relier mon esp32 à ma base de données qui sont connectées dans le même réseau pouvez vous m'aider svp

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.