Saturday, April 24, 2021

Simple MQTT handler in bash

How to subscribe to an MQTT topic efficiently in bash.

This is hosted on a Raspberry Pi which controls a pool pump via a solid state relay on GPIO 4, and these topics :

  • /pool/pump/action 0  # Command, set externally (subscribed)
  • /pool/pump/state  1  # Feedback state (published)


#!/bin/bash -feu
set -o pipefail

"mosquitto_${action}" -h -p 8833 -t "$@"

new_state=$(echo "$1" | sed 's/[^01]//g')
[[ -z "$new_state" ]] && new_state="0"

if /usr/local/bin/ 4 "$new_state"; then
echo "pump state change OK, now $new_state"
echo "ERROR: pump state change fail towards $new_state"

# Ping back
real_state=$(cat /sys/class/gpio/gpio4/value)
mqtt pub '/pool/pump/state' -m "$real_state" -r

export -f mqtt
export -f handler

cfg=$(basename $0 .sh).service
if [[ $action = 'stop' ]]; then
systemctl stop $cfg

elif [[ $action = 'start' ]]; then
systemctl restart $cfg

elif [[ $action = 'install' ]]; then

cat > /etc/systemd/system/$cfg << EOF
ExecStart=$(realpath $0)

systemctl daemon-reload &&
systemctl restart $cfg &&
systemctl enable $cfg

else # here is the deal

# Run only once
LOCK_FILE="/var/lock/$(basename $0 .sh).lock"
exec 9> "$LOCK_FILE" && flock -n 9 || exit 5
trap "rm -f '$LOCK_FILE'; logger 'Stopped $0 watcher on $(date)'" EXIT
logger "Started $0 watcher on $(date)"

# Wait for orders
mqtt sub '/pool/pump/action' |\
xargs -n 1 -I {} /bin/bash -c 'handler "$@"' _ {}



This small script embeds its own ''systemd'' config, just run it with ''install' as the sole parameter.

Wednesday, March 3, 2021

Install CH552 support for platformio (and CH5xx variants) with ch5xduino support

mkdir ~/.platformio/boards
cd ~/.platformio/boards
cat > ch55xduino.json << 'EOF'
  "build": {    
    "f_cpu": "24000000",
    "size_iram": 256,
    "size_xram": 1024,
    "size_code": 14336,
    "size_heap": 128,
    "mcu": "ch552t",
    "cpu": "mcs51"
  "frameworks": [],
  "upload": {
    "maximum_ram_size": 1280,
    "maximum_size": 14336,
    "protocol": "stcgal",
    "stcgal_protocol": "stc15",
    "protocols": [
  "name": "Generic CH552",
  "url": "",
  "vendor": "WCH"

platformio boards|grep ch5xduino
mkdir myproj552
cd myproj552
platformio init --board ch55xduino

cat > platformio.ini <<'EOF'
platform = intel_mcs51
board = ch552
build_flags =

extra_scripts =

cat >


Tuesday, July 7, 2020

Install KiCad 5.1 on xubuntu (no-brainer since the packages are somehow broken)

Big mess. The easiest is to get rid of everything and not rely entirely on the Debian packages. I had to gather some info around and I ended up with the following.

Beware, all existing kicad executable and configuration will be wiped out, as I think that 5.1 does not play well with former versions. Especially, libraries and footprints do not show up after installation. This also makes it annoying to import a former KiCad project in 5.1. I had none so I "simply" started from blank projects, as KiCad is much easier to design new packages and footprint than Eagle CAD I was using so far.

Microreview: dual outlet aquarium air pump

It took me a while to notice the little magic button on the back! Switching it got me twice the throughput ... and twice the noise, but it is still OK. I checked it by myself and emptied a 1.5L bottle in about 50 seconds with one outlet.

This is the pump I used to cool the deposited materiels in my old, other blog post

Soldering consumables

Soldering consumables

This cheap "No Clean Flux Dispensing Pen" pen is a bargain. I used one I chose randomly for years, and largely beyond its recommended date. So I was glad to see it still on the market: "951" DJP CW8100 (about ~5€ delivered from banggood).

I also bought this tip "rejuvenator" (about ~7€ delivered from banggood). It works surprisingly well to remove oxidization that could not be "wiped out" with metal sponges. No fumes, no odor, it kinds of "boils dry" instantly when the tip is dipped in it.
The container states it is some common soldering paste but I am glad it is not, as I already have all kinds of solder paste.

Friday, April 17, 2020

LinuxCNC on an old Dell D430 with a dock

sudo apt-get install git-core
sudo apt-get install libpci-dev
git clone git:// && cd smictrl
git clone git:// debian
fakeroot dpkg-buildpackage -uc -us
sudo dpkg -i ../smictrl_*.deb
sudo sed -i '/exit 0/ismictrl -s 0' /etc/rc.local
sudo reboot

Adding bCNC to the laptop:
git clone

Profile c++ code

To profile an executable and get a nice visual of it (you need the expected flags when compiled of course):

First you need to compile with argument ''-pg'', so that gcc inserts its code all everywhere to time your function calls.

Once you run the project, it will generate a big file named ''gmon.out''. This is mostly unreadable per se. And ''gprof'' hardly makes it more readable.

Here comes '''' and ''dot'', which generate very readable graphs:
gprof exe/router.exe | -s | dot -Tpng -o profiler.png && gwenview profiler.png

Note that ''gprof'' must be given the executable name. It will not run it in anyway, but it needs the symbol table to give you proper names.