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
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:

[Unit]
Description=HA Bridge
Wants=network.target
After=network.target

[Service]
Type=simple
WorkingDirectory=/usr/local/share/habridge
ExecStart=/usr/bin/java -jar -Dconfig.file=/usr/local/share/habridge/data/habridge.config /usr/local/share/habridge/ha-bridge-5.1.0.jar

[Install]
WantedBy=multi-user.target

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
root@chip2:~# 

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)

#!/bin/bash

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

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

BRIGHTNESS=$1

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

if [ "$BRIGHTNESS" -lt 0 ]
then
BRIGHTNESS=0
fi

if [ "$BRIGHTNESS" -gt 100 ]
then
BRIGHTNESS=100
fi

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

LEVEL=$(expr $MAX / 100 \* $BRIGHTNESS)
CURRENT=`cat $DCFILE`
STEP=500
DELAY=0.004s

LOGFILE=/tmp/pwm.log
echo "$CURRENT  Set to $1 : $LEVEL" >> $LOGFILE
if (( "$LEVEL" > "$CURRENT" ))
then
for ((x=$CURRENT ; x<$LEVEL; x+=$STEP))
do
echo $x > $DCFILE
#echo $x #>> $LOGFILE
sleep $DELAY
done
echo $LEVEL > $DCFILE
else
for ((x=$CURRENT ; x>$LEVEL ; x-=$STEP))
do
echo $x > $DCFILE
#echo $x #>> $LOGFILE
sleep $DELAY
done
echo $LEVEL > $DCFILE
fi

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:
ha

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 myip.rocks

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!

See http://www.g26.ca

Share Button

Launch of aproposmarine.com

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 http://www.aproposmarine.com

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.

Screenshot:

Screenshot_2015-01-12-19-45-32

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: http://dotslash.ca/ListeningCam/ListeningCam.apk

 

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 https://addons.mozilla.org/en-US/firefox/addon/highlight-tab/ to download.

To view or download the source (which may include newer updates), go to https://github.com/pvint/highlighttab or check out the git source directly: git clone https://github.com/pvint/highlighttab.git

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
or
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:

#!/bin/bash

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

mysql_bin="/usr/bin/mysql"
mysqldump_bin="/usr/bin/mysqldump"
backup_path="/opt/backups/www/"

if [ $# -eq 3 ]
then
        host=$3
else
        host="localhost"
fi

user=$1
pass=$2

for db in `echo "show databases" | $mysql_bin -u $user -p$pass -h $host | grep -v Database | grep -v information_schema`
do
        for table in `echo "show tables" | $mysql_bin -u $user -p$pass -h $host $db | grep -v "Tables_in"`
        do
                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
        done
done

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

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
and
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