Installing HAP-NodeJS on a Raspberry Pi

I recently starting trying to get USB webcams working with HomeKit.  As I looked for information on the web it looked a bit daunting to implement since Homekit doesn’t officially support web cam just yet.  Some very smart people have done the hard work to get webcams working in HomeKit so it was much easier than I initially thought.

Below is a script to get HAPNodeJS installed and functioning. This is the first step before getting into creating new accessory for live video feeds displaying in HomeKit.

I have used this on RPI 1, 2, and 3 without issues. Works overtime for me but it is key you do this first before anything else. You need to execute this as pi and NOT root. It tells you when you when its necessary to use sudo for each steps.

$ cd /home/pi
$ sudo apt-get update
$ sudo apt-get install git-core libnss-mdns libavahi-compat-libdnssd-dev -y
$ sudo wget http://node-arm.herokuapp.com/node_latest_armhf.deb
$ sudo dpkg -i node_latest_armhf.deb
$ sudo rm -rf node_latest_arm*
$ sudo npm install -g node-gyp
$ git clone https://github.com/KhaosT/HAP-NodeJS.git
$ cd HAP-NodeJS
$ sudo npm install

Special thanks to Alex Skalozub, who reverse engineered the server side HAP.  You can find his research at here. (Sadly, on Nov 4, 2016, Apple sent the DMCA request to Github to remove the research.)

More information is on GitHub: https://github.com/KhaosT/HAP-NodeJS

SunFounder DS18B20 Temperature Module for Raspberry Pi – Part 2

Here is some python code from the SunFounder site that is part of their sensor kit install.

Let’s get back to our Raspberry home directory if you are not already. You can technically out the python script anyplace you like but I prefer the pi user home directory. Type the following if needed in Terminal.

cd /home/pi/

Now let’s create a python script. Copy the following code below then type the following again in Terminal.

sudo nano getTemp.py

Past the text below into the file.  This will get the temp from the sensors that start with “28”

#!/usr/bin/env python
import os
import time

#---------------------------------------------------
#	Note:
#		ds18b20's data pin must be connected to pin7.
#---------------------------------------------------

# Reads temperature from sensor and prints to stdout
# id is the id of the sensor
def readSensor(id):
	tfile = open("/sys/bus/w1/devices/"+id+"/w1_slave")
	text = tfile.read()
	tfile.close()
	secondline = text.split("\n")[1]
	temperaturedata = secondline.split(" ")[9]
	temperature = float(temperaturedata[2:])
	temperature = temperature / 1000
	print "Sensor: " + id  + " - Current temperature : %0.3f C" % temperature


# Reads temperature from all sensors found in /sys/bus/w1/devices/
# starting with "28-...
def readSensors():
	count = 0
	sensor = ""
	for file in os.listdir("/sys/bus/w1/devices/"):
		if (file.startswith("28-")):
			readSensor(file)
			count+=1
	if (count == 0):
		print "No sensor found! Check connection"

# read temperature every second for all connected sensors
def loop():
	while True:
		readSensors()
		time.sleep(1)

# Nothing to cleanup
def destroy():
	pass

# Main starts here
if __name__ == "__main__":
	try:
		loop()
	except KeyboardInterrupt:
		destroy()

 

ds18b20 = '28-051684d013ff'

Now we can save and exit the nano editor.  Ctrl+o to save the file and Ctrl+x to exit.

Now we can run the python script to see the results.

python getTemp.py 

You should see something like the following start to display.

Sensor: 28-051684d013ff - Current temperature : 21.437 C
Sensor: 28-051684d013ff - Current temperature : 21.500 C
Sensor: 28-051684d013ff - Current temperature : 21.500 C
Sensor: 28-051684d013ff - Current temperature : 21.500 C
Sensor: 28-051684d013ff - Current temperature : 21.500 C
Sensor: 28-051684d013ff - Current temperature : 21.500 C

You can stop the script from running with a Ctrl+z.

In part 3 of this tutorial I will show you how to get the temp sensor results showing up in Homebridge for your home automation.

SunFounder DS18B20 Temperature Module for Raspberry Pi – Part 1

I have been using the SunFounder DS18b20 attached to various Raspberry Pi GPIO in my home automation setup.  They are really easy to add to your board and start pulling temperature data in a matter of minutes.  Each room that has a Raspberry has one attached and then using Homebridge the current temperature can be display as a sensor.

You can buy the SunFounder DS18B20 Temperature Sensor Module for Arduino and Raspberry Pi off Amazon for about $8 USD. I am sure you can find them cheaper but I am a sucker for 2 day shipping.

