element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Upcycle It
  • Challenges & Projects
  • Design Challenges
  • Upcycle It
  • More
  • Cancel
Upcycle It
Blog [Upcycle It] WiFi Connected Smoke Detector #3: Connecting dots - MQTT + Node.js + Slack
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: vlasov01
  • Date Created: 8 Apr 2017 8:53 PM Date Created
  • Views 1452 views
  • Likes 6 likes
  • Comments 8 comments
  • mqtt
  • node
  • slack
Related
Recommended

[Upcycle It] WiFi Connected Smoke Detector #3: Connecting dots - MQTT + Node.js + Slack

vlasov01
vlasov01
8 Apr 2017
<< Previous

Project Index

Next >>

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.

image

 

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.

  • Sign in to reply

Top Comments

  • jasonwier92
    jasonwier92 over 8 years ago +2
    Very good write up and thought into your topic. I really like the idea, and I have never done this, is put in if a device type, assuming it is only doing one function. But in reality, the MQTT topic for…
  • vlasov01
    vlasov01 over 8 years ago in reply to mcb1 +1
    Thank you for the feedback! I've added a diagram to clarify events processing sequence. Sergey
  • vlasov01
    vlasov01 over 8 years ago in reply to jasonwier92 +1
    Hi Jason, Thank you a lot for the feedback. I've started coding. I'll publish my Node.js code this week. Here is the part responsible for subscription to smoke detector alarms state/connection state from…
  • vlasov01
    vlasov01 over 8 years ago in reply to jasonwier92

    Hi Jason,

     

    Thank you a lot for the feedback.

    I've started coding. I'll publish my Node.js code this week. Here is the part responsible for subscription to smoke detector alarms state/connection state from any location.

    client.on('connect', () => {
      //subscription to smoke detector alarms and connection states from any location
      client.subscribe('+/alarm/smoke/+')
      //subscription to alarms monitors connection states from any location
      //this subscription is useful for logging/testing
      client.subscribe('+/alarm/monitor/+')
      
      const monitor_topic = 'home/alarm/monitor/connected';
      // publish a message to a topic - "The home alarm monitor is connected"
      client.publish(monitor_topic, 'true', function() {
        console.log("Home alarms monitor connected");
      });
    })

    I prefer not to set device name at root of topic tree as devices may change over time. This change may force changes to all connected devices, which need to publish to any topics on the tree. Please note, that current MQTT standard only allows use of wildcards for subscriptions, but not for publications. So to limit any significant impact of such changes the topic structure should be more stable at least near root of the tree.

     

    MQTT specification includes Keep Alive feature. And Mosquitto implements it as well. In addition to Keep Alive, there is a Last Will and Testament (LWT). It allows MQTT client request the broker to publish a message to a specified topic when connection get unexpectadly lost. Here is a good description when it gets triggered:

    • An I/O error or network failure is detected by the server.
    • The client fails to communicate within the Keep Alive time.
    • The client closes the network connection without sending a DISCONNECT packet first.
    • The server closes the network connection because of a protocol error.
    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jasonwier92
    jasonwier92 over 8 years ago in reply to Workshopshed

    # are multi level

    + are single level

     

    so subscribing to #/sensor/smoke/# would have been better.

     

    I have one node running ESPEasy (outdoor temperature) and it is odd in how it reports.  Wild cards do not work correctly on it.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Workshopshed
    Workshopshed over 8 years ago in reply to jasonwier92

    Hi Jason what's the difference between # and + in the wildcards?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jasonwier92
    jasonwier92 over 8 years ago

    Very good write up and thought into your topic.  I really like the idea, and I have never done this, is put in if a device type, assuming it is only doing one function.   But in reality, the MQTT topic for each sensor inside a device and be specific to what it does.  Then in the future you can monitor based off this structure.  So your Node application, can listen for #/#/smoke/state and get state changes for all locations.  Then query a database to see what to do based off the structure of the topic.  Only suggestions I would make would be the following.

     

    1. Work in a device name or indicator to the structure. I think this would be at the start of the structure, since it is the most broad level you will have.  For example, I have a light controller in my basement, well it also monitors current flow to my 80 tower, so I know when power is out to the tower.  It controls lights for several devices around my home.  So all of it's MQTT topics start with rpi01/...  This way I know what device is doing what and do not need to refer to a document to find what device has that message and command.  Full example like you gave above based off an Edison with IP reservation ending in 138:
      "Eddy138/kitchen/alarm/smoke/connected"
    2. With this example a device might have several locations or might not.  Most likely it will have several functions, like sensor or alarm or power. Also several types and commands|state
    3. Not really MQTT topic structure important, but it will be done via MQTT. One needs to know when sensors are not responding, so I think the best way to do that is with heartbeats being sent out on a regular basis. Last thing you want is a fire that was not detected, because a sensor has stopped working two months ago. If the Edison stops working or gets powered off, it does not send out it's heartbeat and a process at central command can monitor for this heartbeat and alert you when a device is non-responsive.  Two ways to do this:
      1. Every minute send out a MQTT topic of <devicename>/heartbeat with a message of "alive"
      2. Have a process that responds to alive queries, like a master node that publishes <device>/query/status and the device then responds with <device>/alive with a message of "yes", maybe also report battery level, etc.

     

    Once again, great job on getting this started and let me know if we want to roll in a device name.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • vlasov01
    vlasov01 over 8 years ago in reply to DAB

    Thank you for your encouragement and motivation!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube