Niall’s virtual diary archives – Sunday 26 September 2021

by . Last updated .

Sunday 26 September 2021: 23:11. In the last post in my series on my future house build I had hoped to get a custom Home Assistant integration implemented this weekend for the custom logic I wrote in its proprietary language dScript for my keenly priced industrial grade scriptable PIC32-based Devantech dS3484 ethernet relay and input board. I can say that its learning curve was too steep for me to conquer this weekend – though I made excellent progress and it’s just a question of more time to finish it – but ultimately there is only so much completely new material that one can grok through and master after midnight, even after drinking coffee (I honestly can’t think of the last time I drank coffee so late in the day, but it really was needed if I was to make any decent attempt at all that Home Assistant programming documentation).

Anyway, once finished that will be the topic of a future post here in the series. This post is more about what prototyping and empirical testing needs to come next after the Devantech board. Firstly, I want to be clear that my experience with the Devantech board has been very positive. Yes its dScript language definitely could do with a lot more polish – there are a lot of weird things missing or syntax not permitted for no obvious reason – I suspect a single person designed and implemented it, and it has never received much feedback which was acted upon with respect to the programming language itself. Still, it’s definitely powerful enough to get almost any job done – for this Home Assistant integration I am writing I created a small TCPIP control server in dScript which was quite straightforward, as was getting it to push state change updates to any arbitrary third party IP address which registers with it. Implementing that part took only an hour or so, what I got bogged down in this weekend was Home Assistant programming itself.

Still, the Devantech board isn’t the right solution to some of the things I need solving in my future home automation. Firstly, the lack of high frequency PWM was a showstopper for dimming the LED cove lighting in a quality way – the maximum 1Khz software PWM just isn’t enough. Secondly, there are a bunch of sensors I want to use which either can’t fit into the Devantech board (any I2C for example), or aren’t well suited to the volt free digital inputs (though note that the bigger Devantech boards do offer switchable volt free or TTL digital inputs, and I now wish I had bought one of those instead). Finally, these boards are way overkill for use cases not involving > 7A current loads, and as I’d expect we probably will need at least one board per two rooms, that’s excessively expensive given they’re about €100 inc VAT including case each. What I really want is one or two Devantech boards for the high current stuff, and something much cheaper everywhere else.

Cue thus lots, and lots, and lots of research to figure out a solution which can do:

  • high frequency PWM LED strip dimming.
  • can read light switch digital inputs.
  • have an I2C bus for ‘is there a person in this room’ sensing.
  • maybe an ADC for analogue sensors like temperature.
  • LAN connected.
  • PoE powered.
  • either no Wifi at all (security risk/spectrum interference), or can act as a decent enough local Wifi AP, which means ‘can run OpenWRT and can at least do 2x2 11ac (Wifi 5, 866 Mbps) and have true Gigabit Ethernet’.

And obviously do all of this for as cheap as possible both in terms of money and my precious free time in wiring it all up, commissioning it, and maintaining it!

Survey of the market as of Q3 2021

I went through many options: all the Small Board Computers (Raspberry Pi, Rock Pi, Orange Pi, NanoPi etc), all the Microcontrollers (PIC32, ESP32, STM32 etc), dedicated PWM boards, adapting Wifi AP boards to do GPIO. So I can say something about the current cost-benefit balance and tradeoffs in the market as of Q3 2021.

Firstly, the absolute cheapest board capable of true Gigabit Ethernet and 11ac Wifi is the Orange Pi Zero 2 currently going for €30 inc VAT for the board and €5 inc VAT for a 16Gb sdcard = €35 inc VAT. However it’s an Allwinner H616 chipset, it’s not well supported, and there are big question marks over its Wifi implementation quality.

The second cheapest is the venerable Raspberry Pi 4 Model B for €40 inc VAT, assuming you can find one in stock (I could not). It is Broadcom based, which isn’t the best story for being an AP under OpenWrt but thanks to its huge popularity it has been made to work. It has 1x1 11ac Wifi. You need €5 inc VAT for a 16Gb sdcard = €45 inc VAT.

The next cheapest is the Radxa Zero currently for €50 inc VAT, it’s Amlogic S905Y2 based so it’s got really really good software support (e.g. OpenWRT ‘just works’ on this board) and its Wifi AP implementation is excellent. It also has 8Gb of onboard flash saving me needing a sdcard. However it lacks an Ethernet port though it does have USB3, so you’d need to add a USB3 Gigabit ethernet adapter for €15 inc VAT, so it costs €65 inc VAT which is getting close to the Devantech board, and we still don’t have 2x2 Wifi, only 1x1 Wifi.

The next cheapest is the Rock Pi 3A currently for €57 inc VAT + €5 inc VAT for a 16Gb sdcard = €62 inc VAT. This has gigabit ethernet and two PCIe M.2 slots, so you can fit your own 2x2 Wifi card which is certainly another €20 inc VAT if you want Wifi 5 or another €40 inc VAT if you want Wifi 6, so now you’re at €82 - €102 inc VAT.

Add in a Gigabit PoE splitter for €15, and €100 per room is rather a lot of money when there are many rooms in the house.

