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-headlessapt-get install build-essential git
mkdir /usr/local/share/habridge
cd /usr/local/share/habridge

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 >

Paste the following into the 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

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

New site – MyIP.Rocks

Need to know what your real public IP address is? See

It just gives you your IP in large, friendly letters. Don’t panic.

Share Button

Mary Moon is for sale! SOLD

The boat is sold – keeping this here for nostalgia.


My Grampian 26 sailboat, Mary Moon, is for sale!


Share Button

Launch of

Today marks the launch of a site I’ve been working on for a small Canadian business that designs and manufactures (in Canada) pleasure boat accessories. There are nifty cup holders, mounts for chart plotters and other instruments, and more.


Check it out at

Share Button

ListeningCam – Audio Sensing Video Recording on Android

I have been working on an Android app that I find useful: Recording video when there are noises. I initially got the idea when I was wondering if the dog was being good when I am not home, but couldn’t find an app that does this.

Here’s a result of an early test when it was listening for the dog (Note that I’m having it save videos in tiny resolution while testing):

It’s pretty basic so far, but is still a work in progress and hopefully I’ll have something I can distribute soon.



You can view the source code at my GitHub repository.

An APK file is also available for download, but do note that there are still issues, and it may crash, require you to restart your device, etc, but here it is:


The video will start recording when audio level gets above the threshold, and can record for a set amount of time, or record until a certain duration of silence has elapsed.

Some features I may add:

  • Ability to upload automatically to a server
  • Notifications with images or videos sent to email or SMS
  • Ability to check live feed remotely via Internet.
  • Timestamps on the video
  • Delayed start
  • Schedules
  • Better power management
  • Ability to save only audio and no video
  • Motion sensing
Share Button

Highlight Tabs in Firefox!

Ever have an absurd number of browser tabs open and have trouble finding a certain one? Me too, so I wrote an add-on for Firefox to highlight tabs in different colours.

Highlight Tabs addon

See to download.

To view or download the source (which may include newer updates), go to or check out the git source directly: git clone

Updates and new information will be posted at my main page for the addon.

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 “”, 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

Pronounceable Password Generator

Here’s a simple online password generator – I’ll likely add some features, but it works.

Number of passwords:


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