My first version of this is working. Hasn't caught any Node-RED failures yet, but has poked me about a few other connectivity glitches. It is intentionally rather mild at notification, just vibrating once and saving a status icon. There are lots of places where different choices might suit someone else, but this seems like the simplest solution for me.
#!/usr/bin/env python
# A script for Android sl4a to verify Node-RED is alive
# Your Node-RED installation must be accessible from your phone
# If your phone switches between local and internet access,
# you may need two scripts; Tasker can choose based on current connection.
# Be sure to set <address of your Node-RED machine> below!
# Loren Amelang, 31Aug14
# Distributed under the Apache license, like sl4a itself:
# -----
# Copyright 2014 Loren Amelang
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----
# sl4a is available here:
# Installing interpreters (like Python) is detailed here:
# My sl4a has libPython 2.6
# Scripts live in /mnt/sdcard/sl4a/scripts
# To simplify launching the script, create a home screen shortcut:
# Long-press an empty space on a home screen and select Shortcuts,
# then Scripts. Once SL4A opens up, select hbRED.py from the list
# and then click the gear icon.
# One could run Python in an infinite loop, but I already have Tasker running...
# To set Android Tasker to run this script periodically:
# Along the top - Tasks
# Bottom - Green +
# (add task)
# Name - hbRED
# Green checkmark
# (task edit)
# Blue +
# Script
# Run SL4A Script
# (run script)
# Magnifying Glass
# (select script)
# tap name
# Green checkmark
# Green checkmark
#
# Along the top - Profiles
# Bottom - Green +
# (new profile)
# Name - hbRun
# Green checkmark
# (first context)
# Time
# uncheck From and To
# check repeat every 1 hours
# Green checkmark
# (task selection)
# hbRED
# [there is a New Task choice if you did this part first]
import android, os, re
import urllib2
droid = android.Android()
url = 'http://<address of your Node-RED machine>:1880'
# urllib2.urlopen(url[, data][, timeout])
try:
rsp = urllib2.urlopen(url)
except urllib2.HTTPError, e:
print ('No HTTP ')
droid.vibrate(999)
droid.notify(
'Node-RED',
'not responding')
except urllib2.URLError, e:
print ('No URL ')
droid.vibrate(999)
droid.notify(
'Node-RED',
'inaccessible')
else:
hdr = rsp.read()
print (hdr[:50])
print "OK"
os._exit (1)
#eof