Simple test
Ensure your device works with this simple test.
examples/fakerequests_simpletest.py
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2#
3# SPDX-License-Identifier: Unlicense
4
5from adafruit_fakerequests import Fake_Requests
6
7response = Fake_Requests("local.txt")
8print(response.text)
Advanced I2C test
Uses the fakerequests capabilities to create a small I2C temperature sensors database, and look for the correct one.I
examples/fakerequests_advancedtest.py
1# SPDX-FileCopyrightText: 2021 Jose David M
2#
3# SPDX-License-Identifier: Unlicense
4"""
5Example showing the use of Fake_requests to access a Temperature Sensor information
6Database. Inspired on the I2C buddy and a Discussion with Hugo Dahl
7"""
8
9import board
10
11from adafruit_fakerequests import Fake_Requests
12
13# Create the fakerequest request and get the temperature sensor definitions
14# It will look through the database and print the name of the sensor and
15# the temperature
16response = Fake_Requests("fakerequests_i2c_database.txt")
17definitions = response.text.split("\n")
18
19# We create the i2c object and set a flag to let us know if the sensor is found
20found = False
21i2c = board.I2C()
22
23# We look for all the sensor address and added to a list
24print("Looking for addresses")
25i2c.unlock() # used here, to avoid problems with the I2C bus
26i2c.try_lock()
27sensor_address = int(i2c.scan()[-1])
28print("Sensor address is:", hex(sensor_address))
29i2c.unlock() # unlock the bus
30
31# Create an empty list for the sensors found in the database
32sensor_choices = []
33
34# Compare the sensor found vs the database. this is done because
35# we could have the case that the same address corresponds to
36# two or more temperature sensors
37for sensor in definitions:
38 elements = sensor.split(",")
39 if int(elements[0]) == sensor_address:
40 sensor_choices.append(sensor)
41
42# This is the main logic to found the sensor and try to
43# initiate it. It would raise some exceptions depending
44# on the situation. As an example this is not perfect
45# and only serves to show the library capabilities
46# and nothing more
47for find_sensor in sensor_choices:
48 module = find_sensor.split(",")
49 package = module[2]
50 class_name = str(module[3]).strip(" ")
51 try:
52 module = __import__(package)
53 variable = getattr(module, class_name)
54 try:
55 sensor = variable(i2c)
56 print(f"The sensor {class_name} gives a temperature of {sensor.temperature} Celsius")
57 found = True
58 except ValueError:
59 pass
60 except Exception as e:
61 raise ImportError(f"Could not find the module {package} in your lib folder.") from e
62
63if found:
64 print("Congratulations")
65else:
66 print("We could not find a valid Temperature Sensor")