<< Previous |
Integration Design
Once sensor detects smoke, it will publish a message to MQTT topic. Then this message will be passed to a mobile application and at the same time will be converted to a command to switch of all connected electric appliances in the room where smoke has been detected. It is important to define the topic model before coding.
MQTT Topics structure
I've used a topics structure from a best practices article as a starting point. I've defined a topic structure for home monitor/sensor/appliances:
An alarm sensor topic structure for this
<location>/<alarm>/<sensor type>/<connected>|<state>
Examples:
kitchen/alarm/smoke/connected
kitchen/alarm/smoke/state
A monitor topic structure
<location>/<alarm>/<monitor type>/<connected>
Example:
home/alarm/monitor/connected
An appliance topic structure
<location>/<appliance>/<appliance type>/<connected>|<command>
Example:
kitchen/appliance/oven/power
MQTT messages to topics
To simplify things I've decided that a message to these topics can be published as true or false.
Examples:
1. A smoke alarm is connected in a kitchen:
kitchen/alarm/smoke/connected/true
2. A smoke alarm in a kitchen detected smoke:
kitchen/alarm/smoke/state/true
3. A command to turn power off for an oven in a kitchen:
kitchen/appliance/oven/power/off
4. A command to turn off socket 1 of PDU (see PDU Upcycle project) in a workshop:
workshop/appliance/pdu-socket1/power/off
5. A command to turn off outlet 1 of ESP8266-01 and MQTT for AC Outlet Control project by codemonkey0 in a workshop:
workshop/appliance/outlet1/power/off
I need to validate this topics model approach with jasonwier92
Home Alert Monitor
Node.js is a nice platform to create a small server. In my project it will be listening to MQTT topics, sending commands and communication with mobile/web channel.
MQTT Node.js client Installation
Node.js is already installed on Edison. I've added Node MQTT client:
npm install mqtt -g mqtt@2.5.1 /usr/lib/node_modules/mqtt ├── inherits@2.0.3 ├── reinterval@1.1.0 ├── xtend@4.0.1 ├── minimist@1.2.0 ├── concat-stream@1.6.0 (typedarray@0.0.6) ├── commist@1.0.0 (leven@1.0.2) ├── split2@2.1.1 (through2@2.0.3) ├── end-of-stream@1.4.0 (once@1.4.0) ├── pump@1.0.2 (once@1.4.0) ├── mqtt-packet@5.2.2 (process-nextick-args@1.0.7, bl@1.2.0) ├── readable-stream@2.2.6 (buffer-shims@1.0.0, string_decoder@0.10.31, util-deprecate@1.0.2, process-nextick-args@1.0.7, core-util-is@1.0.2, isarray@1.0.0) ├── websocket-stream@3.3.3 (through2@2.0.3, ws@1.1.4, duplexify@3.5.0) └── help-me@1.0.1 (through2@2.0.3, callback-stream@1.1.0, glob-stream@5.3.5)
For test I've opened two SSH terminals to Edison board.
One to subscribe to a test topic
mqtt sub -t 'test' -h 'localhost' -v
and one to send a test message
mqtt pub -t 'test' -h 'localhost' -m 'from MQTT.js'
and got the message in the first window:
test from MQTT.js
Slack client Installation
I'm planning to use Slack mobile application to display a state of the alarm system on the go. Slack allows to create a free channels which can be used by teams/families.
npm install @slack/client -g @slack/client@3.9.0 /usr/lib/node_modules/@slack/client ├── inherits@2.0.3 ├── eventemitter3@1.2.0 ├── url-join@0.0.1 ├── async@1.5.2 ├── retry@0.9.0 ├── pkginfo@0.4.0 ├── ws@1.1.4 (options@0.0.6, ultron@1.0.2) ├── bluebird@3.5.0 ├── https-proxy-agent@1.0.0 (extend@3.0.0, debug@2.6.3, agent-base@2.0.1) ├── winston@2.3.1 (cycle@1.0.3, stack-trace@0.0.9, eyes@0.1.8, isstream@0.1.2, async@1.0.0, colors@1.0.3) ├── request@2.81.0 (aws-sign2@0.6.0, oauth-sign@0.8.2, tunnel-agent@0.6.0, forever-agent@0.6.1, caseless@0.12.0, is-typedarray@1.0.0, stringstream@0.0.5, safe-buffer@5.0.1, aws4@1.6.0, isstream@0.1.2, extend@3.0.0, json-stringify-safe@5.0.1, performance-now@0.2.0, uuid@3.0.1, qs@6.4.0, combined-stream@1.0.5, mime-types@2.1.15, tough-cookie@2.3.2, form-data@2.1.2, hawk@3.1.3, http-signature@1.1.1, har-validator@4.2.1) └── lodash@4.17.4
The next step is to create a channel and obtain a token for integration with Slack API.
Top Comments