Domoticz on Pine64 using some cheap I2C modules

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.

Moderator: leecollings

Post Reply
Moustage
Posts: 3
Joined: Saturday 27 August 2016 11:15
Target OS: Linux
Domoticz version: beta
Location: eu
Contact:

Domoticz on Pine64 using some cheap I2C modules

Post by Moustage »

Used hardware:
Pine64+ 2GB with Ubuntu 16.04 (xfce-desktop) with longsleep kernel 3.10.102 on SD-card (32GB)
BH1750 Luxmeter conected to I2C-Bus (RPi2-connector)
HYT221 Humidity and Temperature conected to I2C-Bus
BMP180 Barometer and Temperature conected to I2C-Bus (you need to recompile the kernel if you will use kernelmodules)
old 40-pin IDE cable, the closed holes I drilled using a small drill (80-pin does not work for me)
small piece of breadboard
Since the cpu in operation heated, so I use the cable and the breadboard to keep the sensors away from it.

Step by step software installation:

First we have to decide where we want to save our compiled programs, for example Downloads.
Open a terminal and enter the following commands,
they lead an update through and install the dependencies for Domoticz.
Detailed instructions stands for the Rpi on domoticz wiki
and is with a little limitation on our Pine64 valid (we have no WiringPi)
so take also a look at https://www.domoticz.com/wiki/Installin ... spberry_PI

Code: Select all

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install nano libi2c-dev i2c-tools cmake make gcc g++ libssl-dev git curl libcurl4-openssl-dev libusb-dev
sudo adduser ubuntu i2c
cd Downloads
mkdir boost
cd boost
wget http://sourceforge.net/projects/boost/files/boost/1.61.0/boost_1_61_0.tar.gz/download
tar xvfz download
rm download
cd boost_1_61_0
./bootstrap.sh
./b2 stage threading=multi link=static --with-thread --with-date_time --with-system --with-atomic --with-regex
sudo ./b2 install threading=multi link=static --with-thread --with-date_time --with-system --with-atomic --with-regex
cd ..
Now we download the sources of Domoticz and compile this

Code: Select all

git clone https://github.com/domoticz/domoticz.git dev-domoticz
cd dev-domoticz
cmake -DCMAKE_BUILD_TYPE=Release CMakeLists.txt
make -j3
with me, the compilation process is running on the first try every time in a crash (24-26% compiled),
Solution: just do it again and re-enter make -j3
now install domoticz (at time of writing this, it is version 3.559)

Code: Select all

sudo make install
Domoticz will be installed to /opt/domoticz

copy the daemon and make domoticz startup at boot

Code: Select all

sudo cp domoticz.sh /etc/init.d
sudo chmod +x /etc/init.d/domoticz.sh
sudo update-rc.d domoticz.sh defaults
Edit the startup script and make some change to reflect your current settings

Code: Select all

sudo nano -w /etc/init.d/domoticz.sh

Code: Select all

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/opt/domoticz
DESC="Domoticz Home Automation System"
NAME=domoticz
USERNAME=ubuntu
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
We change to the directory where we have installed Domoticz and test whether it is running

Code: Select all

cd /opt/domoticz
./domoticz
Now connect with a browser to see if it is up and running.

Code: Select all

localhost:8080
If everything went smoothly, we have Domoticz run!

leave test running domoticz by pressing ctrl+c and a courageous

Code: Select all

sudo service domoticz.sh start
will start the daemon.

In your browser, we reopen the Domoticz page (localhost:8080) and here we have to make a few important settings: click on Setup -> Settings
Here we can change the language and contribute to "Website Protection" a Domoticz user with Password -please remember both!-
under Location we click "to find your location click here" and enter in the window that opens your street number place -> accept
then a click on the red button Apply Settings and we have to log us in Domoticz, with the username and password you typed in settings.
So far so good, domoticz is up and running!
You can also see the domoticzsite in another browser, if you are in the same network.
Find out what is the ip of your pine64 (ifconfig) and type IP-ADRESS-OF-YOUR-PINE64:8080 in your browser (Handy for example)


