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 #8: Mosquitto Bites
  • 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: 14 May 2017 1:27 AM Date Created
  • Views 1061 views
  • Likes 2 likes
  • Comments 5 comments
  • bugs
  • github
  • systemd
  • mosquitto
  • linux
Related
Recommended

[Upcycle It] WiFi Connected Smoke Detector #8: Mosquitto Bites

vlasov01
vlasov01
14 May 2017
<< Previous

Project Index

Next>>

Road to the finish

I've completed development of main components of the solution (monitor and detector) in previous weeks. The next step was integrating them into a complete system and performing testing. One think I've decided to add is automatic start of all required components after Edison reboot. Linux has pretty good capabilities to accomplish this task, so I didn't expected any  surprises. But I was wrong in my expectations.

I've used a simple instruction  Running Your Node.js App With Systemd to start all components of the system. systemd is responsible to orchestrate boot sequence of Linux services. I've added my monitor and detector as services, which have dependencies on Mosquitto.

Here is my sequence of actions:

1. Created smoke detector service /lib/systemd/system/smoke-detector.service file and specified environment variable for Mosquitto:

[Unit]
Description=SmokeDetectorApp
After=mosquitto.service
Requires=mosquitto.service

[Service]
Environment=MQTT_BROKER_URL=mqtt://192.168.10.114
Type=simple
User=root
TimeoutStartSec=10
ExecStart=/usr/bin/node /home/root/detector.js
Restart=on-failure

[Install]
WantedBy=multi-user.target

 

2. Created monitor service /lib/systemd/system/home-monitor.service file and specified environment variables for Mosquitto, Slack and Cronitor:

[Unit]
Description=HomeMonotorApp
After=mosquitto.service
Requires=mosquitto.service

[Service]
Environment=MQTT_BROKER_URL=mqtt://192.168.10.114
Environment=SLACK_API_TOKEN=<my Slack API token>
Environment=SLACK_CHANNEL=<my Slack channel for Alarms>
Environment=CRONITOR_MONITOR=<my Cronitor alarm id>
Environment=CRONITOR_AUTH_TOKEN=<my cronitor API token>
Type=simple
User=root
TimeoutStartSec=10
ExecStart=/usr/bin/node /home/root/home.js
Restart=on-failure

[Install]
WantedBy=multi-user.target

 

3. Reload systemctl

sudo systemctl daemon-reload

 

4. Start new services

sudo systemctl start home-monitor
sudo systemctl start smoke-detector

 

5. Check if there is no issues by looking at failed services

systemctl --failed

and reviewing the system journal entries:

journalctl --since=today

As well status of services can be checked by running:

sudo systemctl status  home-monitor
sudo systemctl status  smoke-detector

 

6. Make it permanent

sudo systemctl enable home-monitor
sudo systemctl enable smoke-detector

 

7. Reboot

init 6

and repeat step 5 after startup.

 

Bump on the road or Mosquitto bites

We have an unusually cold spring in Canada this year. I haven't seen any mosquitoes yet flying outside. But I've got bitten by Mosquitto MQTT broker. Here is the story. I've configured Mosquitto to retain messages. After persistence was configured in /etc/mosquitto/mosquitto.conf the broker created database in /var/lib/mosquitto/mosquitto.db to store messages. During my tests I rebooted the Edison board several times. After one of these reboots the broker fails to start:

root@gate1:~# sudo systemctl status  mosquitto
● mosquitto.service - Mosquitto - lightweight server implementation of the MQTT and MQTT-SN protocols
   Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled)
   Active: failed (Result: start-limit) since Sat 2017-05-13 15:36:53 EDT; 52s ago
  Process: 374 ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf (code=exited, status=1/FAILURE)
  Process: 372 ExecStartPre=/bin/rm -f /var/run/mosquitto.pid (code=exited, status=0/SUCCESS)
 Main PID: 374 (code=exited, status=1/FAILURE)

 

Then I've tried to start it from command line:

root@gate1:~# /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
1494704860: Warning: Mosquitto should not be run as root/administrator.
1494704860: Error: No such file or directory.
1494704860: Error: Couldn't open database.

 

So, something should be wrong with Mosquitto database.

root@gate1:~# ls -la /var/lib/mosquitto/
total 8
drwxr-xr-x  2 root root 4096 May 13 15:31 .
drwxr-xr-x 14 root root 4096 Apr 10 23:04 ..
-rw-r--r--  1 root root 0 May 13 15:31 mosquitto.db

 

Well, it is empty and the broker can't recover it. I've deleted the empty file and the broker was able to start this time without errors.

 

Source control

 

The code is now uploaded to my github repository. If you encountered any issues or would like to request new features you can do it there as well.

  • Sign in to reply

Top Comments

  • mcb1
    mcb1 over 8 years ago +3
    Being bitten by a virtual mosquitto results in a virtual injury that can only be rectified by virtual medicine and healt professionals ... good luck. I wonder if you can have a scriot check that it is…
  • vlasov01
    vlasov01 over 8 years ago in reply to Workshopshed +2
    Thanks Workshopshed ! It will be the best option if I can install a newer version of Mosquitto. I'm wondering who is managing packages life cycle for Intel Edison? It will be nice if this issue is resolved…
  • vlasov01
    vlasov01 over 8 years ago in reply to mcb1 +1
    mcb1 I was thinking about creating a such script, but it will be the last resort option. I'll try first install a new version as per Workshopshed
  • DAB
    DAB over 8 years ago

    Over the years I have found that using software from whatever source entails a learning curve.

     

    The myth that you can save a lot of time and effort has not been my experience.

     

    DAB

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

    mcb1 I was thinking about creating a such script, but it will be the last resort option. I'll try first install a new version as per Workshopshed

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

    Thanks Workshopshed ! It will be the best option if I can install a newer version of Mosquitto. I'm wondering who is managing packages life cycle for Intel Edison? It will be nice if this issue is resolved for all Mosquitto users on Intel Edison. I've buld the new version following Intel publication with minor changes. The process took around 15 minutes. Now I'll need to look if there are any others sideeffects with the new version.

    wget http://mosquitto.org/files/source/mosquitto-1.4.11.tar.gz
    tar xzf mosquitto-1.4.11.tar.gz
    cd mosquitto-1.4.11
    make WITH_SRV=no
    useradd mosquitto
    cd test/broker/
    sudo systemctl start  mosquitto
    sudo systemctl status  mosquitto
    make test
    cd ../../
    cp client/mosquitto_pub /usr/bin
    cp client/mosquitto_sub /usr/bin
    cp lib/libmosquitto.so.1 /usr/lib
    cp src/mosquitto /usr/sbin
    sudo systemctl start  mosquitto
    sudo systemctl status  mosquitto

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

    Make you wonder what Mosquito is using to write the DB file and if that can be configured to cope with unexpected shutdowns.

    I visited the https://mosquitto.org/ website and right at the top is a mention of "Don’t error on zero length persistence files. Closes #316" from Feb this year.

    Unfortunately as others have seen getting the newest version working on the Edison is a non-trivial task.

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

    Being bitten by a virtual mosquitto results in a virtual injury that can only be rectified by virtual medicine and healt professionals ... good luck.

     

    I wonder if you can have a scriot check that it is indeed running and substitue a blank database with the correspondingactions necessary.

    It would be a shame to see it stop due to some form of corruption as it's an essential part of the whole system.

     

    OTOH having more than one sensor might be the answer.

     

    Mark

    • Cancel
    • Vote Up +3 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