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 #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 1381 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 DAB

    Yes, I have included alarm notification when a smoke detector gets disconnected.

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

    I would add an alarm feature if none of the devices respond.

     

    With safety systems, you want to know when you are at risk.  If none of the devices are functioning, you need to know.

     

    DAB

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

    Thank you for your input! I have mostly software background and hardware system engineering is still new to me.

    I can build a set of tests to verify monitoring component during development. MQTT heartbeat will continuously test connectivity with smoke detector during operation.

    What kind of additional tests you think should be created to verify component operation?

     

    Sergey

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

    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 the overall system.

     

    I have found this approach to be a great time saver in the long run.

     

    DAB

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

    I was thinking to use a built-in test feature included in smoke detector to perform end-to-end test.

    So far I'm using just a publishing of a message to the relevant topic (publish "true" to kitchen/alarm/smoke/state topic) from command line or Paho MQTT clients to perform a test of the monitor component  I'm relying on LWT for the connectivity. Do you think I should add additional test features?

    • 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