Getting Started with Micro Python on ESP32

micropython.PNG

I got my little game working on the Raspberry Pi yesterday, and today I got to thinking how I could make it work on something a bit, er, cheaper. It’s not so much that I begrudge paying the price for the Pi, more that I can think of more demanding things to do with the Pi than just run the game. My thoughts turned to the ultra-cheap ESP32 devices that I’ve been playing with. The only snag is that I’ve written the entire game in Python for the Raspberry Pi, and I don’t fancy re-writing it in the C++ that these devices normally run.

So, why not run Pyhon on an ESP 32 device?

This turns out to be really easy. If you’ve installed ESP32 devices as part of your Arduino development environment you will have a useful little program called esptool.py on your machine. To convert a device to Micro Python you just have to plug in one of those ultra-cheap devices, find out what serial port it is connected to (in my case com4) and then use the command below to program the chip with Micro Python.

esptool.py --chip esp32 --port com4 --baud 460800 write_flash -z 0x1000 micropython.bin

This loads the image in the file micropython.bin into the device. To get a Python image, go to the download site and look for ESP32 devices. I used the one in the file esp32-20190714-v1.11-146-g154062d9c.bin

Above you can see what happened when I ran the command.

Above you can see what happened when I ran the command.

When you restart your ESP32 you will be able to talk Python to it via a command prompt. That’s fine, but what you really want is an IDE that lets you write and deploy Python programs.

The best one I’ve found is called Thonny. It’s works with “normal” Python nicely enough, but it also has an option you can use to point it at an embedded device:

thonnydevice.PNG

In the options menu above I’ve told Thonny to search for a device running MicroPython and connect to that. Now, when I run a program it is deployed into the device and executes from there.

The MicroPython installation provides a tiny filesystem that can hold python programs. When you run a program it is transferred into that filesystem and runs from there. There are two special program files on the device, boot.py and main.py. The program in the boot.py file is executed when the device powers up, followed by the one in main.py The Thonny program has an option (in the Device menu) to save the program you are editing one of these files. This makes it really easy to make your Python program run when the device is powered up. I made my game program the main.py one and now I have my original Python program running inside a device that only cost around a fiver.

It wasn’t quite as simple as just copying the files over. The api (application programmer interface) to use GPIO ports and Neopixels are different in the two devices and I discovered that MicroPython does not provide a random.shuffle method. This meant that I had to create my own shuffling function, which wasn’t too hard.

Anyhoo, I now have my program running in Python on a tiny embedded device. Which is rather nice. I’ll put the code up on GitHub later this week.

Hull Raspberry Jam with Two Buttons

twogamebox.PNG

Well, that was fun. I really should have taken some pictures of the jam, it was great fun. There were loads of machines set up and creativity going on all over the place. I took my “Two Button Box” along and developed the rest of the software. Thanks to Ben, Jon and Matt for advice and help on getting my Python program to run each time the box is powered on (this harks back to my Unix days of old - editing the rc.local file to run programs on boot up).

Players need to press the red button when there are more red lights lit, and the blue button when there are more blue lights lit. I’d taken a bit of care to make the gameplay progressive. The game starts simple, with just red and blue lights, but then other colours appear and then they all start to move around.

It’s always nice to see people playing a game that you made. I’m not sure it pushes back any frontiers or brings anything particularly new to gaming, but it is always nice to see someone enjoying playing with something you’ve made; particularly if you enjoyed making it.

You can see it in action here. Thanks to Jon for the video.

Making plans for Hull Raspberry Jam

twoswitchplans.PNG

There’s a Raspberry Jam in Hull tomorrow. There is a kind of development theme where attendees are invited to create a game that only uses two buttons to control it. I love a good constraint. Ask me to make a game and I’ll fall apart. Ask me to make a game that only uses two buttons and I get interested. I mean, as you can see above, I’ve got two buttons….

My plan is to put the buttons, a Raspberry Pi and a power supply in a box. It’s one of the boxes that is supposed to have an Air Quality sensor in it (sorry Robin).

