Micropython and Node-RED

I built the project from this post on chunk of stripboard, and have been testing it out. A quick tour from upper left:

  • A 3 pin (ground, TX, RX) header for the USB to serial (FTDI) adapter.
  • Below that is the ESP-01 breakout.
  • Almost hiding under the ESP-01 are two resistors that form a voltage divider to take the +5V square wave from the 555 down to +3V.
  • On the bottom left is a single WS-2812 (probably a Sparkfun item?). Basically, a NeoPixel
  • At the top in the middle is a voltage regulator that takes the +5V from USB power down to +3.3V for the ESP-01.
  • Below that is a two position terminal where +5V comes in, and is distributed to the NeoPixel, the regulator, and the 555 circuit.
  • In the middle to the right is the 555 chip (the black DIP-8 package), and the resistors and caps that form the square wave. Yes, everything here should be MUCH closer together. I’m sure if I put a speaker on it this thing would pick up AM radio.
  • One the far right are the two screw terminals for the two magnetic switch sensors.

It seemed to work, so the next step was to get the data into something useful. I did have it logging to a Go service and to a node.js service, but to do ‘real work’ with the data but without starting from scratch I installed Node-RED and after a day of futzing around I had some nice data and graphs.

But first, the key point to keep in mind is that Node-RED can consume JSON via the HTTP In node. That means that all I did in Micropython was this:

r = urequests.post("", json={'ak': 'apidummykey', 'name': gDict['nn'], 'state': newState, 'tLow': tLow, 'tHigh': tHigh, 'trig': trig, 'mem': gc.mem_free()})

And Node-Red sees any posted JSON data in the msg.payload object, and can manipulate or display it as you like.

I think the other thing that is super-important, if it didn’t jump out at you yet, is this is plain HTTP, not encrypted HTTPS. I did a quick test with HTTPS, but it’s going to take a lot more optimization to get enough free memory, and probably some finesse with the watchdog timer (which kept killing the code when it connected to an HTTPS node.js server). The easy answer is to move to an ESP32 based board; it has enough memory and features to make it a good default choice, and the price is right (about $11 retail, $7 eBay right now).

The dashboard in Node-RED – it’s pretty powerful right out of the box; below is the dashboard for just this one project. The color of the main circular status widget and text mirrors the color of the NeoPixel on the board. I also added some debug information, to tune up the logic & parameters I use to determine door position:

There is a lot going on here:

  • I used a circular dashboard gauge to show the position of the door; this puts a large, colorful object in plain view. A closed door is green, during door motion the color changes to blue, and when it’s fully open it’s red.
  • A small graph shows the historical door position, so you know if the door was recently opened or not.
  • There is text for the door position (‘Ajar’, in this example, because it’s not hooked up to the magnetic switches yet)
  • The Sensor Status uses a timeout node, and if it’s not fed with data from the ESP-01 often enough it changes from Online to Warning to Offline (and the color changes, too).
  • The Raw Data section shows a two-line graph representing the 555’s square waves average high and low durations. This is the average number of milliseconds that GPIO0 is in each digital state (low or high). You’ll notice a large, mostly flat spot overnight where something bad happened; the node seemed to stop sending JSON information via TCP (but I was getting errors in a Go log service that are sent via UDP). It took a full power cycle to recover; restarting the code didn’t help.
  • And lastly, a graph of free memory…

I added the free memory graph this morning, as I suspect that not closing the sockets correctly was the issue, so it was eventually running out of resources. I put in a few s.close() calls, added the data for memory to the JSON status message, and I’m currently watching it like a hawk. It might be fixed, but I won’t know until tomorrow. But if it works I’ll pop it into a plastic container of some sort, and mount it for a few months of actual use.