SunFounder DS18b20
SunFounder DS18b20

Here is my routine for setting these up on a Raspberry Pi. Note, I connect them direction to the GPOIO pins and don’t use a breadboard of any kind. I need to tuck my Raspberry Pis away in various areas of the house and they generally just hang off the Raspberry.

You will be connection a ground, signal and power using a female to female jumper wire.

Connect your jumper wires to the module then to the Raspberry Pi GPIO pins. I’ll assume what show what pin is which on your RPi.

GPIO7 to Sig
5V to ACC
GRD to GRD

Upgrade your kernel

sudo apt-get update
sudo apt-get upgrade

Edit that the following config file with nano or your preferred editor.

sudo nano /boot/config.txt

Then scroll to the bottom of the final and add the following. This might already exist but commented out. Uncomment that line by removing the # in front of it if that is the case.

dtoverlay=w1-gpio

Then reboot.

sudo reboot

Mount the device drivers and confirm whether the device is effective or not.

sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices/
ls

The result should be something similar to this.

root@rasberrypi:/sys/bus/w1/devices# ls
28-051684d013ff w1_bus_master1

28-051684d013ff is an external temperature sensor device, but it may vary with every client. This is the serial number of your ds18b20.

Check the current temperature

cd 28-051684d013ff
ls

The result should be similar to the following.

root@rasberrypi:/sys/bus/w1/devices/28-051684d013ff# ls
driver id name power subsystem uevent w1_slave

Now enter the following to get the temperature.

cat w1_slave

The result is as follows.
root@raspberrypi:/sys/bus/w1_slave/28-051684d013ff# cat w1_slave
53 01 4b 46 7f ff 0c 10 2d : crc=2d YES
53 01 4b 46 7f ff 0c 10 2d t=21187

The second line t=21187 is current temperature value. If you want to convert it to degree Celsius, you can divide by 1000, that is, the current temperature is 21187/1000=21.187.

If you want to convert that to Fahrenheit remember that 0 °C = 32 °F. This is probably a bit more helpful. multiply by 9, then divide by 5, then add 32.

21.187 * 9 = 190.683
190.683 / 5 = 38.1366
38.1366 + 32 = 70.1366

21.187 C or 70.1366 F

In Part 2 of this tutorial let’s look at using Python to display the temperature on the screen.

How to determine the Raspberry PI board version

I have 5 Raspberry’s in my house running various home automation devices/systems.  I often forget what board is what version since they are all headless and tucked away in the house.   I found this useful in determining what version board I have.

Open Terminal and type the following.

cat /proc/cpuinfo

This should produce something similar to this based on your Raspberry Pi board.

processor : 0
model name : ARMv6-compatible processor rev 7 (v6l)
BogoMIPS : 897.37
Features : half thumb fastmult vfp edsp java tls
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xb76
CPU revision : 7

Hardware : BCM2708
Revision : 000e
Serial : 0000000006475d8a

Look at the Revision result and compare it to the following table to get your board version.

The variants currently available are :

Model and PCB Revision RAM Hardware Revision Code from cpuinfo
Model B Rev 1 256MB 0002
Model B Rev 1
ECN0001 (no fuses, D14 removed)
256MB 0003
Model B Rev 2 256MB 0004
0005
0006
Model A 256MB 0007
0008
0009
Model B Rev 2 512MB 000d
000e
000f
Model B+ 512MB 0010
Model B+ 512MB 0013
Compute Module 512MB 0011
Compute Module 512MB 0014 (Embest, China)
Model A+ 256MB 0012
Model A+ 256MB 0015 (Embest, China)
Model A+ 512MB 0015 (Embest, China)
Pi 2 Model B v1.1 1GB a01041 (Sony, UK)
Pi 2 Model B v1.1 1GB a21041 (Embest, China)
Pi 2 Model B v1.2 1GB a22042
PiZero v1.2 512MB 900092
PiZero v1.3 512MB 900093
Pi 3 Model B 1GB a02082 (Sony, UK)
Pi 3 Model B 1GB a22082 (Embest, China)

 

RPi Arcade Project Update: 11/24/13

I was back on the control panel again today. I decided to paint the panel white. Before I was able to paint I had to drill two holes and test the trackball mounting. I might have to order special carriage bolts but I’m going with what I was able to find at Home Depot. They seem to work.

I planned to use the original plexiglass cover despite having some carvings in it. After painting the panel white using the original panel was not going to work. Scratches really stand out. I am going to have to get a plexiglass cover professionally cut. Not sure what that is going to cost me.

