Home › Category Archives › Uncategorized

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:

[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

Changing permalink for a single WordPress Page

On WordPress 3.6 or 3.7, when you have the permalink type on the default /?page=33 style, you cannot edit the permalink for an individual page. If you change the global setting for permalinks to something like /page-title, the “Edit” button will now appear beside the permalink (Near the page title) when you edit the page.

Share Button