Category Archives: MQTT

Android Phone as Server

Why am I showing you a picture of a cracked-screen phone?

Linux on Android PhoneWell because this particular phone is a bust Oukitel K10000, the phone with the world’s longest-lasting battery and an excellent workhorse. My friend Aidan gave me this (rather than chucking it away) some time ago and it has been sitting doing nothing. All that is wrong with it is a cracked (and exceedingly dangerous on the fingers) screen. I’ll bet I’m not the only one with such a phone lying around wasting space.

Well, as of yesterday, it is a Debian server with all my usual stuff going on quietly in the background – with the screen normally off – running freezing cold and hopefully being super reliable.

This is an experiment only – beware – if your phone or tablet dies it is your problem….  oh and your Android phone/tablet needs to be ROOTED. 

Imagine turning your old, dust-covered phone into a sleek, battery backed-up server with unfeasibly long backup time, immunity to any mains spikes, a silent, fast Debian base for all the stuff in my script – which includes Node-Red, Apache/PHP, SQLITE (+ PHPLiteAdmin), MQTT, MC, Ha-Bridge and more!  If you’ve been following this blog you’ll know about the script.

So this article applies to ROOTED Android phones and we’ve only tested around Android 5.0 onwards.  In my case I rooted the phone with KingRoot (note, NOT the one in the PlayStore which is a con – avoid it - but the one at ) – please note that rooting phones is beyond the scope of this article and if you are not confortable with this you should not do it. There are a lot of links out there on the subject and many of them are fraudulent.

tmpCD52There is an APP in the Play Store called Linux Deploy. It is also on GitHub. Beware that this is essentially undocumented unless you are Russian – so please don’t ask how you can use your phone’s GPS or Sound from Debian – not a clue!

You should have a modern-ish WiFi enabled (or hardwired if you like) Android phone or tablet with at least 16GB of internal FLASH (which should give you 10GB working space).   If you only have 8GB that will leave you only 2GB which - really – isn’t enough.

Getting Debian 8 on the phone:  After ensuring your phone/tablet is rooted, Install the App.

Linux on Android PhoneIn the app, on the top LEFT menu – find  REPOSITORIES and select Debian 8 for ARM.

On the bottom right is a drop down – you should go in there and select INSTALLATION TYPE – directory (OR FILE with a reasonable file size limit – say 5GB – the 2GB default will NOT work). Change the user to “pi” with suitable password in the same menu. TICK for allowing initialisation – and use of SSH server. Also where you see INIT SYSTEM change that to sysv. 

Then TOP RIGHT menu  - INSTALL – that might take some time – top right again CONFIGURE – then bottom menu START – and you should have a working Linux you can get into with for example WINSCP for that “pi” user. The IP address is clearly shown in the App.

I suggest not going further until you are comfortable with the above – took me several attempts because I didn’t follow the above exactly (well, and Antonio and I were learning at the same time).

Running the script: Via WinSCP or similar, put the script into the pi directory – changing permissions as normal - run the script – and ensure PHONE is selected – it isn’t by default. Come back after lunch. The script will no doubt suggest a reboot. Instead, hit the STOP button on the bottom of the phone screen – wait for it complete, hit the START button – wait – and you should now have everything in the script running!

Now – I’m using this to run my script – but I’m thinking you COULD use it to serve files etc. – I’ve not tried this but I’m guessing it would be easy to access an SD card…. and make that a folder…. hmmm.

Anyway, it is now the day after I installed all this – the phone is sitting there “off” and unlike my FriendlyArm board with it’s whirling fan, is sitting silently and freezing cold yet ran the script much faster than any of my SBCs – around 40 minutes.

K10000 running Debian 8No guarantees as there just isn’t enough info about Linux Deploy out there (unless you find/translate some) – but it does seem to work well now that we’ve made sufficient alterations to the script to take this particular setup into account. A fun project, to be sure. Now, I know this is a not a fair comparison and tomorrow we might come back and say … no good (for whatever reason).. but at £107 for that particular phone… compare – how much would it costs for a Raspberry Pi 3, uninterruptable power supply able to keep the phone going for something like a couple of days with no power, a high-def touch screen, a solid case for the whole thing.. indeed ,it might be possible to use a cheap tablet…  I was looking on Ebay – 16GB Android tablet – perfectly working except for missing buttons and cracked digitiser – around £10

One thing to note – I’ve turned rsyslog messages off – it was spitting out all sorts of unwanted helpful messages when you changed brilliance on the phone or disconnected the USB etc –REALLY annoying.. If you feel you need logging on -

sudo service rsyslog start

That will stay on until the next stop-start…

Node-Red running on a K10000 phoneSuch a shame it isn’t clear how to access some of the peripherals like audio. But who knows – someone reading this article may already have solved such challenges.

Please note: the pretty colours on the right do not come as standard. Click images to see larger versions.

This is really starting to look GOOD!!!!

Revelation: I’ve now taken on-board ideas from others and thanks for that – both people in here and on Google+ – most of the other solutions are longwinded and need other packaged  so up to now Linux Deploy – I’m now installing on my old Nexus 7 2012 UTTERLY SUCCESSFULLY (though not the fastest kid on the block) ( after rooting it with the Nexus toolkit ) - using Linux Deploy (which is completely self-contained and offers full root access – is looking the best). The ONLY thing you can’t do is use the Android peripherals – because of lack of UK info but this morning I figured it all out. 

We’ve also tested this one OnePlus One (model BACON) and a Xiaomi Redmi 3 model IDO). The K10000 has now been up for several days.

Ok, bear with me – you have Node-Red on Linux – and MQTT. So, you run Tasker on the phone (in the Android section) with MQTT – and now you have access to and control of all of the Android facilities that TASKER can handle (i.e. just about the lot) from within the Debian environment. Doddle.. now all I need is some time!!


Fauxmo Alexa Delights

Amazon EchoI’m going to cut a very long story short here.  I’ve been looking at ESP8266 emulations of WEMOs because up to now this seems to be a popular device to emulate when using with your talking Amazon Echo. If anyone wants to come up with something that will take ANY word, not just ON or OFF – please do feel free to do the hard work. In another article, I described how, with the aid of a small SKILL at the Amazon end, an SSL certificate and some Node-Red code, we managed to make a generalised system to extract words out of Echo – and that is controlling my heating and all sorts right now -  marvellous. If you’re not up to speed with ECHO, MQTT or NODE-RED see earlier blogs in here.

Simple On Off

But what if you just want ON and OFF with no HTTPS, no messing at the Amazon end?  I checked out ESP8266 emulations. AWFUL. I’ll not mention names and I’m sure this was just done as a demo – but the one I tested was written in Arduino/ESP code – connected to WIFI and acted as an ON/OFF Wemo – MARVELOUS. Except that with the first sign of WIFI trouble – it fell over and died – absolutely no attempt at making it work regardless. Well as you know, WIFI dies from time to time. Bin.

So what next?

I found something others will have spotted called FAUXMO which is basically a single Python script –that emulates one or more WEMO type devices. At first it did not work – so half a day later having learned enough Python to get me by I realised that this was intended to call web pages – to turn things on and off. I understood there was an MQTT version of it – but that a friend of mine was having trouble getting that working – so  - I decided to go for broke and modify the original.

All you need here is the Pi or similar equipped with MQTT and with Python installed – Node-Red is not needed. Indeed once you see how this works – it would be very easy to take the cheapest of these little SBCs – the FriendlyArm NEO for example and make it look like several devices – instead of MQTT you could turn ports on and off in Python.

Cheapest multi-output Alexa-enabled device on the planet?

