8266上傳到mysql數(shù)據(jù)庫的方法是通過連接Wi-Fi將數(shù)據(jù)傳輸?shù)椒?wù)器上的mysql數(shù)據(jù)庫中。
#include#include #include #include // Replace with your network credentials const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; // Replace with your MySQL IP address and credentials IPAddress server_addr(192,168,1,1); // MySQL server IP char user[] = "your_USER"; char password[] = "your_PASSWORD"; WiFiClient client; MySQL_Connection conn((Client*)&client); void setup() { // Connect to Wi-Fi network WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to Wi-Fi..."); } Serial.println("Connected to Wi-Fi"); } void loop() { if (WiFi.status() == WL_CONNECTED) { // Use HTTP client to fetch data from a remote server HTTPClient http; http.begin("http://example.com/data.txt"); int http_code = http.GET(); if (http_code >0) { Serial.printf("[HTTP] GET... code: %d\n", http_code); // Parse data and upload to MySQL server String payload = http.getString(); MySQL_Cursor* cur_mem = new MySQL_Cursor(&conn); cur_mem->execute("USE mydatabase"); String query = "INSERT INTO mytable (data) VALUES ('" + payload + "')"; cur_mem->execute(query); delete cur_mem; } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(http_code).c_str()); } http.end(); // Disconnect from Wi-Fi network WiFi.disconnect(); delay(5000); } }
以上代碼將連接Wi-Fi,并通過HTTP客戶端從服務(wù)器上獲取數(shù)據(jù)。然后將數(shù)據(jù)上傳到MySQL服務(wù)器中的mytable表中。