Software Engineering Philosopy

This picture is one of mine. You can find it on Flickr

Had an email from a Peter who’s reading the C# Yellow Book at the moment. He asked about how to learn more about software engineering. I quite like my reply, so I’m putting it in my blog….

I worry a bit about too much emphasis on "software patterns". I just tend to write code that solves the problem, and then discover afterwards that I've used a particular pattern. Things like "dependency injection" just seem like programming common sense to me, but then again I have been programming for a very long time.

By all means read up on the different patterns, but do it from a perspective of refining your technique, not finding a quick solution to a problem. If you use a pattern without understanding how it really works that can end badly.

My strong advice is just to keep writing code. Build your knowledge and develop a habit of looking at existing systems and trying to figure out how you would make them work and what design decisions were made when they were created. If you do it right you’ll be learning new stuff and having new ideas all the time. I still am.

A lot of programming and software design is down to practice and experience, just like any other field. And you also need to remember that any working system is built up of compromises and that it is very rarely that you will find the best solution. You might find the fastest, or the smallest, or the quickest to build, but not the best.  So decide what kind of “best” that you want, and go for that.

The case of the missing shift key

Can you spot what’s missing here?

Sundeep got in touch today. She’s looking at getting a very cheap, simple, laptop to develop Arduino software. She asked me what I thought of the GEO Book 1, which is presently on sale at the very attractive price of 99 pounds. It’s got a Celeron processor, a mere 2G of memory and uses rather slow eMMC memory for the rather paltry 32G of storage that it provides.

But it only costs 99 pounds.

I had a look around and the reviews of it were not great, but these reviews were for a device priced at 160 pounds, not 99.

As it happened, I was in PC World buying another hard disk (I’ve run out of space for my photos) and they had the GEO on show. So I took a look.

First impressions were good. The display is fine, the construction smart enough and there seemed to be a reasonable turn of speed. I sneaked behind the rolling demo and fired up Word to get an idea of the performance. Everything fired up reasonably well. Quick enough to be useful. Then I tried to type something and it came out kind of wrong. So I tried again. My text was being mangled. Then I took a proper look at the computer.

There is no SHIFT key on the right hand side of the keyboard. As a touch typist I find this key rather useful. When I type W I use the shift key with my right hand and press w with my left. This did not work. I thought I was going mad at first, and that I’d failed to spot something fundamental. But no. There really is no SHIFT key on the right hand side.

I can’t believe they’ve made a computer like this. I was thinking of getting one of the devices just to see what you can do with a really cheap machine. I had high hopes. But not if I can’t type on it. If you fancy getting your hands on a Windows 10 machine that is so cheap that you could regard it as disposable then I can just about recommend it. But if you want to type properly on the keyboard you’ll need to be careful.

There might be some cunning keyboard mapping code that you could add to steal one of the other keys on that row to work as a shift, and you can of course plug an external keyboard the USB port, but for me I’m afraid that the GEO is a no-go. Even at such an amazing price point.

IMPORTANT NOTICE ABOUT C4DI HARDWARE GROUP

If you’re wondering where the Meetup site for the c4di hardware meetup group has gone, we’ve moved our operations to GetTogether. Alternatively you can just turn up at a Meetup at c4di on the first and third Thursday of each month, starting at 6:00 pm.

Note that the above picture is not one of mine, I’m using an image from unsplash.

Using the Second Serial Port on the Azure IoT Devkit

For some time I’ve been trying to create an Air Quality Sensor using the Azure IoT Developer Kit. It’s a nice little device that has a bunch of environmental sensors, a nice OLED screen and a couple of buttons. It is also properly secure, in that it contains secure storage and you can validate access using X500 certificates.

I like it a lot, but I’ve not been able to use it for air quality work as I couldn’t get the second serial port to work on it. This is important because the air quality sensors that we are using deliver their readings over a serial connection. I’ve tried doing what the documentation says to do but it just didn’t work.

I posted a question about this on the GitHub repository for the kit and got a response indicating that what I was doing should just work.

But it didn’t.

So I had a proper look in the code in the device SDK. After a bit of digging I discovered that two of the source files are missing statements that make the second serial port available to Arduino programs. The file Variant.cpp needs the statement:

UARTClass Serial1(UART_1);

..and the file Variant.h needs the statement:

extern UARTClass Serial1;

