Compile early and compile often

yet more leaves.png

I’ve been writing a big and complicated program for a while. I’m not sure if it is supposed to be big and complicated, it just seems to be turning out that way. And one of the things I’ve remembered from my days of doing this kind of thing is “compile early and compile often”. If you wait a while before hitting the compile button you can sometimes get so many errors that it can take you a while to figure out. Modern editors are quite good at spotting coding mistakes as you write, but there is nothing like letting the compiler take a proper look at your code.

I’ve been hitting compile every time I’ve written a few lines. That way, if I get an error I don’t have to look through much text. I do this even though I’ve no intention of running the program at that stage. It’s just that when I’ve done a bit that I think should compile it is worth getting the proper opinion on the matter.

When I was teaching programming we used to get people come in with 500 line programs that didn’t compile and they weren’t sure why. They’d written all the code down and then hit compile and were surprised by the number of errors they got. I used to tell them that a good way to start would be with an empty program that compiles and then add the statements a small amount at a time, compiling at each stage.

Conducting currents

The toaster failed yesterday. It did this at exactly the same time as the lights went out and the radio stopped working. Coincidence? I think not.

It turned out that one of the currents had fallen out of a teacake and landed between the element wire and ground. The resulting current flow (I like the way that sounds) had triggered the earth trip on that power circuit. So all I had to do was figure this out, and then go into the garage and flip the switch into the on position.

Current through currents is not always a win.

Solder pranking

When I was younger than I am now and much less responsible one of the standard workshop tricks we used to play on each other was to replace someone’s solder with a piece of solid core wire. They look very similar, it’s just that one of them doesn’t melt when you apply it to the soldering iron.

I did this to myself today. I’ve been wiring up some connected little boxes with solid core wire and I started to try and melt it. It took me a while to figure out what I’d done. If I’d done it to anyone else I’d be quite impressed with myself. As it was, all I did was burn my fingers a little bit…

Fixing Windows 10 USB problems

mouse power.png

Ever since I got my desktop (four years ago) I’ve had a problem with my mouse and Bluetooth connections randomly disconnecting. This was very irritating, particularly if it happened as I was coming into land or under heavy fire. Or both.

Number one son reckoned that my machine was possessed in some way, but it turns out that the truth is much simpler. I was allowing Windows 10 to turn off these devices to save power. I’ve no idea why a desktop machine should turn off your mouse and keyboard connections in this way, but it does. You have to explicitly tell it not to by using the USB Input Device Properties dialog above that you can find in the Device Manager. I made the change (for all my USB devices) a while back and it has worked for me so far.

Alarms are complicated

I’ve always found it surprising how simple things end up being complicated. Take adding alarms to my Connected Little Boxes…. I want to have alarms that I can set in the device. When an alarm time is reached I want the device to perform an assigned action, perhaps make a sound, drive a servo or change the colour of a light. This is simple I say to myself. And I write a function to celebrate:

bool atAlarmTime(struct ClockAlarm *alarm, struct clockReading *reading)
{
    if (alarm->hour != reading->hour)
    {
        return false;
    }

    if (alarm->minute != reading->minute)
    {
        return false;
    }

    return true;
}

The function will return true when the clock reaches the alarm time. I can use this to trigger whatever action is bound to the alarm. Simple.

Well, not really. My program will be repeatedly checking the alarm to see if it has triggered. So if I just use the above function I will find that an alarm will trigger continuously for the entire alarm minute when it due. And what about when I turn the device on or change the time? Do I want it to ignore “past” alarms or not? If my device was controlling a heating system I’d want it to respond to an alarm even to turn the heating on even if that event was in the past. Otherwise I’d turn the system on at 12:00 and freeze because the event that turned on the boiler was set to 7:00am.

I’ve ended up setting up options and flags and writing code that spins through the set alarms looking for ones that should have been triggered and triggering them if that is what the user wants. Yep. Alarms are complicated.

"Don't Buy" PIR Sensors

dontbuy pir.png

More PIR advice from Rob….

The UK consumer magazine Which? has a “don’t buy” advisory that it slaps on really poor products. I think I’ll have a go at that. I rate these HC-SR505 PIR sensors as a don’t buy. I got them thinking they would work as well as the HC-SR501 sensors that I’m using for my “Connected Little Boxes”. It turns out that they are a bad bet for two reasons.

  1. When they detect a person they hold that signal for around 13 seconds before releasing it. The timeout of the HC-SR501 can be adjusted and it has two different detection modes which are much more flexible.

  2. They stop working after a few hours.

Definite don’t buy.

Things not to do with PIR sensors

pir lense.png

“Hi. I’m Rob. I do stupid things with hardware so you don’t have to”

Today I thought it might be a good idea to see what happens if you try to use a PIR sensor without the plastic dome lens on top. I figured that I’d be happy to sacrifice a bit of sensitivity for a cooler aesthetic. In other words I wanted to get rid of the ugly dome and put the sensor behind a 3d printed panel.

Preliminary tests suggested that this might work, so I designed and printed a case lid and put the sensor behind it. Then it didn’t work at all. I think that 3D printed PLA is probably a bit too opaque to the infra-red signal that the sensor uses. Oh well.

Cascadia Code Looks Lovely

cascadia.png

I’m a very old programmer. I can remember when “Courier New” was the monospaced font that everyone was using. Then came “Consolas” which I still really like.