Setting up the hardware:

We turn off the Pine64.
Now connect your modules with the Pine64 I2C-Bus on RPi2-Connector

my setup for example:

Code: Select all

Pine64				BH1750	BMP180	HYT221
3V3 Pin1	-->		3V3 -->	3V3 -->	3V3	
GND Pin9	-->		GND -->	GND -->	GND
SDA Pin3	-->		SDA -->	SDA -->	SDA
SCL Pin5	-->		SCL -->	SCL -->	SCL
Pullup resistors are with me not necessary, but may need to be retrofitted (~ 4k7 Ohm each SDA and SCL -> 3V3)
Please look at the data sheet for every module how the pinout is

Double check that everything is connected correctly!

turn Pine64 on. open terminal and type

Code: Select all

i2cdetect -y 1
check whether your modules are detected, should something like that, depending on the connected modules

Code: Select all

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- 23 -- -- -- -- 28 -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- 77

23 = BH1750
28 = HYT221
77 = BMP180
In a terminal, we create a new folder, I created a hidden, but you can also use /opt/domoticz/scripts/python

Code: Select all

cd ~
mkdir .domoticz
cd .domoticz
Here we set the programs that evaluate our sensors and send the values to Domoticz.
I use Python scripts for that, so we have to install the needed python modules

Code: Select all

sudo apt-get install python-smbus python-requests
For example it does not matter which sensor we want to use as long as the values are getting with python.
You can simply use the following script snippet:

Code: Select all

#!/usr/bin/python
# needed python-module python-smbus and python-requests
# import smbus 		# only needed @ I2C
import json
import urllib2
import requests
import base64

try:
    from urllib.request import Request, urlopen
except ImportError: # Python 2
    from urllib2 import Request, urlopen

# Please let stand the quotes
domoticzserver = "127.0.0.1:8080"	# your domoticz url on Pine64
domoticzusername = "USERNAME"		# your username in domoticz
domoticzpassword = "PASSWORD"		# your password in domoticz
domoticz_idx = "IDX"			# the idx-number generated from domoticz

# place your code here
VALUE = # value from your sensor you will send to domoticz

domoticzurl = 'http://'+domoticzserver+'/json.htm?type=command&param=udevice&idx='+domoticz_idx+'&svalue='+str(VALUE)
credentials = '{domoticzusername}:{domoticzpassword}'.format(**vars()).encode()
response = urlopen(Request(domoticzurl, headers={'Authorization':
    b'Basic ' + base64.b64encode(credentials)})).close()
Please take a look at https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s for more details

Here are two simple sample-codes,
for a cheap BH1750 module, it generate only one value

Code: Select all

nano -w bh1750.py
type in

Code: Select all

#!/usr/bin/python
# sample script for a BH1750 Luxmeter module
# using I2C

import smbus
import json
import urllib2
import requests
import base64

try:
    from urllib.request import Request, urlopen
except ImportError: # Python 2
    from urllib2 import Request, urlopen

# Define some constants from the datasheet
BH1750_address_i2c	= 0x23 # Default device I2C address
BH1750_POWER_DOWN	= 0x00 # No active state
BH1750_POWER_ON		= 0x01 # Power on
BH1750_RESET		= 0x07 # Reset data register value

# IMPORTANT change here the values for your environment
domoticzserver="127.0.0.1:8080"			# domoticz url
domoticzusername = "YOUR_USERNAME"		# your username given in domoticz
domoticzpassword = "YOUR_PASSWORD"		# your password given in domoticz
domoticz_idx = "IDX_NUMBER_FROM_DOMOTICZ"	# the idx-number generated in domoticz
 
# Start measurement at 4lx resolution. Time typically 16ms.
CONTINUOUS_LOW_RES_MODE = 0x13
# Start measurement at 1lx resolution. Time typically 120ms
CONTINUOUS_HIGH_RES_MODE_1 = 0x10
# Start measurement at 0.5lx resolution. Time typically 120ms
CONTINUOUS_HIGH_RES_MODE_2 = 0x11
# Start measurement at 1lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_HIGH_RES_MODE_1 = 0x20
# Start measurement at 0.5lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_HIGH_RES_MODE_2 = 0x21
# Start measurement at 1lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_LOW_RES_MODE = 0x23
 
