Making "The Exchange" - an AI you an ring up

Perhaps when I’ve built it I can ask it why it doesn’t work…

I hate it when things in a project go too well. It usually means that things are due to go not very well in a short while. I’ve got my Large Language Model running on a Pi 5 and I thought I’d use this to create “The Exchange”. This will work with “The Red Telephone”. The idea is that you pick up the receiver on your telephone and dial a “3”. A robotic voice asks you to state your business. You give your question and then put the receiver down. After a decent interval “The Exchange” rings back with the answer.

To make this work the phone needs to capture audio input from the phone, use speech to text to get the question text and send this to the Large Language Model Pi 5. The Pi 5 will respond with the answer and I can use the text to speech in the red phone to deliver the answer. Sounds simple enough.

I’ve found this lovely library which can run on the Raspberry Pi Zero in the phone and convert speech to text. It’s a bit slow, but I don’t care about that because I can record the question and then use speech to text on recorded sound file after the user has rung off. So the next thing I need to do is find something I can use to record audio into the Pi. Up pops https://www.npmjs.com/package/node-record-lpcm16 and that works a treat too. At this point my spidey sense is tingling a bit because things are going too well.

So I start to build the program. I write the code that tells the user to state their question and then records their response. It’s bound to work because I’ve tested it. But of course it doesn’t. The speech playback (using eSpeak) works a treat but the audio recorder fails because it can’t find the input device. Everything works fine individually. The only time it fails is when I ask it to do what I want it to do. I get this a lot when writing software.

I do have a fix though. If I run the whole application as a super-user it works. I’ve no idea what the speech generator is doing with the sound device, but giving the sound recorder awesome system powers seems to enable it to find a sound input device and make a recording.

I’ve spent a bit more time investigating the problem. I’ve added a timeout after the speech output finishes to give it time to release resources. I’ve tried different device names rather than the default one. But nothing works.

It’s not a huge problem if the application has to run as supervisor I suppose (although I’m not a fan of this approach). And I consider “Because it works that way” a perfectly reasonable answer to the question “Why have you done it that way?”. So I’m going to pop the question on the back burner for now and carry on.

Start your node.js applications when your Raspberry Pi boots

Spring is coming…

If you’ve got an application (perhaps one that lives inside a red telephone) which you want to run when your machine boots there is a really good way to do this. You turn your application into a service. You can make your application start when the Pi boots and you can also start and stop the application from the command line.

We can use the systemd to this for us. This orchestrates the startup and management of services. To use it with a Node.js application, we create a systemd service unit file.

Start your editor of choice and create a file named redserver.service (if you’re controlling a red telephone - otherwise use a more meaningful name) within the /etc/systemd/system/ directory. This file defines a service for systemd to manage.

[Unit]
Description=Red Phone Server/home/rob/RaspberryPi-DialTelephone
After=network.target

[Service]
ExecStart=/usr/bin/node /home/rob/RaspberryPi-DialTelephone/redserver.js
WorkingDirectory=/home/rob/RaspberryPi-DialTelephone
Restart=on-failure
User=rob

[Install]
WantedBy=multi-user.target

Note that the file sets the working directory for the application and specifies the command that starts the service. You can now enable and start your service with the following commands:

sudo systemctl enable redserver
sudo systemctl start redserver

The service will now run each time the Pi boots. You can use systemctl to stop the service. This is useful if you want to debug the service - things don’t normally go well if you have two copies of the thing running…

sudo systemctl stop redserver

If you want to restart the service you just start it again.

sudo systemctl start myapp

If you ever want to disable the service completely and stop it running when the system boots you can use this:

sudo systemctl disable myapp

I’ve found this very useful. So useful that I’ve made a blog post so that I can find it again later…

Running a Large Language Model on a Raspberry Pi 5

I never said it was perfect

I found this article which describes how you can get a something a bit like ChatGPT running on a Raspberry Pi. You need the latest Raspberry Pi 5, and it also needs the largest one with 8Gb of memory but it does work. It’s a bit slow and not very accurate, but it is fun to chat with. I think it is very useful to show slightly broken versions of the technology to folks so that they start to understand their limitations.

I’m now very tempted to make an “exchange” for the Red Phone which runs this engine and rings you back with answers to questions.

Robot Power Adapter

Very useful if you kept buying 12v power adapters instead of 5V power adapters….

One of our students showed me a useful gadget today. You can plug a 12 supply into it and get out 5v, 3.3v and the 12v going in. The two onboard regulators aren’t good for particularly high currents at around 800ma each, but this is a terribly useful controller if you are building a robot and want 12 for the motors and then the other voltages for your onboard devices.