So what about instead fitting fewer but better APs e.g. the TP-Link Mercusys MR70X is a 2x2 Wifi 6 AP with three gigabit ethernet ports for €45 inc VAT with an Amlogic chipset already with early OpenWRT support, share one of those between a few rooms each, and then per room fit something much much cheaper for the lights and sensors?

The choices for further testing

Out of all the microcontrollers and small board computers I calculated all the bits which would needed and these two are what I have chosen for further investigation:

  1. Microcontroller approach:

    • STM32F401: 84Mhz ARM Cortex M4F, 64Kb RAM, 256Kb flash. €5 inc VAT delivered. Nicknamed ‘the Black Pill’.
    • W5500 ethernet connected by SPI. €7 inc VAT delivered.

    Total: €12 inc VAT each

    Advantages:

    • Up to four hardware PWMs capable of > 20 Khz frequency, or up to five SPIs, or up to three I2Cs, or up to seven 12-bit ADCs, or up to twenty digital inputs or outputs.
    • No OS nor filesystem, so hard realtime, once debugged maintenance likely will be very low.
    • Arduino ecosystem available via STM32duino.
    • Cortex M4 is ARMv7 and there is even hardware single precision floating point, which is nice as you can avoid fixed precision integer arithmetic for say percentage calculations.

    Disadvantages:

    • I really can’t think of any, though I may find some once I actually use the board, and it’s been a LONG time since I last did work on a truly bootstrapped embedded microcontroller.


  2. Microcomputer approach:

    • Raspberry Pi Zero: 1Ghz ARM11, 512Mb RAM. €6 inc VAT.
    • USB2 to Fast Ethernet adapter. €9 inc VAT.
    • 16Gb micro sdcard for storage. €5 inc VAT.

    Total: €20 inc VAT each

    Advantages:

    • Up to two hardware PWMs capable of 250 Mhz frequency, or up to two SPIs, or up to one I2C, or up to twenty-four digital inputs or outputs.
    • Camera connector. And the GPU can hardware encode to h.264!
    • Full USB stack on USB ports, so anything USB is available to you.
    • Half the current cost of the cheapest Raspberry Pi with ethernet, the Raspberry Pi 1 Model B+ currently going for €30 inc VAT.
    • Passable hardware single and double floating point, which Raspbian jumps through a lot of hoops on your behalf to make it appear seamless.
    • Full Debian, so I have plenty of up to date experience using this.

    Disadvantages:

    • Despite how astonishing it is that you can nowadays get a full computer with full fat Linux for €20, it still costs 67% more than the Microcontroller option. Multipled many fold, that adds up.
    • No ADCs so no analogue inputs possible without additional hardware.
    • ARM11 (first gen ARMv6) is really ancient. Like twenty years old now. It’s full of weirdness and quirks, though Raspbian does a fabulous job at mostly papering over the cracks.
    • Runs a proper Linux and so will need security updates etc and other ongoing maintenance. It also won’t like sudden power loss much.

Why not the alternatives?

Why STM32F4 instead of Arduino? It’s simply a price-performance thing – that STM32F4 runs rings around an ATmega CPU, having orders of magnitude more RAM, Flash, performance, and being 32-bit not 8-bit, and having a modern C++ toolchain instead of a proprietary partial standard library compiler. Yes it costs 25% more – the STM32F4 is €5 delivered versus €4 inc VAT delivered for the ATmega – but am I going to sweat an extra euro here for how much more you get? No.

Why STM32F4 instead of ESP8266? For €3 inc VAT delivered – even less than the ATmega – you can get a comparable specification ESP-12E dev board, and for €2 more it can come with a really nice breakout adapter board which saves lots of hassle soldering. It also comes with built-in wifi and bluetooth, though only 2.4Ghz 11n, and I really have to hand it to Espressif that they’ve really thought through making it as nice as possible for people like me yet cheaper priced and better specced any other alternative. However whilst comparable in specification, there are fewer GPIOs, ADCs, I2Cs and so on which means I’d need to fit more them with more PoE power adapters which makes them more expensive overall.

Why STM32F4 instead of ESP32? This is much tougher to explain, because in most ways the ESP32 is far superior. Historically ESP32 cost a touch more and STM32 a touch less, so its better feature set was somewhat proportional to the cost increase. Recently though they’re both about €5 inc VAT delivered, and in most ways it’s not a fair fight: 3x faster clock speed, 2x more cores, 8x more flash, 8x more RAM, AND 11n Wifi and Bluetooth v4.2, plus up to sixteen PWMs, or up to five SPIs, or up to four I2Cs, or up to eighteen 12-bit ADCs, or up to thirty-four digital inputs or outputs – the ESP32 is a whole lot of microcontroller for the money.

