【BIGTREETECH PI development board】Connect various sensors and controllers to the intelligent hub through MQTT
[Copy link]
This post was last edited by moveing on 2023-8-8 23:51
Sensors are like the eyes of smart homes, collecting various data in the environment, summarizing them and executing the set automation program according to the situation. The controller is equivalent to the hands of smart homes, and automation tasks are implemented through the controller. The original project used sensors such as temperature, humidity, light, combustible gas, human presence, face recognition, and actuators such as servos and access control.
Each sensor is in a different location in a different room, so the only way to connect to the hub is wireless. The current mainstream ones are Bluetooth, WiFi, and ZigBee. Bluetooth has a short transmission distance and requires a single-chip microcomputer to control the Bluetooth module. ZigBee is relatively expensive... Each sensor is equipped with a ZigBee. There are about 20 to 30 in the whole house. The cost is amazing. WiFi is more friendly, with more information, more models, longer transmission distance, and no need for a separate gateway. The most important thing is that the cost is extremely low! ESP01S : 8MBflash, 2 GPIOs, 3V power supply 71mA. It can be said to be a single-chip microcomputer with WiFi function. The unit price for small batch purchases is only 5r+!
Direct access is not possible, it needs to go through the MQTT server we built earlier. It can be understood this way: the MQTT server is the data center of transmission, all sensors, the subsequent controllers, and the HASS smart hub must be connected to it.
The data is not encrypted during the transmission process and is sent in the form of broadcast within the local area network. Each data packet has a Topic, and all devices in the network will receive this data packet. However, if the Topic does not match the one it has subscribed to, the device will directly discard it. Only when it matches will it read the data further.
Therefore, we need to assign a unique Topic to each sensor (each, not each type) for publishing , and then hass needs to subscribe to the Topics of all sensors; each actuator needs to subscribe to a unique Topic to receive instructions from hass.
Although each sensor is different, the general idea is the same. Sensor: ESP-01S configures WiFi, MQTT address, published Topic, reads sensor data, and sends it regularly. Actuator: Configure WiFi, MQTT address, select subscribed topic in advance, construct data packet structure, write unpacking function, and write action function. ESP-01S flash is too small to do OTA, so be sure to test the program, otherwise you need to disassemble it and re-burn it, which is very troublesome.
One thing to note: GPIO0 cannot be pulled down during normal startup, and it will enter the burning mode. Some sensors require two GPIO ports, so you need to pay attention when connecting. If it is really unavoidable, there is another way, which is to power on the ESP-01s first, delay for three seconds, and then power on the sensor, so that you can avoid entering the burning mode. I won’t go into details on how to achieve it.
Here is a dht11 temperature and humidity program. Other sensors such as light, combustible gas, and human body are similar and have no structural changes.
#include <SimpleDHT.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
const char* ssid = "3.1415926";
const char* password = "13305451048";
const char* mqtt_server = "192.168.100.182";
const char* client_id = "North-Room-dht11-1";
const char* TOPIC = "homeassistant/sensor/North-Room-dht11-1/state";//发布的Topic
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
int pinDHT11 = 2;//dht11连接esp-01s 2gpio口
SimpleDHT11 dht11(pinDHT11);
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}//回调函数
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");// 正在连接mqtt
if (client.connect(client_id)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");// 5秒后重试
delay(5000);
}
}
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}//连接WiFi函数
void setup() {
Serial.begin(115200);
Serial.println("system setup");
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
Serial.println("---------------------------");
byte temperature = 0;
byte humidity = 0;//温湿度变量
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err=");
Serial.println(err);
delay(1000);
return;
}
Serial.print("Sample OK: ");
Serial.print((int)temperature);
Serial.print(" *C, ");
Serial.print((int)humidity);
Serial.println(" H");
String payload = "{";
payload += "\"温度\":";
payload += "\"";
payload += temperature;
payload += "\"";
payload += ",";
payload += "\"湿度\":";
payload += "\"";
payload += humidity;
payload += "\"";
payload += "}";
char attributes[100];
payload.toCharArray(attributes, 100);
client.publish(TOPIC, attributes);//发送数据
delay(1500);//不要频繁发送,减小网络压力
}
After burning the program, power on, and open the emqx server web terminal, you can see a new connection
All connected sensors can be seen here. Be sure to define the name for easy management
Next, display the sensor information on the hass panel
In configuration.yaml, various sensors are classified by type and entered into their respective topics.
mqtt:
light:
- command_topic: "homeassistant/light/Room/"
name: "客厅灯"
sensor:
- state_topic: "homeassistant/sensor/North-Room-dht11-1/state"
name: "北卧室温度"
unit_of_measurement: "℃"
value_template: '{{ value_json.batt }}'
- state_topic: "homeassistant/sensor/North-Room-Ray-1/state"
name: "北卧室窗外光线"
unit_of_measurement: ""
value_template: '{{ value_json.batt }}'
- state_topic: "homeassistant/sensor/North-Room-Ray-2/state"
name: "北卧室室内光线"
unit_of_measurement: ""
value_template: '{{ value_json.batt }}'
- state_topic: "homeassistant/sensor/South-Room-Ray-1/state"
name: "南卧室窗外光线"
unit_of_measurement: ""
value_template: '{{ value_json.batt }}'
- state_topic: "homeassistant/sensor/South-Room-Ray-2/state"
name: "南卧室窗内光线"
unit_of_measurement: ""
value_template: '{{ value_json.batt }}'
- state_topic: "homeassistant/sensor/Kitchen-fire/state"
name: "可燃气传感器"
unit_of_measurement: ""
value_template: '{{ value_json.batt }}'
binary_sensor:
- state_topic: "homeassistant/sensor/North-Room-body/state"
name: "北卧室人体"
device_class: opening
value_template: '{{ value.x }}'
- state_topic: "homeassistant/sensor/South-Room-body/state"
name: "南卧室人体"
device_class: opening
value_template: '{{ value.x }}'
Just add various sensors to the dashboard
|