Pimoroni Pi MMe base

Fun times..

The Raspberry Pi 5 is a seriously powerful beast. I’ve been very impressed by the way I can run even demanding graphical applications at reasonable speed. Today I thought I’d speed it up even more by adding a “proper” hard disk to it. I got hold of an MMe base from Pimoroni and popped a 256G solid state disk in it. The total cost was less than a video game. The base was easy to fit and setup. the hardest bit was feeding the funky ribbon cable into the connectors on the Pi and the base. Once they were connected I screwed the base onto the Pi and fired it up. My Pi was already running from an SSD memory card so I used Raspberry Pi Imager to put an operating system on the new disk, swapped the boot order using raspi-config and we were off. The difference in speed for loading applications is amazing. The system boots in around the same time it takes my PC to wake up from sleep.

If you are after a speedup for your Pi which will turn it into a much more viable desktop replacement you should take a look at this.

Raspberry Pi 5 is really rather good

I didn’t order a brand new Raspberry Pi 5 when it was first announced. I think I managed to buy the earlier versions on release day, but for some reason this time I wasn’t that interested. However, I had a need for a fast Pi recently and so this week I finally got one.

It is really rather good.

When I got the Raspberry Pi 4 I was kind of expecting it to be a viable desktop alternative. After all, that is what some people were saying it was. It wasn’t. Browsing was slow and Visual Studio Code ran too slowly to be properly useful. But the Pi 5 is much more of a contender. Some of this might be down to the amount of memory available, my Pi 5 has 4G of ram which must help things along. I’m using it over VNC and I think I’d be happy enough to work there. Visual Studio Code seems to trundle along quite happily and web pages open instantly.

It is much more expensive than the original Pi. And of course you have to add the cost of the case, power-supply and micro-sd card to the total price. But for the money I reckon you get something pretty impressive and properly useful. And if you want to go back to the days of tiny, cheap Raspberry Pi’s you just have get yourself a Pi Zero.

Effortless Debugging with Your Pico Probe: No More Sudo!

Hey there, Raspberry Pi enthusiasts! Are you tired of summoning sudo every time you need to commune with your Pico debug probe? Fear not! Today, we're delving into a nifty trick to make your Pico probe obedient without the superuser incantations. And for those of you using Visual Studio Code, this trick is a game-changer.

Why Bid Farewell to sudo?

Summoning sudo for debugging is like wearing a full suit to a casual brunch; it's overkill and can be risky. Moreover, when you're using elegant tools like Visual Studio Code, running the debugger as a superuser inside the tool is a bit tricky. Let's simplify this, shall we?

The Power of udev Rules:

In the Linux realm, udev rules are like spells that control how the system interacts with various devices. By crafting a specific udev rule, we can grant ourselves permission to access the Pico debug probe without escalating our privileges to the superuser level.

Crafting the Universal Pico Probe Rule:

Most Pico debug probes share the same Vendor and Product IDs, so we can usually use a standard rule for all. Here's how to do it:

The Magic Numbers: For the Pico debug probe, the typical IDs are: Vendor ID 2e8a and Product ID 000c. We'll use these in our rule.

Creating Your Rule: Head over to /etc/udev/rules.d/ and conjure a new file called 99-pico-debug-probe.rules Sprinkle the following line into it:

SUBSYSTEM=="usb", ATTRS{idVendor}=="2e8a", ATTRS{idProduct}=="000c", MODE="0666"

This incantation essentially tells your system, "Hey, let everyone read and write to this device, will you?"

Enforcing the Rule: To make the rule take effect, use this spell:

sudo udevadm control --reload-rules && sudo udevadm trigger


What If My Device Is Different?

Got a different device? No problem! Plug it in, run lsusb, and look for the ID format VendorID:ProductID. Use these values to create a custom rule.

Post-Reboot Harmony:

Your new rule is like a loyal house-elf, diligently working after every reboot, ensuring smooth, sudo-free access to your debug probe.

Conclusion:

And there you have it! A simple yet elegant solution to make your debugging sessions with Raspberry Pi Pico as smooth as butter. No more sudo, no more hassle, especially when you're working in sophisticated environments like Visual Studio Code.

Happy debugging, and may your code be bug-free!

Author’s note: I didn’t write this. ChatGPT did. I worked with ChatGPT to solve my problem and then asked it to write a blog post about the solution “In the style of Rob Miles”. I’m quite proud of this, although I didn’t do much of the work….