There’s a kind of reason in this madness, in that I’ve often wondered how good the Raspberry Pi is for creating what I’d call “pure” embedded devices. The Pi Zero W is very close in price to things like ESP32 based devices and brings a lot of advantages, including much more power and a “full fat” operating system. I get to write in Python - which I love - and I can actually run the IDE on the device itself if I want to.

You can see my first step above. The first thing I do is make sure that all the components will fit inside the box. As you can see, I think it will probably be OK. The only snag I’ve hit so far is that the holes for the buttons need to be a little bit larger than the ones in the box itself. However, a few minutes with a drill have sorted that one out.

The box that I’m using, like pretty much everything in my life, came from Amazon. You can get it much more cheaply if you are prepared to order it from China via Aliexpress. It’s supposed to be used as a junction box for connecting cables. It has eight big holes all round the sides into which you can run cables and connect them together, or put devices into. It has these rubber inserts for all the holes that you don’t want to run cables into and I got to wondering how good they would be at defusing light from a neopixel. Perhaps I could put some lights in the box and use these as the basis of a game of some kind. Time to build a circuit…..

firstcircuit.PNG

What we have here is the first circuit. The two micro-switches will be fitted onto the buttons. The coloured pixels are to be stuck inside the box against each of the rubber fittings. The chain of lights is connected to a single GPIO (General Purpose Input/Output) pin on the Pi Zero. The buttons are connected to two other GPIO pins. There’s a splendid library available from AdaFruit that allows a Python program running in a Raspberry Pi to set the colour of each of the leds and also read the state of the inputs. So in no time at all I had the lights on under program control. Some tests have indicated that they will work quite well. I’ll put them all in the box tomorrow.

The next thing I need is an idea for a game. What I’ve come up with is a very simple idea. Each light around the box will light up with a particular colour. If there are more red lights lit than blue lights, the player must press the red button. If there are more blue lights lit than red lights, the player must press the blue button.

This should be easy to code and might make for some compelling short term game play, particularly if I add a bit of complication as the game is played.

I’m looking forward to taking the box to the Raspberry Jam tomorrow and finishing off the code.

Mobile Phone Navigation is still rubbish

Wrong side of the tracks….

Wrong side of the tracks….

The satnavs that I’ve used in cars are mostly OK. The ones I’ve used in phones are mostly awful. Some observations:

  • The official Apple Maps application for iPhone actually consumes power faster than a normal car USB connector can charge it. So on a long journey your phone can go flat even if it is plugged in. On the plus side, it does make for quite a good hand warmer.

  • The Google Maps application (which you switch to because the Apple Maps one kills the battery) has a navigation display that is obscured by appointments that pop up on the screen and have no obvious way of cancelling. This is great if you are on the way to a scheduled meeting. Not.

  • The Official Apple Maps app (which you decide to use for a short journey) just plain gets things wrong. Exhibit A above. Searched for the West Avenue car park in Filey, asked the phone to navigate me there. Got taken to a residential street across the railway from my destination. Rather than using the geographical location of the thing I’ve searched for (which it seems to know because it draws it in the right place) it instead navigates to the post code which covers an area including lots of wrong destinations)

O2 Apple Watch Data Fix

After all the fun and games getting the Apple Watch on O2 I was hoping for a happy ending. But this didn’t happen because although we could quite happily make phone calls with the device all the network and SMS features were broken. I was thinking for a while that I’d have to ring up and they’d try to sell me another watch. Wah.

But the good news is that the fix was quite simple. I opened the Apple Watch app on the connected iPhone and then went to the Mobile Data tab. This shows you the data used by the watch. Just as the phone was heading to the page it displayed a “Carrier settings update” message and offered me the chance to confirm. After doing this the watch seems to work fine with SMS and mobile data. So, if you’ve bought one (or even several) Apple watches from O2 and can’t get the data to work you might like to try this.

Raspberry Pi Model 4 a bit slow at 4K

So I’ve got my Raspberry Pi model four running and I thought I’d try it driving a 4K monitor. Promotions of the device have made much of the fact that this new version has not one, but two video outputs and they can run 4K video.

I don’t want two 4K displays, but a single one that works correctly would be nice. Unfortunately the performance is not great. It might be me, but it seems that although the desktop looks splendid it does seem to lag a bit. Moving the mouse around turns into a game of skill, where you have to stop moving the mouse before it arrives at the desired destination. This might be a great idea for a video game, but it isn’t much fun when you are trying to use the device.

