main.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <Wire.h>
  2. #include <WiFi.h>
  3. #include <ArduinoHttpClient.h>
  4. #include <base64.h>
  5. const char* username = "..."; // 修改为您的用户名
  6. const char* password = "..."; // 修改为您的密码
  7. const char* device_id = "A00002";
  8. const char* ssid = ".."; // 您的 WiFi 网络名称
  9. const char* passwords = "..."; // 您的 WiFi 密码
  10. const char* serverAddress = "192.168.3.31"; // 服务器地址
  11. const int serverPort = 5001; // 服务器端口
  12. const uint8_t address = 0x38; // AHT10 设备地址,默认为 0x38
  13. float temperature; // 温度变量
  14. float humidity; // 湿度变量
  15. int data[6]; // 存储传感器数据的数组
  16. WiFiClient wifiClient; // WiFi 客户端
  17. HttpClient httpClient = HttpClient(wifiClient, serverAddress, serverPort);
  18. void AHT10_measure() {
  19. // 先读
  20. Wire.requestFrom(address, (uint8_t)6); // 从设备地址请求 6 个字节的数据
  21. while (Wire.available()){
  22. for (int i = 0; i < 6; i++) {
  23. data[i] = Wire.read(); // 读取数据存储到数组中
  24. }
  25. }
  26. // 解析温湿度数据
  27. humidity = ((data[1] << 12) | (data[2] << 4)) | (data[3] >> 4);
  28. temperature = ((data[3] & 0x0f) << 16) | (data[4] << 8) | data[5];
  29. humidity = (humidity / pow(2, 20)) * 100;
  30. temperature = (temperature / pow(2, 20)) * 200 - 50;
  31. // 等待
  32. delay(500)
  33. // 发送
  34. Wire.beginTransmission(address); // 发送开始信号,并指定设备地址
  35. if ((data[0] & 0x08) == 0) {
  36. Wire.write(0xE1); // 发送初始化命令
  37. Wire.write(0x08);
  38. Wire.write(0x00);
  39. } else {
  40. Wire.write(0xAC); // 发送测量命令
  41. Wire.write(0x33);
  42. Wire.write(0x00);
  43. delayMicroseconds(75); // 延迟等待传感器完成测量
  44. }
  45. // 结束发送
  46. Wire.endTransmission();
  47. // 发送到服务器
  48. sendDataToServer();
  49. }
  50. void sendDataToServer() {
  51. // 构造 URL,包含温度和湿度数据
  52. String url = "/data?temperature=" + String(temperature) + "&humidity=" + String(humidity) + "&device_id=" + String(device_id);
  53. // 添加身份验证信息到请求头
  54. String auth = String(username) + ":" + String(password);
  55. String base64Auth = base64::encode(auth); // 编码为 Base64 字符串
  56. // 构造 Authorization 请求头
  57. httpClient.beginRequest();
  58. httpClient.get(url); // 修改此处为包含温度、湿度和设备ID参数的完整URL
  59. httpClient.sendHeader("Host", serverAddress);
  60. httpClient.sendHeader("Authorization", "Basic " + base64Auth);
  61. httpClient.sendHeader("Connection", "close");
  62. httpClient.sendHeader("Content-Type", "application/json");
  63. httpClient.endRequest();
  64. // 打印温度和湿度信息
  65. Serial.print("发送温度: ");
  66. Serial.print(temperature);
  67. Serial.println(" ℃");
  68. Serial.print("发送湿度: ");
  69. Serial.print(humidity);
  70. Serial.println(" %");
  71. // 连接服务器
  72. int statusCode = httpClient.responseStatusCode(); // 获取响应状态码
  73. if (statusCode == 200) {
  74. Serial.println("数据上传成功!");
  75. } else {
  76. Serial.println("数据上传失败!");
  77. }
  78. }
  79. void connectToWiFi() {
  80. Serial.println(); // 打印空行
  81. Serial.println();
  82. Serial.print("连接wifi: ");
  83. Serial.println(ssid); // 打印连接到的 WiFi 网络名称
  84. Serial.print("wifi密码: ");
  85. Serial.println(passwords);
  86. Serial.print("服务器地址: ");
  87. Serial.println(serverAddress);
  88. WiFi.begin(ssid, passwords); // 连接到 WiFi 网络
  89. while (WiFi.status() != WL_CONNECTED) {
  90. delay(500); // 延迟 0.5 秒
  91. Serial.print(".");
  92. }
  93. Serial.println(""); // 打印空行
  94. Serial.println("wifi连接成功!"); // 打印 WiFi 连接成功信息
  95. Serial.print("IP地址: ");
  96. Serial.println(WiFi.localIP()); // 打印获取到的 IP 地址
  97. Serial.print("信号强度");
  98. Serial.println(WiFi.RSSI());
  99. }
  100. void setup() {
  101. Wire.begin(20, 21); // 初始化 I2C 总线,指定 SDA 和 SCL 引脚
  102. Serial.begin(9600); // 初始化串口通信
  103. connectToWiFi(); // 连接到 WiFi 网络
  104. }
  105. void loop() {
  106. AHT10_measure(); // 测量温湿度数据
  107. delay(2000); // 延迟 2 秒
  108. }