mysqlcarduinoiotesp8266wifi

Connecting to a MySQL database with an ESP8266WiFi chip


I'm trying to connect to a remote MySQL database directly from my arduino to do some telemetry on some hardware. However the code gets stuck while connecting to the db, and gives always the answer "no db found". Where am I wrong?

I'm sure that I'm correct with the user/pass thing, however I really can't figure out why it won't connect to the db to execute the query.

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

char ssid[] = "mywifissid";
char pass[] = "mywifipass";
char mysqlUser[] = "mysqluser";
char mysqlPass[] = "mysqlpassword";
char id[] = "someidforthearduino";
WiFiClient wifiClient;
MySQL_Connection mysqlConnection ((Client *)&wifiClient);
IPAddress mysqlServer (/*some kind of address for the mySQL server*/);
bool is_Sending = false;
char queryToExecute[128];
char queryUpdate[] = "somequery";
int nPresses = 0;

void setup() {
  Serial.begin(115200);
  Serial.println("Inizializzazione pin in corso...");
  pinMode(D4, INPUT_PULLUP);
  Serial.println("Connessione alla rete in corso...");
  WiFi.disconnect();
  WiFi.begin(ssid,pass);
  while(WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connesso con ip ");
  Serial.println(WiFi.localIP());
  Serial.println("Inizializzazione completata");
}

void loop() {
 if (!digitalRead(D4) && !is_Sending) {
    is_Sending = true;
    nPresses++;
    Serial.println("Rilevata pressione tasto. Connessione in corso...");
    if (mysqlConnection.connect(mysqlServer,3306,mysqlUser,mysqlPass)) {
      Serial.println("");
      Serial.println("Connesso. Inserimento dato...");
      sprintf(queryToExecute, queryUpdate, nPresses, id);
      MySQL_Cursor *c = new MySQL_Cursor(&mysqlConnection);
      c->execute(queryToExecute);
      delete c;
      Serial.println("Aggiornamento effettuato!");  
    } else {
      Serial.println("No db found.");
    }
    mysqlConnection.close();
    is_Sending = false;
    
  }
}

Solution

  • I figured it out. The code is correct, I just typed the wrong IP for the MySQL server! I discovered it by opening the command prompt and pinging the host name;