Then it does work. I’ve tested it and everything. The picture above shows my cunning test rig, using a USB to serial adaptor that I wired to the devkit. If you’re connecting the device using a MicroBit connector you will find that pin 1 is TXD and pin 2 is RXD.

I’ve put the changes into a fork of the devkit sdk that you can find here. I’ve also told the developer team about my fix, so that it should appear in the “proper” version soon. I’m very pleased to be able to contribute a tiny bit to the development of the device. I feel like a grown up programmer…..

The only problem I have left to solve is that my Air Quality sensor needs a 5 volt power supply. While the USB connector on the Developer Kit board receives this from the host device the power connection is not surfaced anywhere on the board. I can see why this is: you wouldn’t want people inadvertently sending 5 volt signals into the input pins, but it means that my wiring is going to have to be a bit more complicated than it might be.

Anyhoo, I can now start porting my Air Quality Sensor code into the Devkit, which is nice. I’m also going to make a little box for it too.

Air Quality Designs and Software now on GitHub

If you woke up with an urge to create an Air Quality sensor out of either a Heltec or a Wemo device then I’ve got good news for you. You can download all my software and hardware designs from here. You can make sensors that use LoRa in OTAA or APB (these will make sense if you know LoRa or go to my talk later this month). You can also use MQTT to talk to Azure IOT Hub or another MQTT broker including Mosquitto.

All configuration is performed using JSON messages that can be sent via the serial port or over the air. It is very easy to configure a device, no need to re-burn the software each time you want to deploy another sensor with a different name and network configuration.

I call this a “shirk in progress” release, in that I’ve been too lazy to release any new versions for a while. However, I'm now using this as my main development repository and so you can expect updates, including more detailed documentation, over time.

Using an Azure Function to receive MQTT Messages

The video shows me pressing a button on my Air Quality sensor to trigger the sensor to send a new reading via MQTT. The action then switches to the output of an Azure Function running on my PC which is responding to this. I’m very pleased with this.

MQTT (Message Queuing Telemetry Transport) is a neat way to connect embedded devices together. I’ve used it to talk to the Hull Pixelbot devices over the internet using Azure IOT Hub. Using MQTT in this way has meant that my server applications can send commands to the robots to tell them what to do, and also receive messages back.

Creating a server application is all very well, but it is a bit too much like hard work for me when I’m writing code for the Air Quality sensors. In that situation I don’t really want to have a server application sitting in the cloud twiddling its digital thumbs while it is waiting for incoming messages.

