Recently I blogged about an update to pricing for the Sonoff mains control units – now down to £3.50 ($4.85) and our readers even reminded us of some potentially great software for those units (as well as my own but then mine is more of a kitchen sink approach and the Sonoffs have only 1Meg of Flash so you lose OTA). Lots of comments – all good.
Updates at the end – slowly cracking the GPIO – got some I/O working!
Meanwhile, you’ll have noted elsewhere on the blog that I’ve found a fabulous, hassle-free battery backup solution in the Ravpower units.
Much MORE Cheapness: And now…. a possible CHEAP alternative to Raspberry Pi for a central controller.
I know – heard it all before… so have I but read on…
As some of you know, I’ve pretty much settled on Debian as the base for a central controller using Node-Red, SQLITE, Mosquitto MQTT and Apache/PHP (to run the likes of PHPLiteAdmin) and I’ve done that based on a Raspberry Pi2/3 for a number of reasons – including working 3.5mm audio, speed, ease of use, support, hardwired network (I think the central controller should be hardwired as it has to be rock-solid), less to learn and… well you can read all my reasons elsewhere.
Well, I’ve been experimenting with the latest FriendlyArm units and one of them stands out a mile as a possible contender for cheapest central controller (Pi Zero is out as it does not have an Ethernet connector, audio connector etc and by the time you add in connectors and the other stuff it is no longer cheap).. How about a Pi lookalike that is SMALLER than a Pi, for well under £10 + post – hence when put together forming what HAS to be the cheapest overall IOT setup on the planet??
Check out the FriendlyArm NanoPi-M1 – a small, square ARM-based unit with the following Spec (you can read the full spec on their site – these are just the important bits)
- Allwinner H3, quad-core Cortex A7 running in this case – JESSIE
- Mali 300 graphic chip
- 512MB RAM (1Gig available)
- 10/100 Ethernet
- 3.5mm or HDMI audio
- IR Receiver
- USB * 3
- HDMI out
- Camera interface
- Debug serial and GPIO
They are the important details – what IS equally important for me is that it works with my installation script – hence the whole setup as described above can be done easily in maybe a couple of hours. https://bitbucket.org/snippets/scargill/KqgAe – you should be familiar with running scripts before tacking this – this one was meant for the Pi – not the M1 – and you absolutely must read the comments as the script is general in nature – for the M1 you need to create the Pi user and groups manually (easy – commented in the script just to keep everything in one place) and set up the hostname (2 files ) to whatever you want – then copy the script over and give it the extra permission before running. I’ve just got it working and can confirm that SQLITE and PHPLITEADMIN work (hence Apache and PHP), Node-Red, Webmin and Mosquitto all work and in Node-Red I can access THREE UARTS – the first one of four is for debug so you can’t rssily get to that but it beats having to disable it as on the Pi. I can access Node-Red as nanopim1:2880
Not touched GPIO but that’s an issue for most non-Pi boards… hopefully someone can tackle that and make the GPIO available within Node-Red. This is looking REALLY good.
Audio works – but (and I had this on the Orange Pi PC) there is a very short burst of sound on the end of the IVONA recording – it has to be some kind of special character getting through – it is most definitely NOT the MP3 playback and when Ivona processes the file name it adds nothing on…. anyway, something for the weekend as they say along with polishing this blog up – for now – it seems like it might be possible to make a minimal IOT solution with a central processor and some mains controllers for under 20 quid!!!!! Stunning.
As I was writing this, and as the Debian image available with the M1 is only 4GB long – I was awaiting info from FriendlyArm as to how to expand as you might find with a Raspberry Pi to the full size of the SD. There are solutions out there and if you’re going to live forever there’s plenty of time to understand most of them.
FriendlyArm came back to me this morning with a simple solution. My 8GB card was sitting with half of it’s space unavailable. Support sent me some instructions for sending data over the serial debug port – but it turned out this was just exactly the same as I could achieve with a terminal.
it was suggested I reboot – and lo – the full size of the SD became available to me – 8GB. I wondered if I could push my luck – so I used Win232DiskManager to make a copy of the 8GB disk…. and then copied that image to a 16GB SD. I installed the 16GB SD into the M1 – powered up and tried the same again.
I have to say I was not expecting miracles – but the results were the same and after a reboot I had my normal 16GB SD setup, no problem.
You could of course install Armbian for the M1 which automatically resizes to fit the SD but I’ve not yet tried this to make sure my stuff works so you’re on you’re own there – I did install it and it does work as a desktop complete with Libre Office etc. if that’s your thing.
Lovely – and so here is a cheap way to get Node-Red and other tools running pretty much as if you were on a Raspberry Pi – except of course for GPIO. None of the usual Pi tools are available – I’ve have settled for the command line PIGPIO or similar as you can call that from a node – but no – there still remains a pretty uncomfortable learning exercise perhaps with some compiling to get control over the GPIO. In this instance I don’t need GPIO so its not a problem but I do with the designers of these systems would recognise that Raspberry Pi has set the pace.
I have tested taking their MATRIX examples of C code – and loading them into the M1 and compiling using the GEANY editor that comes with the Debian setup – almost – and by copying all of the C files into one and adjusting include paths, I HAVE compiled some GPIO control that a ROOT user can use – but I just don’t know enough about the MAKE files to alter the path to do this properly.
Still – the point here is – the board seems to work well and is a CHEAP solution. When I think of the hassle I had trying to get an Orange Pi to do this….
Benchmarks: So I guess what might be on your mind is… and so this cheap board – how does it compare to the Raspberry Pi 2 and 3? I only had a 2 handy for testing and some tests too longer, some less, overall the M1 was MARGINALLY faster than the Pi2 – I would go so far as to say they are comparable.
The test I’m running incidentally is a simple one – so you know..
To install (as root): apt-get install sysbench
To run (as root): sysbench –test=cpu –cpu-max-prime=20000 run
The M1 as an infra-red input which I’ve not tested – it would be nice if someone got that working with Node-Red.
GPIO Update: I was looking for stuff on GPIO on the M1 when I spotted in a forum the chap who developed Armbian talking – and he mentioned that the hardware in the M1 is the same as the Orange Pi One. Well, off I went in search of GPIO for THAT board.
I found this.
Now – it still needs ROOT access which is a pain in the bum… but… I followed the instructions – as ROOT user – but in my /home/pi directory I’d created…. I went to the examples – and ran the one called blink_led.py – nothing – I tried every pin on the connector. I figured nothing gained, nothing lost – so I change the port (led=port.X) to PA9 – LO – a flashing LED – PA19 (purely at random) – LO – another flashing light.
So a lookup table is needed to get the ports to match those on the M1 – and there has to be a way to avoid ROOT access as I want to call this stuff in Node-Red – but GPIO – works. I’m sure there will be other stuff for the Orange Pi One by now – worth checking. I guess what is needed is some kind of daemon running as root user that an exe file running as a normal user can fire parameters at. I’m sure someone cleverer than I could adapt PIGPIO or similar.
Anyway – there you go – just about everything a Pi2 does – and 3 fully usable UARTS and now – some GPIO. All for a fraction of the cost of a Pi2. Got to be worth investigating.
and THEN I discovered THIS…..
I typed as per the link:
The prompt returned as if nothing had happened – but then…. I noted on refresh that/sys/class/gpio_sw directory had magically appeared.
echo 1 > /sys/class/gpio_sw/normal_led/data
That’s one of the examples in that link – erm… NOPE it didn’t work…. no such thing.
So I gave this a try
echo 1 > /sys/class/gpio_sw/PA9/data
YUP – GPIOA9 on the connector lit up.
Sadly again – this stuff would not work for user PI but by the morning I’d pieced together that you need that “data” to have write permissions for other than the OWNER!
As ROOT – this works.
/bin/chmod -R 0666 /sys/class/gpio_sw/*/data
So I put that into the /etc/rc.local file just before “exit 0”, saved and rebooted. LO and behold – it all works as user Pi !!! I can now at least do simple GPIO output from Node-Red using an EXEC node. Well, it’s a start!
I’ve tested the Pxx items available in /sys/class/gpio_sw and the ones that seem to work AND are on the connector include: PA0,PA1,PA6,PA7,PA8,PA9,PA21,PG6,PG7,PG8,PG9. I have avoided touching the UART pins as I think I’d rather have the UARTS available.
Of course at this point I’ve no idea how to READ one of those pins.
WiringPi and GPIO: I did originally try the adaptation of WIRINGPI here and hence the GPIO command line utility – which didn’t seem to object to any commands – but on the other hand didn’t do a THING with them.
Then armed with my successes above, I ran
This produced a list of states of pins with no resemblance to what I was using – but as I toggled PA9 on and off I noted a change from the readall command on GPIO.23.
Could it be:
gpio write 23 1
YES – it turned on PA9.
gpio read 23
YES, it returned the value.
gpio mode 23 in
The light went out and I realised that floating the pin would return 1, grounding the pin would return 0.
I looked for more info on WiringPi and found this – https://projects.drogon.net/raspberry-pi/wiringpi/the-gpio-utility/
With my pin set as an input – the pullup modes worked too.
gpio mode 23 up/down/tri
It turns out that what I was calling PA6 is called GPIO.7 in the WiringPi setup – and this is a PWM pin – could I be so lucky?
No. No matter what pin number I tried – setting PWM mode was not having it. Still – input and output – that’s a step up!! It also turns out that the second part of my rc.local work is NOT needed for WiringPi – just the first line (ie. the modprobe bit).
- Pin 40 GPIOA14 gpio write 16 on
- Pin 38 GPIOA15 gpio nothing default on
- Pin 36 GPIOA13 gpio write 15 on
- Pin 32 GPIOA7 gpio write 26 on
- Pin 28 GPIOA18 gpio write 21 on
- Pin 26 GPIOA17 nothing default off
- Pin 24 GPIOC3 gpio write 10 on
- Pin 22 GPIOA1 gpio write 0 on
- Pin 18 GPIOG9 gpio write 27 on
- Pin 16 GPIOG8 gpio write 26 on
- Pin 12 GPIOA6 gpio write 7 on (also PWM test pin see further down)
- Pin 10 GPIOG7 gpio write 29 on
- Pin 8 GPIOG6 gpio write 28 on
- Pin 7 GPIOG11 nothing default on
- Pin 11 GPIOA0 gpio write 2 on
- Pin 13 GPIOA2 gpio write 6 on
- Pin 15 GPIOA3 gpio write 3 on
- Pin 19 GPIOC0 gpio write 12 on
- Pin 21 GPIOC1 gpio write 13 on
- Pin 23 GPIOC2 gpio write 14 on
- Pin 27 GPIA19 gpio write 30 on
- Pin 29 GPIA20 gpio write 25 on
- Pin 31 GPIOA21 gpio write 11 on
- Pin 33 GPIOA8 gpio write 22 on
- Pin 35 GPIOA16 nothing default on
- Pin 37 GPIOA9 gpio write 23 on
SO – then after all of this – one of our readers pointed me to a C program (see link below), easily compiled on the M1 or any other similar program that in this case simply runs PWM – of course – for a particular pin – but that’s not a problem – you could easily pass parameters – and one WILL.
NOW – that’s all fine and good but it will only run as user ROOT which if you are running node-red – is about as much use as a chocolate fireguard. Until I read THIS (for me game-changing) example about changing the user… so assuming our compiled program is called PWM and is run as ./PWM except it won’t as user PI for example…
Take your C program and
gcc -o pwm pwm.c -l wiringPi
sudo chown root:root pwm
sudo chmod 4755 pwm
I have to tell you I am WELL chuffed about THIS discovery because essentially it means I can make my OWN command line program… and customise it for different boards!!!! AND it will run as user Pi on Node-Red using the EXEC node. Our friend might call the C program a very simple one – but that and the conversion to run as Pi or similar user – makes a BIG difference to a lot of us!!
I HAVE tried this and it DOES PWM modulate that pin – which means getting other pins to work should be a breeze.
THE ONLY PROBLEM is that this is software PWM – because despite the fact that the pin corresponding to 7 is in fact a PWM pin – if you try the wiringPi command for proper PWM – the system objects – and throws a message out – clearly the pins that can be set as PWM are predefined for another board… does anyone out there understand the Wiring Pi code sufficiently to modify it? If PWM won’t work properly – maybe the likes of I2c etc will suffer a similar fate.
But here’s the problem – though WiringOp just happens to work for GPIO on both the M1 and the NEO, and compiled WiringOp programs can be made to work as user Pi, as the company’s Matrix software which DOES give you access to I2c etc. seems to have some issues with any other than ROOT access – and FriendlyArm do not currently know how to get around that. The manual for the Matrix software is even now in Chinese only!