各位大神好!最近嘗試用ESP32傳送資料到MySQL,但遇上解決不了的問題(附圖中)
我使用Arduino IDE編寫,當WiFi連接完成再來與MySQL連接卻卡住了,檢查WiFi、MySQL帳密都正確(在同一個區域網路中),port也正常沒被擋,奇怪的是MySQL能看到有嘗試登入但失敗,有點像連線逾時?請教各路大神是否知道哪裡出問題,或者是否有人遇過類似狀況呢?
附上MySQL連接程式碼
#include <WiFi.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
const char* ssid = ""; //WIFI 名稱
const char* WIFI_password = ""; //WIFI 密碼
IPAddress server_addr( , , , ); // MySQL IP地址
unsigned int server_port = 3306; // MySQLport
char user[] = ""; // MySQL帳號
char password[] = ""; // MySQL密碼
char db[] = ""; // 數據庫
WiFiClient client;
MySQL_Connection conn((Client *)&client);
void setup() {
Serial.begin(115200);
delay(100);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, WIFI_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("Connecting to database...");
if (conn.connect(server_addr, server_port, user, password, db)) {
Serial.println("Connected to database");
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
char query[] = "SELECT * FROM your_table";
cur_mem->execute(query);
delete cur_mem;
} else {
Serial.println("Connection failed");
Serial.println("Exiting program");
while (true) {
delay(1);
}
}
}
void loop() {
delay(1000);
}