I’m hoping that this is caused by early versions of the operating system not being optimised for the new hardware, but if you’re thinking of getting a model 4 I’d advise you to get your hands on a device and have a play before you splash any cash.

Awesome Overhang fun

20190711_074824003_iOS (2).jpg

So I’m printing some camera clips on Una, my Ultimaker (seven years old this year). I forgot to ask for support for the print. I needed support because the top half of the clip just sticks out into the air. If I’d asked for support the gap on the left of the picture above would have been filled with removable printed bits that support the “ledge” that is being printed.

However, when the print finished I was impressed that even with the lack of support I’d got something that is probably usable. You can see the dangling loops of filament at the bottom of the ledge but after a few layers they had bunched together enough to get enough material to complete the print.

I’m not proud of my inability to specify support when a print needs it, but I’m very proud of the way that Una coped without it.

Uninspiring O2 Apple Watch

Drop everything - your order’s ready to pick up.

Turn off the TV. Put down your sandwich. Get off the bus. Whatever you’re doing, it’s time to hotfoot it to your O2 store. If you can’t come right now, that’s okay. We’ll hold it there for seven days. But don’t leave it any longer, otherwise we’ll assume you’ve changed your mind and we’ll have to cancel your order.

I had to work very hard to get the above message. The plan was simple. I thought it would be nice to replace number one wife’s ageing Version 0 Apple watch with a new and cellular version. And O2 were doing a discount offer that looked rather tempting. What could go wrong?

Lots.

I thought I’d perform the transaction online. Half way through - just after I paid my 20 pound deposit - the web page crashed. No order, no nothing, just a 20 pound transaction on my bank account. So I rang up and asked them to sort it out. Of course they can’t do that. There’s no transaction to find. But they can sell me the watch again. Cue 25 minutes of minutely detailed purchase discussion. The kind of thing I’d been trying to avoid by buying online. At the end of this fun and games I was promised an email containing a link that I could use to complete the transaction.

Guess what? That link crashed too. Another 20 pound transaction on the account. So I rang up again to try and sort if out and, bless my soul if they don’t try to sell me a watch for the third time. I suggested that it was time to get someone along who knew what was going on. Finally I got escalated to the point where someone asked “Are you using a pop-up blocker on your browser” .I’m not, but the merest mention of issues like these sends me scurrying off to try a different browser. And finally, after another 20 pound payment, I get an order completed. I’d carefully used Google Chrome to perform the purchase because I worry about browser issues breaking things but it turned out that this was broken. And the transaction that came through was made using Edge. Go figure.

Apparently the other payments will disappear after a while. They had better. Or I’ll probably have to go online and buy another watch to sort it out….

Anyhoo, a couple of days later I got the breathless email at the start of this page. So today we hurried along to the O2 store to get our hands on the treasured technology. It was a very uninspiring pickup. I wasn’t expecting fireworks, or a red carpet. This was just as well. I’ve had more engagement picking up parcels from the Post Office. In fact it was just like that - even down to the DHL wrapper the watch came in.

Pro tip for O2 shop staff: you can greatly enhance the pickup experience for customers by taking the item out of the postage bag before giving it to the customer, and then showing an interest in what was bought. I guess there’s no commission to be had in handing something over, but that’s not really the point as I see it.

I got the watch installed after three or four hours. Software updates over Bluetooth take an age. And it seems to work fine. But I can’t recommend the customer experience at all.

Talking Air Quality at South Hunsley school

48177975986_d6c7b01bf7_z.jpg

David got in touch a while back. Would I like to talk to his students at South Hunsley school? Would I? Oh yes. So today it was into the bag with the Air Quality Sensor Top Hat and a few other goodies and then off back to school. They were a great bunch of folks. And they were even polite enough to laugh at my first (and only) joke. I was talking about air quality, hardware, self promotion and whatnot. I said at the time I’d put some useful links onto my blog (always a good way to drive traffic). Here they are.

To find out about the Arduino and get the software you can go here.

To make yourself poorer buying things from China you can go here. You can get cheap Arduino Uno devices, along with a whole bunch of other hardware.

