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.