Setting up a Pi from bare Raspbian to work as a Node-Red controller

Here is a set of instructions to go from bare Raspbian to work as a Node-Red controller and also send text (sms) alerts using a 3G usb dongle.

For various reasons I’ve needed to retrace my steps a few times, so here I’ll document them all for my and your convenience. Starting point is a fresh download of Raspian (2014-01-07). End point will be auto-running a monitoring node with sms capability.

My latest trick is to create a 200MB ‘dummy’ partition at the end of the SD card using gparted before putting the card in the pi. Because the auto-resize script can’t deal with this arrangement I also expand the partition in gparted. This way the main, expanded filesystem will be a little bit smaller, to cope with the fact that sometimes SD cards are a few MB smaller than you would like. Now I can image and recover the main two partitions and if the dummy partition gets corrupted because it is truncated then it can just be deleted.

After first boot, ssh using username pi and password raspberry. Run sudo raspi-config to enter the easy config. Change password and set the timezone, then in advanced – change hostname, ensure that the graphics split is 16MB, enable SPIĀ and reboot.

Ok, now after logging back in we do a quick install of some packages via aptitude (also adding in mosquitto for mqtt brokering and gammu for text messaging):
sudo aptitude update && sudo aptitude upgrade -y
sudo aptitude install bootlogd monit mosquitto gammu gammu-smsd

Now get the node stuff: (not sure if the npm updates are needed or not tbh!)
sudo dpkg -i node_latest_armhf.deb
rm node_latest_armhf.deb
sudo npm install node-red -g
cd /usr/local/lib/node_modules/node-red
sudo npm install serialport stately wiring-pi
sudo npm update

Configure gammu (from this excellent page, thanks Mattias!) by identifying the port. Plug in your 3G dongle with a valid, activated sim card. Use dmesg | grep ttyUSB to find the USB port used for the modem. I found that I had three ports assigned and I tried them all to find that the last one assigned worked. Gammu has a config script helper you can activate with a sudo gammu-config – the only thing I needed to change was the port. Then your should get a sane response from sudo gammu --identify like this:

Device : /dev/ttyUSB2
Manufacturer : Huawei
Model : unknown (K3520)
Firmware : 11.314.12.02.00
IMEI : 15 digit no.
SIM IMSI : 15 digit no.

Now we need to edit the gammu daemon config file with sudo nano /etc/gammu-smsdrc to put the port and speed used by the modem in, e.g. /dev/ttyUSB2 and at19200. To test it you can start the daemon with sudo service gammu-smsd start and then echo "Hello Earth, Mars calling" > /var/spool/gammu/outbox/OUT###########.txt (replacing the #’s with your phone number) you should get a text!

And a killerĀ autostart script from here (thanks Antoine Aflalo) and monit script from here (thanks Giovanni):

sudo nano /etc/init.d/node-red


# Provides: node-red
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start or stop the node-red server

OPTIONS=”–max-old-space-size=192 red.js -v”

. /lib/lsb/init-functions

start_daemon () {
start-stop-daemon –start –chdir $SCRIPT_DIR –background \
–chuid $USER \
$START_STOP_OPTIONS –make-pidfile –pidfile $PIDFILE \
–exec $DAEMON — $OPTIONS >> “${LOG}” 2>> “${LOG}” </dev/null
log_end_msg 0

case “$1” in
log_daemon_msg “Starting daemon” “$NAME”

log_daemon_msg “Stopping daemon” “$NAME”
start-stop-daemon –stop –quiet \
–chuid $USER \
–chdir $SCRIPT_DIR \
–exec $DAEMON –pidfile $PIDFILE –retry 30 \
–oknodo || log_end_msg $?
log_end_msg 0
$0 stop
sleep 5
$0 start
status_of_proc “$DAEMON” “$NAME”
exit $?

echo “Usage: $0 {start|stop|restart}”
exit 1
exit 0

And now we can do a:

sudo chmod 755 /etc/init.d/node-red && sudo update-rc.d node-red defaults

to install the startup script. On reboot, the node-red daemon should start, along with the mqtt & text message server.

To configure monit, edit /etc/monit/monitrc if desired to set the time intervals of the checks and to set the users who can do web monitoring. Add a line at the end to include an additional directory for service definitions:

include /etc/monit/monitrc.d/*

Finally create a file to configure the monitoring of node-red (again tweaked from Giovanni to cope with authentication enabled on the pi) with sudo nano /etc/monit/monitrc.d/node-red

check node-red with pidfile /var/run/
start program = “/etc/init.d/node-red start”
stop program = “/etc/init.d/node-red stop”

if failed url http://username:password@localhost:1880 timeout 10 seconds then restart
if 3 restarts within 5 cycles then timeout

If node-red dies, then monit will start it up again within 30 seconds. Monit can be monitored and controlled remotely quite easily on port 2812.

To send a text message via node red, just configure a file node to save a file to the /var/spool/gammu/outbox with the format OUT############.txt where the hashes are the number you want to send. Once the message is sent gammu will automagically move the file to the sent folder for you.

Leave a Comment

Your email address will not be published.

27 − 23 =