Home › Category Archives › Linux

HOWTO: NextThing CHIP as Home Automation Device and PWM 12V LED Dimmer

This little project is simply to set up a CHIP to control 12V LED lighting on/off/dim. My purpose is to contol the LED lighting with PWM in my boat using an Amazon Echo. To do this we need to set up ha-bridge on the CHIP device, enable PWM on CHIP, and then wire up a simple circuit to control the 12V DC that runs the LED lighting.

Note that I did this using a freshly flashed CHIP with 4.4 kernel (headless). I’m not certain, but I think the stock kernels <=4.3 had PWM issues.

Let’s start! Here is my list of commands, all run as root user:

apt-get update
apt-get upgrade
apt install -t jessie-backports  openjdk-8-jre-headless ca-certificates-java openjdk-8-jdk-headless
apt-get install build-essential git
mkdir /usr/local/share/habridge
cd /usr/local/share/habridge
wget https://github.com/bwssytems/ha-bridge/releases/download/v5.1.0/ha-bridge-5.1.0.jar

Now, create the file /etc/systemd/system/habridge.service with the following contents:

Description=HA Bridge

ExecStart=/usr/bin/java -jar -Dconfig.file=/usr/local/share/habridge/data/habridge.config /usr/local/share/habridge/ha-bridge-5.1.0.jar


Configure it to run at boot:

systemctl daemon-reload
systemctl enable habridge.service

Note: To run manually you can simply do:

java -jar ha-bridge-5.1.0.jar

Now, let’s fix the PWM output:

apt install device-tree-compiler
cp /boot/sun5i-r8-chip.dtb /boot/sun5i-r8-chip.dtb.bak.$(date -d "today" +"%Y%m%d%H%M")
apt-get install device-tree-compiler
fdtput -t s /boot/sun5i-r8-chip.dtb "/soc@01c00000/pwm@01c20e00" "status" "okay"
fdtput -t s /boot/sun5i-r8-chip.dtb "/soc@01c00000/pwm@01c20e00" "pinctrl-names" "default"
fdtput -t x /boot/sun5i-r8-chip.dtb "/soc@01c00000/pwm@01c20e00" "pinctrl-0" "0x67" # 0x63 for older v4.4

Simple! Now, reboot and verify that PWM looks ok:

root@chip2:~# ls -l /sys/class/pwm/pwmchip0/pwm0
total 0
-rw-r--r-- 1 root root 4096 Jan 15 01:42 duty_cycle
-rw-r--r-- 1 root root 4096 Jan 14 19:45 enable
-rw-r--r-- 1 root root 4096 Jan 14 19:45 period
-rw-r--r-- 1 root root 4096 Jan 14 19:45 polarity
drwxr-xr-x 2 root root 0 Jan 15 23:10 power
-rw-r--r-- 1 root root 4096 Jan 15 23:10 uevent

If you get “file not found” something is likely wrong above…

Ok, let’s create a script that can control the PWM output:

cd /usr/local/share/habridge/
mkdir scripts
cd scripts/
cat > pwmTest.sh

Paste the following into the pwmTest.sh file (press CTRL-d to get back to the prompt after pasting)


USAGE="Usage: $SELF brightness (0-100)"
SELF=`basename "$0"`

if [ -z "$1" ]
echo $USAGE
exit 1


# Check to make sure the argument is a number
if [ "$BRIGHTNESS" -eq "$BRIGHTNESS" ] 2>/dev/null
# Is a number
echo $USAGE
exit 1

if [ "$BRIGHTNESS" -lt 0 ]

if [ "$BRIGHTNESS" -gt 100 ]

MAX=`cat /sys/class/pwm/pwmchip0/pwm0/period`

LEVEL=$(expr $MAX / 100 \* $BRIGHTNESS)