Raspberry Pi Popup in Leeds

I bought a new mousemat too

We were in Leeds today Christmas shopping. I was really pleased to see a Raspberry Pi popup in Victoria Gate. We went in and had a look around. I tried to impress the staff by showing them my by-line in a copy of HackSpace magazine they had on sale. I think it worked. They did give me a free sticker.

The store was excellent. The staff were great and it was lovely to be able to take a proper look at some devices I’ve only ever seen in pictures up to now. I hope they open it again.

Redphone for Halloween

Only a bit scary

The “RedPhone” lives. The bell sounds like a real phone. But the insides are now possessed by a Raspberry Pi running JavaScript…..

This runs on the local network

It hosts a web page into which you can type messages and send them to the phone. When you send the message the phone rings and if you pick up the receiver you hear a ghostly voice dictate the message to you. If you pick up the receiver to make a call you get the dial tone. If you dial “2” and replace the receiver the phone will ring a few seconds later and deliver a ghostly message of doom:

randomMessages = [
    "I know what you did last summer",
    "Is that you, Boris?",
    "Look out of the window.",
    "They are on to you.",
    "Look behind you."
    ];

Other messages may be available later….

It’s been great fun to build. You’ll be able to find out all about it an upcoming article in HackSpace magazine.

Don't Buy a Raspberry Pi3 Model A+

Just Say No

It seemed an appealing purchase. I need a small Raspberry Pi to put inside a telephone (as you do) and this looked like it might offer a bit more power and connectivity than a Pi Zero. It arrived yesterday and I must say I’ve been completely underwhelmed by its performance. I was kind of hoping to be able to use remote development in Visual Studio Code to write and deploy Python code in the Pi. It doesn’t work. It never actually displays an error message. It just maxes out and then stops.

On the other hand the Pi Zero 2 that I got seems to work a lot better. I thought the devices would be broadly equivalent, but it seems that the zero is much better.

Magazine Takeover

They are both jolly good reads

I’m in two magazines this month. Which is nice. If you pick up a copy of the latest HackSpace magazine you will find my article about creating a PICO Powered Bluetooth remote control for a light. This is known as the “milkshake” project because I put the device in an empty milkshake container. Delicious. Then, in MagPi magazine you can find a reprint of my article about making a chord keyboard. This is known as the “missing wires” project because I forgot to add some of the wiring to the design.

PICO Examples vs OneDrive - Fight!

Size comparison with an SD card. Remember them?

I’m doing some work on the Raspberry Pi PICO. To make things easier I bought a debug probe. It’s tiny. It comes with all the cables that you need for your PICO, but you do need to solder three pins onto your PICO device to connect it up. That is, unless you get the much sought-after “H” versions of the PICO which come with pre-soldered pins and a tiny socket that connects directly using the cable provided with the kit.

It works a treat. Rob’s pro-tip: read the instructions very carefully and do all the steps, otherwise getting it working takes twice as long.

I installed the PICO SDK using a script you can find here. The script works fine, but by default it installs the examples into one of your document folders (or at least it did for me). I use OneDrive for file synchronisation and the next thing that happend (at least on my machine) is that OneDrive went nuts and started uploading thousands of tiny files into the cloud. I don’t remember seeing an option to install the examples somewhere else when I ran the script, but if you do the install I’d strongly recommend you look for it and put the examples somewhere in a folder which is not being synced with the cloud.

Heading to Driffield

David asked me if I fancied giving a talk to some sixth formers at Driffield. A chance to perform in front of an interested audience? Count me in. I took along a few toys, the trombone controller and my cut-price laptop. Much fun was had. The students were great. Lots of lovely questions (although one person did ask how tall I am - and after I had specifically told them not to do that). Kids eh?

I love telling the tale of embedded development. This is the best time ever to be doing it. Making stuff has never been so easy, so cheap, and so useful for building up your personal brand. I’m looking forward to going back some time in the future to see what they have been making.

David had brought in some trombones for me to look at, including the super shiny one above. It was nice to be able to compare the action of my sensor with something real.

Rob at Dot Net North

I’m back on the road again. Dot Net North have kindly invited me to be the speaker at their first in-person event since the pandemic kicked off. Really looking forward to the event. I’m going to be talking about making music with hardware. There will be devices you can build, devices you can marvel at and hopefully devices that work in front of an audience.

The event is in Manchester on the evening of Tuesday 20th September. You can sign up here.

Using the comport object in Pure Data

