envsubst – Substitute Variables for Environment Variables

Hi πŸ‘‹,

Introduction

In this short article I want to showcase a nice and useful Linux command.

The comand πŸ₯ envsubst πŸ₯. In short it Substitutes the values of environment variables.

It’s great for populating configuration files with values from environment variables, a common operation for developers containerizing their applications.

Example Usecase

Let’s say we are containerizing an application and we have the following file configuration.yaml, and we want to modify the values of the environment field and the log_level field without adding the additional complexity of mounting the configuration.yaml file into the container/pod.

configuration:
  server: "the-app"
  environment: "production"
  log_level": "info"

To change the values of the environment and loglevel fields we create a configuration-template.yaml like so:

configuration:
    server: "the-app"
    environment: "$ENV_ENVIRONMENT"
    log_level": "$ENV_LOGLEVEL"

Then we run eventsubt to substitute the configuration values:

export ENV_ENVIRONMENT=stagging
export ENV_LOGLEVEL=trace

envsubst < configuration-template.yaml > configuration.yaml

The resulting configuration.yaml file will contain:

configuration:
    server: "the-app"
    environment: "stagging"
    log_level": "trace"%  

Conclusion

The envsubst command enables us to write configuration files with placeholders that will be replaced with actual values from the environment variables. Before being aware if this command I always thought that I have to somehow parse the files, substitute variables dynamically, ensure that the substitution didn’t change the file format or break something. Now I just envsubst .

Thanks for reading and happy dev-ing! πŸ–₯οΈπŸ§‘β€πŸ’»

envsubst help

Usage: envsubst [OPTION] [SHELL-FORMAT]

Substitutes the values of environment variables.

Operation mode:
-v, –variables output the variables occurring in SHELL-FORMAT

Informative output:
-h, –help display this help and exit
-V, –version output version information and exit

In normal operation mode, standard input is copied to standard output,
with references to environment variables of the form $VARIABLE or ${VARIABLE}
being replaced with the corresponding values. If a SHELL-FORMAT is given,
only those environment variables that are referenced in SHELL-FORMAT are
substituted; otherwise all environment variables references occurring in
standard input are substituted.

When –variables is used, standard input is ignored, and the output consists
of the environment variables that are referenced in SHELL-FORMAT, one per line.

Report bugs in the bug tracker at https://savannah.gnu.org/projects/gettext
or by email to bug-gettext@gnu.org.

A custom HomeKit accessory with Python

Hi πŸ‘‹,

In this short article I want to showcase how I implemented a custom HomeKit accessory with python.

My Home Assistant’s SD card died πŸͺ¦ a few days ago and the support for GPIO based sensors will be removed in newer releases. This makes it unsuitable for my needs, while giving me the perfect opportunity to try other things.

To continue monitoring temperature and humidity in my home I’ve built a custom HomeKit accessory with HAP Python.

The Sensor

A BME680 air quality sensor is used to monitor temperature and humidity. It is connected to the PI according to the following diagram:

The communication with the Pi is done using the I2C protocol. If you want to use I2C in your own setup, it has to be enabled using raspi-config, as it doesn’t come enabled by default.

# Execute
sudo raspi-config
# Then select Interfacing options->I2C and enable it.

Connection can be tested with the following command:

sudo apt-get install build-essential libi2c-dev i2c-tools python-dev libffi-dev git
/usr/sbin/i2cdetect -y 1
pi@raspberrypi:~ $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- 76 -- 

It will output the address that the sensor is using, in our case the 0x76 I2C address.

The Code for the Accessory

You can browse the full code for the accessory and bme680 sensor in my git repo.

To run the program, clone the repository and ensure that you’re running it under the pi user, otherwise you will need to change some things.

cd /home/pi && git clone git@github.com:dnutiu/bme680-homekit.git && cd bme680-homekit
sudo apt-get install libavahi-compat-libdnssd-dev
pip3 install -r requirements.txt

Verify that the program works by running python3 main.py. Running it the first time will prompt you to add the accessory to the Home app. If you miss this step you can repeat it by deleting the accessory.state file located in pi’s home directory and by running the program again.

After you’ve verified that it works, you can setup a systemd service to run the accessory’s python script when the PI boots

Copy the bme680-homekit.service to /etc/systemd/system and check that the service is running.

sudo cp bme680-homekit.service /etc/systemd/system
sudo systemctl status bme680-homekit

If you want to run this under another user rather than the pi, you’ll need to tweak the bme680-homekit.service file.

Congratulations for making it this far! πŸŽ‰

You can browse more code examples in the HAP-Python repository.

Thanks for reading and have fun! πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» βš™οΈ

How to connect Ethernet devices to WiFi via WDS

Hi πŸ‘‹,

In this blog post I will show you how to connect an Ethernet only device to Wi-Fi using an extra router and the WDS functionality. I initially wanted to install the TP Link Archer T4U WiFi adapter driver on my Ubuntu 20.04 PC but unfortunately the driver is no longer supported.

Since I really needed high internet speed for my PC, I decided to connect it via an Ethernet cable and buy another TP-Link router to use in WDS mode.

WDS mode allows routers to communicate with one another without using wires.

βš™οΈ The configuration of the WDS router is as follows. I left the operation mode in Router Mode.

Then, I navigated to Network -> LAN and set the IP address to 192.168.0.2. Because my old router had the IP address of 192.168.0.1.

The next step would be to navigate to Wireless -> WDS. Click survey and select the SSID of your root router. If the WDS bridging is successfully you should see a Connected message βœ….

Finally, go to Network -> DHCP Server and disable it. All these operations should be performed on the router that you want to bridge, not the root router! πŸ•ΈοΈ

Now that your router has a WDS bridge to the root router, you can connect your PC to it via an Ethernet cable and place it next to your PC. A brand new 5GHz Wi-Fi adapter might have the same price πŸ’° as a new router πŸ€‘, and to be honest unless you’re doing some penetration testing I don’t see the need of a WiFi adapter.

Thanks for reading and happy hacking! 🐧

How to make RØDE audio interface work on Linux

Hi πŸ‘‹,

I have recently updated the firmware of my RODE audio interface from 1.12.x to 1.13, and the device was no longer working properly on Linux, it had trouble with the microphone. I found out that it was an audio sampling issue.

dmesg -kH

[feb21 16:20] usb 1-9.3: 1:1: cannot set freq 44100 to ep 0x82
[  +0,005008] usb 1-9.3: 1:1: cannot set freq 44100 to ep 0x82
[  +0,005019] usb 1-9.3: 1:1: cannot set freq 44100 to ep 0x82
[  +0,004971] usb 1-9.3: 1:1: cannot set freq 44100 to ep 0x82
[  +0,015019] usb 1-9.3: 1:1: cannot set freq 44100 to ep 0x82

To fix it, I did the following:

1. Edit Pulse’s πŸ“– daemon.conf.

nano /etc/pulse/daemon.conf

And add the following lines:

default-sample-format = s24le
default-sample-rate = 48000
alternate-sample-rate = 48000

2. Disconnect the device and then kill the pulse daemon with πŸ”ͺ pulseaudio -k.

3. Connect back the device πŸ”Œ, it should work without problems.

Thanks for reading and happy hacking! πŸ₯·