Orange Pi ZeroBefore I go further – you might have a simple requirement – far simpler than we usually cover in here – a single box that “does it all”, talking to Amazon Alexa with multiple outputs looking like multiple devices… well, the Orange Pi ZERO available at under £9 (inc shipping) from AliExpress could just be the ticket? Install Armbian, MQTT and Python – and… read on.


You need a client called Paho MQTT to bring MQTT to Python… so here’s what I did in order… we’re looking at a Pi or similar, running Node-Red and MQTT broker. In my case with username and password.

sudo pip install paho-mqtt

Seconds later, done – MQTT now available inside Python – that was easy.

I had to add a header into the code – to import mqtt – and I had to define username, password, mqtt address.

That all went into the top of the file and should be pretty obvious.

I then modified some code – and the definitions of two sample devices – “kitchen lights” and “bedroom lights”.

Here are the modifications.


import paho.mqtt.client as paho


Change to how recognised terms are actually used

class rest_api_handler(object):
    def __init__(self, on_cmd, off_cmd):
        self.on_cmd = on_cmd
        self.off_cmd = off_cmd

    def on(self):
        return 200

    def off(self):
        return 200

And finally the bit you're interested in - device definitions... you only have to change the user settings at the top then this lot.

    ['bedroom lights', rest_api_handler('toesp/bedlights|{out0:1}', 'toesp/bedlights|{out0:0}')],
    ['kitchen lights', rest_api_handler('toesp/kitchenlights|{out0:2}', 'toesp/kitchenlights|{out0:3}')],

So what you’re looking at are the words that Alexa will accept (“kitchen lights” for example) followed by ON OR OFF. The two following strings are MQTT topic and PAYLOAD separated by | for firstly ON and secondly OFF.

As you can see it is easy to add new devices – you do with the MQTT what you will – in my case this would be talking directly to devices – more likely I’ll fire instructions off that Node-Red can handle for manual override of lighting etc.

To get this all to work you need to be running the program and say “Alexa find devices” to your ECHO – it will find the devices and you’re ready to go. If you make changes – restart the program – re-find devices.

And here  is the complete – modified Python code – complete with the original author info – after all I didn’t make THAT many changes even if it did end up taking me all day.

Don’t worry about it looking over-powering – you only need to know about the stuff above – the rest is just a case of pasting into into a file.

#!/usr/bin/env python

The MIT License (MIT)
Copyright (c) 2015 Maker Musings
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

# For a complete discussion, see

import email.utils
import requests
import select
import socket
import struct
import sys
import time
import urllib
import uuid
import paho.mqtt.client as paho


# This XML is the minimum needed to define one of our virtual switches
# to the Amazon Echo

