8266上传到mysql数据库

更新时间:02-08 教程 由 白满川 分享

8266上传到mysql数据库的方法是通过连接Wi-Fi将数据传输到服务器上的mysql数据库中。

#include#include#include#include// Replace with your network credentialsconst char* ssid = "your_SSID";const char* password = "your_PASSWORD";// Replace with your MySQL IP address and credentialsIPAddress server_addr(192,168,1,1); // MySQL server IPchar user[] = "your_USER";char password[] = "your_PASSWORD";WiFiClient client;MySQL_Connection conn((Client*)&client);void setup() {// Connect to Wi-Fi networkWiFi.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 serverHTTPClient 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 serverString 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 networkWiFi.disconnect();delay(5000);}}

以上代码将连接Wi-Fi,并通过HTTP客户端从服务器上获取数据。然后将数据上传到MySQL服务器中的mytable表中。

声明:关于《8266上传到mysql数据库》以上内容仅供参考,若您的权利被侵害,请联系13825271@qq.com
本文网址:http://www.25820.com/tutorial/14_2076061.html