【Follow me第二季第2期】智能家居之智能书房
[复制链接]
本帖最后由 鲜de芒果 于 2024-10-2 17:28 编辑
1. 任务要求
- 通过外部LTR-329 环境光传感器,上传温湿度到HA,通过HA面板显示数据
- 通过外部AHT20温湿度传感器,上传温湿度到HA,通过HA面板显示数据
- 使用板载LED模拟台灯,可通过HA面板控制其开关与亮度调节
- 书房自动光线调节是一个智能书房的重要组成部分,它能够根据环境光线的变化自动调整台灯的亮度或开关状态,以确保阅读或工作时的光线既充足又舒适
2. 硬件准备
3. HomeAssistant集成
**MQTT** 设备的发现将使人们能够在 [HomeAssistant](https://www.home-assistant.io) 方面只需要很少的配置工作就可以使用 **MQTT** 设备。配置是在设备本身和设备使用的主题上完成的。
> MQTT 发现默认启用,但可以禁用。发现主题的前缀(默认:`homeassistant`)可以更改。配置详情请参阅 [MQTT 选项部分](https://www.home-assistant.io/integrations/mqtt#configure-mqtt-options)
3.1 配置主题
向 MQTT 发送配置主题后, HomeAssistant 会自动发现当前传感器。 当前任务中使用了一个板载LED灯,作为 HomeAssistant 中的 Light 组件。该组件在 HomeAssistant 的 仪表盘 中可以远程控制板载LED的开和关。使用 MQTT 客户端向配置主题发送消息即可。本项目中使用了三个传感器 + 一个台灯,具体配置主题如下:
-
- {
- "name":"table-lamp",
- "device_class": "light",
- "command_topic":"homeassistant/light/FollowMe2-2-table-lamp/switch",
- "state_topic":"homeassistant/sensor/FollowMe2-2/state",
- "brightness_command_topic": "homeassistant/light/FollowMe2-2-table-lamp/brightness/set",
- "brightness_state_topic": "homeassistant/sensor/FollowMe2-2/state",
- "state_value_template": "{{ value_json.builtinLed }}",
- "brightness_value_template": "{{ value_json.brightness }}",
- "unique_id":"FollowMe2-2-table-lamp",
- "device":{
- "identifiers":[
- "Arduino UNO R4 WiFi"
- ],
- "name":"UNO R4 WiFi",
- "manufacturer": "Arduino",
- "model": "UNO R4 WiFi",
- "hw_version": "1.0"
- }
- }
-
-
- {
- "device_class":"temperature",
- "state_topic":"homeassistant/sensor/FollowMe2-2/state",
- "unit_of_measurement":"°C",
- "value_template":"{{ value_json.temperature}}",
- "unique_id":"FollowMe2-2-study-temperature",
- "device":{
- "identifiers":[
- "Arduino UNO R4 WiFi"
- ],
- "name":"UNO R4 WiFi",
- "manufacturer": "Arduino",
- "model": "UNO R4 WiFi",
- "hw_version": "1.0"
- }
- }
-
-
- {
- "device_class":"humidity",
- "state_topic":"homeassistant/sensor/FollowMe2-2/state",
- "unit_of_measurement":"%",
- "value_template":"{{ value_json.humidity}}",
- "unique_id":"FollowMe2-2-study-humidity",
- "device":{
- "identifiers":[
- "Arduino UNO R4 WiFi"
- ],
- "name":"UNO R4 WiFi",
- "manufacturer": "Arduino",
- "model": "UNO R4 WiFi",
- "hw_version": "1.0"
- }
- }
-
-
- {
- "device_class":"illuminance",
- "state_topic":"homeassistant/sensor/FollowMe2-2/state",
- "unit_of_measurement":"lx",
- "value_template":"{{ value_json.ambientLight}}",
- "unique_id":"FollowMe2-2-study-illuminance",
- "device":{
- "identifiers":[
- "Arduino UNO R4 WiFi"
- ],
- "name":"UNO R4 WiFi",
- "manufacturer": "Arduino",
- "model": "UNO R4 WiFi",
- "hw_version": "1.0"
- }
- }
4. 代码实现
-
- #include <Wire.h>
- #include <WiFiS3.h>
- #include <ArduinoMqttClient.h>
- #include <ArduinoJson.h>
- #include <Adafruit_AHTX0.h>
- #include <Adafruit_LTR329_LTR303.h>
-
- #include "arduino_secrets.h"
-
- #define LOOP_DELAY 10
- #define SENSOR_REPORT_INTERVAL 5
- #define STATE_ON "ON"
- #define STATE_OFF "OFF"
-
- char ssid[] = SECRET_SSID;
- char pass[] = SECRET_PASS;
- int status = WL_IDLE_STATUS;
-
-
- WiFiClient wifiClient;
- MqttClient mqttClient(wifiClient);
- const char broker[] = "192.168.2.120";
- int port = 1883;
- char mqtt_user[] = MQTT_USER;
- char mqtt_pass[] = MQTT_PASS;
-
- const char switch_subscribe_topic[] = "homeassistant/light/FollowMe2-2-table-lamp/switch";
-
- const char brightness_subscribe_topic[] = "homeassistant/light/FollowMe2-2-table-lamp/brightness/set";
- bool isNeedReportState = true;
- bool ledState = false;
-
- const char publish_topic[] = "homeassistant/sensor/FollowMe2-2/state";
- uint32_t tick = 0;
- uint8_t brightness = 0;
-
- Adafruit_AHTX0 aht;
- float temperature = 0;
- float humidity = 0;
- Adafruit_LTR329 ltr = Adafruit_LTR329();
- float ambientLight = 0;
- ltr329_gain_t gain = LTR3XX_GAIN_1;
- ltr329_integrationtime_t integrationTime = LTR3XX_INTEGTIME_100;
- float integrationTimeVal = 0.1;
- uint16_t visible_plus_ir;
- uint16_t infrared;
-
-
-
- void setLightState() {
- if (ledState) {
- if(0 == brightness) {
- brightness = 255;
- }
- analogWrite(LED_BUILTIN, brightness);
- } else {
- analogWrite(LED_BUILTIN, 0);
- }
- }
-
-
- void setup() {
-
- Serial.begin(115200);
- pinMode(LED_BUILTIN, OUTPUT);
-
-
- Wire1.begin();
-
- if (WiFi.status() == WL_NO_MODULE) {
- Serial.println("WiFi模块通信失败!");
- while (true);
- }
-
-
- Serial.print("尝试连接到 WIFI SSID: ");
- Serial.println(ssid);
- while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
- Serial.print(".");
- delay(5 * 1000);
- }
- Serial.println("WIFI 连接成功!");
-
-
- mqttClient.setUsernamePassword(mqtt_user, mqtt_pass);
- Serial.print("尝试连接到MQTT服务器: ");
- Serial.println(broker);
- if (!mqttClient.connect(broker, port)) {
- Serial.print("MQTT 连接失败! ");
- Serial.println(mqttClient.connectError());
- while (1);
- }
- Serial.println("MQTT连接成功!");
-
- mqttClient.onMessage(onMqttMessage);
- mqttClient.subscribe(switch_subscribe_topic);
- mqttClient.subscribe(brightness_subscribe_topic);
-
-
- if (!aht.begin(&Wire1)) {
- Serial.println("未找到 AHTX0 传感器!");
- while (1) delay(10);
- }
- Serial.println("AHTX0 传感器初始化成功!");
-
- if (!ltr.begin(&Wire1)) {
- Serial.println("未找到 LTR329 传感器!");
- while (1) delay(10);
- }
- Serial.println("LTR329 传感器初始化成功!");
- }
-
- void loop() {
- if(isNeedReportState) {
-
- char msg[100] = {0};
- sprintf(msg, "{\"builtinLed\": \"%s\", \"brightness\": %d,\"temperature\":%.2f,\"humidity\":%.2f, \"ambientLight\":%.2f}",
- ledState ? STATE_ON : STATE_OFF, brightness, temperature, humidity, ambientLight);
- mqttClient.beginMessage(publish_topic);
- mqttClient.print(msg);
- mqttClient.endMessage();
- isNeedReportState = false;
- }
- if(0 == (tick % (SENSOR_REPORT_INTERVAL * 1000 / LOOP_DELAY))) {
-
- sensors_event_t humi, temp;
- aht.getEvent(&humi, &temp);
- temperature = temp.temperature;
- humidity = humi.relative_humidity;
-
-
- if (ltr.newDataAvailable()) {
- ltr.readBothChannels(visible_plus_ir, infrared);
-
- float ratio = infrared / (visible_plus_ir + infrared);
- if(ratio < 0.45) {
- ambientLight = (1.7743 * visible_plus_ir + 1.1059 * infrared) / (1 << gain) / integrationTimeVal;
- } else if(ratio < 0.64 && ratio >= 0.45) {
- ambientLight = (4.2785 * visible_plus_ir - 1.9548 * infrared) / (1 << gain) / integrationTimeVal;
- } else if(ratio < 0.85 && ratio >= 0.64) {
- ambientLight = (0.5926 * visible_plus_ir + 0.1185 * infrared) / (1 << gain) / integrationTimeVal;
- } else {
- ambientLight = 0;
- }
- }
-
- isNeedReportState = true;
- }
-
- setLightState();
- mqttClient.poll();
- delay(LOOP_DELAY);
- tick ++;
- }
-
-
- void onMqttMessage(int messageSize) {
- char topic[100] = {0};
- String message;
-
- Serial.print("Received message from topic: ");
- strcpy(topic, mqttClient.messageTopic().c_str());
- Serial.println(topic);
-
-
- for (int i = 0; i < messageSize; i++) {
- message += (char)mqttClient.read();
- }
-
-
- Serial.print("Received message: ");
- Serial.println(message);
-
- if(0 == strcmp(switch_subscribe_topic, topic)) {
-
- if(0 == strcmp(STATE_ON, message.c_str())) {
- if(false == ledState) {
-
- isNeedReportState = true;
- }
- ledState = true;
- } else {
- if(true == ledState) {
-
- isNeedReportState = true;
- }
- ledState = false;
- }
- } else if(0 == strcmp(brightness_subscribe_topic, topic)) {
-
- uint8_t b = message.toInt();
- if (brightness < 0 || brightness > 255) {
-
- return;
- } else {
- brightness = b;
- setLightState();
- isNeedReportState = true;
- }
- }
- }
5. 智能家居自动化
本项目中结合 Adafruit LTR-329 Light Sensor 环境光传感器模块作为书房环境光照监测。当光照低于阀值时,自动打开台灯(本例中没有对接真实的智能台灯,使用板载LED模拟,可以控制开关与亮度)。光照充足时自动关闭台灯。实现自动调整书房光线,无需手动干预。在节能方面,避免不必要的照明,节省电力。同时保持书房光线在最佳水平,保护眼睛,提高阅读和工作效率。
6. 效果展示
7. 演示视频
8. 结语
Arduino Uno R4 WiFi智能书房是一个基于Arduino Uno R4开发板和ESP8266 Wi-Fi模块的智能家居项目。这个项目的主要功能是通过Wi-Fi连接,实现对书房内环境以及各种设备的远程控制和管理。
通过这个项目,我们不仅提高了对物联网技术的理解和运用能力,也学会了如何将理论转化为解决实际问题的应用。希望这些经验和心得能为未来的项目提供参考和启发。
9. 参考资料
10. 传送门
11. 项目源码
源码.zip
(7.76 KB, 下载次数: 2)
|