<< Previous |
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.
Top Comments