bus = smbus.SMBus(1)  # on Pine64 we use I2C-1 (RPi-2 Connector Pin3=SDA Pin5=SCL) 

def convertToLux(data):
# Simple function to convert 2 bytes of data into a decimal number and calculate the lux
# see page 7 on datasheet for formula
  return ((data[1] + (256 * data[0])) / 1.2)

def readLight():	# read data from BH1750
  data = bus.read_i2c_block_data(BH1750_address_i2c,ONE_TIME_HIGH_RES_MODE_1)	# change measurement method here
  return convertToLux(data)

lux = readLight()

#for testing remove #
#print(lux)

domoticzurl = 'http://'+domoticzserver+'/json.htm?type=command&param=udevice&idx='+domoticz_idx+'&svalue='+str(lux)
credentials = '{domoticzusername}:{domoticzpassword}'.format(**vars()).encode()
response = urlopen(Request(domoticzurl, headers={'Authorization':
    b'Basic ' + base64.b64encode(credentials)})).close()
and for a humidity-temperature module (HYT221), it provide 3 values -humidity-temperature-humidex-
the dew point is also displayed by domoticz

Code: Select all

nano -w hyt221.py
type in

Code: Select all

#!/usr/bin/python
# Reading measurements from HYT 221 humidity and temperature sensor
# See: http://www.ist-usadivision.com/objects/media/data-sheets/product/humidity-module/HYT-221.pdf
# Version 1 by Caerandir May 2014
# modified by moustage August 2016

import smbus
import json
import urllib2
import requests
import base64

try:
    from urllib.request import Request, urlopen
except ImportError: # Python 2
    from urllib2 import Request, urlopen

bus = smbus.SMBus(1)	# on Pine64 we use I2C-1 (Pi-2 Connector Pin3=SDA Pin5=SCL) 
HYT221_address_i2c = 0x28	# default address of HYT 221

domoticzserver="127.0.0.1:8080"			# domoticz url
domoticzusername = "YOUR_USERNAME"		# your username given in domoticz
domoticzpassword = "YOUR_PASSWORD"		# your password given in domoticz
domoticz_idx = "IDX_NUMBER_FROM_DOMOTICZ"	# the idx-number generated in domoticz

# Initialize sensor to acquire measurements
def MeasurementRequest():
        void = bus.write_quick(HYT221_address_i2c)

# Read Humidiy and Temperature to 14 bit accuracy
def ReadMeasurement():
        # Acquire 4 byte from sensor
        Readout = bus.read_i2c_block_data(HYT221_address_i2c,0,4)
	return Readout

MeasurementRequest()
raw_hyt221 = ReadMeasurement()

# Evaluate flags
CommandMode = (raw_hyt221[0] & 128) > 0
Stale = (raw_hyt221[0] & 64) > 0

# Assemble high and low bits for humidty (clear the leading 2 bits)
RawHumidity = (raw_hyt221[0] & 63) * 256 + raw_hyt221[1]

# Calculate relative humidity in percent
# RH [%] = (100 / (2 14 - 1)) * RH raw see page 13 datasheet
RelativeHumidity = ((100.0/(16384-1)) * RawHumidity)          # 16384 = 2^14

# Assemble high and low bits for temperature
RawTemperature = (raw_hyt221[2] * 256 + (raw_hyt221[3] & 252)) / 4

# Calculate temperature in Celsius
# T [C]= (165 / (2 14 - 1)) * T raw - 40
Temperature = (165.0 / (16384-1)) * RawTemperature - 40.0

# Print raw data and measurements
#        print "Raw data: ", Readout
#        print "Commandmode: ", CommandMode
#        print "Stale: ", Stale
#        print "Raw Humidity: ", RawHumidity
#        print "Raw Tempetrature: ", RawTemperature

