Skip to content

Commit 517f263

Browse files
authored
Merge pull request #4 from jposada202020/main
advance_example
2 parents 9a8b253 + ceb349e commit 517f263

4 files changed

+97
-0
lines changed

docs/examples.rst

+10
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,13 @@ Ensure your device works with this simple test.
66
.. literalinclude:: ../examples/fakerequests_simpletest.py
77
:caption: examples/fakerequests_simpletest.py
88
:linenos:
9+
10+
Advanced I2C test
11+
-----------------
12+
13+
Uses the fakerequests capabilities to create a small I2C temperature sensors database, and
14+
look for the correct one.I
15+
16+
.. literalinclude:: ../examples/fakerequests_advancedtest.py
17+
:caption: examples/fakerequests_advancedtest.py
18+
:linenos:

examples/fakerequests_advancedtest.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# SPDX-FileCopyrightText: 2021 Jose David M
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
"""
5+
Example showing the use of Fake_requests to access a Temperature Sensor information
6+
Database. Inspired on the I2C buddy and a Discussion with Hugo Dahl
7+
"""
8+
import board
9+
from adafruit_fakerequests import Fake_Requests
10+
11+
# Create the fakerequest request and get the temperature sensor definitions
12+
# It will look through the database and print the name of the sensor and
13+
# the temperature
14+
response = Fake_Requests("fakerequests_i2c_database.txt")
15+
definitions = response.text.split("\n")
16+
17+
# We create the i2c object and set a flag to let us know if the sensor is found
18+
found = False
19+
i2c = board.I2C()
20+
21+
# We look for all the sensor address and added to a list
22+
print("Looking for addresses")
23+
i2c.unlock() # used here, to avoid problems with the I2C bus
24+
i2c.try_lock()
25+
sensor_address = int(i2c.scan()[-1])
26+
print("Sensor address is:", hex(sensor_address))
27+
i2c.unlock() # unlock the bus
28+
29+
# Create an empty list for the sensors found in the database
30+
sensor_choices = list()
31+
32+
# Compare the sensor found vs the database. this is done because
33+
# we could have the case that the same address corresponds to
34+
# two or more temperature sensors
35+
for sensor in definitions:
36+
elements = sensor.split(",")
37+
if int(elements[0]) == sensor_address:
38+
sensor_choices.append(sensor)
39+
40+
# This is the main logic to found the sensor and try to
41+
# initiate it. It would raise some exceptions depending
42+
# on the situation. As an example this is not perfect
43+
# and only serves to show the library capabilities
44+
# and nothing more
45+
for find_sensor in sensor_choices:
46+
module = find_sensor.split(",")
47+
package = module[2]
48+
class_name = str(module[3]).strip(" ")
49+
try:
50+
module = __import__(package)
51+
variable = getattr(module, class_name)
52+
try:
53+
sensor = variable(i2c)
54+
print(
55+
"The sensor {} gives a temperature of {} Celsius".format(
56+
class_name, sensor.temperature
57+
)
58+
)
59+
found = True
60+
except ValueError:
61+
pass
62+
except Exception as e:
63+
raise ImportError(
64+
"Could not find the module {} in your lib folder.".format(package)
65+
) from e
66+
67+
if found:
68+
print("Congratulations")
69+
else:
70+
print("We could not find a valid Temperature Sensor")
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
0x18,MCP9808 temp sensor,adafruit_mcp9808,MCP9808
2+
0x37,PCT2075 Temperature Sensor,adafruit_pct2075,PCT2075
3+
0x40,Si7021 Humidity/Temp sensor,adafruit_si7021,SI7021
4+
0x40,HTU21D-F Humidity/Temp Sensor,adafruit_htu21d,HTU21D
5+
0x40,HTU31D breakout,adafruit_htu31d,HTU31D
6+
0x44,SHT31 Humidity/Temp sensor,adafruit_shtc3,SHTC3
7+
0x38,AHT20 Temperature & Humidity Sensor,adafruit_ahtx0,AHTx0
8+
0x5C,AM2320 Humidity/Temp sensor,adafruit_am2320,AM2320
9+
0x44,SHT4x temperature and humidity sensors,adafruit_sht4x, SHT4x
10+
0x70,SHTC3 Humidity and Temperature Sensor,adafruit_shtc3,SHTC3
11+
0x48,TMP117 Temperature sensor,adafruit_tmp117,TMP117
12+
0x60,MPL115A2 I2C Barometric Pressure/Temperature Sensor,adafruit_mpl115a2,MPL115A2
13+
0x48,TC74 Digital Temperature Sensor,adafruit_tc74,TC74
14+
0x48,ADT7410 analog temperature Sensor,adafruit_adt7410,ADT7410
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-FileCopyrightText: 2021 Jose David M
2+
#
3+
# SPDX-License-Identifier: Unlicense

0 commit comments

Comments
 (0)