If you want to buy one of the tiny LoRa enabled devices with a screen search for “Heltec LoRa”. The devices are sold in pairs for some reason, make sure you get the ones which are marked as 868Mhz.

You can also get the tiny cheap WiFi devices here, search for “Lolin D1 mini”.

If you want to get an air quality sensor you can get one of the good ones by searching for “SDS011”. If you want to measure temperature, pressure and humidity, search for “BME280”.

If you want some fun coloured leds search for “WS2812” followed by “ring”, “strip” or “panel”. These are dead easy to interface to any of the devices I’ve mentioned above. But make sure you get hold of a properly beefy power supply if you are driving more than 12 or so to full brightness.

I’ve not had a problem buying from China. In over 50 orders I’ve only had one not turn up, and I’ve only had a couple of broken devices.

You can find my Air Quality stuff on GitHub here. That has drivers for the hardware that you can buy and there are also circuit diagrams and box designs.

Byte ordering is a thing....

48177980026_ebc8ea2177_z.jpg

I’m upgrading the Air Quality sensor software so that I can send configuration messages over LoRa to the sensor. The thing I’m most interested in is the ability to tell the sensor the rate at which it should send readings. It would be useful to have a message sent every five minutes or so for testing, and then dial back the rate once the sensor has been installed. I’m using a tiny packet format to do this. The packet is three bytes long:

01 58 02

The first byte is the command number (there is only one command at the moment). The other two bytes are the number of seconds the sensor should wait between sending each reading. The value I want to send won’t fit into a single byte (a byte can represent an unsigned integer up to the value 255 and I want to have intervals longer than 255 seconds) so I’m using two bytes. The first byte is the “low” byte of the value I’m sending and the second byte is the “high” byte. In the tiny sample above the low byte is HEX 58 and the high byte is 2. This gives an interval of 2 * 256 + whatever 58 hex is in decimal. This boils down to an interval value of 600 seconds, or 10 minutes.

I wrote the software, tested it, installed it in all four of the sensors we are building and then set them off. I then sent the above command to the sensors to set 10 minute updates and then relaxed in the knowledge of a job well done.

And all my sensors stopped transmitting completely. Wah.

Turns out that I am an idiot. I’d done everything right except the last bit, when I’d sent the following command:

01 02 58

I’d got the MSB and LSB bytes the wrong way round. Rather than having 2*256 I now have HEX 58 times 256. A big number. A six hour or so number. So all my sensors were now not going to transmit for six hours.

Sending the new command wasn’t a problem, but because of the way that we are using LoRa the sensors only look for new commands when they send a reading. So I had to wait for six hours before the command would get picked up and acted on. This is something to bear in mind about LoRa. The remote node is not always listening for a message, it only turns on after it has sent something. This is one way they manage to get such impressive battery life for LoRa connected devices.

I managed to solve the problem by turning the sensors off and on again. They always transmit a packet when they start up so I was able to update the timing with more sensible values without having to wait six hours.

Shelf collapse chaos

48196733357_7741ca2507_z.jpg

That was scary. We were having a quiet coffee downstairs when we heard a noise rather like the one that a house makes when it falls down. A tsunami of bangs and crashes that must have lasted a minute or so. We hurried upstairs to find out what was left of the top half of the building and expecting not to find much..

It turns out that one of my shelves along the top of my project room had come loose, depositing onto the floor all the boxes and bits and bobs I’ve been stashing up there for the last few years. The good news is that nothing was broken that didn’t deserve it. I’ve put most of the stuff back, given everything a proper tighten and I’m going to keep an eye on it in future. Such excitement.

Final day in Iceland

48177995236_07c44e0a8f_k.jpg

Last day in Iceland today. We visited the capital city which seems to be less like a capital city than pretty much any other place I’ve been. There is some stunning architecture, some lovely art galleries and a whole slew of souvenir shops (almost as many as there are in Whitby) but there was not the level of hustle and bustle that I have seen in most capitals. Not that this was a bad thing, it was nice just to be able to saunter round from one really nice place to another.

Home tomorrow. Oh well.

Keflavik and the Blue Lagoon

