0

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

我确定我对用户/传递的事情是正确的,但是我真的不知道为什么它不会连接到数据库来执行查询。

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

1 回答 1

1

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

于 2020-10-21T15:06:59.770 回答