Create a QR code from the command line

Lindsay, my wife, has been working on her online shop (gestempelt & gedruckt — unique handicrafts inspired by nature) for the last few months. Now that it’s complete, she's eager to get the word out with flyers. For this, she needed a QR code with the shop's URL to make it easier for people to scan.

So how do you generate a white QR code with the rest being transparent without needing to sign up to a website?

Use qrencode and ImageMagick on the command line.

First, install the applications using:

brew install qrencode imagemagick

Then, generate the QR code:

# Create the QR code (white on black).
qrencode -s 20 --foreground=ffffff --background=000000 -l H -o "URL.png" "https://gestempelt-und-gedruckt.ch/"

# Replace black with "transparent".
convert URL.png -fuzz 10% -transparent black URL_transparent.png

Published: 2024-11-03

Tagged: TIL Tools QR code

Building a temperature sensor for Home Assistant with a DHT22 sensor, an ESP8266 and ESPHome

In order to better understand our temperature needs and to humour my interest to tinker with electronics I've decided to build some temperature and humidity sensors and have them installed both in- and outdoors. I'll be using ESPHome which talks to Home Assistant for this build.

I'm started off with a board from AliExpress containing a DHT22 temperature and humidity sensor with an attachable ESP8266 microcontroller.

Programming the ESP8266 microcontroller

To program the ESP8266 one needs an adapter (ESP-01 Adapter Board) which needs to be slightly modified with a button so that we can enable its programming mode (instructions).

ESP-01 Adapter Board used to program the ESP8266 Underside of the ESP-01 Adapter Board used to program the ESP8266 showing the pins used for wiring the button to

Assembly

All the parts required to assemble the temperature and humidity sensor

Having the parts ready I'm logging in to my Home Assistant and heading to the ESPHome view to add a new device. I create a new configuration and choose the ESP8266 as the device, then once it offers the "Install" option I insert the controller to be programmed into the adapter board, keep the button pressed and plug it into one of the USB ports of my Raspberry Pi running Home Assistant. Once plugged in I release the button again. At this stage I can then click the option to install the first version of ESPHome onto the controller via "Plug into the computer running ESPHome Dashboard" (be aware that this might take a while as it has to fetch dependencies and compile the custom build). This step will flash enough for the controller to be able to connect to the Wi-Fi and be updatable via ESPHome running on Home Assistant. It's important to note that the Raspberry Pi likely won't have enough juice to start the controller and hence it won't be doing anything after being programmed.

At this point I disconnect the programmer, take the controller out of the adapter, connect it with the board containing the DHT22 sensor and feed it with 5V from a power supply. One should see 2-3 LEDs light up. Shortly after the new device should appear as connected in ESPHome at which point the configuration of the device can be extended with options to tell the controller what kind of sensors are connected and with what kind of name it should be reporting the values to Home Assistant.

sensor:
  - platform: dht
    pin: GPIO2
    model: DHT22_TYPE2
    temperature:
      name: "Balcony Temperature"
    humidity:
      name: "Balcony Humidity"
    update_interval: 60s

Once added, save the configuration and click the "Install" button to remotely update the configuration on the device via the wireless connection. If you look at the logs of the ESPHome component for about 60s it should eventually log the temperature and humidity it just collected via the DHT22 sensor.

[13:13:26][D][dht:048]: Got Temperature=15.5°C Humidity=80.3%
[13:13:26][D][sensor:093]: 'Balcony Temperature': Sending state 15.50000 °C with 1 decimals of accuracy
[13:13:26][D][sensor:093]: 'Balcony Humidity': Sending state 80.30000 % with 0 decimals of accuracy

Assembled board & controller without the case

At this point I knew that the components and network connection worked as intended and that I can continue with the integration of it into the case. For the case I've used a modified version of Andreas Spiess's configurable 3d printable box with an arm that ensures the DHT22 sensor is far enough away from the controller and power supply to not be affected by it.

The case requires the DHT22 to be de-soldered and extended which is a bit finicky.

De-solder the DHT22 sensor from the board

After a bit of fiddling this is the end result just prior to clicking the box shut.

Finished integration of the DHT22 sensor board with ESP8266 and power supply into the 3D printed case

I've used some double-sided tape to fix the boards to the case, used a cable tie to ensure the power cord isn't pulling too much on the power supply and finally used some heat shrink tubing for the sensor connections just to be on the safe side. Worth noting, while I did write that this sensor goes onto the balcony, it's not intended to be operated in a wet environment as both the sensor is exposed and the 3d printed case isn't waterproof.

Published: 2024-10-13

Tagged: ESP8266 DHT22 Home Assistant Microcontroller ESPHome Smart Home

Use Swiss keyboard shortcuts for line commenting in Sonic Pi

Every now and then after a day at work I enjoy toying around with Sonic Pi, an amazing application for making music with code.

There are a lot of great talks by Sam Aaron the creator of Sonic Pi on Youtube but I like this one quite a bit.

So while I was sitting there with my glass of Port working through the Sonic Pi Tutorial I had the urge to gain back my text editing fluency I'm used to from my day job. Luckily Sonic Pi supports a lot of keyboard shortcuts however as of now they're optimised for a keyboard layout of the english language.

After a bit of tinkering and the help of Karabiner-Elements I managed to re-map my line-commenting shortcut of CMD+Shift+7 (I'm working on a MacBook with a Swiss German keyboard) to the shortcut defined by Sonic Pi M-/.

The configuration you'll want is:

{
  "title": "Remap CH comment combo to EN comment combo",
  "rules": [
    {
      "description": "Remap CH comment combo to EN comment combo",
      "manipulators": [
        {
          "conditions": [
            {
              "bundle_identifiers": [
                "net\\.sonic\\-pi\\.app"
              ],
              "type": "frontmost_application_if"
            }
          ],
          "from": {
            "key_code": "7",
            "modifiers": {
              "mandatory": [
                "left_command",
                "left_shift"
              ]
            }
          },
          "to": {
            "key_code": "keypad_slash",
            "modifiers": [
              "left_command"
            ]
          },
          "type": "basic"
        }
      ]
    }
  ]
}

If you also got this need then following these steps should set you up.

  1. Install Karabiner-Elements
  2. In your terminal go to ~/.config/karabiner/assets/complex_modifications
  3. In there create a file remap_ch_comment_combo_to_en_comment_combo.json and fill it with the configuration above.
  4. Then start Karabiner Elements, go to the Complex Modifications tab Complex Modifications tab and there click the Enable button for the new configuration Enable button.

Published: 2022-07-14

Tagged: Karabiner-Elements Sonic Pi

Archive