Today finds us at the very edge of Iceland, up past Keflavik at the Garður Old Lighthouse which also hosts a really nice restaurant. The weather is trying to make amends for being super wet earlier in the week, not that it has stopped us doing much but it does rather spoil your photographs when you can’t see the top half of the sky for cloud. Anyhoo, this is an amazing place. Even better in good weather.

For the afternoon we headed to the Blue Lagoon, a hot spring that is a “must visit” in the same way that Disneyland is a must see if you ever go to Florida. Just like Disneyland it was expensive, but the experience was worth it. You are out in the open in cloudy blue water that is around the same temperature as a warm bath. Apparently the minerals in the water are good for your skin. There are also mud packs which we tried (they were included in the package). The consensus of the folks around me was that the mud pack didn’t do much for me. The only comment of note was that I looked slightly less scary once I had washed it off.

48178064537_b428c14c14_z.jpg

The spa is surrounded by amazing landscape.

Travelling the Golden Circle

What with me now being a confident Icelandic driver we thought we’d spend today driving “The Golden Circle”. After “sanity checking” the route with the ever-helpful hotel staff we headed off into the unknown on the wrong side of the road….

Actually, I’ve decided that it is not “the wrong side of the road” if everyone is doing it. Not that this is stopping me getting this ghastly frisson each time I see a car coming towards me on the “wrong” side. Then I realise that I’m on the wrong side too, and all is well. Roundabouts are a challenge though, it feels as if I’m deliberately turning into the face of oncoming traffic when I enter one.

Anyhoo, we set off with a bunch of locations programmed into the phone. It all worked splendidly,. I’d taken my venerable old Fuji FX100 camera and managed to get some reasonable pictures. You can click through the images to larger versions on Flickr if you are so minded.

Heading to Iceland (not the shop)

We were up bright and very early this morning to fly to Iceland. Silly me, I thought that because our flight left at 6:30 am the airport would be pretty much deserted when we arrived. Not so. It turns out that, probably because of airport fees or something) that the place was absolutely packed at 5:00 am. Folks were downing lagers and bottles of wine in preparation for their trips. Good for them. We made do with a coffee and a bun.

Never been to Iceland before. We’re staying at a little hotel just outside Reykjavik for a few days. We’ve hired a car too (such bravery). Should be fun. We’ve already discovered that there is a waterfall a few minutes walk from the hotel.

Raspberry Pi Model 4

raspberry pi 4.PNG

I remember when the Raspberry Pi was first announced. A credit card sized proper computer which cost 25 quid? A likely story.

Twenty five million sales later, the story is looking more likely than ever. And now we have version 4.0. This version is interesting for a whole bunch of reasons. They reckon it has around twice the speed of the previous version, and you can now get it in memory sizes that go from 1 to 4 Gbytes. It now supports two monitors and 4K video output (although its a bit tricky if you try to use both at the same time). It also has two USB 3.0 sockets and the specs speak of much faster data transfer. The mounting holes look to be in the same place, and the pin-out for the “hat” connector is the same, although the pins can be re-purposed to get some extra I2C and serial ports which is very welcome.

The bad news is that there are a few “breaking” changes from the previous device. The video sockets are now micro-HDMI and there are two of them, which means that case designs will have to change and you’ll need to change your video cables. The power is now supplied via a USB-C socket and you need a bit more of it. The days of hanging a Raspberry Pi off a USB socket on a PC may be over. And if you want to get full speed all the time (or play with overclocking) you’ll need to add cooling to prevent the processor slowing down when it gets too warm.

But this does mean that you can consider using the Pi as a desktop replacement. I know they say that with every new release of the platform, but this time it might actually be true. The extra ram will make it possible to multi-task with ease, and the faster USB sockets mean you can connect fast hard drives too. However, having said that, I reckon that the extra power is interesting because it makes it easy to use a Pi as a proper “edge computing” device. It should be very possible to run some proper AI on this platform, allowing you to put a lot of computing power right next to your sensors, which makes things very interesting.

I can’t find any word from Microsoft as to whether the IoT version of Windows 10 will be ported to the Raspberry Pi 4. I really hope that it will be. Windows 10 on this platform would be awesome.

I’ve ordered myself a device, I’m looking forward to it coming and having a play.