Subversion Repositories yaws

Rev

Rev 3 | Rev 10 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 ron 1
#!/usr/bin/env python3
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
#
24
import re, smbus
25
i2cbus = None
26
 
27
debug = true
28
 
29
# find out which revision of RPi we are running on
30
info = open('/proc/cpuinfo')
31
for line in info:
32
    line = line.rstrip()
33
    if re.search('Revision', line)
34
        x, x, rev = line.split()
35
        if rev = '0002':
36
            i2cbus = 0
37
        else:
38
            i2cbus = 1
39
 
40
# BME280 Temperature, Pressure & Humidity sensor
41
_bme280_probe_ = [0x76, 0x77]
42
bme280_address = None
43
 
44
# BH1750 Ambient light level sensor
45
_bh1750_probe_ = [0x5c, 0x23]
46
bh1750_address = None
47
 
48
# PCF8574 8 pin I/O device (wind direction sensor)
49
_pcf8574_probe_ = [0x20, 0x21, 0x22, 0x24, 0x25, 0x26, 0x27]
50
pcf8574_address = None
51
 
52
if i2cbus is not None:
53
    bus = smbus.SMBus(i2cbus)
54
 
55
    for device in _bme280_probe_:
56
        try:
57
            bus.read_byte(device)
58
            bme280_address = device
59
        except: # exception if read_byte fails
60
            pass
61
    for device in _bh1750_probe_:
62
        try:
63
            bus.read_byte(device)
64
            bh1750_address = device
65
        except: # exception if read_byte fails
66
            pass
67
    for device in _pcf8574_probe_:
68
        try:
69
            bus.read_byte(device)
70
            pcf8574_address = device
71
        except: # exception if read_byte fails
72
            pass
73
 
74
if debug:
75
    if i2cbus is not None:
76
        print('i2cbus=',i2cbus)
77
        if bme280_address is not None:
78
            print('bme280=', hex(bme280_address))
79
        else:
80
            print('bme280 not found')
81
        if bh1750_address is not None:
82
            print('bh1750=', hex(bh1750_address))
83
        else:
84
            print('bh1750 not found')
85
        if pcf8574_address is not None:
86
            print('pcf8574=', hex(pcf8574_address))
87
        else:
88
            print('pcf8574 not found')
89
    else:
90
        print('i2cbus not found, sensors not availabe')
91
# end of if debug