element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • 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
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • 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 #4: Monitor Design Decisions - 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: 16 Apr 2017 2:25 AM Date Created
  • Views 1662 views
  • Likes 4 likes
  • Comments 8 comments
  • design decision
  • mqtt
  • node.js
  • slack
Related
Recommended

[Upcycle It] WiFi Connected Smoke Detector #4: Monitor Design Decisions - MQTT + Node.js + Slack

vlasov01
vlasov01
16 Apr 2017
<< Previous

Project Index

Next>>

 

Alarms Monitor and design decisions

 

Alarms monitor is the one of the main components. It listens to alarms, sends notifications and commands. It relies on MQTT broker for communication. MQTT protocol allows fine tune communication depending on specific context. Here are several design decision that I've made to configure it for my connected smoke detector.

 

Quality of Service (QoS)

MQTT protocol supports three types of QoS.

  • QoS 0 : at most once : The packet is sent once, so if it lost - nobody will know.
  • QoS 1 : at least once : The packet is sent until it gets confirmation from the broker. But it may generate duplicates, which is a bit difficult to handle.
  • QoS 2 : exactly once : In this case client and broker have a longer negotiation, bit the message will be delivered exactly once with no duplicates.

For the connected smoke alarm I've decided to use "exactly once". It provides the highest possible level of consistency (so I will not loose alarm notifications). There is a small trade-off. To achieve higher consistency it is using a bit more packets, which is not very important for a local network. MQTT client and broker must support selected QoS. MQTT.js and Mosquitto supports all MQTT QoS options.

 

Keep Alive

Keep Alive timer is how long the connection will be kept alive until the server will disconnect the client. The Keep Alive timer for the connected smoke sensor is very important, as it provides status of the alarm availability. At the same time I will assume that in case of fire it will send alarm before detector gets destroyed.So I'll set it to quite a big number 5 minutes (300 seconds) for the sensor, which mean I'll get alert once it stopped working with a 5 minutes delay. On other side, I'll keep default 10 seconds value for the monitor itself. The frequent communication is acceptable as MQTT broker and the monitor are collocated on the same Intel Edison board.

 

Last Will and Testament (LWT)

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 gets unexpectedly lost after a timeout defined by Keep Alive parameter. 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.

 

Message Retention

I'd like to know last reported alert and connection state of alarms, even if monitor or broker gets restarted. It can be achieved by setting retain parameter to true. The downside of it that they can be reported twice - once at time of report and second time after restart of the monitor.

 

MQTT.js client and connection parameters

It all comes together when the monitor opens MQTT connection, creates subscriptions and report its own status

const monitor_topic = 'home/alarm/monitor/connected';

const client = mqtt.connect(mqtt_url,{
  keepalive: 10, // after 10 second time-out the broker will publish a message if the monitor dies
  will: {
    topic: monitor_topic,
    payload: 'false',
    qos: 2, //Exactly once
    retain: true
  }});

client.on('connect', () => {
  //subscription to smoke detector alarms and connection states from any location
  client.subscribe('+/alarm/smoke/+', {qos: 2})
  //subscription to alarms monitors connection states from any location
  //this subscription is useful for logging/testing
  client.subscribe('+/alarm/monitor/+', {qos: 2})
  
  // publish a message to a topic - "The home alarm monitor is connected"
  //Mosquitto conf /etc/mosquitto/mosquitto.conf should be configured to support retained messages
  //persistence true
  //persistence_location /var/lib/mosquitto/

  client.publish(monitor_topic, 'true', {qos: 2, retain: true}, function() {
    console.log("Home alarms monitor connected");
  });
})

 

Node.js Environment Parameters

It is a good practice to keep environment related parameters like broker URL, Slack channel and API keys outside from the code. So far I defined MQTT_BROKER_URL, SLACK_API_TOKEN and SLACK_CHANNEL.

  • Sign in to reply

Top Comments

  • DAB
    DAB over 8 years ago +2
    As with any system, you will need to refine your operation, alert and response issues over time based upon the actual performance of the devices. I think you have a good start. Are you planning to include…
  • vlasov01
    vlasov01 over 8 years ago in reply to mcb1 +2
    Thank you for providing information about your monitoring system! I haven't thought about such rules. I'll consider to use Complex Event Processor (CEP) to process events from multiple streams (sensors…
  • DAB
    DAB over 8 years ago in reply to vlasov01 +2
    I do. Adding test features enable you to quickly isolate problems between the hardware and software. It also enables you to do incremental build testing to verify component operation as you add each to…
  • vlasov01
    vlasov01 over 8 years ago in reply to mcb1

    Thank you for providing information about your monitoring system! I haven't thought about such rules.

    I'll consider to use Complex Event Processor (CEP) to process events from multiple streams (sensors) over sliding time windows. Apache Beam project is one of such CEPs and has MQTT readers/writers. There are two excellent introduction articles on this subject https://www.oreilly.com/ideas/the-world-beyond-batch-streaming-101  and https://www.oreilly.com/ideas/the-world-beyond-batch-streaming-102 . I can try to run Apache Beam on Intel Edison (if I have time) as it comes with per-installed JVM

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

    As with any system, you will need to refine your operation, alert and response issues over time based upon the actual performance of the devices.

     

    I think you have a good start.

     

    Are you planning to include a test feature to verify connections and operating sensors?

     

    DAB

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

    It always sounds simple, until you need to include all the exceptions and error checking. image

    We utilise the same principle in our monitoring system, where we expect communication and then report when it exceeds a preset time.

     

    The next big choice is what happens when it hasn't had any smoke alarms reporting in.

    In a true "Fail To Safety" situation the devices would assume the worst and de-power, etc.

     

    If you had two sensors in the same area, you could decide that the minimum number reporting in is 1, but that requires some configuration.

    What happens when the only detector is in 'maintenance' or removed.

     

    It's when this happens and there is a bypass that things tend to go wrong. image

    You may be able to have an override function that allows it to work for x hours and can only be 'over-riden' x times.

     

     

    I'm looking forward to the next bit.

     

     

    Mark

    • Cancel
    • Vote Up +1 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 © 2026 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