色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

esp8266向mysql發(fā)送數(shù)據(jù)

吉茹定2年前15瀏覽0評論

在很多物聯(lián)網(wǎng)應(yīng)用中,將傳感器數(shù)據(jù)存儲到數(shù)據(jù)庫中是非常必要的。MySQL是一款流行且穩(wěn)定的數(shù)據(jù)庫管理系統(tǒng),而ESP8266是一款低成本且易于使用的Wi-Fi模塊。本文將介紹如何使用ESP8266將傳感器數(shù)據(jù)發(fā)送到MySQL數(shù)據(jù)庫中。

首先,先要確保ESP8266能成功連接到Wi-Fi網(wǎng)絡(luò)。以下是ESP8266連接Wi-Fi網(wǎng)絡(luò)的代碼:

#include <ESP8266WiFi.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
}

接下來,需要在MySQL服務(wù)器上創(chuàng)建一個用于存儲傳感器數(shù)據(jù)的數(shù)據(jù)庫和表。以下是可以使用的MySQL腳本:

CREATE DATABASE sensor_data;
USE sensor_data;
CREATE TABLE sensor_data readings (id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, temperature FLOAT, humidity FLOAT, timestamp TIMESTAMP);

在ESP8266中,需要安裝MySQL Connector/C++庫和MySQL Connector/Arduino庫。這些庫都可以在Arduino IDE中進行安裝。

以下是ESP8266發(fā)送傳感器數(shù)據(jù)到MySQL數(shù)據(jù)庫中的代碼:

#include <ESP8266WiFi.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
IPAddress server_addr(xxx, xxx, xxx, xxx); // MySQL服務(wù)器的IP地址
char user[] = "your_USER"; // MySQL服務(wù)器的用戶名
char password[] = "your_PASSWORD"; // MySQL服務(wù)器的密碼
char db[] = "sensor_data"; // 要連接的數(shù)據(jù)庫名
WiFiClient client;
MySQL_Connection conn((Client *)&client);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println("Connecting to MySQL...");
if (conn.connect(server_addr, 3306, user, password, db)) {
Serial.println("Connected to MySQL");
} else {
Serial.println("Connection failed");
}
}
void loop() {
float temperature = readTemperature();
float humidity = readHumidity();
storeReading(temperature, humidity);
delay(5000);
}
float readTemperature() {
// 讀取溫度值的代碼
}
float readHumidity() {
// 讀取濕度值的代碼
}
void storeReading(float temperature, float humidity) {
char query[256];
MySQL_Cursor *cursor = new MySQL_Cursor(&conn);
sprintf(query, "INSERT INTO readings (temperature, humidity) VALUES ('%.2f', '%.2f')", temperature, humidity);
cursor->execute(query);
delete cursor;
}

以上代碼中,readTemperature()和readHumidity()函數(shù)需要根據(jù)實際情況進行實現(xiàn)。storeReading()函數(shù)將溫度和濕度值插入到MySQL數(shù)據(jù)庫中。

到此為止,ESP8266就可以成功將傳感器數(shù)據(jù)發(fā)送到MySQL數(shù)據(jù)庫中了。