Subversion Repositories yaws

Rev

Rev 11 | Rev 17 | Go to most recent revision | 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
25
from gpiozero import pi_info
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()
36
rev = pi_info.revision
37
if rev == '0002':
38
    i2cbus = 0
39
else:
40
    i2cbus = 1
9 ron 41
 
42
# BME280 Temperature, Pressure & Humidity sensor
43
_bme280_probe_ = [0x76, 0x77]
44
bme280_address = None
45
 
46
# BH1750 Ambient light level sensor
47
_bh1750_probe_ = [0x5c, 0x23]
48
bh1750_address = None
49
 
50
# PCF8574 8 pin I/O device (wind direction sensor)
51
_pcf8574_probe_ = [0x20, 0x21, 0x22, 0x24, 0x25, 0x26, 0x27]
52
pcf8574_address = None
53
 
54
if i2cbus is not None:
55
    bus = smbus.SMBus(i2cbus)
56
 
57
    for device in _bme280_probe_:
58
        try:
59
            bus.read_byte(device)
60
            bme280_address = device
61
        except: # exception if read_byte fails
62
            pass
63
    for device in _bh1750_probe_:
64
        try:
65
            bus.read_byte(device)
66
            bh1750_address = device
67
        except: # exception if read_byte fails
68
            pass
69
    for device in _pcf8574_probe_:
70
        try:
71
            bus.read_byte(device)
72
            pcf8574_address = device
73
        except: # exception if read_byte fails
74
            pass
75
 
76
if debug:
77
    if i2cbus is not None:
78
        print('i2cbus=',i2cbus)
79
        if bme280_address is not None:
80
            print('bme280=', hex(bme280_address))
81
        else:
82
            print('bme280 not found')
83
        if bh1750_address is not None:
84
            print('bh1750=', hex(bh1750_address))
85
        else:
86
            print('bh1750 not found')
87
        if pcf8574_address is not None:
88
            print('pcf8574=', hex(pcf8574_address))
89
        else:
90
            print('pcf8574 not found')
91
    else:
92
        print('i2cbus not found, sensors not availabe')
93
# end of if debug