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"""
 8import board
 9from 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
14response = Fake_Requests("fakerequests_i2c_database.txt")
15definitions = response.text.split("\n")
16
17# We create the i2c object and set a flag to let us know if the sensor is found
18found = False
19i2c = board.I2C()
20
21# We look for all the sensor address and added to a list
22print("Looking for addresses")
23i2c.unlock()  # used here, to avoid problems with the I2C bus
24i2c.try_lock()
25sensor_address = int(i2c.scan()[-1])
26print("Sensor address is:", hex(sensor_address))
27i2c.unlock()  # unlock the bus
28
29# Create an empty list for the sensors found in the database
30sensor_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
35for 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
45for 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
67if found:
68    print("Congratulations")
69else:
70    print("We could not find a valid Temperature Sensor")