-
Notifications
You must be signed in to change notification settings - Fork 1
/
gdev_i2c_Sensor_BH1750.py
157 lines (114 loc) · 5.4 KB
/
gdev_i2c_Sensor_BH1750.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#! /usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
I2C module BH1750 Ambient-Light-Sensor for Visible Light (also known as GY-30)
"""
###############################################################################
# This file is part of GeigerLog.
#
# GeigerLog is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GeigerLog is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GeigerLog. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
__author__ = "ullix"
__copyright__ = "Copyright 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024"
__credits__ = [""]
__license__ = "GPL3"
from gsup_utils import *
# Document: https://datasheetspdf.com/datasheet/BH1750FVI.html
# Ambient Light Sensor IC Series, Digital 16bit Serial Output Type, Ambient Light Sensor IC, BH1750FVI
# Example: https://wolles-elektronikkiste.de/en/bh1750fvi-gy-30-302-ambient-light-sensor
class SensorBH1750:
"""Code for the BH1750 sensors"""
name = "BH1750"
addr = 0x23 # "The standard I2C address of the module is 0x23. You can change
# it to 0x5C by connecting the address pin (ADDR or ADD) to HIGH."
# id # sensor does not have id
handle = g.I2CDongle # default for use by 'I2C' device; RaspiI2C defines its own
def __init__(self, addr, I2Chandle=None):
"""Init SensorBH1750 class"""
self.addr = addr
if I2Chandle is not None: self.handle = I2Chandle
def SensorInit(self):
"""check ID, check PID, Reset, enable measurement"""
defname = "SensorInit: " + self.name + ": "
# dprint(defname)
setIndent(1)
# check for presence of an I2C device at I2C address
if not self.handle.DongleIsSensorPresent(self.addr):
# no device found
setIndent(0)
return (False, "Did not find any I2C device at address 0x{:02X}".format(self.addr))
else:
# device found
gdprint(defname, "Found an I2C device at address 0x{:02X}".format(self.addr))
# reset
gdprint(defname, self.SensorReset())
dmsg = "Sensor {:8s} at address 0x{:02X} ".format(self.name, self.addr)
setIndent(0)
return (True, dmsg)
def BH1750enableMeasurements(self):
"""setting Power to On"""
# not needed, see datasheet: "'Power On' Command is possible to omit."
tmsg = "set PowerOn"
register = 0x01
readbytes = 0
data = []
answ = self.handle.DongleWriteRead (self.addr, register, readbytes, data, addrScheme=1, msg=tmsg)
def SensorgetValues(self):
"""get data in One time L-resolution mode"""
# duration: ISS: 1 ... 2.5 ms
start = time.time()
defname = "SensorgetValues: {:10s}: ".format(self.name)
# cdprint(defname)
setIndent(1)
# One time L-resolution mode
tmsg = "get LowMode"
register = 0x23
readbytes = 2
data = []
answ = self.handle.DongleWriteRead (self.addr, register, readbytes, data, addrScheme=1, msg=tmsg)
if len(answ) == readbytes:
vis = (answ[0] << 8 | answ[1]) / 1.2
response = (vis, )
else:
# Error: answ too short or too long
edprint(defname + "incorrect data, answ: ", answ)
response = (g.NAN, )
duration = 1000 * (time.time() - start)
# gdprint(defname, "Vis:{:<0.3f}".format(*response) + ", dur:{:0.1f} ms".format(duration))
setIndent(0)
return response
def SensorGetInfo(self):
info = "{}\n" .format("Ambient Light (Visible)")
info += "- Address: 0x{:02X}\n" .format(self.addr)
info += "- Variables: {}\n" .format(", ".join("{}".format(x) for x in g.Sensors["BH1750"][5]))
return info.split("\n")
def SensorReset(self):
"""Reset the data register"""
# Reset command is for only reset Illuminance data register. ( reset value is '0' )
# It is not necessary even power supply sequence.It is used for removing previous
# measurement result. This command is not working in power down mode, so
# that please set the power on mode before input this command.
#
# duration: ELV: ??? ms
# ISS: 1.0 ms
# Raspi5: 0.8 ms
start = time.time()
defname = "SensorReset: "
tmsg = "Reset"
register = 0x07 # reset
readbytes = 0
data = []
answ = self.handle.DongleWriteRead(self.addr, register, readbytes, data, addrScheme=1, msg=tmsg)
duration = 1000 * (time.time() - start)
return defname + "Data register => 0; dur:{:0.1f} ms".format(duration)