# Formula found @ http://iridl.ldeo.columbia.edu/dochelp/QA/Basic/dewpoint.html
dew_point = Temperature - ((100 - RelativeHumidity)/5.0)	# calculate dew point for humidex
hum_stat = 0				# 0 = normal
if (dew_point > 16.0) : hum_stat = 3	# 3 = wet
if (dew_point < 15.0) : hum_stat = 1	# 1 = comfortable
if (dew_point < 10.0) : hum_stat = 2	# 2 = dry

# for testing remove #
#print "Hrel: ", RelativeHumidity, " %"
#print "T: ", Temperature, " C"
#print dew_point
#print hum_stat

domoticzurl1 = 'http://'+domoticzserver+'/json.htm?type=command&param=udevice&idx='+domoticz_idx+'&nvalue=0&svalue='+str("{0:.1f}".format(Temperature))+';'+str("{0:.1f}".format(RelativeHumidity))+';'+str(hum_stat)
credentials = '{domoticzusername}:{domoticzpassword}'.format(**vars()).encode()
response = urlopen(Request(domoticzurl1, headers={'Authorization':
    b'Basic ' + base64.b64encode(credentials)})).close()
On our Domoticzpage we click on Setup -> Hardware
We choose from the selection field "Type" "Dummy (Does nothing, use for virtual switches only)
In the Name field, we enter a name, for example: sensors
and click the Add button

Above, we now have the entry sensors and can here click on the button "Create Virtual Sensors"
in the window we choose among the Slider our first sensor, for example Lux and assign a name,
This name will later appear on the Domoticzsurface, you can choose any name.
These points we repeat for all the sensors that we have, so in my case:
for BH1750: Create Virtual Sensor -> slider: Lux -> Name: Lux
for HYT221: Create Virtual Sensor -> slider: Temp + Hum -> Name: HumidityTemperature
for BMP180: Create Virtual Sensor -> slider: barometer -> Name: Barometer
Create Virtual Sensor -> slider: Temperature -> Name: Temperature

Now click on setup --> devices
and see to what values domoticz for idx numbers assigned and fit them into your python script (i.e. domoticz_idx = "12").

Test does the script works,

Code: Select all

chmod +x bh1750.py
./bh1750.py
should the values are displayed in domoticz

if all goes well, now we install a cronjob so that our scripts are called repeatedly

Code: Select all

crontab -e

Code: Select all

* * * * * /home/ubuntu/.domoticz/bh1750.py	# path to your script
*/2 * * * * /home/ubuntu/.domoticz/hyt221.py
*/5 * * * * /home/ubuntu/.domoticz/bmp180.py
the BH1750 will called every minute, the HYT221 every 2 minutes, and BMP180 every 5 minutes

Thats all, have fun with Domoticz.
Many Thanks to all the developer for that nice work!
sebspi
Posts: 1
Joined: Sunday 06 November 2016 15:31
Target OS: Linux
Domoticz version:
Location: Grenoble, France
Contact:

Re: Domoticz on Pine64 using some cheap I2C modules

Post by sebspi »

I'm getting stuck at this point:
with me, the compilation process is running on the first try every time in a crash (24-26% compiled),
Solution: just do it again and re-enter make -j3
any idea on how I may pass this?

Thx!

Seb
Moustage
Posts: 3
Joined: Saturday 27 August 2016 11:15
Target OS: Linux
Domoticz version: beta
Location: eu
Contact:

Re: Domoticz on Pine64 using some cheap I2C modules

Post by Moustage »

You can download the stable version, for ARM 64bit, it works very well for me.
https://domoticz.com/downloads/
user
Posts: 1
Joined: Friday 29 November 2019 10:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Domoticz on Pine64 using some cheap I2C modules

Post by user »

Thank you Moustage, even though this is a rather old post, it seems to be the only item I could find with a usable example how to connect the BH1750 directly (so without an esp8266 or) to to the computer on which Domiticz runs. Great!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest