From d9cb958eef802b0102da5b302315de013fd362a7 Mon Sep 17 00:00:00 2001 From: Llewelyn Trahaearn Date: Fri, 2 Aug 2019 14:18:35 -0700 Subject: [PATCH] Include simple example programme demonstrating basic periodic data acquisition usage. --- README.rst | 17 +++++++++++------ examples/sht31d_periodictest.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 examples/sht31d_periodictest.py diff --git a/README.rst b/README.rst index 460771b..311f1dc 100644 --- a/README.rst +++ b/README.rst @@ -63,14 +63,12 @@ And then you can start measuring the temperature and humidity: print(sensor.temperature) print(sensor.relative_humidity) -You can instruct the sensor to start periodically measuring the temperature -and humidity at a set interval and retrieve the stored measurements: +You can instruct the sensor to periodically measure the temperature and +humidity, storing the result in its internal cache: .. code:: python sensor.mode = 'Periodic' - print(sensor.temperature) - print(sensor.relative_humidity) You can adjust the frequency at which the sensor periodically gathers data to: 0.5, 1, 2, 4 or 10 Hz. The following adjusts the frequency to 2 Hz: @@ -79,9 +77,16 @@ You can adjust the frequency at which the sensor periodically gathers data to: sensor.frequency = 2 - The sensor is capable of storing eight results. The sensor stores these -results in an internal FILO cache. +results in an internal FILO cache. Retrieving these results is simlilar to +taking a measurement. The sensor clears its cache once the stored data is read. +The sensor always returns eight data points. The list of results is backfilled +with the maximum output values of 130.0 ÂșC and 100.01831417975366 % RH: + +.. code:: python + + print(sensor.temperature) + print(sensor.relative_humidity) The sensor will continue to collect data at the set interval until it is returned to single shot data acquisition mode: diff --git a/examples/sht31d_periodictest.py b/examples/sht31d_periodictest.py new file mode 100644 index 0000000..ce933a9 --- /dev/null +++ b/examples/sht31d_periodictest.py @@ -0,0 +1,30 @@ +#!/usr/bin/python3 +import time +import board +import busio +import adafruit_sht31d + +# Create library object using our Bus I2C port +i2c = busio.I2C(board.SCL, board.SDA) +sensor = adafruit_sht31d.SHT31D(i2c) + +print('\033[1mSensor\033[0m = SHT31-D') +print('\033[1mSerial Number\033[0m = ', sensor.serial_number, '\n') +sensor.frequency = 1 +sensor.mode = 'Periodic' +for i in range(3): + if i == 2: + sensor.heater = True + if i == 1: + time.sleep(4) + print('\033[91mCache half full.\033[0m') + else: + time.sleep(8) + if sensor.heater: + print('\033[1mHeater:\033[0m On') + sensor.heater = False + print('\033[1mTemperature:\033[0m ', sensor.temperature) + if not sensor.heater: + print('\033[1mHeater:\033[0m Off') + print('\033[1mHumidity:\033[0m ', sensor.relative_humidity, '\n') +sensor.mode = 'Single'