使用 ESP8266WiFi 芯片连接到 MySQL 数据库

Connecting to a MySQL database with an ESP8266WiFi chip

我正在尝试直接从我的 arduino 连接到远程 MySQL 数据库,以便在某些硬件上进行遥测。然而,代码在连接到数据库时卡住了,并且总是给出“找不到数据库”的答案。我哪里错了?

我确定我对 user/pass 的看法是正确的,但是我真的不明白为什么它无法连接到数据库来执行查询。

#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;
    
  }
}

我想通了。代码是正确的,我只是为 MySQL 服务器输入了错误的 IP!我是通过打开命令提示符并 ping 主机名发现它的;