本帖最后由 lanhemeng 于 2018-1-14 19:44 编辑
1.先参考囧神的帖子安装MQTTUbuntu安装Mosquitto也就是MQTT(无坑版);
2.在configuration.yaml添加如下代码:
mqtt:
broker: 127.0.0.1
port: 1883
client_id: home-assistant-1
keepalive: 60
username: mqtt
password: MQTT
discovery: true
3.在configuration.yaml中添加sensor, 这里以攀藤G5S PM2.5,甲醛,温湿度三合一传感器为例;参考了大神的帖子
攀藤G5+WidoraAIR(ESP32) 空气质量检测 接入HomeAssistant
sensor:
- platform: mqtt
state_topic: 'dev/ptg5s'
name: 'HCHO'
unit_of_measurement: 'mg/m3'
value_template: '{{ value_json.HCHO}}'
- platform: mqtt
state_topic: 'dev/ptg5s'
name: 'PM2.5'
unit_of_measurement: 'μg/m3'
value_template: '{{ value_json.pm2p5}}'
- platform: mqtt
state_topic: 'dev/ptg5s'
name: 'PM1.0'
unit_of_measurement: 'μg/m3'
value_template: '{{ value_json.pm1p0}}'
- platform: mqtt
state_topic: 'dev/ptg5s'
name: 'PM10'
unit_of_measurement: 'μg/m3'
value_template: '{{ value_json.pm10}}'
4.Node FL程序
A.添加serial 串口节点,并设置串口
B.添加Function节点,并填充以下JS代码;
arry = new Uint8Array(msg.payload);
PM1CF=arry[4]*256+arry[5];
PM25CF=arry[6]*256+arry[7];
PM10CF=arry[8]*256+arry[9];
PM1DQ=arry[10]*256+arry[11];
PM25DQ=arry[12]*256+arry[13];
PM10DQ=arry[14]*256+arry[15];
HCHO=(arry[28]*256+arry[29])/1000;
T=(arry[30]*256+arry[31])/10;
H=(arry[32]*256+arry[33])/10;
Version=arry[36];
Err=arry[37];
global.set("PM1CF",PM1CF);
global.set("PM25CF",PM25CF);
global.set("PM10CF",PM10CF);
global.set("PM1DQ",PM1DQ);
global.set("PM25DQ",PM25DQ);
global.set("PM10DQ",PM10DQ);
global.set("HCHO",HCHO);
global.set("T",T);
global.set("H",H);
global.set("Version",Version);
global.set("Err",Err);
var msg1 = { payload:PM1CF};
var msg2 = { payload:PM25CF};
var msg3 = { payload:PM10CF};
var msg4 = { payload:PM1DQ};
var msg5 = { payload:PM25DQ};
var msg6 = { payload:PM10DQ};
var msg7 = { payload:HCHO};
var msg8 = { payload:T};
var msg9 = { payload:H};
var msg10 = { payload:Version};
var msg11 = { payload:Err};
return [msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9,msg10,msg11];
C.以甲醛为例,再添加一个Function,添加如下代码;
msg.payload={"HCHO": parseFloat(msg.payload)}
return msg;
C.添加一个MQTT输出节点,并新添加一个MQTT Server
D.依次添加其他Sensor,部署,完成接入
这里只是以串口设备为例,其他设备如485 modbus,WebSocket等设备也可以通过这种方法接入,非常简单;
完整Node FL 如下:
|