Amazon Dash, Node Red and Scargill’s ESP code

The Journey

So I was excited to hear that Amazon was finally going to be selling the dash buttons in the UK, and being a prime member, I promptly ordered two. I am now the proud owner of a finish and an andrex dash button.

My goal of buying the dash button was to be able to use it as an IOT button as many people have done in the past and considering it had been out in the US for ages, I thought the process would be fairly simple. Unfortunately I was wrong.

The first thing any sane person would do is check out the node database to see if there was a node available that had already been written. Success! There is a node available, so after installing all of the prerequisites (libpcap-dev), I installed the node, restarted the node red service and logged in. I dragged the node in and attached a debug node to the output to catch any messages. After configuring the node with the buttons MAC address, I pressed deploy and … I got an error straight away telling me that there were no buttons to listen to. Odd. I’d configured the MAC address correctly and that was the only option available but it still wouldn’t work. I contacted the nodes author and he also couldn’t help work out why I was getting the error. I even reinstalled my setup (using Peter Scargill’s install script) and only installed that node and it still wouldn’t work.

Next step, I hunted around for another node that would allow me to capture the ARP messages from the amazon dash button. I stumbled upon a node called PCAP. I tried to install this node but all it did was fail on install. I didn’t want to install anything that was going to be ran as a custom script that wasn’t fully controllable through node red, so I had to come up with something else.

The Solution

I instead logged into my router and gave the MAC address (after finding it using node dash button as describe in that link) a static IP address. This way I would always know what IP address the button would have. I then added the ping node. I set the ping node to ping the IP address every 2 seconds, which I found to be enough to determine whether the button had been pressed or not. This setup meant no extra nodes were required and it was all controllable through node red.

Controlling a Lamp

Now as you may have guessed from a previous post, I use Peter Scargill’s ESP code on my Sonoff controllers. So far, these have been used to control table lamps to come on at dusk and go off later in the evening. But one thing my wife didn’t want to do was “find my phone, to unlock it, to open an app, to press a button on the screen all to just turn on a light” when she wants to override the Timer controlHence the need for the amazon dash button.

So my next task was to be able to control the lamp with a single push button, whilst node red knew whether the lamps status and would turn it off when it’s on and vice versa.

I eventually came up with this flow.

flow