echo "$CURRENT  Set to $1 : $LEVEL" >> $LOGFILE
if (( "$LEVEL" > "$CURRENT" ))
for ((x=$CURRENT ; x<$LEVEL; x+=$STEP))
echo $x > $DCFILE
#echo $x #>> $LOGFILE
sleep $DELAY
for ((x=$CURRENT ; x>$LEVEL ; x-=$STEP))
echo $x > $DCFILE
#echo $x #>> $LOGFILE
sleep $DELAY

Fix the permissions:

chmod +x pwmTest.sh

Now set up the HA device: In your web browser go to your CHIP’s IP, and you should see the HA-Bridge webpage. Click “Add/Edit” and fill out the page like the following:

Note: After you fill in the script etc. for the “On Items” (as well as Dim and Off) be sure you click the green “Add” on the right!

Once it’s filled in, click “Add Bridge Device” at the top and this part is done.

If you only want to control a small LED, all you need to do is connect the LED from the PWM output to GND with a small resistor in series and it should work!

Once this is done, I was able to say (to my Amazon Dot) “Alexa, discover”, and once it had discovered the new “device”, I can say “Alexa, set cabin light to 25%” and it fades in to 25% power!

Coming next: Controlling a 12V LED light! I’ve done it… just need to document it.

Share Button

Backing up MySQL database by database and table

Backing up mysql databases using mysqldump is pretty quick and painless:
mysqldump -u username -ppassword -h hostname db_name > backup_file.sql
mysqldump -u username -ppassword -h hostname --all-databases > backup_file

The only problem is that it becomes quite cumbersome recover a particular table.

I wrote a little script to backup each table in each database to its own file:


