Merge pull request #4 from jposada202020/main

advance_example
This commit is contained in:
Melissa LeBlanc-Williams 2021-05-17 07:49:51 -07:00 committed by GitHub
commit 517f2637ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 97 additions and 0 deletions

View file

@ -6,3 +6,13 @@ Ensure your device works with this simple test.
.. literalinclude:: ../examples/fakerequests_simpletest.py
:caption: examples/fakerequests_simpletest.py
:linenos:
Advanced I2C test
-----------------
Uses the fakerequests capabilities to create a small I2C temperature sensors database, and
look for the correct one.I
.. literalinclude:: ../examples/fakerequests_advancedtest.py
:caption: examples/fakerequests_advancedtest.py
:linenos:

View file

@ -0,0 +1,70 @@
# SPDX-FileCopyrightText: 2021 Jose David M
#
# SPDX-License-Identifier: Unlicense
"""
Example showing the use of Fake_requests to access a Temperature Sensor information
Database. Inspired on the I2C buddy and a Discussion with Hugo Dahl
"""
import board
from adafruit_fakerequests import Fake_Requests
# Create the fakerequest request and get the temperature sensor definitions
# It will look through the database and print the name of the sensor and
# the temperature
response = Fake_Requests("fakerequests_i2c_database.txt")
definitions = response.text.split("\n")
# We create the i2c object and set a flag to let us know if the sensor is found
found = False
i2c = board.I2C()
# We look for all the sensor address and added to a list
print("Looking for addresses")
i2c.unlock() # used here, to avoid problems with the I2C bus
i2c.try_lock()
sensor_address = int(i2c.scan()[-1])
print("Sensor address is:", hex(sensor_address))
i2c.unlock() # unlock the bus
# Create an empty list for the sensors found in the database
sensor_choices = list()
# Compare the sensor found vs the database. this is done because
# we could have the case that the same address corresponds to
# two or more temperature sensors
for sensor in definitions:
elements = sensor.split(",")
if int(elements[0]) == sensor_address:
sensor_choices.append(sensor)
# This is the main logic to found the sensor and try to
# initiate it. It would raise some exceptions depending
# on the situation. As an example this is not perfect
# and only serves to show the library capabilities
# and nothing more
for find_sensor in sensor_choices:
module = find_sensor.split(",")
package = module[2]
class_name = str(module[3]).strip(" ")
try:
module = __import__(package)
variable = getattr(module, class_name)
try:
sensor = variable(i2c)
print(
"The sensor {} gives a temperature of {} Celsius".format(
class_name, sensor.temperature
)
)
found = True
except ValueError:
pass
except Exception as e:
raise ImportError(
"Could not find the module {} in your lib folder.".format(package)
) from e
if found:
print("Congratulations")
else:
print("We could not find a valid Temperature Sensor")

View file

@ -0,0 +1,14 @@
0x18,MCP9808 temp sensor,adafruit_mcp9808,MCP9808
0x37,PCT2075 Temperature Sensor,adafruit_pct2075,PCT2075
0x40,Si7021 Humidity/Temp sensor,adafruit_si7021,SI7021
0x40,HTU21D-F Humidity/Temp Sensor,adafruit_htu21d,HTU21D
0x40,HTU31D breakout,adafruit_htu31d,HTU31D
0x44,SHT31 Humidity/Temp sensor,adafruit_shtc3,SHTC3
0x38,AHT20 Temperature & Humidity Sensor,adafruit_ahtx0,AHTx0
0x5C,AM2320 Humidity/Temp sensor,adafruit_am2320,AM2320
0x44,SHT4x temperature and humidity sensors,adafruit_sht4x, SHT4x
0x70,SHTC3 Humidity and Temperature Sensor,adafruit_shtc3,SHTC3
0x48,TMP117 Temperature sensor,adafruit_tmp117,TMP117
0x60,MPL115A2 I2C Barometric Pressure/Temperature Sensor,adafruit_mpl115a2,MPL115A2
0x48,TC74 Digital Temperature Sensor,adafruit_tc74,TC74
0x48,ADT7410 analog temperature Sensor,adafruit_adt7410,ADT7410

View file

@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: 2021 Jose David M
#
# SPDX-License-Identifier: Unlicense