AtlasI2C: A Python package to communicate with Atlas Scientific devices in I2C mode.
This package provides functionality that is based on the example code from Atlas Scientific. It has the following goals:
- Provide a simple and clean codebase with test coverage
- Reduce code duplication by making the codebase available from PyPi
- Provide comprehensive support for Atlas Scientific EZO sensors
This package provides the following modules:
atlas_i2c module can be thought of as the client that talks to the server, similar to how an HTTP client talks to an HTTP server. The server in this scenario is the Atlas Scientfic EZO sensor. Instead of talking over TCP using HTTP, however, it talks to the server over the I2C bus, using Linux device files (e.g.
The module uses the following protocol to communicate with a sensor:
- Open the device file for reading and writing
- Send a command string (e.g. "R") to the device by writing it to the device file
- Wait for N milliseconds for the sensor to process the command
- Read the resulting data from the device file
At the lowest level, this module's
write() methods can be combined with
time.sleep() to communicate with a sensor:
In : from atlas_i2c import atlas_i2c In : sensor_address = 102 In : dev = atlas_i2c.AtlasI2C() In : dev.set_i2c_address(sensor_address) In : dev.write("R") In : time.sleep(1.5) In : result = dev.read("R") In : result.status_code Out: 1 In : result.data Out: b'0.922' In : result.original_cmd Out: 'R'
The module also provides a
query() method to conveniently wrap the above protocol into a single method:
In : result = dev.query("R", processing_delay=1500) In : result.status_code Out: 1 In : result.data Out: b'0.926' In : result.original_cmd Out: 'R'
The result of calling the
query() methods in the above code snippets is a
CommandReponse object. Here is an example of creating a
CommandResponse object manually and populating it:
In : from atlas_i2c import atlas_i2c In : response = atlas_i2c.CommandResponse() In : response Out: <atlas_i2c.atlas_i2c.CommandResponse at 0x7fbd40f48370> In : response.sensor_address = 10 In : response.sensor_address = 102 In : response.original_cmd = "R" In : response.response_type = "str" In : response.status_code = raw_data
commands module provides encapsulations intended to simplify interactions with sensors. Command attributes and methods can be accessed at the class level, thus it's not necessary to instantiate a command.
The module defines constants for each command class:
In : from atlas_i2c import commands # To find the arguments a command supports: In : commands.BAUD.arguments Out: (300, 1200, 2400, 9600, 19200, 38400, 57600, 115200) # To get a formatted command string: In : commands.BAUD.format_command(300) Out: 'Baud,300' # A command may not support any arguments: In : commands.READ.arguments In :
Not all commands have been implemented. The
format_command method on unimplemented commands will raise a
sensors module provides a higher-level encapsulation of a sensor in the form of the
Sensor class. The intention is that the
Sensor class is used as the primary means of communication; it uses the lower-level
AtlasI2C class to perform all functions, such as reading data from a sensor.
In : from atlas_i2c import sensors In : sensor = sensors.Sensor("Temperature", 102) In : sensor.connect() In : response = sensor.query(commands.READ) In : response.status_code Out: 1 In : response.data Out: b'0.923' In : response.original_cmd Out: 'R'
Supported Python Versions
This module requires Python >= 3.6.
atlas_i2c uses Tox for test automation, which includes linting, formatting and static type checking. To run Tox:
> tox [output truncated] py38: commands succeeded py37: commands succeeded py36: commands succeeded mypy: commands succeeded lint: commands succeeded format: commands succeeded congratulations :)
Installation can be done using Pip:
> pip install atlas-i2c
> python setup.py bdist_wheel > pip install dist/atlas_i2c-$version-py3-none-any.whl
Atlas Scientific Sensor Datasheets
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Hashes for atlas_i2c-0.3.1-py3-none-any.whl