if [ $# -lt 2 ]
        echo "Usage: backup_all_tables username password [host]"


if [ $# -eq 3 ]


for db in `echo "show databases" | $mysql_bin -u $user -p$pass -h $host | grep -v Database | grep -v information_schema`
        for table in `echo "show tables" | $mysql_bin -u $user -p$pass -h $host $db | grep -v "Tables_in"`
                echo "Backing up $host.$db.$table to $backup_path${host}_$db.$table.sql"
                $mysqldump_bin -u $user -p$pass -h $host $db $table > ${backup_path}${host}_$db.$table.sql

save the script as “backup_all_tables.sh”, edit the $backup_path variable to suit, do a chmod +x backup_all_tables to make it executable, and run it using:
./backup_all tables username password hostname
If the database is on your local machine you can omit the hostname.

This will backup the files in the format “host_db.table.sql”, ie: “localhost_mysql.user.sql”

Share Button

Huge sendmail queue – clearing it out

There are numerous reasons that the mail queue can get over full, ranging from general network problems, to having a user repeatedly sending an email with a large attachment to an incorrectly typed address.

The first thing you likely want to do is get some information on what’s in the queue:
To find out how many messages are in the queue:
mailq | tail -1
This will give you the total number of emails in the queue, ie:
Total requests: 6592
You might also want to know how old or new they are, so:
ls -lt | tail -10
ls -ltr | tail -10
will show you the oldest 10 and the newest 10 respectively.
To have sendmail retry the emails, simply run (as root):
sendmail -q -v
If you are in a situation where it has filled up the disk and need to fix it FAST, move the directory to another partition and then have sendmail process it from there (stopping sendmail with “/etc/init.d/sendmail stop” is recommended:
mkdir /root/tmpq
mv /var/spool/mqueue/* /root/tmpq
sendmail -q -v -oQ/root/tmpq

(if you get an error like “Argument list too long” instead run “mv /var/spool/mqueue /root/tmp” with sendmail stopped, then restart it)

Share Button

Recursively Change Text in Files

Handy little Perl one liner example to find and replace text across multiple files/directories… as an example, to change every .php file recursively from the current directory, changing “find this” to “replace with this”:

find . -name '*.php' -print0 | xargs -0 perl -pi -e 's/find this/replace with this/g'

Share Button

Support for JACK Audio in Adobe Flash

I use JACK quite frequently for playing guitar and recording etc, and while it is quite flexible, one thing I was unable to do was this: Get Flash audio to route through JACK. Most commonly this is when I want to play a Youtube video, and I find I have to stop jackd, which requires shutting down most JACK-aware applications, play the video, restart JACK, blah blah blah. Not really functional.


A quick search and I found libflashsupport-jack – and was excited! Look, something to make Flash work with JACK! So I checked out the git repo at git://repo.or.cz/libflashsupport-jack.git but it wouldn’t compile. Looked into it, and the major problem was that it includes a deprecated file in the kernel headers: linux/videodev.h, but alas, as of kernel version 2.6.38 the header file linux/videodev.h is no longer included in the kernel.


Long story short: I got the file, put it in with the libflashsupport source, hacked around with the code a bit, and TADA! I can now play Flash when JACK is running!


Even more importantly, for you that is, I’ve made a fork of the old repo, and have made the new working version available!


I’ll set up a proper homepage for this little project, but for now I’ll just link to the github repo and some instructions Note: if you go to http://github.com/pvint/libflashsupport-jack2 there is also a link to download a zipfile):

git clone git://github.com/pvint/libflashsupport-jack2.git
cd libflashsupport-jack2
sh bootstrap.sh
make install

(You must be root to do the make install, of course)
On a 64bit platform, you’ll likely need to create a symlink as well to get Flash to use this:

ln -s /usr/local/lib/flashsupport.so /usr/lib64


Now, fire up your JACK and your browser (You may need to restart the browser if it’s running), and try it out, it should just work!


If you have any issues, feel free to drop me a not here or by email.

Share Button

Connecting multiple sound devices with JACK and ALSA

The problem: JACK connects to one (generally the default) sound device, but you may have a second (third, fourth, etc) device that you want to record from or playback on.

Example situation (Actually, my situation):
Device #1: The NVidia sound card built in to the PC/Laptop.
Device #2: Guitar amplifier connected via USB
Device #3: USB headset/microphone.

Software I use:
(Note that this is all free software for Linux, versions may exist for other operating systems)

  • QJackCtl – essentially to start/stop JACK
  • Patchage – Used for making the connections between devices
  • alsa_in/alsa_out – Used to add the extra sound devices for JACK. If you installed JACK, you should have these command line programs
  • Ardour – for recording the tracks
  • aplay and arecord – part of the alsa-utils package

First Steps

Before starting anything, connect the devices, and use aplay and arecord to list what our devices are:
To list the playback devices:

$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: NVidia [HDA NVidia], device 0: CONEXANT Analog [CONEXANT Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: NVidia [HDA NVidia], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: LX3000 [Microsoft LifeChat LX-3000], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

And the capture devices:

$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: NVidia [HDA NVidia], device 0: CONEXANT Analog [CONEXANT Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: LX3000 [Microsoft LifeChat LX-3000], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: Amplifier [Mustang Amplifier], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Now, to translate this into standard ALSA lingo, the NVidia built-in device is card0, so we will refer to it as HW0, The M$ LX-3000 headset is card 1, thus referred to as HW1, and the Fender Mustang Amp is HW2.

Okay, so now running QJackCtl and clicking “Start” should get JACK running on HW0, the built-in sound device (Assuming it’s still at default settings). If you now start Patchage, you should see something like the following:
Now for the fun part; let’s get the input from the amplifier connected. Open up a terminal and run “alsa_in -j Amplifier -d HW:2” and you should see something like:

$ alsa_in -j Amplifier -d hw:2
selected sample format: 16bit
delay = 4094
delay = 1019

Don’t worry if you get a couple of those “delay” messages when it first starts up.
Now, let’s get the mic on the headset connected:

$ alsa_in -j Mic -d hw:1
WARNING: chennel count does not match (requested 2 got 1)
selected sample format: 16bit
delay = 3255

Note the message about channel count – that’s just because it defaults to 2, if you don’t like seeing the message you can add a “-c 1” to the command.

Now the last thing, the headset speakers:

$ alsa_out -j Headphones -d hw:1 -r 44100
selected sample format: 16bit

Note that I added a “-r 44100” – in my situation everything else defaults to 44100, but the headset defaults to 48000 and sounds like crap.

Now, let’s check on what it looks like in Patchage now:


Now we’re getting somewhere!

So now, I’ll fire up Ardour and add a couple tracks – 1 stereo track for the guitar and one mono track for the vocals. (I normally also use Hydrogen for a drum track as well, but I’ll skip that in the interest of keeping this shorter) Once that’s done, we’ll make the connections in Patchage by right-clicking on items that are connected automatically that we don’t want and selecting “Disconnect All”, then dragging the mouse from one device to another to “wire them up”.

Once it’s done, you should see something like the below:

I know the connections in Patchage might look a bit confusing, but play around with it a bit and you’ll find it’s pretty intuitive.

And now, you’re ready to Rock!

Share Button

Automatically Enable USB Headset

I, along with many others, would like a USB headset to be the default sound device in ALSA whenever it’s connected, and it seems to be a tough thing to find out how to do.

Short answer: Create UDEV rules to invoke a script that will modify your ~/.asoundrc file.

Long answer:


  1. You’re using Linux kernel >=2.6
  2. You’re using ALSA.
  3. You’re using UDEV
  4. You already have the headset working

First, we need to know what device we are looking for, so with the headset unplugged, fire up a terminal and type:
ls /dev/snd/
You’ll see something similar to the below:

localhost ~ # ls /dev/snd
by-path  controlC0  hwC0D0  hwC0D3  pcmC0D0c  pcmC0D0p  pcmC0D3p  seq  timer

Now, plug in the headset and check it again, ie:

localhost ~ # ls /dev/snd
by-id    controlC0  hwC0D0  pcmC0D0c  pcmC0D3p  pcmC1D0p  timer
by-path  controlC1  hwC0D3  pcmC0D0p  pcmC1D0c  seq

Notice the new items, in my case, the ones we are interested in are pcmC1D0p and pcmC1D0c. Note: The suffixes "p" and "c" indicate "Playback" and "Capture". For the purposes of this article, I’ll ignore the capture device, but the same process applies for it as for the playback.

Knowing the device name, we can now create the UDEV rules. Rules files are kept in /etc/udev/rules.d on my Gentoo system – other distros should be similar. You may have no files in there, or there may be many. I made a new file called /etc/udev/rules.d/99-audio.rules. the 99- prefix indicates the order in which the rules files will be applied, and I simply chose 99 to make this one run last.

My /etc/udev/rules.d/99-audio.rules file:

KERNEL=="pcmC1D0p", ACTION=="add", DRIVERS=="usb", PROGRAM="/home/pvint/headsetToggle.sh '%E{ACTION}'"
KERNEL=="pcmC1D0p", ACTION=="remove", DRIVERS=="usb", PROGRAM="/home/pvint/headsetToggle.sh '%E{ACTION}'"

Rough explanation: When UDEV sees a kernel message about the device "pcmC1D0p" via USB, and the action that occurred was that it was "add", it runs the script specified with the ACTION as a parameter (in this case, "add". Note that you’ll need to be root to do this.

Now for the script that actually changes the device, I should put it somewhere better, but I created /home/pvint/headsetToggle.sh with the following contents:

# Set default ALSA device appropriately when USB  headset is plugged/unplugged
if [ "$1" = "add" ]
        echo 'defaults.pcm.card 1' > /home/pvint/.asoundrc
        echo 'defaults.pcm.card 0' > /home/pvint/.asoundrc

Note that if you had anything in the .asoundrc file, it’ll be clobbered. This could easily be avoided by creating two dummy files like .asoundrc_headset and .asoundrc_normal and having the script copy the appropriate one to .asoundrc. The above was simple and suited my purpose just fine.

That should do it, so restart UDEV (ie: /etc/init.d udevd restart) and try it out.


  • Check your /var/log/messages to ensure there were no errors with the UDEV rules
  • Some applications need to be restarted before this takes effect.
  • The script will be run as root, so be careful
Share Button