But despite on feature set alone not being a contest, the STM32F4 still has a few big advantages:

  • It is ARM which has an enormous ecosystem, not Xtensa DSP which has not. This matters a lot – all my sensors have code examples, drivers or python modules written and tested on either ARM or ATmega, and usually both. Getting all of those working well on Xtensa with the current state of its ecosystem seems unlikely.

  • ESP32 makes better a lot of the quirks and surprises that were in ESP8266, but they’re not entirely gone. For example it still has non-linear and unstable ADCs, albeit less non-linear and unstable than the ESP8266, but you still get crappy resolution at the bottom and top i.e. they’re range constrained, a classic sign of a not fully mature solution, and they also experience a good bit of sampling and thermal noise normal ADCs do not. And it’s not just the ADCs which are quirky, ultimately Espressif just aren’t quite up there with the big boy league in terms of quality and predictability yet. Don’t get me wrong, they are constantly getting better, and every new product generation is another leap forward. But you’ll still be fighting their lack of ecosystem depth which just isn’t an issue if you’re on ARM.

  • Ultimately I don’t actually need all those features – I don’t have enough stuff to connect into them without running more cable than it’s worth. The STM32F4 has enough i/o for my use case, it also has enough RAM and enough flash. It’ll do.

If one day the ESP32 + RJ45 ethernet boards currently costing €15 inc VAT delivered came down to around €8 inc VAT, then I might see myself using ESP32 for various solutions e.g. it would be quite unbeatable for controlling irrigation in a greenhouse where accuracy isn’t so important. But at €15 the STM32F4 + RJ45 easily beats it at €12, even with the hassle of soldering and wiring.

Finally why the Raspberry Pi Zero? To be honest there is no close competitor at all in this choice of microcomputer – nothing else comes close to the bang for the buck you get for €6 inc VAT. Sure it’s two decade old and is quirks-ridden technology, but its ecosystem support is very, very deep and very, very wide. Even its Wifi edition which in my opinion is terrible value for what you get for doubling its price still has few good competitors for that price – the quality of the boards and their ecosystems on the market between the Zero W for €12 inc VAT and the Raspberry Pi 3 Model B for €30 inc VAT I don’t think are worth the money saved in terms of added hassle and working around poor QoI e.g. buggy USB stacks making your USB Ethernet dongle not work quite right.

Hybrid approach

You’re probably gathering that I’m currently favouring the Microcontroller approach, but I in fact expect to adopt a hybrid approach. Why? Well it’s that camera connector on the Raspberry Pi Zero, it means that I can fit to it a 5MP OV5647 camera with infra red lights for €10 inc VAT including delivery, thus saving me having to buy Hikvision cameras each costing €80-100 or so. Anywhere which is not a bedroom nor bathroom will have multiple cameras looking at it. This is partially for security, but it’s mainly because I believe I can estimate human motion and coordinates within a three dimensional reconstruction by combining images using something like https://github.com/alyssaq/reconstruction. This, in turn, means I don’t need to mount – and more importantly, not need to wire – distance sensors running along the walls to detect where people are within a room, and thus adjust the brightness of the lighting in that locality in order to conserve power (as my LED strips will consume rather a lot of power if fully bright).

There are, annoyingly, STM32F4 models also with a camera connector, but I don’t think the Chinese sell cheap clones of them yet. Also, even if I had a STM32 here, I very much doubt a ~200Mhz ARM CPU can do any meaningful compression on a 5MP video feed before it needs to be sent out on a SPI connected Fast Ethernet port, which at best probably can’t push more than 40Mbit, so that’s a max resolution of 1280x1024 @ 30fps (and likely less in practice). And even then, your Gigabit network will rapidly saturate with lots of 40Mbit video streams running along it. Whereas the original Raspberry Pi (which has the exact same SoC as the Zero) has h.264 hardware encoding, and for which there is a nice Python library to have the camera and video encoding acceleration hardware cooperate and write the output to a socket. It should be able to make at least Full HD @ 30fps for 8Mbit or so, and that seems to me a nice cost saving over standalone Hikvision cameras.

Thus, I expect Raspberry Pi Zeros in the public spaces wherever I also need a camera (approx three per space for triangulation), and Black Pills everywhere else including outdoors as they’re cheap and replaceable enough that I don’t need to care too much about them getting damp or full of spiders for example.

Conclusion

I have placed an order for both and a whole load of sensors for me to experiment with. Finding a Raspberry Pi Zero without wifi in stock for something approximating its proper price was tough, I ended up going with German BerryBase and they got it delivered with a bunch of sensors and cables where it was cheaper to get it from them than Aliexpress within a week for a good price. For the Black Pill and all the other sensors where after postage it was still cheaper, I went with Aliexpress and that’ll likely be in transit for a good few weeks yet.

After the next post likely on the custom Home Assistant integration, I’ll start testing sensors with the Raspberry Pi Zero and I’ll no doubt write a post on my experiences with each of those named by exact model number (I did a ton load of research choosing each of them very carefully). However next weekend I also intend to take a break from staying up until 4am doing this stuff, because I’m getting a bit sick of feeling like crap every weekend, and I’d like a break from the slog of it.

#house




Go back to the archive index Go back to the latest entries

Contact the webmaster: Niall Douglas @ webmaster2<at symbol>nedprod.com (Last updated: 2021-09-26 23:11:39 +0000 UTC)