Here is the sexy new look to the control panel.

20131124-180725.jpg

20131124-180745.jpg

20131124-180759.jpg

20131124-180811.jpg

20131124-180828.jpg

20131124-180838.jpg

RPi Arcade Build Update: 11/23/13

I tackled the control panel today. I have 8 hours easy into cutting, stripping, cleaning, and cutting the panel some more. I am wore out to be honest.

I was able to use the original metal panel. This is a nice plus since the panel has the bolts for the joysticks already. I had planned to only use wood but I managed to cut the trackball hole without too much trouble into the metal control panel. A 3” hole saw did the work and it was a clean cut. It was a little cold outside but with my son’s help and we made steady progress. After cutting the trackball hole I used my Dremel to clean up the tough edges.

20131124-012517.jpg

Next I planned to strip off the original sticker on the control panel. I bet it took me 2 hours to get the sticker off and the surface cleaned up. The sticker came off using a razor blade easily enough but the sticker left behind the sticky part. I scrapped, and scrapped, and scrapped some more. Then I sanded it and scrapped even more. Finally it was free of the tacky mess.

20131124-013141.jpg

20131124-013155.jpg

I ran into my first issue when I realized I cut the trackball hole too far down on the control panel. This mistake prevented the control panel from fully closing. I ended up having to trim out the particle board to get the control panel to fully close. Again the Dremel to the rescue.

20131124-013525.jpg

Now it was time to cut some holes. I have added a total of 13 holes to the control panel. I bought what is called a step drill bit and it worked out great.

I added 8 button holes across the top. 1 and 2 player buttons will be centered with 3 buttons on each side. These were super hard to cut since I had to go through metal and wood.

20131124-013917.jpg

20131124-014221.jpg

20131124-014246.jpg

20131124-014253.jpg

The last button I added was for a spinner. This hole is placed right above the trackball. Since Tempest was one of my favorite games as a child I had to have the spinner.

Tomorrow I plan to paint the control panel white and try to cut some new plexiglass to cover it. I also need to find the right sized carriage bolts to mount the trackball.

RPi Arcade Build Update: 11/20/13

I have some new equipment added to the arcade cabinet tonight. To get the sound working (louder) I installed a LP-2020A+ Lepai Tripath Class-T hi-Fi Audio Mini Amplifier. This meant I needed a USB sound card as well. I went with a 7.1 Channel USB External Sound Card Adapter. Finally a 7 port USB hub was added. Total price for this was about $40.

After getting the sound card configured and working with the Raspberry Pi it is loud. Really loud. I am using the cabinet speaker from the original Tekken 2 cabinet and it sounds incredible!

20131120-235847.jpg

20131121-000743.jpg

The rest of my efforts tonight surrounded figuring out how I’m going to work the control panel. I think I have a sold plan now so keep your fingers crossed it all works out.

Wonder Woman’s RPi Case

If Wonder Woman needed a case for her Raspberry Pi she would want this case. It would match her invisible plane. This is the same paper case I designed but I cut it from an over head projector plastic sheet.

The entire time I was folding it I kept say it was not going to work. When I finished it was better than I could have imagined. Check out the photos.

20131119-232018.jpg

20131119-232031.jpg

20131119-232042.jpg

20131119-232104.jpg

20131119-232119.jpg

Here is the paper version next to the invisible Wonder Woman version.

20131119-232131.jpg

RPi Arcade Build Update: 11/18/13

Lots of work tonight on the cabinet. Clean, clean, clean. I removed the coin doors and scrubbed them. They are fully in tacked. I removed all the wires from the Tekken 2 games that I won’t be using.

Coin door before Coin door removed Cleaned up now Cool control All back together

I stripped the control panel down but couldn’t get the plastic cover off it since it has security screws in it. Really?

Control panel guts Stripped down Security screw, ugh

Here is a photo of my old control panel and the Tekken panel. I hope to get most the buttons, the spinner, and the trackball on the new one. Space might be an issue so we will see.

20131119-230024.jpg

The bezel on the cabinet can be switched from portrait to landscape. I have been running portrait for a few days but I just switched it back to landscape. The viewing angle was terrible in portrait.

Portrait mode Landscape is better Ms. Pac-Man anyone?

Last, a power supply came with the cabinet so I managed to hook up the light. Happy that it works! Now I’m wondering if I can power the RPi off the power supply?

Lights off the power supply Power supply details

I do have PiMAME up and running as well. Without the control panel working it it just not much fun. I’ll plan out the panel tomorrow. That’s all for now.