| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #include <Wire.h>
- #include <WiFi.h>
- #include <ArduinoHttpClient.h>
- #include <base64.h>
- const char* username = "..."; // 修改为您的用户名
- const char* password = "..."; // 修改为您的密码
- const char* device_id = "A00002";
- const char* ssid = ".."; // 您的 WiFi 网络名称
- const char* passwords = "..."; // 您的 WiFi 密码
- const char* serverAddress = "192.168.3.31"; // 服务器地址
- const int serverPort = 5001; // 服务器端口
- const uint8_t address = 0x38; // AHT10 设备地址,默认为 0x38
- float temperature; // 温度变量
- float humidity; // 湿度变量
- int data[6]; // 存储传感器数据的数组
- WiFiClient wifiClient; // WiFi 客户端
- HttpClient httpClient = HttpClient(wifiClient, serverAddress, serverPort);
- void AHT10_measure() {
- // 先读
- Wire.requestFrom(address, (uint8_t)6); // 从设备地址请求 6 个字节的数据
- while (Wire.available()){
- for (int i = 0; i < 6; i++) {
- data[i] = Wire.read(); // 读取数据存储到数组中
- }
- }
- // 解析温湿度数据
- humidity = ((data[1] << 12) | (data[2] << 4)) | (data[3] >> 4);
- temperature = ((data[3] & 0x0f) << 16) | (data[4] << 8) | data[5];
- humidity = (humidity / pow(2, 20)) * 100;
- temperature = (temperature / pow(2, 20)) * 200 - 50;
- // 等待
- delay(500)
- // 发送
- Wire.beginTransmission(address); // 发送开始信号,并指定设备地址
- if ((data[0] & 0x08) == 0) {
- Wire.write(0xE1); // 发送初始化命令
- Wire.write(0x08);
- Wire.write(0x00);
- } else {
- Wire.write(0xAC); // 发送测量命令
- Wire.write(0x33);
- Wire.write(0x00);
- delayMicroseconds(75); // 延迟等待传感器完成测量
- }
- // 结束发送
- Wire.endTransmission();
- // 发送到服务器
- sendDataToServer();
- }
- void sendDataToServer() {
- // 构造 URL,包含温度和湿度数据
- String url = "/data?temperature=" + String(temperature) + "&humidity=" + String(humidity) + "&device_id=" + String(device_id);
- // 添加身份验证信息到请求头
- String auth = String(username) + ":" + String(password);
- String base64Auth = base64::encode(auth); // 编码为 Base64 字符串
- // 构造 Authorization 请求头
- httpClient.beginRequest();
- httpClient.get(url); // 修改此处为包含温度、湿度和设备ID参数的完整URL
- httpClient.sendHeader("Host", serverAddress);
- httpClient.sendHeader("Authorization", "Basic " + base64Auth);
- httpClient.sendHeader("Connection", "close");
- httpClient.sendHeader("Content-Type", "application/json");
- httpClient.endRequest();
- // 打印温度和湿度信息
- Serial.print("发送温度: ");
- Serial.print(temperature);
- Serial.println(" ℃");
- Serial.print("发送湿度: ");
- Serial.print(humidity);
- Serial.println(" %");
- // 连接服务器
- int statusCode = httpClient.responseStatusCode(); // 获取响应状态码
- if (statusCode == 200) {
- Serial.println("数据上传成功!");
- } else {
- Serial.println("数据上传失败!");
- }
- }
- void connectToWiFi() {
- Serial.println(); // 打印空行
- Serial.println();
- Serial.print("连接wifi: ");
- Serial.println(ssid); // 打印连接到的 WiFi 网络名称
- Serial.print("wifi密码: ");
- Serial.println(passwords);
- Serial.print("服务器地址: ");
- Serial.println(serverAddress);
- WiFi.begin(ssid, passwords); // 连接到 WiFi 网络
- while (WiFi.status() != WL_CONNECTED) {
- delay(500); // 延迟 0.5 秒
- Serial.print(".");
- }
- Serial.println(""); // 打印空行
- Serial.println("wifi连接成功!"); // 打印 WiFi 连接成功信息
- Serial.print("IP地址: ");
- Serial.println(WiFi.localIP()); // 打印获取到的 IP 地址
- Serial.print("信号强度");
- Serial.println(WiFi.RSSI());
- }
- void setup() {
- Wire.begin(20, 21); // 初始化 I2C 总线,指定 SDA 和 SCL 引脚
- Serial.begin(9600); // 初始化串口通信
- connectToWiFi(); // 连接到 WiFi 网络
- }
- void loop() {
- AHT10_measure(); // 测量温湿度数据
- delay(2000); // 延迟 2 秒
- }
|