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