Subversion Repositories yaws

Rev

Rev 18 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
16 ron 1
#!/usr/bin/python3
3 ron 2
# -*- coding: utf-8 -*-
3
#
4
#  sensors/__init__.py
5
#
6
#  Copyright 2020 Ron Wellsted <ron@wellsted.org.uk>
7
#
8
#  This program is free software; you can redistribute it and/or modify
9
#  it under the terms of the GNU General Public License as published by
10
#  the Free Software Foundation; either version 2 of the License, or
11
#  (at your option) any later version.
12
#
13
#  This program is distributed in the hope that it will be useful,
14
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
#  GNU General Public License for more details.
17
#
18
#  You should have received a copy of the GNU General Public License
19
#  along with this program; if not, write to the Free Software
20
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21
#  MA 02110-1301, USA.
9 ron 22
#
23
#
16 ron 24
import re, smbus, w1thermsensor
17 ron 25
import gpiozero
9 ron 26
i2cbus = None
27
 
11 ron 28
debug = True
9 ron 29
 
30
# find out which revision of RPi we are running on
16 ron 31
#info = open('/proc/cpuinfo')
32
#for line in info:
33
#    line = line.rstrip()
34
#    if re.search('Revision', line):
35
#        x, x, rev = line.split()
18 ron 36
if  gpiozero.pi_info().revision == '0002':
16 ron 37
    i2cbus = 0
38
else:
39
    i2cbus = 1
9 ron 40
 
41
# BME280 Temperature, Pressure & Humidity sensor
42
_bme280_probe_ = [0x76, 0x77]
43
bme280_address = None
44
 
45
# BH1750 Ambient light level sensor
46
_bh1750_probe_ = [0x5c, 0x23]
47
bh1750_address = None
48
 
49
# PCF8574 8 pin I/O device (wind direction sensor)
50
_pcf8574_probe_ = [0x20, 0x21, 0x22, 0x24, 0x25, 0x26, 0x27]
51
pcf8574_address = None
52
 
53
if i2cbus is not None:
54
    bus = smbus.SMBus(i2cbus)
55
 
56
    for device in _bme280_probe_:
57
        try:
58
            bus.read_byte(device)
59
            bme280_address = device
60
        except: # exception if read_byte fails
61
            pass
62
    for device in _bh1750_probe_:
63
        try:
64
            bus.read_byte(device)
65
            bh1750_address = device
66
        except: # exception if read_byte fails
67
            pass
68
    for device in _pcf8574_probe_:
69
        try:
70
            bus.read_byte(device)
71
            pcf8574_address = device
72
        except: # exception if read_byte fails
73
            pass
74
 
75
if debug:
76
    if i2cbus is not None:
77
        print('i2cbus=',i2cbus)
78
        if bme280_address is not None:
79
            print('bme280=', hex(bme280_address))
80
        else:
81
            print('bme280 not found')
82
        if bh1750_address is not None:
83
            print('bh1750=', hex(bh1750_address))
84
        else:
85
            print('bh1750 not found')
86
        if pcf8574_address is not None:
87
            print('pcf8574=', hex(pcf8574_address))
88
        else:
89
            print('pcf8574 not found')
90
    else:
21 ron 91
        print('i2cbus not found, i2c sensors not availabe')
9 ron 92
# end of if debug