[{“id”:”46ae5f70.e7708″,”type”:”inject”,”z”:”9d87bc8.12f194″,”name”:””,”topic”:””,”payload”:”auto”,”payloadType”:”str”,”repeat”:””,”crontab”:””,”once”:false,”x”:350,”y”:200,”wires”:[[“47b3b39e.0b578c”]]},{“id”:”b1114460.7b54b8″,”type”:”inject”,”z”:”9d87bc8.12f194″,”name”:””,”topic”:””,”payload”:”on”,”payloadType”:”str”,”repeat”:””,”crontab”:””,”once”:false,”x”:350,”y”:240,”wires”:[[“47b3b39e.0b578c”]]},{“id”:”3d0ca059.0c2e”,”type”:”inject”,”z”:”9d87bc8.12f194″,”name”:””,”topic”:””,”payload”:”off”,”payloadType”:”str”,”repeat”:””,”crontab”:””,”once”:false,”x”:350,”y”:280,”wires”:[[“47b3b39e.0b578c”]]},{“id”:”47b3b39e.0b578c”,”type”:”bigtimer”,”z”:”9d87bc8.12f194″,”outtopic”:””,”outpayload1″:”{out12:1}”,”outpayload2″:”{out12:0}”,”name”:”Big Timer”,”lat”:””,”lon”:””,”start”:”sunrise”,”end”:”sunset”,”starttime”:”5004″,”endtime”:”0″,”startoff”:”0″,”endoff”:”5″,”outtext1″:””,”outtext2″:””,”sun”:true,”mon”:true,”tue”:true,”wed”:true,”thu”:true,”fri”:true,”sat”:true,”jan”:true,”feb”:true,”mar”:true,”apr”:true,”may”:true,”jun”:true,”jul”:true,”aug”:true,”sep”:true,”oct”:true,”nov”:true,”dec”:true,”suspend”:false,”random”:true,”repeat”:false,”atstart”:true,”x”:520,”y”:300,”wires”:[[“713a4259.39f3dc”],[“7e1401c6.c4a6e”],[]]},{“id”:”713a4259.39f3dc”,”type”:”mqtt out”,”z”:”9d87bc8.12f194″,”name”:””,”topic”:””,”qos”:””,”retain”:””,”broker”:””,”x”:690,”y”:240,”wires”:[]},{“id”:”eaac1d79.b36ac”,”type”:”ping”,”z”:”9d87bc8.12f194″,”name”:”Dash Button 1″,”host”:””,”timer”:”2″,”x”:170,”y”:360,”wires”:[[“b1d29e64.73c1b”,”3bce536b.43476c”,”3bd1ca92.e72e46″]]},{“id”:”b1d29e64.73c1b”,”type”:”debug”,”z”:”9d87bc8.12f194″,”name”:””,”active”:false,”console”:”false”,”complete”:”false”,”x”:350,”y”:320,”wires”:[]},{“id”:”37b52a64.27e5b6″,”type”:”debug”,”z”:”9d87bc8.12f194″,”name”:””,”active”:false,”console”:”false”,”complete”:”false”,”x”:1010,”y”:360,”wires”:[]},{“id”:”88c16401.8a45a8″,”type”:”change”,”z”:”9d87bc8.12f194″,”name”:””,”rules”:[{“t”:”set”,”p”:”topic”,”pt”:”msg”,”to”:”Button”,”tot”:”str”}],”action”:””,”property”:””,”from”:””,”to”:””,”reg”:false,”x”:700,”y”:400,”wires”:[[“6392cb74.15abb4”]]},{“id”:”a7d80525.a2ac38″,”type”:”function”,”z”:”9d87bc8.12f194″,”name”:”Function”,”func”:”context.lampstatus_lounge1 = context.lampstatus_lounge1 || ‘initiallamp’;\ncontext.buttonstatus = context.buttonstatus || ‘initialbutton’;\n\nif (msg.topic == ‘Lounge_Lamp1’) {\n\tif (msg.payload == ‘0’) {\n\t\tvar lampstatus1_lounge1 = ‘0’;\n\t}\n\telse if (msg.payload == ‘1’) { \n\t\tvar lampstatus1_lounge1 = ‘1’;\n\t}\n\tif (context.lampstatus_lounge1 == ‘initiallamp’) {\n    context.lampstatus_lounge1 = lampstatus1_lounge1;\n    }\n    else if (context.lampstatus_lounge1 != ‘initiallamp’) {\n    context.lampstatus_lounge1 = lampstatus1_lounge1;\n    }\n}\n\nif (msg.topic == ‘Button’) {\n\tif (msg.payload == ‘0’) {\n\t\tvar buttonstatus1 = 0;\n\t}\n\telse if (msg.payload == ‘1’) { \n\t\tvar buttonstatus1 = 1;\n\t}\n\tif (context.buttonstatus == ‘initialbutton’) {\n    context.buttonstatus = 0;\n    }\n    else if (context.buttonstatus != ‘initialbutton’) {\n    context.buttonstatus = buttonstatus1;\n    }\n}\n\nif (context.lampstatus_lounge1 == ‘0’ && buttonstatus1 == ‘1’) {\n    msg.payload = \”on\”;\n    return msg;\n}\n\nif (context.lampstatus_lounge1 == ‘1’ && buttonstatus1 == ‘1’) {\n    msg.payload = \”off\”;\n    return msg;\n}”,”outputs”:1,”noerr”:0,”x”:1000,”y”:400,”wires”:[[“4d0f715.40d149”]]},{“id”:”29f48606.40587a”,”type”:”rbe”,”z”:”9d87bc8.12f194″,”name”:””,”func”:”rbe”,”gap”:””,”start”:””,”inout”:”out”,”x”:850,”y”:360,”wires”:[[“37b52a64.27e5b6″,”a7d80525.a2ac38”]]},{“id”:”3bce536b.43476c”,”type”:”switch”,”z”:”9d87bc8.12f194″,”name”:”false”,”property”:”payload”,”propertyType”:”msg”,”rules”:[{“t”:”false”}],”checkall”:”true”,”outputs”:1,”x”:330,”y”:360,”wires”:[[“fa4afff0.66977”]]},{“id”:”3bd1ca92.e72e46″,”type”:”switch”,”z”:”9d87bc8.12f194″,”name”:”true”,”property”:”payload”,”propertyType”:”msg”,”rules”:[{“t”:”gt”,”v”:”0″,”vt”:”num”}],”checkall”:”true”,”outputs”:1,”x”:330,”y”:400,”wires”:[[“a671da0f.89fef8”]]},{“id”:”fa4afff0.66977″,”type”:”change”,”z”:”9d87bc8.12f194″,”name”:””,”rules”:[{“t”:”set”,”p”:”payload”,”pt”:”msg”,”to”:”0″,”tot”:”str”}],”action”:””,”property”:””,”from”:””,”to”:””,”reg”:false,”x”:490,”y”:360,”wires”:[[“88c16401.8a45a8”]]},{“id”:”a671da0f.89fef8″,”type”:”change”,”z”:”9d87bc8.12f194″,”name”:””,”rules”:[{“t”:”set”,”p”:”payload”,”pt”:”msg”,”to”:”1″,”tot”:”str”}],”action”:””,”property”:””,”from”:””,”to”:””,”reg”:false,”x”:490,”y”:400,”wires”:[[“88c16401.8a45a8”]]},{“id”:”6392cb74.15abb4″,”type”:”rbe”,”z”:”9d87bc8.12f194″,”name”:””,”func”:”rbe”,”gap”:””,”start”:””,”inout”:”out”,”x”:850,”y”:400,”wires”:[[“a7d80525.a2ac38″,”37b52a64.27e5b6”]]},{“id”:”4d0f715.40d149″,”type”:”change”,”z”:”9d87bc8.12f194″,”name”:””,”rules”:[{“t”:”set”,”p”:”topic”,”pt”:”msg”,”to”:””,”tot”:”str”}],”action”:””,”property”:””,”from”:””,”to”:””,”reg”:false,”x”:480,”y”:440,”wires”:[[“47b3b39e.0b578c”]]},{“id”:”7e1401c6.c4a6e”,”type”:”change”,”z”:”9d87bc8.12f194″,”name”:””,”rules”:[{“t”:”set”,”p”:”topic”,”pt”:”msg”,”to”:”Lounge_Lamp1″,”tot”:”str”}],”action”:””,”property”:””,”from”:””,”to”:””,”reg”:false,”x”:700,”y”:360,”wires”:[[“29f48606.40587a”]]}]

This basically takes a reading from the amazon button every 2 seconds but only passes the results on if they change. It then changes the reading (from false or a number) to 1 or 0. This is then passed through a function that compares this value from the button to the value from big timer. If the light it on it pass the word off to the timer and vise versa.

My next challenge is to control more than one lamp with each button, which won’t be hard to do, I’ll just have to decide what to do of only one of them is on. Do I go both on or both off first?

Eclipse Neon Build Environment for Peter Scargills ESP Code- Windows Only

One of the things I have been playing with recently are ESP8266’s.

When I first started to use these little wifi units, I was using some software called ESPEasy. Its a great piece of software that installs onto an ESP module using either the node flasher software (which I will cover further down) or using the arduino IDE. Once configured, it is then possible to use the built in over-the-air capability to keep the software up to date. The software has a number of different types of sensors built in with a large array of plugins also available.

In conjunction with ESPEasy I was also using Openhab as the main brain of the system. All in all it seems like a very capable system if you have time to learn all about the XML files etc that are required to be edited to get it up and running. But for me, having a young family and studying for a master’s degree, it was something that I didn’t really have time to get my head around.

So I started to look about and see what other alternatives I could find. This led me to Peter Scargills work. He uses a combination of node-red and MQTT to control his own software he has written for the ESP8266’s.

To use it, you could rely on Pete’s prebuilt ROMs and update it as and when he compiles a new version. But if you’re like me and want to be able to roll your own, it can be difficult to get the build environment up and running if you don’t know what you’re doing. Hopefully this guide will get you all setup and ready to go.

This guide is based on using a computer that is running windows 10 x64. It also uses the latest version of Eclipse, the espressif SDK and the latest Java JDK.

First, download Eclipse Neon from this link Eclipse Neon Link. Once downloaded, run the installer. The following screen willbe displayed.

Eclipse Installer

Select Eclipse IDE for C/C++ Developers as shown above. Then click install and accept the terms and conditions. Once installed, close the installer without launching eclipse.

As noted in the comments by Nigel Kendrick, python 2.7 is also required (I already had this installed so didn’t notice the requirement). The latest version can be found here. Please download and install.

Now navigate to CHERTS Thread over on the ESP forums as we need to get some of his files to allow the environment to be set up correctly. Download files number 1 (I personally downloaded the 2.1.0 beta version, but the older 2.0.9 version should also be fine), 4 and 5. Also download the latest Java JRE from this Link and the latest espressif SDK (currently 2.0.0) from this Link.

Install the espressif devkit from CHERTS, the mingw install (ensuring you unselect the gui interface) and the java JRE.

Extract the espressif devkit addon. Right click on the .bat file and select edit. For me at least, the file doesn’t run in windows 10 so we need to remove some checks from the file first. Remove the following text from the batch file and the save it.

net session >nul 2>&1
if not %ERRORLEVEL% EQU 0 (
echo Error: To install packages need admin rights.
echo.
echo Run the script as an administrator.
echo.
echo To run as administrator, click on the file install.cmd the right mouse
echo button and select ‘Run as administrator’
echo.
echo Press Enter to exit.
pause >nul
goto :eof
)
echo Press Ctrl-C to cancel, Enter to continue
pause >nul
cd /d %~dp0

Right click on the .bat file again and click run as administrator. Allow the .bat file to install all of the required addons. Press enter when asked and the window will close.

Extract the espressif SDK and then cut the ESP8266_NONOS_SDK to C:/Espressif. Rename the ESP8266_SDK to ESP8266_SDK_BAK and rename ESP8266_NONOS_SDK to ESP8266_SDK.

Now open the control panel. This can be achieved in Windows 10 by right clicking on the start button and selecting control panel. Select “System and Security” and then “System”. In the left hand section, select “Advanced System Settings”. On the window that appears, select “Enviroment Values”. On the window that appears, ensure that “path” is selected in the variable column and then press the top edit button. Add the following locations to the variables

c:\MinGW\bin

c:\MinGW\MSYS\1.0\local\bin

c:\MinGW\MSYS\1.0\bin

Its now time to download Peter Scargills esp code. There are two ways you can do this. Either download the complete repository which would be a snapshot of the repository at that point in time, or you can create your own bitbucket account, take a fork of Peter’s repository and then keep it up to date by merging his commits into your fork. I personally have a fork that I keep up to date with his commits. The software I use to do this is GitKracken. Seems to work better than Atlassians SourceTree.

So assuming you have Pete’s code somewhere accessable, you also need to download one more file before we can begin. His code references esptool2.exe, which we need to place in c:\esptools for the software to compile. The link to the precompiled version can be found on this page.

Now launch eclipse.

Click “file” and then “open projects from file system or archive”. Click the directory button and navigate to the folder containing Pete’s code. Then click finish.

I usually right click on the main hackitt_mqtt_dev folder in the project explorer in the code and then click clean project. Then right click and build project. Everything should then compile correctly.

So now just follow Pete’s instructions found here to load the code

Update: Added requirement for Python 2.7 as noted in the comments