I’m building a Raspberry Pi 4 based device that uses a thing called Pure Data to play sounds in response to incoming MIDI messages. The device also has a controller and display which is controlled by a Raspberry Pi PICO. I’ve connected the hardware for a serial connection and discovered how to send serial messages from the Circuit Python program in the PICO. Now I need to get the messages from the PICO into my music making software. This is how to do it.

The above patch fragment (is that even a thing?) shows how it we can surface serial data in a Pure Data patch. However, before we can do this we have to add the comport object to your Pure Data installation. If you re using a Raspberry Pi it turns out that this is very easy:

sudo apt install pd-comport

This console command installs the comport object. Now we can add one to our patches. The patch above shows that we can send it messages to make it do things. The message on the left sets the device name. The second messages sets the baud rate. The third message asks the comport to display all the available comports that Pure Data has found.

When the above patch is triggered by a message on the inlet it will set up a comport and set the baud rate to 19200. Any messages that we send to the comport object will be sent out of the comport. These need to just lists of bytes. If the comport receives any bytes from these are sent out of the bottom of the comport object. In a while we’ll take a look at how we can encode and decode messages that we want to send over the serial connection.

Linking a Raspberry Pi 4 and a PICO over a serial connection

I want the Pure Data patches in my Chocolate Synthbox to be able to display lights that flash in time with the music. The lights in question are a bunch of neopixels connected to a Raspberry Pi PICO which is handing all the inputs and outputs for the device. I’ve done this to keep the design simpler and to remove any chance of issues with the sound code on the Pi interfering with the pixel animations.

However, to make it work I have to connect the Pi and the PICO together. Both devices have plenty of serial ports, so the best way is just to use one of those.

On the Raspberry Pi 4 (note that this only works for the 4) there are four serial ports which are surfaced on the “hat” connector. You have to enable them and they surface as devices you can use.

You enable them by editing the /boot/config.txt file:

sudo nano /boot/config.txt

Then, if you want to enable serial port 2 (which I do) add the following line at the end of the file:

dtoverlay=uart2

Save the file and then restart the Pi. You can now ask it which pins it uses with the command

dtoverlay -h uart2

The important part of this information is the “uart 2 on GPIOS 0-3. This means that the pins will be assigned as follows:

Pin 27 GPIO0 UART2 TX
Pin 28 GPIO1 UART2 RX

The other two pins (GPIO2 and GPIO2) can be used for hardware handshaking, but I’ve not enabled that. The device will appear as /dev/ttyAMA1, I’m going to use it in Pure Data (but that’s a different blog post. For this one, let’s discover how to connect the port to a PICO. I’ve decided to use uart1 in the PICO. This can be used on pins GP8 (TX) and GP9 (RX). So the wiring I need is:

Raspberry PI         PICO
Pin 38 GND           Pin 13 GND
Pin 27 GP0 UART TX   Pin 12 GP9 UART1 RX
Pin 28 GP1 UART RX   Pin 11 GP8 UART1 TX

Note that the TX and RX are crossed over. The PIC is running Circuit Python, this is how to connect a program to this port:

serial_port = busio.UART(board.GP8, board.GP9, baudrate=19200,receiver_buffer_size=500)

Now if the Circuit Python program in the PICO sends data to this port it can be picked up in the PI.

Auto-Start a Pure Data Patch on Raspberry Pi Bootup

Today I needed to find a way to auto start a Pure Data patch when a Raspberry Pi boots up. This is so that the Chocolate Synthbox (patent pending) can play sounds without the owner having to do anything to get it going.

Above you can see how I did it. The /etc/xdg/lxsession/LXDE-pi/autostart file contains commadns that are obeyed once the desktop has loaded. I used this command to start the editor:

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

I added the line you can see at the bottom:

sudo pd /home/pi/Desktop/Synthbox/ChocSynthboxMain.pd

This works well for me because the file Synthbox on my desktop contains the file ChockSynthMain.pd If you want to use it you change this to the location of your Pure Data patch.

You can use this technique to start any other programs on power up.

Moving a Raspberry Pi window around

I love working with PureData on the Raspberry Pi. But there are one or two issues. You can see a partiularly irritation one above. When you create a new window in PureData the window is positioned so that the top of the window us underneath the task bar. This means that you can’t click on the top of the window to move it around the screen. I’ve been racking my brains for ages to find a way to get the window into a more useable position. It turns out that the solution is really simple. If you hold down the ALT key and then click anywhere in a window you can then drag that window around the screen.

Wonderful when you know it….