SETUP_XML = """<?xml version="1.0"?>
    <manufacturer>Belkin International Inc.</manufacturer>
    <modelName>Emulated Socket</modelName>

DEBUG = False

def dbg(msg):
    global DEBUG
    if DEBUG:
        print msg

# A simple utility class to wait for incoming data to be
# ready on a socket.

class poller:
    def __init__(self):
        if 'poll' in dir(select):
            self.use_poll = True
            self.poller = select.poll()
            self.use_poll = False
        self.targets = {}

    def add(self, target, fileno = None):
        if not fileno:
            fileno = target.fileno()
        if self.use_poll:
            self.poller.register(fileno, select.POLLIN)
        self.targets[fileno] = target

    def remove(self, target, fileno = None):
        if not fileno:
            fileno = target.fileno()
        if self.use_poll:

    def poll(self, timeout = 0):
        if self.use_poll:
            ready = self.poller.poll(timeout)
            ready = []
            if len(self.targets) > 0:
                (rlist, wlist, xlist) =, [], [], timeout)
                ready = [(x, None) for x in rlist]
        for one_ready in ready:
            target = self.targets.get(one_ready[0], None)
            if target:

# Base class for a generic UPnP device. This is far from complete
# but it supports either specified or automatic IP address and port
# selection.

class upnp_device(object):
    this_host_ip = None

    def local_ip_address():
        if not upnp_device.this_host_ip:
            temp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                temp_socket.connect(('', 53))
                upnp_device.this_host_ip = temp_socket.getsockname()[0]
                upnp_device.this_host_ip = ''
            dbg("got local address of %s" % upnp_device.this_host_ip)
        return upnp_device.this_host_ip

    def __init__(self, listener, poller, port, root_url, server_version, persistent_uuid, other_headers = None, ip_address = None):
        self.listener = listener
        self.poller = poller
        self.port = port
        self.root_url = root_url
        self.server_version = server_version
        self.persistent_uuid = persistent_uuid
        self.uuid = uuid.uuid4()
        self.other_headers = other_headers

        if ip_address:
            self.ip_address = ip_address
            self.ip_address = upnp_device.local_ip_address()

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.bind((self.ip_address, self.port))
        if self.port == 0:
            self.port = self.socket.getsockname()[1]
        self.client_sockets = {}

    def fileno(self):
        return self.socket.fileno()

    def do_read(self, fileno):
        if fileno == self.socket.fileno():
            (client_socket, client_address) = self.socket.accept()
            self.poller.add(self, client_socket.fileno())
            self.client_sockets[client_socket.fileno()] = client_socket
            data, sender = self.client_sockets[fileno].recvfrom(4096)
            if not data:
                self.poller.remove(self, fileno)
                self.handle_request(data, sender, self.client_sockets[fileno])

    def handle_request(self, data, sender, socket):

    def get_name(self):
        return "unknown"
    def respond_to_search(self, destination, search_target):
        dbg("Responding to search for %s" % self.get_name())
        date_str = email.utils.formatdate(timeval=None, localtime=False, usegmt=True)
        location_url = self.root_url % {'ip_address' : self.ip_address, 'port' : self.port}
        message = ("HTTP/1.1 200 OK\r\n"
                  "CACHE-CONTROL: max-age=86400\r\n"
                  "DATE: %s\r\n"
                  "LOCATION: %s\r\n"
                  "OPT: \"\"; ns=01\r\n"
                  "01-NLS: %s\r\n"
                  "SERVER: %s\r\n"
                  "ST: %s\r\n"
                  "USN: uuid:%s::%s\r\n" % (date_str, location_url, self.uuid, self.server_version, search_target, self.persistent_uuid, search_target))
        if self.other_headers:
            for header in self.other_headers:
                message += "%s\r\n" % header
        message += "\r\n"
        temp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        temp_socket.sendto(message, destination)

# This subclass does the bulk of the work to mimic a WeMo switch on the network.

class fauxmo(upnp_device):
    def make_uuid(name):
        return ''.join(["%x" % sum([ord(c) for c in name])] + ["%x" % ord(c) for c in "%sfauxmo!" % name])[:14]

    def __init__(self, name, listener, poller, ip_address, port, action_handler = None):
        self.serial = self.make_uuid(name) = name
        self.ip_address = ip_address
        persistent_uuid = "Socket-1_0-" + self.serial
        other_headers = ['X-User-Agent: redsonic']
        upnp_device.__init__(self, listener, poller, port, "http://%(ip_address)s:%(port)s/setup.xml", "Unspecified, UPnP/1.0, Unspecified", persistent_uuid, other_headers=other_headers, ip_address=ip_address)
        if action_handler:
            self.action_handler = action_handler
            self.action_handler = self
        dbg("FauxMo device '%s' ready on %s:%s" % (, self.ip_address, self.port))

    def get_name(self):

    def handle_request(self, data, sender, socket):
        if data.find('GET /setup.xml HTTP/1.1') == 0:
            dbg("Responding to setup.xml for %s" %
            xml = SETUP_XML % {'device_name' :, 'device_serial' : self.serial}
            date_str = email.utils.formatdate(timeval=None, localtime=False, usegmt=True)
            message = ("HTTP/1.1 200 OK\r\n"
                       "CONTENT-LENGTH: %d\r\n"
                       "CONTENT-TYPE: text/xml\r\n"
                       "DATE: %s\r\n"
                       "LAST-MODIFIED: Sat, 01 Jan 2000 00:01:15 GMT\r\n"
                       "SERVER: Unspecified, UPnP/1.0, Unspecified\r\n"
                       "X-User-Agent: redsonic\r\n"
                       "CONNECTION: close\r\n"
                       "%s" % (len(xml), date_str, xml))
        elif data.find('SOAPACTION: "urn:Belkin:service:basicevent:1#SetBinaryState"') != -1:
            success = False
            if data.find('<BinaryState>1</BinaryState>') != -1:
                # on
                dbg("Responding to ON for %s" %
                success = self.action_handler.on()
            elif data.find('<BinaryState>0</BinaryState>') != -1:
                # off
                dbg("Responding to OFF for %s" %
                success =
                dbg("Unknown Binary State request:")
            if success:
                # The echo is happy with the 200 status code and doesn't
                # appear to care about the SOAP response body
                soap = ""
                date_str = email.utils.formatdate(timeval=None, localtime=False, usegmt=True)
                message = ("HTTP/1.1 200 OK\r\n"
                           "CONTENT-LENGTH: %d\r\n"
                           "CONTENT-TYPE: text/xml charset=\"utf-8\"\r\n"
                           "DATE: %s\r\n"
                           "SERVER: Unspecified, UPnP/1.0, Unspecified\r\n"
                           "X-User-Agent: redsonic\r\n"
                           "CONNECTION: close\r\n"
                           "%s" % (len(soap), date_str, soap))

    def on(self):
        return False

    def off(self):
        return True

# Since we have a single process managing several virtual UPnP devices,
# we only need a single listener for UPnP broadcasts. When a matching
# search is received, it causes each device instance to respond.
# Note that this is currently hard-coded to recognize only the search
# from the Amazon Echo for WeMo devices. In particular, it does not
# support the more common root device general search. The Echo
# doesn't search for root devices.

class upnp_broadcast_responder(object):
    TIMEOUT = 0

    def __init__(self):
        self.devices = []

    def init_socket(self):
        ok = True
        self.ip = ''
        self.port = 1900
            #This is needed to join a multicast group
            self.mreq = struct.pack("4sl",socket.inet_aton(self.ip),socket.INADDR_ANY)

            #Set up server socket
            self.ssock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,socket.IPPROTO_UDP)

            except Exception, e:
                dbg("WARNING: Failed to bind %s:%d: %s" , (self.ip,self.port,e))
                ok = False

            except Exception, e:
                dbg('WARNING: Failed to join multicast group:',e)
                ok = False

        except Exception, e:
            dbg("Failed to initialize UPnP sockets:",e)
            return False
        if ok:
            dbg("Listening for UPnP broadcasts")

    def fileno(self):
        return self.ssock.fileno()

    def do_read(self, fileno):
        data, sender = self.recvfrom(1024)
        if data:
            if data.find('M-SEARCH') == 0 and data.find('urn:Belkin:device:**') != -1:
                for device in self.devices:
                    device.respond_to_search(sender, 'urn:Belkin:device:**')

    #Receive network data
    def recvfrom(self,size):
        if self.TIMEOUT:
            ready =[self.ssock], [], [], self.TIMEOUT)[0]
            ready = True

            if ready:
                return self.ssock.recvfrom(size)
                return False, False
        except Exception, e:
            return False, False

    def add_device(self, device):
        dbg("UPnP broadcast listener: new device registered")

# This is an example handler class. The fauxmo class expects handlers to be
# instances of objects that have on() and off() methods that return True
# on success and False otherwise.
# This example class takes two full URLs that should be requested when an on
# and off command are invoked respectively. It ignores any return data.
# Clearly somewhat modified by PS to take in a string - split in two and send out to MQTT
class rest_api_handler(object):
    def __init__(self, on_cmd, off_cmd):
        self.on_cmd = on_cmd
        self.off_cmd = off_cmd

    def on(self):
        return 200

    def off(self):
        return 200

# Each entry is a list with the following elements:
# name of the virtual switch
# object with 'on' and 'off' methods
# port # (optional; may be omitted)

# NOTE: As of 2015-08-17, the Echo appears to have a hard-coded limit of
# 16 switches it can control. Only the first 16 elements of the FAUXMOS
# list will be used.

# Clearly the above is changed. Here we have MQTT commands in the form topic|payload

    ['bedroom lights', rest_api_handler('toesp/bedlights|{out0:1}', 'toesp/bedlights|{out0:0}')],
    ['kitchen lights', rest_api_handler('toesp/kitchenlights|{out0:2}', 'toesp/kitchenlights|{out0:3}')],

if len(sys.argv) > 1 and sys.argv[1] == '-d':
    DEBUG = True

# Set up our singleton for polling the sockets for data ready
p = poller()

# Set up our singleton listener for UPnP broadcasts
u = upnp_broadcast_responder()

# Add the UPnP broadcast listener to the poller so we can respond
# when a broadcast is received.

# Create our FauxMo virtual switch devices
for one_faux in FAUXMOS:
    if len(one_faux) == 2:
        # a fixed port wasn't specified, use a dynamic one
    switch = fauxmo(one_faux[0], u, p, None, one_faux[2], action_handler = one_faux[1])

dbg("Entering main loop\n")

client = paho.Client()
client.username_pw_set(mqtt_user, mqtt_pass)

while True:
        # Allow time for a ctrl-c to stop the process
    except Exception, e:


(if this lot complains about spacings - get the original article and make my changes

So – put the file in a directory – I put it into /home/pi/fauxmo/ and gave the file execute permissions (no idea if that’s needed or not but as I do that with scripts I thought it best - 0754 if you're interested).

I ran the file in the /home/pi/fauxmo folder at a terminal as:

python –d

The –d is for debug – you can scratch that once you are running – and then find a way to make it run reliably on power up. That is possibly described here:  if someone has the working command line for this please do send it in – might save some experimenting.

And that’s it. Great fun and instantly useful without lots of hassle. With Node-Red you could fire the MQTT in to do with what you want – or fire straight to MQTT-accepting ESP8266s (what use are ESPs that DON’T support MQTT you might ask) – or if you look at the chunk of code handling MQTT – you could pull that apart and instead just control ports!!

So here’s a thought – one of our readers was kind enough to remind me that the Orange Pi Zero is now available and it is CHEAP. Normally I’d not touch Orange with a bargepole but the Armbian Jessie implementation seems to be now up to speed. SO -

Under £9 for the cheapest, might not be that happy running Node-Red but it will certainly run PYTHON and MQTT (well, you don’t even need MQTT for that matter if you just want to control the odd output or send stuff through serial etc)… so for under £9 you could have several Alexa-enabled devices !!! That can’t be bad.

This will no doubt mutate as time goes on!


A5 V11 Router

A5-11 RouterOr – the cheapest router on the planet – probably. I should say at the outset this experiment did not end too well – currently waiting for another unit to arrive – but worth a read in case you’re about to embark on using this unit.

I had to pick one up – at just a few quid (I paid £3.82 but I note they’ve put the price up to £4.41) I could not help myself. 2 weeks ago I ordered this unit from China and this morning it turned up.

Of course once I’d received it (in a very pretty package comprising the 3g router, a USB lead and instructions in Chinese and English) I had to go off to find out how to actually make use of it.

Here’s the specification for those interested:

  • SoC – Mediatek/Ralink) RT5350F MIPS processor @ 360MHz
  • System Memory – 32MB RAM (W9825G6EH-75).
  • Storage – 4MB NAND flash (Pm25LQ032)
  • Connectivity – Wi-Fi 802.11b/g/n up to 150 Mbps; 1x 10/100M Ethernet
  • USB – 1x USB 2.0 host port, 1x micro USB port for power
  • Misc – Power LED, factory reset pinhole
  • Power – 5V via micro USB port
  • Dimensions –  6.1 x 2.3 x 1.4 cm


    So – typically instructions on the web were not at first a lot of use - “The unit will default to Plug into your network and connect” – log into it… well that’s fine except you CAN’T unless you happen to have a 192.168.100.x setup !!

    shellAnyway as it turns out  - when I plugged power into the little unit, and plugged a network cable in, it adopted an address on my network –

    web interfaceAnd so off I went to find Telnet on my PC – and sure enough with username and password admin and admin I found an utterly useless shell! But then I noted in the instructions something about a web page – seems reasonable as routers usually have web pages – and lo – a web interface!!

    A good start –of course the idea of buying this was to fit it out with openWRT and MQTT to truly make the cheapest MQTT-based home network on the planet.. but I had to have a play in the meantime. Considering these things have 4Mbit of Flash, this is not a bad start.

    I could not test the 3g side as I don’t have a 3g dongle (my phone has data for months over here in Spain before THREE decide I’ve been here too long so no need for one). So I tried changing the WLAN settings – surprising amount of info in there – well impressed at the price.

    The WIFI signal, with the unit lying horizontally was somewhat down from a “proper” router but sitting vertically, from the next room I noted a signal almost as good as my TP-Link.

    SpeedtestI decided, silly as it seemed to go for a speed test. I mean – what DO you expect for £4?

    I connected my phone via WIFI to the router. Incidentally all of the operations I tried needed a reboot of the unit - I guess they had to take shortcuts somewhere - but no problem as it only took a few seconds each time. With the dish-connected WIFI we have here in Galera, I get around 10Mbps download and 3Mbps upload. I ran the speed test ( on my phone while using the unit as a router – no more than a metre away from the little unit. I have to say, impressed I was!!! I’m thinking of ordering another one just for that use alone! 

    I mean, if nothing else, just for some extra range in the house without cluttering the place up with kit – this thing is smaller than the power supply you need to power it!!! Apparently we’re looking at 300ma at 5v (so, 1.5w) so the most rubbish USB supply would do the job. The unit itself was at this point slightly warm, nothing more. For heaven’s sake it even has NTP time synchronisation.

    Next, I noted something about “network storage” – which was enabled… I grabbed a handy FAT32-formatted USB stuck and put a (780MB) movie on it – and plugged the USB stick into the router unit (this is all guesswork)… I noted that the stick was not hing special speed-wise as it had taken an age to copy the movie over (nearly 3 minutes) on my fast PC.

    Here’s where I hit a snag – despite network storage being enabled-  I could not figure out how I might use this – the IP address was already being used for the web interface – and the instructions for network storage were non-existent. A quick look on my PC for devices showed nothing.

    So, on the off-chance, I went into file explorer on the PC and typed \\  -  sure enough it asked for a user and password – the default was still set at admin and admin – and lo – a directory called MEDIA appeared. Within that directory was a folder for system information-  and my movie.  I clicked on the movie…. and sure enough – it worked.

    RouterWell impressed by all of this – I left that running on the PC… no stuttering…. just working. I went to the phone which was still at this point connected to the little unit wirelessly. ES File Explorer – streaming media player – sure enough – the movie played simultaneously on my PC (hardwired to the net work) and the HTC One M8 phone.

    At this point I was beginning to wonder if I really wanted to upgrade this little unit to something else – it just works SO well for it’s size and price. DMZ, DDNS – oh and of course the point is it is supposed to provide WIFI and WIRED access from a dongle - but I could not test the latter.

    Static routing and MAC-Binding – how on EARTH did they fit all this stuff in such a small space and for that price – this thing has more useful features than the utterly useless router that Plusnet gave me, back in the UK!!!

    I used the little SHELL software I referred to earlier – and despite the list of instructions being minimal – actually, it responds to far more instructions including cat /proc/meminfo which immediately told me this unit had the full 32MB of memory -  apparently other units often have only 16GB.

    Feeling REALLY ambitious? Check out this guy’s video and blog – he apparently has DEBIAN running on it!!!

    upgradingIn reality, all I wanted was to try Open Router for the ability to run MQTT on this little box.  So, using software from here… I used the standard upgrade option in the menu to load the image – with a warning that a bad image would BRICK the router…. 2 minutes later – everything said “100%” – but of course – no more visuals as it was sitting on the same address as my main router at  presumably?

    By now I was wondering if this was such a good idea…. the original blue light on the little router was now flashing white and blue!!!  I figured that MUST be a good sign and took the whole lot over to my laptop and plugged the router into the laptop – so that this was a completely separate network.

    Erm, no. After a couple of reboots, I ended up with the device still fitted with original Qualcomm software – and now on it’s own address range of – I guessed that – and sure enough – it was running DHCP. I changed the address to that of my network – and disabled DHCP and I was back to square one. (it turns out you need a better bootloader as this one won’t take unofficial software updates!!)

    And that’s where I am up to now – this I can see is going to be a long blog… but I thought you might like to see where I’m up to – even at this point I think this is a must-have gadget at the price. If I can get MQTT on it – that’ll be a bonus.

    Right now – to update this unit to OpenWrt – you need to put in a new bootloader, the original won’t let you use a non-official image…. that’s fine – except the TFTP to let you put that in – isn’t supplied with the module. Also you need to load a smaller version of OpenWrt to load the bigger one – and the links for that are dead (along with most of the links for the bootloader). Some very much out of date material out there.  If anyone has a known working Busyboxlink (with known working links in it) I’m sure lots of us would benefit. I found a usable link for the bootloader -

    But I don’t THINK that is useful on it’s own. Some links suggest putting the file on a website and WGET it – no wget in BusyBox shell!!!

    After changing the IP address of the unit and turning off DHCP – I found that all worked well until the next reboot – and then the unit would not come back to the same address or indeed respond to any address -  and needed restoring to factory settings with a pin. 

    At this point I was considering cutting my wrists when I stumbled across this.

    Off I went – and sure enough the files I needed were there.  I realised there was a wget command in there and as usual “just whip out your Linux computer”  to run a webserver to serve the file up – right. Anyway, I discovered MONGOOSE web server – a one-file webserver that sets up shop in the directory in which you run the program – marvelous.

    With that I was able to follow the instructions in the Hackaday blog. Well, almost – the file name was not the same as the on in the blog – with 6 extra letters on the end (bear that in mind if you try this), the file once grabbed with WGET was not in the /tmp directory contrary to the instructions – (I used ./ instead) and once all of that was done  - the address of the new router was not as promised but – that cost me half an hour Smile Oh – and the –vn command in the instructions turns out to be a –n command !!

    Anyway at the end of it all I ended up being able to Telnet (Windows command line with address as parameter) to the new OpenWrt router on my little sub-£4 box.

    But all was not well (again) – There was an instruction to change the password – and this was also suggested in the blog above.  Simple enough except when I did that it said “No space left on device” – not enough even to put a new password!!?!?!! Then I remembered of course that I’d left the openWrt install file in place – so using RM I got rid of that and NOW I could update my password. I was expecting at this point the Telnet service to stop working and SSH to start working.

    And so it did – but can I HELL log in with SSH – I have Bitwise SSH client on the PC – I can see it knows about the router as other addresses provide different error messages but it simply WILL NOT log in.. 

    Update 7/7/2016: WELL it turns out that the version of openWRT I used didn’t have SSH activated – yes, amazingly after telling people to put a new password in – and then rely totally on SSH – it was not installed.

    It turns out that inside the unit  there are 4 connectors – 3 are relevant as they are ground, serial in, serial out. They can be attached to an FTDI at 57600 giving access to the shell (a 2013 version I may add). I changed the address in /etc/config/networks  using the built in VI editor – to one in my address range and added a gateway. This then enabled me to grab another version of the openWRT software… here .. and the link I used was

    On reboot I could see I had a NEW shell (2015) and I set the password again – and set the address again as this was all lost in the update.

    NOW I could SSH into the unit and use the shell in a terminal.

    In theory all that is left is to mount a memory stick – expand available space and add in the full graphical environment? Well, plugging in a memory stick works – it is acknowledged in the shell – erm, but no SDA1 appears in DEV…

    I started to follow these instructions for adding block device support

    opkg update
    opkg install kmod-usb-storage block-mount kmod-fs-ext4
    mkswap /dev/sda1
    swapon /dev/sda1
    mkdir -p /mnt/share
    mount -t ext4 /dev/sda2 /mnt/share -o rw,sync

    And about half way through the second line I ran out of storage on the device!! However the installation continued. At the end of line 2 above I was getting pretty depressed – but checked – and sure enough I had an SDA and SDA1 in /DEV/

    But no room to do anything – not even to create /mnt/sda1 or similar. Just utterly out of room. and finally – after trying various image with varying success I tried the image in this project - and – BRICKED. Project on hold until another unit arrives Smile

  • Facebooktwittergoogle_pluspinterestlinkedin

    Gear S2 Classic and Home Control

    For a change from processor boards, let me tell you about my new WATCH.

    Scargill watchFor a long time I’ve been a faithful Pebble user, the watch has a week’s battery life, a display you can read in sunlight and a host of applications.  I had one of the very originals which sadly did not survive a swimming expedition in a saltwater hotel pool and the guys at Pebble replaced it without question. However things move on and I was sitting with a neighbour perhaps 3 weeks ago now and I notice his wrist hovering as if to say “you need this, Peter”.

    He flicked his wrist and the most wonderful Tag Heuer display popped up.  a beer later and he showed me the watch again – this time with a Gucci watch face. I could bear it no longer.

    So, several days later my Gear S2 Classic turned up. It is a work of art, to put it mildly.  I’ll get the bad bit out of the way first – the battery lasts 2-3 days – but the good part of that is the proximity charger, another work of art you just drape the watch over with no connection to charge – in VERY little time, less than an hour and it’s ready to go.

    This alone with the pictures should be enough to make you get your wallet out – but then I remembered I have TASKER on my mobile phone (HTC ONE M8) – and that talks to MQTT. Could I be so lucky?

    YES – there’s an APP for that. A small add-on APP for the watch lets me scroll through all my Tasker tasks and run them at a touch. If you’ve used tasker – you’ll know you can do just about anything with it – including (with an add-on) send MQTT messages.

    If you, like me, have no artistic skills then you can create your own watch-face in no time at all like the one I’ve shown on the right – done right in Mr Time Maker’s web page – but if you have a little skill you can make a DAMNED good emulation of just about any watch on the planet.

    Just as an aide the watch has technology underneath to do your pulse rate and there is also the usual health stuff like how many steps you’ve taken, weather locally, news, alerts etc.. the list is endless and growing by the minute.

    So now not only do I have a black-leather-strapped work of art attached to my wrist (you really DO have to see the display – which, depending on watch face (and there are many hundreds of them thanks to Mr Time Maker app) can be seen in reasonably bright (Spanish) sunshine, but also I can control all my household gadgets in comfort.

    The links in the above article should help you. Enjoy. If interested do check pricing though for the watch. It varies dramatically.


    Remote checking Home Control Units

    Those of you who are aware of the Home Control project (Home Control 2016) will know that the software/hardware combination of a Raspberry Pi (running Jessie, Node-Red, SQLite and Mosquitto) and ESP12 (running the software described in the Home Control project) is proving very reliable – but there are occasions when you may wish to be absolutely sure that units are actually doing something.  One way to do that is simply to put timeouts on every response you expect… but that makes for complicated coding. A simpler way is to regularly ask each unit for the status of one of the outputs. It doesn’t matter what the response is, as long as there is a response.

    As it happens I’m having an issue right now – the heating controller part of my system back home in the UK is working perfectly except of an on-going issue with Plusnet broadband. They are denying it is their fault but the broadband fails perhaps once a day, maybe more, maybe once every two days but eventually it will fail. Most of the time the TP-Link TD-9980 router reconnects within a minute or so – but for reasons I don’t yet understand, occasionally it just will not reconnect. The solution to that was simple and two-fold, a standard timer which turns the router off for a minute in the early hours every day. That’s the “belt and braces” approach. In addition, the Raspberry Pi is polling Google every few minutes. If it fails totally over a 15 minute period to contact Google, an ESP8266 unit will reboot the router.

    All of this worked perfectly for several days and then… the heating relay ESP8266 refused to log back in after one of these “cuts”. I can only put it down to being right on the edge of signal. The reason I say that is that late last year we had horrendous problems with WIFI here in Spain, some kind of attack. It went away but during that time over several days I was losing WIFI constantly so I made damned sure the ESP units would reliable log back in, in the event of temporary failure of MQTT or WIFI or both etc. And yet just occasionally this particular unit will not log back in – a power cycle fixes the problem but that means I need to know about it and ask someone to pull the plug for a moment.

    So – I’ve implemented this for the two most important devices in the building – the main heating temperature sensor – and the ESP that controls the relay to turn the heating on and off.


    Timeout NodeWhat you see above is as follows: 2 Node-Red “inject” nodes (standard) fire out requests every 5 minutes to MQTT to ask the relay and temperature units for the state of output 0. 

    Which command is irrelevant as long as it is a query which returns a result and otherwise has no effect on anything.  So for example with a topic holly1/toesp and a payload of {out0?} the system is expecting a response of OFF or ON (irrelevant here) from topic holly1/fromesp/out0

    The two incoming MQTT nodes (left, purple) pick up on the response and feed them into my “timeout” nodes  (node-red-contrib-timeout).

    As you’ll see below, the operation is simple – the timeout node doesn’t care what is incoming as long as something is. if it gets nothing for 60 minutes it will time out and send a payload out – in this case thanks to the standard email node (green) an email to me. 

    During that time, normally, the MQTT nodes would have responded every 5 minutes – ie 20 times, topping the timeout node up to 3600 seconds even if most of the messages were missing (and there is no reason why any should be missing).  In this case I’ve not put messages for “safe state” or “warning” – so nothing goes out unless the timeout actually runs down to zero.

    Click on any of these images for larger versions.

    So while this does not solve the problem, it does mean that I’m alerted to the issue and can ask someone to disconnect and reconnect the relay unit. With what has been 100% reliability of Raspberry Pi 2 boards (with a decent Samsung microSD or similar) I can be pretty sure I’ll get an email if something goes wrong.

    I hope this proves useful to others.


    Home Control 2017

    Introduction (Latest update August 09, 2017)

    HomeSick of reading about other people’s home control? Want to make your own? Got some electronics and software experience? You’re going to LOVE THIS! Rock-solid home control – low cost, DIY.

    If you’ve read my early blogs you’ll know that Aidan Ruff and I have been working in home control since the late 20th century. Together, we ran an electronics R&D company for many years and one of our products was a home control system called APPCON.

    Home Control 2017

    Continue reading Home Control 2017


    Itead Slampher and Sonoff

    Slampher - in Scargill's review of Slampher and SonoffRemind you of IKEA? No? Heard of ESP8266? Yes? Well, Slampher and Sonoff are two products from Itead and I think you’ll like at least one of them. They arrived at my desk today. Slampher is a screwfit lamp fitting – take the E27 lamp out – put Slampher in – screw the lamp back in and you’re in business with a remote controlled lamp. It really is that easy.

    This blog updated July 26, 2017

    There’s a tiny button on the unit that you press to turn the lamp on or off manually if you ever need to – or hold it down and you can make it listen for a controller – the handy – and i have to say very nice quality – keychain controller which can control 4 devices.

    Handset - in Scargill's review of Slampher and SonoffThe REALLY good thing is that both of these products use ESP8266 so if you want to go in and re-program them – as I have  – to handle MQTTon your WIFI!!!! You CAN do but there is an APP if you don’t want to do it the hard way. I don’t have the App at this time so I can’t comment… but it’s an ESP8266 – how hard can it be to get that right.

    Slampher and SonoffThe other product is called Sonoff and it’s just a little box that you wire mains into – and out the other end you fasten it to whatever you want – and control it the same way. SO the units obviously have both WIFI (ESP) and a 433Mhz receiver – because the little handset is 433Mhz.  Sonoff would not look out of place on a skirting board at all (proper little screw connectors inside – no soldering needed).

    These are NOT expensive – in fact that really IS the point – Sonoff in particular – I could not build one at that price – and I guess that is part of the charm.  I can make this stuff with my eyes shut but by the time I put it all together and in a box….

    The units I have are solid looking – the little mains controller Sonoff – the PCB inside is well put together with proper isolation on the PCB (some of you will have seen some HORRIBLY DANGEROUS pcbs in the past when it comes to UK mains but little Sonoff is ok at least at first glance).  I’ve not had a chance to pull the lamp unit apart yet – that will come later.

    Pricing: I found this – $4.85 – if you live in the UK the postage brings it to $9.37 but even then! I checked out the cost of 3 – $14.55 all in to UK that is $5 each – that’s around £3 or so.

    DIY programming: You’ll like this if you know how to program ESP8266 chips. I now have a diagram for Sonoff – initially the connectors did not seem to match those on the diagram – so I contacted Itead – they were keen to point out that they have a cloud system and App and of course the remote control via radio – all of which I’ll lose when I reprogram the board – but hey…. I want to control these with MY kit.

    Look at the diagram below… this is looking at the TOP of the board. There is a 4 way or 5 way connector. If the latter you might want to get the meter out – but on the 4-way (mine)….  the SQUARE pin is not as you might think ground – it is 3v3 – then we have serial, serial and ground. If you wire your FTDI as per the wiring diagram below it should work.  The relay is controlled by GPIO12.

    I was happy to see cooperation from Itead – obviously they would prefer that you use their cloud software and most will – but we are hackers and would never be satisfied without control, right? Ignore the LED, it is fastened to the RF device so you don’t have control over that. Right now I don’t have enough info to make any use of the RF device. If someone knows better please write in. Would be nice to have both.

    Warning – you are on your own if you start this – you will need your own software and messing with the board will kill any warranty.  Clearly this must NOT be plugged into the mains while programming – use FTDI at 3v3 settings. No doubt 5v would cause damage.


    Sonoff board



    CLARIFICATION – the square pin is NOT ground – the square hole is 3v3. You should see that the round hole is connected to the flood filling (if you have good eyesight).

    Use FTDI at 3v3 setting – in the original boards,  press the large button –THEN power up FTDI. DO NOT connect mains power while doing this – you will fry. Test MQTT connectivity, disconnect, put back in the box, use. NOTE – the newer boards marked RF do not seem to attach GPIO0 to the button – see later blog – it may be necessary to make a VERY find short at power=up – of two surface mount components.

    As for actual use.. here’s the interface in Node-Red


    Complicated, isn’t it. The node on the left is one of Andrei’s UI components – the node on the right is the standard MQTT connection – all of course running on my Raspberry Pi2 and already in charge of the heating and a load of experiments.

    Here’s what it looks like on the phone…


    and here it is - waiting for the screws which for the LIFE of me I can’t find… works a TREAT. The output relay is on GPIO12.


    I have modified my general purpose MQTT software to run with the Sonoff devices and it works a treat.   - see the home control 2016 project in here where you’ll also find binary files.  A couple of settings need changing from normal – I normally use GPIO2 for WIFI setup and the SONOFF WIFI units use GPIO0…  also  - GPIO13 which I use as a status indicator – turns out the Sonoffs use them but reference 3v3 so I have an inversion setting so they flash off most of the time instead of on all the time – it’s all in the DOC file in the project above. With both of those settings updated... you can also use the button as an on-off button if you like (toggle).

    The SONOFFs have only 1MByte of FLASH which would kill off OTA for all but the simplest of projects – my code needs the larger size IF you want OTA. With this in mind, is it worth paying extra for the Electrodragon? Some say the quality of the latter is not so good? But they do use an ESP-12 which will have the normal 4Mbytes of FLASH… what do you think. Update Jly 2016:  The Electrodragons didn't turn out so well - but now I've just proven that it is in face easy with a fine soldering iron to fit a £0.37 32Mbit FLASH chip to these, replacing the original - which means full OTA and no problems expanding.  See for more info.

    July 2017 - By now most of us are familiar with the Sonoff units but there is always the cost of postage..

    See above - includes postage - absolute bargain.


    More Websockets

    In a previous blog item here I covered the use of MQTT, jQuery Mobile and Websockets to make a controller web page for mobile phones/tablets etc. here

    I showed just a basic and not too pretty page with buttons that would control real items, in my case via Websockets on a Raspberry Pi, firing off MQTT (also on the Pi) to ESP8266 modules (using our own code based around TuanPMs MQTT project).

    Well, I’ve been doing a bit more playing around – here’s a better way to make the buttons – but all based on the previous article so you’ll need to read that first.  I’ve also added a second page – merely to show you how that is done easily – and you could expand that ad-infinitum – but I’ve ALSO shown how to get information back in from the same web socket as I was asked about this aspect. It’s very easy (there was a time when I was terrified of the subject).

    So first of all this time my Node-Red code is slightly different – I’ve separated off the incoming websocket node from the outgoing one – as long as they refer to the same item they don’t have to be joined together.


    So in this version – the same code in “process commands” – as instructions from the web page go out to MQTT to control various gadgets. See other blog item.

    Here, a Timestamp (standard Node-Red item) pushes out a timestamp every second – and another function converts that into a simple string as payload – which is then sent back, every second, to the web page. Normally you might send JSON data back for further parsing in the web page but in this case it’s a simple string.

    Here’s the code inside timeConvert.

    var dt = new Date(msg.payload);
    var msg = {    'payload':  dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds() }
    return msg;

    Page 1And so to the web page itself: I’ve improved it some way as you’ll see as I figured out how to get 4 buttons wide (trivial – I just didn’t spot it in jQuery Mobile the last time around).

    As you can see it is quite pretty, has a page button near the top to switch to another page (which is empty as yet so I won’t show it here but it will be obvious in the code how it works and how you can keep adding more.  I’m not happy about the look of the paging button but it’ll do for now.

    Then way down at the bottom left – you’ll see a timestamp – that is coming from Node-Red and is in this case updating every second. It COULD be status information – or anything – but for the purpose of demonstration (and it’s also nice to know that something is actually working by visual feedback like this) it is just a string as described above.

    I wish I could figure out a simple additional class to colour these buttons individually instead of making themes for each button type – perhaps someone else could contribute here.

    So now this is a quite reasonable control page with the option for more pages (not going to get any more buttons on this page).   I did think of having Node-Red return a string with the states of the various outputs and changing the buttons accordingly – but that would not have made for easy reading.  Anyone implementing this will soon figure out how to add such functionality.  Instead of returning simple text, it would make sense to similarly send back jSon as there is a very easy to use function to split this up.

    Recall too from the previous blog that I’ve implemented (in the .htaccess file) simple password protection not shown here and not of much use locally but pretty essential if you want to control from outside.

    Here is the web page code for the above, note I’ve changed the name of the websocket to /myapp3 – could have called it anything, really.  See the “onmessage” function – you don’t get any easier than this. There you go…

    newmenuUpdate: 23/08/2015 – there is a better way to do navigation in jQuery mobile (I’m still learning) but it seems incomplete. A slide-in-from-the-left menu system is easy to create – but if you try to make one menu system for all pages – there is no formatting – I guess this will be available in a future version – however, replicating a menu system for each page has it’s advantages as you can disable the current page link! 

    Here is a version with the awful button missing hence saving some space on the page. So this is a little like the swipe side menu in NETIO except I’ve implemented it by touching the top title bar because swiping doesn’t work too well on a PC and you might be testing this in a normal browser!!!

    The title bar is sitting doing nothing anyway, so why not.

    Actually I like this – on a typical phone you could easily get half a dozen pages and some explanatory text or imagery in that side menu… hell, you could even get some more buttons in there and a logo!

    Here you go..



    I wrote a while ago about using web sockets as against something like NETIO for controlling the home – most folk liked the article but I think part of it was a little complicated – and at the time I’d not really thought it out to make it as simple as possible.

    So, this morning I started again and now I have a decent and easy way to make control interfaces from the mobile phone – to ESP boards. I won’t go into MQTT in detail here – I’ll assume you have an MQTT interface of some description on your WIFI boards – if you need more info on that, look elsewhere on the blog.

    jquery mobile themesI’ll also assume you have node-red running somewhere. In my case I have a Raspberry Pi2 sitting in a corner with battery backup, running node-red, MQTT and Apache web server.

    So – in order to make a half-decent looking phone interface, I decided to go for jQuery Mobile – it is cheap, easy to run and there is a theme generator.

    I went along to and made myself a set of THEMES – that is some CSS. You can’t get any easier – you just drag the colours onto the various items – save the ZIP file – and then refer to the THEMES folder in your web page.

    Here’s what I did – you might want less colours, more colours, different colours etc.… you  can change all aspects of the jQuery Mobile interface which is basically a bunch of items that work well on mobile phones and tablets. Click on these images for larger versions.

    So – what’s needed then is a web page…. that shows a series of buttons (jQuery Mobile), a means to send off button presses to Node-Red (web sockets) and some means of then controlling things (MQTT).

    In my case I put the web page and dependencies in a folder within /var/www on my Raspberry Pi (could be anywhere – a Windows PC, whatever).  I can access these pages externally and hence there is a password on the folder – that is done in my case with an.htaccess file but you might have other ideas.

    The page should begin with a header for accessing jQuery, jQuery Mobile and the themes etc.…  I figured as the phone has to have an Internet connection to work anyway, there was no point in filling up the Pi with libraries other than my custom theme..

            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>Simple Controller</title>
            <link rel="stylesheet" href="themes/" />
            <link rel="stylesheet" href="" />
            <script src=""></script>
            <script src=""></script>
            <link rel="stylesheet" href="themes/petes.min.css" />

    phoneAnd so above, in the HEAD section of the web page (which I simply edit with NodePad++) links to the local theme and remote jquery. That local stuff all comes in the zip file from the themeroller site – well it does if you make the title when saving “petes” – you might want to call it something else.

    Here’s my test page which works equally well on a mobile phone as in a web browser  - I’ve checked this out in Chrome and Edge and on my Android 5 phone – identical results.

    As you can see I’ve gone for gross, using matching colours (more or less) for the colour controls I’m actually firing off.

    The only difference between the buttons is a simple addition of data-theme="g" or whatever letter – if you look at the theme image at the top, as you add more themes, they start off as “a” then “b” etc. We’ll see this code later.

    Rather than put a ton of code in here – here’s a snippet with the entire page.

    You’ll see the header information followed by some setup for web sockets.

    The basis of my controller as you’ll know if you’ve read Home Control 2015 in this blog, is to send MQTT data to my ESP8266 boards, so for example to control the RGB output on GPIO0 of a board called RED1,  I’ll send the following…. the topic would be red1/toesp and the payload would be for example {rgb:0,255,0,0,70,1}

    The above means – send a message to board red1 and tell it to fire out a WS2812b signal on GPIO0 to turn 70 LEDs on fully bright red.

    I wanted the web page to be as simple a possible – adding to a button something like….


    And that is exactly what I’ve done. But firstly the mechanics of the Node-Red web sockets interface.

    In Node-Red, I have this:


    Simpler than it looks, I’ve dropped in a WS input module (standard node-red node unit) and out – and connected them together.

    the input listens to “/myapp2”  - could be /fred  or anything you like to call it.  Yes folks that is ALL there is to the setup – but…. assuming my web page connects to this – I need to do something with incoming data (in this blog I’m ignoring firing data back to the page but that is also easy – I referred to that in the last blog – the point of web sockets of course is that this is a continuous connection between the web page – and node-red web socket control.

    So what I need is an incoming MQTT message and topic – and that needs firing out to MQTT to control things. The only bit of code here is in “process commands” – this is a function module (drag and drop) and this is what I put in it.

    var jsondata=JSON.parse(msg.payload);

    if (jsondata.topic)
        return msg;

    Simple enough – any incoming information is assumed to be in json format and is “parsed” (built in function) so that I can extract a payload and a topic to simply fire off to MQTT and on to my boards.

    As I’m always going to need “/toesp” on the end of the topic – I add it in here rather than needlessly replicate it in the web page.

    The web page itself needs to fire out something like (in this case)….


    The inner braces are for my boards…

    So – my board ends up with:

    topic:  red1/toesp

    payload: {rgb:0,255,0,0,70,1}

    I wanted to keep the actual buttons as simple as possible so there is a function in the web page to glue the topic and payload together.

    function send(topic,payload)

    and so all we need now is the connection between the web page and node-red web socket interface (simple as they’re both on the same server) hence if you look at the web page you’ll see a definition for wsUriC which is then used in wsConnect() which is called on page load. This makes the connection.

    ws.send() sends a string to Node-Red and my function ensures the string is in the right format.

    A typical button looks like this..

                            <div class="ui-block-a" ><input data-theme="a" type="button" value="red" onClick='send("red1","rgb:0,255,0,0,70,1")'></div>

    Note the very simple onClick function call to send a command off to a particular board…. and if you study the web page example, putting aside the formatting commands for jQuery Mobile (which you can learn all about here  - there is WAY more than just buttons available to you - but I’ll leave that to the reader to explore (or not).

    So I press a button on the phone – and the light comes on or changes colour or whatever. There is no feedback – that would be covered by a call-back function in the page and jQuery to alter the shape or colour of elements accordingly – really quite easy once you get started. I thought of putting that in here but I’ve probably given you enough to think about as it is.

    Hope you find this useful. When the connection is working incidentally you get a little TICK at the top of the page – or a little cross if not.



    p.s.  If you’re wondering what all this cave reference is about – head over to

    Coming up soon: I hope to review the inexpensive NEXION touch display (one is on it’s way) to make a nice wall-mounted thermostatic control – which will talk via serial to an ESP-01 which in turn will control a relay and have a DHT-22 chip for temperature sensing – and Node-Red for background timing control – and of course I will have phone-based monitoring and over-ride based on code very similar to what you see here.


    Lua Revisited

    Back in the dark ages when the ESP8266 was something new, I had a go at the nodeMCU software, essentially implementing the Lua language (or a subset of it) on the ESP8266.  At the time I was not familiar with Lua but thanks to some of the excellent resources out there on the web, I quickly caught up.

    Sadly, my expectations exceeded the ability of the implementation to match and every time I went past flashing a few lights – CRASH. I gave up in disgust.  I’m not complaining as that gave me the impetus to learn the hard way how best to program these chips – which I maintain is using the C language – and in my case via the unofficial development kit which is just an excellent piece of work by Mikhail Grivorev, leading to a complete Windows environment with a ton of examples. Between that and TuanPMs excellent MQTT code as a starting point  I’ve been happily programming away and even built up a working relationship with Espressif in the process. My home control project is well under way and now just waiting for some new boards – and a function that Espressif have promised to shut down PWM.

    LuaMeanwhile this morning, I can’t remember what I was reading that triggered me off, I decided to have a quick look at the node-MCU code again.  I had it sitting in the development environment but figured it would be out of date and so went off to the repository to study any changes – which I duly put into the code. The GIT Makefile is useless for Windows but fortunately there was one in the environment – and as it happens, a good one. I wasn’t about to go backward so I made sure I was linking to the very latest SDK which at this time is 1.1.2 with at least one patch – all available from the Espressif forum.

    I checked the settings in the Makefile, adjusted the SPI speed up to 80Mhz, set the FLASH size to 4Mb to suit the sparkly new ESP-210 that is sitting here looking for a job – and started the compile process. Within a minute and without any issues, the code was flashed. I have to say I was expecting at least one or two issues – but no.

    I set up a 9600 baud terminal, in the process discovering for the first time that you can have multiple terminals at once in the Eclipse environment – handy (it would be more handy if it always reported the right location of bugs but you can’t have everything). Sure enough – a working Lua board.

    I told it about my WIFI setup and sure enough it grabbed that too.  The project has come a long way since the early days and now (apparently) supports json, a file system, timers, pwm, i2c, spi, 1-wire, net, mqtt, coap, gpiio, wifi, adc, uart and more. I’ll believe all of that when I see it.

    I was delighted to see that both the integer and float versions are available – that’s a plus! I also noted support for the WS2812b chips – I noted that as I had to make a change to the source code and I’m delighted to note that they’ve made use of a local compiler directive that I was not even aware of to ensure accurate timing. Wish I’d seen that a month ago when I started work on my own WS2812b support. 

    WS2812B LEDs on nodeMCU LuaI plugged a (short) length of WS2812b strip (a custom board we had made) into the ESP-210, fired the instruction and… worked first time out of the box. Impressive.

    I’m pleased to see the nodeMCU docs have come on a long way – sadly the website was running like a DOG at the time but here’s a few stats I managed to get off the board.

    print(node.heap()) returned 20kbytes which is more than when I tried it – but then it used to die when the heap got down to 5K so we’ll have to wait and see how reliable that is.

    3 Channels of PWM are available – at 10 bit resolution. Clearly some work to be done here as Espressif have just released a library handling more outputs than that at nearer to 14 bit resolution – and it works as I’ve been using it in my own research and projects. However – handy enough.

    There are libraries for sending to displays complete with fonts – now that I’m interested in – if this will do reliably MQTT and report results to a display I have a use for this already – wall thermostats.

    So, overall, a very brief test – it is possible with the right environment to get interactive and easy to use nodeMCU up and running in minutes – you don’t need special boards and the software is free. Can’t be bad – how complex a program you can make withou running out of RAM remains to be seen. Here is some more info on nodeMCU.


    Web Sockets Web Page Part 3

    For this to make sense you should look at Websockets Web Page Part 1 and Websockets Web Page Part 2 first.

    In this final section I add styling to the page  and also change the code so that you can send a partial message  i.e. {“my_input”:”Hello”} without affecting other parts of the page.

    So in order to demonstrate this latest version you’ll need to go make yourself a theme (actually only one file needed for this – the css file) and put it in a directory called themes under the page shown here.

    You’ll also need to change the Node-Red to this…


    You’ll see here that I have to two experimental injects… and also I pull out the incoming web socket data – process the SWITCH and send a command off to one of my boards via MQTT to turn a light on or off, so NOW our simply page as well as looking pretty, actually DOES something. It does not take too much of a stretch of the imagine to ponder how to replace those injects with status info from, say an input or an MQTT input etc..

    Here’s what my new page looks like…

    new page

    The process commands box looks like this..


    As you can see – I’m merely taking the input message – and parsing it – and I’m only interested in “my_switch”… I return in this case a message to turn my ESP8266-board relay output on or off.

    Here is the code for the page – don’t forget to set the WS to your Node-Red websocket and don’t forget the theme data…

    [github file="/scargill/various/blob/master/blog2.html"]

    Note a link to a second page – as yet doing nothing but basically you can build a multi-page app all in one html file… check out Jquery Mobile site – or click the link in the page.


    A Fresh Pi2 Start


    The thing about rushing in with something new is that you tend to bring your learning mistakes with you.

    Firstly for new readers, why am I fascinated with the Raspberry Pi? Well I’ve been working on IOT for some time now and I’ve tried out many ideas most of which you’ll see blogged about in here. I originally used an Atmel 1284 based solution of my own (own board, own software) to control a number of Atmega328-based boards by short range radio. Indeed right now I have 3 properties – two here in the Northeast and one in Spain which are using this successfully. But there were a lot of range issues with the radios and I was running out of steam with the 1284s and along came the ESP8266 chips… This opened up so many possibilities but I really needed something more powerful at the helm without spending more money (many have heard me say “I’m not spending £60 to switch a lightbulb”) – and in the nick of time along came the Raspberry Pi2 – which for £30 is a bargain even though you really have to learn just a little Linux to make good use of it.

    The Raspberry Pi 2 was so new to me a matter of weeks ago (never had the original Pi) that I loaded the entire kitchen sink onto it. In anticipation that this might happen I bought a second Pi2. That gave me all sorts of issues, stalled keyboard, memory problems.. I w2as convinced it was the microSD chip… then I realised the other day that this could all be put down to a lousy power supply! I replaced that with something meaty and I’ve never looked back.  Mindful of the fact that my 32Gb microUSB drive for the first Pi2 was taking an hour or more to back up I went out and bought a Sandisk Extreme 16Gb and an Anker USB3 adaptor and that really made a difference – but still I was basing everything on a bloated software base. So this week I sat down and started from scratch.

    You can save many, many megabytes by un-installing the rubbish that comes with the Pi2, that Wolfram thing which I cannot imagine any use for, the games etc. Having stripped the Pi down to essentials I then installed Webmin (and if you are not using this, why not?) – which by its nature needs PHP and MQSQL to run – and that’s fine as I have a use for both (graphing). When fitting the WIFI dongle to the board (who wants another wire) I realised I could do with the equivalent of the Windows WiFi properties window and stumbled on Conky – I’ll let you research that one. I then installed Mosquitto with some help from Mike at (and others) and finally with LOADS of help from the guys responsible for Node-Red, put that up (the installation is very different to my first attempt).

    With all of that in place and all the latest updates (courtesy of Webmin) I’ve barely used over 3.5GB which is excellent because as time goes on the size of the microSD will start to shrink if nothing else because these devices have limited lives and the more expensive chips have intelligence on-board to move data around as parts of the chip die. I’m hoping to get several years of life out of this when I’m done so the more spare capacity the better.

    So there we are, a clean start on which to build my ESP8266 and Arduino-based home control.