Program Loading
There are numerous ways to deal with loading programs for automatic execution within Linux®, some more sophisticated than others.
We'll take the simple way out and show you shell scripts, which are invaluable tools for automating command line entries. You may find them useful for your application.
Server-side Script
We offer here a shell script to simplify start-up of our "master" ARTIK board from the Actions and ARTIK Cloud article.
To configure automatic loading and running of the Mosquitto MQTT broker and Node-RED MQTT client:
- Copy the shell script below into a file.
- Create the
p.py
file discussed here. It will be called from our shell script. - Make the shell script file executable by running the command
chmod 777 _filename_
- Refer to this article about adding the IP address found to the
/etc/hosts
file. You will want to use the same method (and server name) you decided on there.
Then, after each boot of the ARTIK module, run the shell script from its directory
./_filename_
Main Shell Script Contents
Create the shell script below. It calls the p.py
script described later.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #!/bin/bash mqtthost=`ifconfig wlan0 | grep 'inet ' | awk '{ print $2 }'` export mqtthost echo if [ -z $mqtthost ]; then echo "No Wi-Fi IP address found!" exit 1 fi echo Found $mqtthost as WLAN echo python ~/.node-red/p.py $mqtthost echo mosquitto & echo node-red & |
Retrieval of IP Address
The output of the ifconfig
line echoes the IP address that has been found from a previous Wi-Fi connection.
We assign the returned value to an environment variable named mqtthost
that will be used by a Python script to update the MQTT broker IP address in Node-RED. We then test the variable and if empty, exit the script.
Setting IP Address in Node-RED
The canvas that you use to set up nodes and functions in Node-RED provides an ideal development environment. But what happens when the development is finished and you no longer need to look at the flows?
Node-RED stores all your development work on that canvas into a JSON-formatted file named flows_localhost.json
under the /root/.node-red
directory
(localhost
will be the name of your hostname
). Once your development is complete, nothing in that file ever needs to change, except for one value: the MQTT broker address.
You can use a Python script to update the broker
parameter in the file automatically on boot. Since Python has built-in JSON handling features, it becomes almost trivial to search for the broker
object and update its value.
p.py
script
1 2 3 4 5 6 7 8 9 10 11 12 13 | #!/usr/bin/python import os import json flowfile='/root/.node-red/flows_localhost.json' with open(flowfile, "r") as fp: jsonObject = json.load (fp) jsonObject[1]['broker'] = os.environ['mqtthost'] print "Assigned Node-RED to broker:" print jsonObject[1]['broker'] with open(flowfile, "w") as fp: json.dump(jsonObject,fp) |
Regarding the work space canvas: You can choose not to bring up its page on your browser. If you want to prevent it from being accessed, you can simply comment out its uiPort
setting in the settings.js
file.
Starting Mosquitto and Node-RED
To start our support programs from the script, we just run mosquitto
and node-red
in background mode.
There are other ways to do it, and some may be better for your application. For information, refer to this Node-RED page.
If your application also requires a simple general purpose Web page server, read the /etc/mosquitto/mosquitto.conf.example
file. Mosquitto may be able to host what you need and save you extra effort.
Starting Server-side Arduino Sketch
Loading and running the executable sketch file is described here. Once you're ready, you can simply add the .elf
file and its path as the last line to run in your script.
Client-side Script
The script functions on the client side are similar to those on the server side, but simpler, so we will just touch on one point of additional interest here.
Linking IP Address to Common Name
MQTT clients will need to know the Common Name and IP address of the MQTT server/broker. As the IP address may change dynamically, you will need a mechanism for finding it and linking it to a name.
We discuss the issue of assigning names to IP address in /etc/hosts
within our article here.