What I really want is a way of connecting a lump of code (ideally a C# method) to the event “Hey out there. I’ve got a new Air Quality Reading”. It turns out that Azure Functions just do this. You deploy the function into the cloud and bind it to the Azure IOT Hub and the endpoint that you want it to listen to. And then, whenever your remote node (which could be anywhere in the world) wants to send a message it posts an MQTT update and your function runs.

This is my function at the moment:

[FunctionName("SensorMQTTReceiver")]
public static void Run([IoTHubTrigger("devices/#",
Connection = "IoTHubConnectionString")]EventData message, ILogger log)
{
    log.LogInformation(
$"C# IoT Hub message: {Encoding.UTF8.GetString(message.Body.Array)}"
}

I stole most of how to do this from a very useful howto here. The next thing to do is add some code to the function that puts the data values from the post into a data store and perhaps add handling of alerts if we see air quality values which are out of range.

The next thing to do is build a similar mechanism to process LoRa updates sent via the things network.

PCB Design at the Connected Humber Hardware Group Meetup

Thanks to Robin for the picture

We had another great session at the Connected Humber Hardware Group tonight. Paul was telling us how to design our own printed circuit boards and get them made up. This is now so cheap that every electronics hobbyist should be getting their own custom boards made up. It turns out that it is all a matter of workflow, in that the sequence in which you put the design together is the important part. Unfortunately I was helping out with a recalcitrant LCD panel during this bit, so I’ll have to ask the other members to help me along when I start making my own circuits.

I want to make at least four, one for the fully featured Air Quality sensor, one for the “Mini MQTT” sensor, another for the Hull Pixelbot dual brain version and a final one as a badge which I think will look cool.

We’ll be continuing our exploration of PCB design at later meetups. They are free to attend, great fun, and take place on the first and third Thursday of each month at c4di in Hull. You can just turn up on the day (as a bunch of people did tonight) but you can also sign up for notification of upcoming events here.

Talking LoRa and Air Quality Sensors at Hull University on 28th November

Air Quality sensors inside and outside the box

I’m presenting a session at Hull University about low powered radio networks and air quality sensors in a couple of weeks. I’ve just written the description:

"LoRa is a low powered, long range radio networking technology with the potential to open up new embedded applications. It makes it easy and cheap to create connected devices. Rob Miles will explain how LoRa works, what makes it particularly interesting and why you should start working with it. He will show how the Connected Humber group are using LoRa to move Air Quality data from sensor to server, and how you can create your own LoRa devices very easily."

It sounds rather interesting. I’m definitely going to go along.

Learn PCB Design at Connected Humber on Thursday

This could be your design….

This week we are having a rather special Hardware Group meetup at c4di. We will be looking at the creation of printed circuit boards (PCBs). We will be using a freely available tool which can be used on lots of different computers.

If you have any interest in making stuff you really should come along. It is now very cheap to get PCBs made and they make construction much, much easier. And of course they look really professional.

We start at 6:00 at c4di in Hull on Thursday 15th November. You can join our group on GetTogether here.

Get Your Eyes Tested

For the last few days I’ve been telling everyone I meet how sharp the world now appears to me. I’ve got some new glasses. They’ve made a huge difference to the quality of my life. I’m now able to read text by looking at the letters, rather than inferring the meaning from the approximate shape of the words. This is probably going to massively improve the quality of my writing.

Then agane probuably not.

Anyhoo, the serious message here is that you need to get your eyes tested regularly. I left mine for a bit longer than I really should have, and this resulted in me spending a lot more time squinting at things than should have been the case.

I think regular eye tests are especially important if you spend long periods of a working day staring at a video screen. I’ve gone the whole hog and actually replaced my “computer” glasses too and I’m really pleased that I have. Because everything is just so sharp now…

Mini Air Quality Sensor Now Working

I spent quite a bit of today getting my tiny air quality sensor working. I’ve ported the code for the full fat sensor over, so that it has full MQTT setting control and all the other trimmings. It seems to work OK. I’ve mounted the pixel ring up against the 3D printed case and the result is rather impressive, and really hard to photograph.

The biggest problem that I have is that my “artistic” back, which contains programatically generated vent holes, doesn’t seem to let in enough air (or the air inside just circulates). I’m going to have to re-design that part. I’m toying with adding a little fan to get some forced ventilation. I’ll put all the code and the hardware design files on Github when they’re finished.

Cheap Power

I’m always on the lookout for cheap power. So I was intrigued by these phone battery packs in the pound shop in Hull. It turns out that they are actually quite usable if you need to power small electronic devices. I’m pretty sure that the advertised capacity of 1200 mAh is more than a bit optimistic, but I do know that a fully charged battery powered my “full fat” air quality sensor for well over two hours, which is good for demonstrations and the odd bit of remote sampling.

Apparently there are some that cost twice as much (that’s two pounds) which last around twice as long.

Making a Mini Air Quality Sensor

I’ve got the large, “full fat” Air Quality sensor just about working. But the hardware cost of the device is a bit on the high side. So today I’ve spent the day grappling with FreeCAD to try a design a much smaller, WiFi only, device. This uses a cheap Wemos D1 Mini and a five pound air quality sensor and a pixel ring. The cost of this hardware is only around ten pounds, which makes it much cheaper to build.

I’m using a new construction technique where the components are mounted on a tray which is then fitted into the box. This removes the need to connect things together inside the box, which helps. To make this work I’ve created a design for a Wemos D1 mini “blank” which can be cut out of the tray. This seems to work quite well. I’ve made another blank for the pixel ring.

I’ve used random numbers to design the vent holes in the back panel. I love using software to create physical objects. Tomorrow I’ll print the components and start building up the circuit.

Hello to Asda

We had some robots on display…

Robin and I were working in the c4di this morning when a bunch of folks dropped by with John to take a look at what we were doing. I had my old glasses on and taking a quick look at them, decided that it was perhaps a bunch of students, what with everyone looking fresh-faced and keen and whatnot. So I gave them what I thought was good advice for people in their position. I told them what I do with the robots, and the importance of playing with tech, learning how to write well and working hard etc etc

Turns out that they already work hard, as they were all Asda employees who had come along to take a look at what c4di gets up to. Oh well, I still think that my advice still holds good though….

Hull Pixelbots and the wrong kind of quotes

One of the downsides of thinking “I’ll use my phone for all my pictures” seems to be that don’t take any photographs at all when I should. This is how it was today at the Hull Pixelbot talk I gave at the university. However, the talk went well with a very appreciative audience.

I hope I didn’t come over as too obsessive compulsive when, during the session I started to fret about the use of the wrong kind of double quote in some of my sample code. Stupid “Smart Quotes” had stuck its fingers into my text and broken it. I hate it when I see it in other folk’s stuff, and I usually try to make sure that my quote characters are the right stuff.

For those of you who’ve no idea what I’m on about, its the “ in the top line should be vertical double quotes like the rest of the sample. It’s important to me, but perhaps I should get out more…..

For connoisseurs of quotation marks you can find the slide deck here.

Board Game Expertise

A while back I was moaning about the way that there seem to be robots out there which are looking for search terms on sites and then emailing the owners asking to place paid content or link to them.

Then I got an email last week from Kendra telling me about Game Cows, a board game site. I’ve posted about the odd board game here and there, and she seems to have picked up on that. The site has some useful reviews and whatnot, and if you’re into board games you should find it interesting.

Printed C# Yellow Book now available with added duck

It’s finally here! Just in time for Christmas. If someone in your family has been asking all year for a yellow book about the C# programming language then I have good news. The C# Yellow Book is now available from Amazon in full 3D form, with pages and everything. And the cover has a specially taken duck photograph.

And you could also buy one for every room in your house. Especially if you have a really big house. That way you’d never be more than a few feet from a duck. Which has got to be a good thing.

Using Three Serial Ports with the Heltec LoRa 32

Prototype and “floral” version

I’m making pretty good progress with the Air Quality sensor. I’ve now got a command protocol running between the devices and MQTT or LoRa. As you can see above, I now have the ability to customise the splash screen. Which is very useful if you like cheese.

Anyhoo, I’ve decided that the sensor really needs GPS so that each air quality reading can be tagged with location. I can configure fixed latitude and longitude values into the device, but for real portable usefulness we need it to be location aware.

Getting a GPS device wasn’t a problem, I had one lying around. But connecting it to the Heltec processor that I’m using was more tricky. I’ve got one serial port I want to use as a connection to the host computer and another that I’m using to connect to dust particle sensor. But now I need a third connection for the GPS receiver.

After some rather unsuccessful fiddling with different kinds of software serial connections I decided that it was best to go back to first principles and see if the hardware had anything to offer.

It turns out that it does. The ESP32 chip that the Heletc uses actually contains three serial ports. The problem is that they are not always connected to pins that you can access easily. But the good news is that the ESP32 chip is capable of mapping functions to pretty much any pin and the Heltec libraries take advantage of this. You can map the transmit and receive functions of a serial port when you open it:

HardwareSerial GPS_Serial(2);
GPS_Serial.begin(9600, SERIAL_8N1, 13, -1);

The first statement creates a hardware serial port. The second sets it to receive from pin 13. I don’t really want to transmit anything to my GPS device at the moment (I’m a bit short of spare pins to be honest) and so I’ve set the TX pin to number -1 which means “Don’t use this port for transmit”.

It works a treat. And thanks to this fantastic library I’m now tagging sensor readings with location. The next thing to do is re-design the case to take the GPS sensor and print another flower for the top.

Switch Electronics in Hull

Don’t try clicking on the Add to Cart option…

A while back I had the urge to own some cables that had crocodile clips on each end. They are very useful if only have two hands and you want to connect a bunch of things together. I found some in ebay at a good price, ordered them and they arrived in good time. Just what I wanted. Already found them useful.

Before I threw away the envelope I had a quick look at the accompanying slip. Turns out the company that sent them is based in Hull, around fifteen minutes from my house. I went there yesterday and bought a bunch of stuff.

Awesome.

Since the demise of Maplin I’ve been wondering where I’m going to get all the electronic bits and bobs that I occasionally have a need for. Buying from the internet is all very well, but it does introduce a bit of a delay into projects as you wait for them to arrive.

Well, Switch Electronics have solved all that for me. Their prices are pretty keen and they have lots of interesting components, even a special “Maker” section. If you’re based around Hull I’m sure you’ll find them very useful.