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.

Azure IoT Hub for Free

orchids.png

Everything I achieved today I achieved before 9:30. It was all downhill after that. But I did achieve quite a bit. I’m making a bunch of connected devices and I want to use MQTT to link them together. I could set up my own MQTT broker on a local Raspberry Pi but that would only work around my house and I want these to work around the world. My weapon of choice for these things is Azure IoT hub which lets you connect remote devices to a central server (the clue is in the name).

It turns out that you can set up a Microsoft Azure account for free and then create an IoT hub which is also free and allows for up to 8,000 messages per day. I’m not sure how many messages I’ll need, but I’m hoping that will be enough for now.

Having created my free IoT hub the next thing I wanted to do was connect my devices to this. I’ve done it before and it worked just fine. I did it today and it didn’t. This was the cue to spend most of the day (or at least the portion up to the point where I gave up) trying t figure out why. Unsuccessfully. Wah.

It's the hope that kills you...

Today I saw the ultimate in error messages. I wasn’t doing anything particularly adventurous, just trying to play a video file. However, the error really scared me. I wish I’d taken a picture of it. The error said:

This video cannot be played. Please try again later. 

Such evil in 9 words. The video file was eminently playable, this device just couldn’t play that particular format. One of the things about computers that I like the most is the way that given the same inputs a computer will produce the same outputs. The error flies in the face of this, raising the possibility that next time you run the program it will work. I’m wondering if this is a reference to the possibility that in the future a later version of the program will be able to do it. Or that the program might learn how to do it? Or that in a parallel universe it actually does work. Who knows?

Most football fans will tell you that for a supporter it’s the hope that kills you. Hoping against all the evidence that this season will be “the one” even though you have a feeling in deep in the pit of your stomach that actually it won’t be. Perhaps this is a new trend by software developers to encourage users to keep coming back and trying again….

New MacBook Air First Impressions

Don’t try and download the PC migration tool from the Apple site. I got this.

Don’t try and download the PC migration tool from the Apple site. I got this.

It is always a bit of a worry when a new device becomes available just after I’ve got paid for some writing. That has just happened with the new MacBook Air. I was thinking of getting a new tablet and this would work out at around the same price once I’d bought a keyboard and everything else you seen to need. I also fancy having a go with GarageBand. I’ve only bought the cheapest MacBook because everything seems to be based on the same processor, the Apple M1 device.

I’m intrigued by the possibilities presented by the new processor. Many years ago I had one of the first ever tablet PCs powered by a Tramsneta Crusoe chip. The idea was that clever circuitry would “morph” the intel instructions into Transmeta ones as the program loads so that a simple low-powered RISC chip could be used in a portable computer.

The idea might have been a good one but the reality was horrible. You could actually watch (and wait for) on screen buttons going in and out when you clicked them. Programs took ages to “wind up” when you opened them and the whole thing was very painful to use.

No such problems with the MacBook Air. It fairly whizzes along. I’ve got Microsoft Office running and even though the Mac is using the Rosetta2 application to convert the Word code from Intel to Arm there’s no perceptible lag and the everything so far is very snappy. I’ve installed Visual Studio Code and that works a treat too.

The only problem that I’ve found is that an issue with “Big Sur” - the new Apple operating system - seems to have broken deployment to ESP devices. The espytool.py program that builds images and sends them to devices fails because the Python pyserial library doesn't work properly. There is a sort of fix, you can find it here, but it is a bit of a pain. The good news is that the build process goes quickly up to the point where it explodes.

I’ve moved my Microsoft ecosystem onto the new platform. OneDrive is showing me all my usual files and I’ll put Edge on next so that I’ve got the best browsing experience. It will be interesting to see how the battery life goes. What with this and the lovely Microsoft Surface Pro X which also uses an ARM processor, not to mention the impressive looking desktop cpus from AMD, these must be worrying times for Intel.

New Garage Door

We have a new garage door. Hold the front page.

Only minutes after I’d told number one wife that the previous door (which was the same age as the house - i.e. very old indeed) was absolutely fine I went outside to fetch something from the garage and one of the door support wires snapped. The effect was most impressive. The door jumped out of my hands and slammed shut. It was as if the thing was possessed. I’m pleased I wasn’t underneath it when it failed.

The new door can be opened and closed without the aid of a broom handle and lots of swearing. Which is a definite improvement.