Welcome back to 2-Bullet Tuesday, where we’ll share useful tips and awesome stuff sent in by Omega2 users all around the world. Check out creative projects and tutorials that will supercharge your IoT development!
Connecting to Google Cloud IoT Core
Dismayed by the lack of Google Cloud in our Quick-Start Cloud Platforms page on the Docs, Daz Wilkin, intrepid Googler & Onioneer, took it upon himself to get the Omega2 talking to Google Cloud IoT Core. Check out his short tutorial on using the
mosquitto_pub commands to connect and talk to GCP IoT Core!
Great work Daz!
Running your Program as a Service
If you’ve written an awesome program for your Omega and then found yourself wondering how to have this program start automatically, run all the time, and automatically relaucnh if it ever crashes then this tip is for you! We’ll be showing you how to write a procd init script to make your favorite program into a service!
We’ll first write a script that will register the program as a service, then learn how to launch the service, and then watch it run!
Creating the Service
All services on the Omega are registered with procd, the OpenWRT process management daemon, with scripts in
/etc/init.d . The script for a service will specify how the service is started/stopped, how it will run, what files it cares about and a whole lot more. The scripts must be executable and follow a specific syntax. Here’s a generic example script for a fairly simple service:
This service will launch the program indicated by
[YOUR COMMAND HERE] and will make sure it relaunches anytime the program exits.
As an example, let’s say we’ve just written and compiled a GPIO Edge Detection program for the Omega, and the executable can be found at
/root/gpioIrq. To run this program as a service, we’ll add a file
/etc/init.d/gpio-irq with the following:
Notice how we enabled forwarding stdout and stderr to logd, more on that later.
Remember to make it executable:
chmod +x /etc/init.d/gpio-irq
Controlling the Service
Ok, now that we have our service, we’ll want to learn to control it! For the purposes of these examples, we’ll be using the
gpio-irq service from above.
Starting the Service Manually
Now the program will run as a service. If it exits for whatever reason, procd will automatically start it up again (thanks to that
procd_set_param respawn line).
Stopping the Service
This will stop the running instance of the program and not respawn any more instances. Use this if you don’t want it to run anymore.
Restarting the Service
This will stop the running instance of the service and launch a fresh instance. This is useful if you’re debugging something or you’ve changed the configuration for that service.
Enable the Service to Run at Boot
This is the really powerful part – run your program as a service automatically at boot.
Disabling the Service to Run at Boot
No longer need it to run at boot? No problem!
Reading Output from a Service
Since the program from our service isn’t running on the command line, we won’t be able to see the program output. However, it might still be useful to take a peek at the output once in a while.
You’ll notice that in our
gpio-irq service we forwarded stdout and stderr to
logd. This is the OpenWRT system log, to read the log messages, we’ll use the
logread will output log messages generated by all of the processes running on your Omega2. To narrow it down to just the output of our
gpioirq program, we’ll use
logread | grep -i gpioirq
If your program has been outputting a whole bunch, we can narrow it down even more to just the last 50 lines:
logread | grep -i gpioirq | tail -n 50
And there you have it, in a few quick minutes you’ve turned your program into a service that will always run on your Omega. Our gpioIrq example program doesn’t do anything but output text, but we’re sure you can cook up lots of cool things to do with a GPIO edge detection program running as a service on your own Omega!
Thanks for reading and have a great week!
P.S. Just a heads up, we’re going to be making some changes to 2-Bullet Tuesday in the coming weeks 🙂