And now we have “Cascadia Code”. I’ve decided that this typeface is one of the reasons that I like Microsoft Terminal so much. It is the default font used by that program. I think it looks really good. There’s a whole family of Cascadia fonts including some very nice “light” monospaced fonts. Well worth a look if you want to spruce up your documentation a bit.

Microsoft Terminal is Awesome

microsoft terminal.png

Of late I’ve been spending quite a lot of my time in Windows PowerShell. One of the things that drives me a bit nuts about it is the horrible choice of colours for errors and whatnot. I’ve quite often found myself having to cut the text out of the window and paste it into Notepad just so I can read it with proper contrast. Colour combinations such as dark red on dark blue don’t work well for me. Perhaps its my age.

Anyhoo, I’ve found a way round this which works really well. It’s called Windows Terminal and you can download it from the Windows App store. It has tabs so you can have multiple shells open in the same window - which is great if you’re talking to several remote systems using ssh. It uses the same colour scheme but for some reason the text looks a lot better. It has awesome profile support that you can fiddle with and customize to your heart’s content. It also lets you use images as the backdrops to your terminal sessions.

Well worth a look.

Horrid little emails

horridemail.png

I’ve been getting these horrid little emails for a while. The one above is from a “quantum astrologer” called Roxanne. According to the message in the email the man in my life has “has done things in his past that are resurging today making him cold and distant...”. Blimey. Maybe what she really wants me to do is click the link at the bottom to “make sure that I don’t receive any emails from us”. Ugh.

Some things can be more fixed than they need to be...

Waking up in a toasty warm house is all very well, but perhaps not at 2:00 am in the morning.

It got so cold last night that the heating decided it needed to heat things up. So it did. In the middle of the night. I’ve now adjusted the night-time trigger levels so that the heating only comes on when it gets properly artic code at night.

Hopefully the next post here won’t be heating related.

Some things need fixing more than once....

A while back I fixed my central heating. And yesterday I did it again. It turns out that having a device that turns on the heating system is good. But having a heating system that actually heats when turned on is even better. The valve that actually lets warm water flow round the house was sticking, resulting in cold toes for Robert. Not a good thing. So now I am a little bit poorer, but a whole lot warmer.

Self destructing setting storage

lids.jpg

It turns out that creating useful embedded devices seems to be around 80% setting storage and management. I’ve created a nice little setting manager that exposes the settings as C structures, stores them in EEPROM and allows settings to be updated via JSON messages or via direct commands. Settings are bound to the process or sensor that needs them and it works well.

I thought it might be nice to be able to make a complete dump of all the settings so that I can configure a device by sending them all once. This mostly worked. The mostly bit was caused by the way that the settings are stored in EEPROM as they are received. Writing to EEPROM takes quite a long time, certainly longer than setting information takes to arrive down the serial port. So my program loses track of the inputs and misses some setting values.

“No problem” I thought. I’ll create a new setting that tells my device not to persist the setting values as they are received. That way I flip this setting and receive all the values at full speed. Then I save all the settings after they have been updated.

I added the code and got it all going. It mostly worked again. This time it would store more settings but still lose the plot towards the end of the setting file. It took me a little while to work out what was happening.

The block of settings I was restoring contained an instruction to turn the setting persistence back on again. So, half way through downloading the settings the program promptly breaks itself…

Using SSL security with ESP32 and ESP8266

bigerror.png

The story so far…..

Robert has been trying to connect an ESP8266 embedded device to an Azure IoT Hub, something he has done many times with no problems. Except it no longer works. Having spent a day failing, perhaps it is time for Robert to have some success. Now read on…..

Yesterday was not a good day. Today was better. To summarise my problem: I want to use my embedded devices with Azure IoT hub so that I can exchange messages with services in the cloud. Azure IoT hub is a “proper service” in that it supports device registration and uses the Secure Sockets Layer (SSL) for connections. I’ve written my code so that it uses the secure WiFi client in the ESP8266 and it has worked before. But yesterday it broke and I couldn’t figure out why. Today I did. Go me.

It turns out that the solution is simple. Add the statement above to set your secure client to insecure mode. But why does this work and what does it do? Does it mean that my connection is no longer secure? Now read on again…..

There are two reasons for using SSL. The first is to make sure that the data that is being transferred securely. I don’t want someone to be able to peek into the packets and pull out passwords and data. The second reason for using SSL is to make sure that the devices is talking to the right host. Without this authentication it would be possible for someone to make a system that pretends to be Microsoft Azure, accept a device connection and then steal all my stuff.

The first part of the problem is solved by encryption. The systems each end of the connection use keys to encrypt and decrypt the data so that anyone watching it go past would just see gibberish.

The second part of the security, making sure my host is really Azure, is solved by certificates which are issued by certification authorities. Microsoft have a certificate for Azure and my my device can verify this. However, to make this work I have to make a copy of the public part of the certificate and store it in my device, which is not something I’ve bothered with in the past. It turns out that I’m not really concerned about someone spoofing Microsoft Azure just to see my data. There is always a balance to be struck between security and convenience, and this is where I draw the line. It may be the wrong place to draw it, but I’m OK with it so far.

This means that my devices don’t have local certificate copies. Up until recently (and with the ESP32 device) the lack of local validation doesn’t cause a problem. But something in the Arduino libraries has apparently changed (or I’m using a different library now). Anyhoo, to make it work now you have to tell the WiFiClientSecure instance created to manage the connection that you are using insecure mode. This doesn’t mean that your data is now being sent insecurely. What it means is that the device is not verifying the server any more. And then it works.