Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
MrYsLab committed May 30, 2021
2 parents 96e0933 + 05f9961 commit e1eadbb
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 173 deletions.
87 changes: 51 additions & 36 deletions examples/dht.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,85 @@

import sys
import time

from telemetrix import telemetrix

"""
This program monitors a DHT 22 sensor.
This program monitors two DHT22 and two DHT11 sensors.
"""
PIN = 9


# indices into callback data for valid data
REPORT_TYPE = 0
PIN = 1
HUMIDITY = 2
TEMPERATURE = 3
TIME = 4
# REPORT_TYPE = 0
# READ_RESULT = 1
# PIN = 2
# DHT_TYPE = 3
# HUMIDITY = 4
# TEMPERATURE = 5
# TIME = 6

# indices into callback data for error report
REPORT_TYPE = 0
PIN = 1
ERROR_VALUE = 2
# REPORT_TYPE = 0
# READ_RESULT = 1
# PIN = 2
# DHT_TYPE = 3
# TIME = 4


# A callback function to display the distance
# noinspection GrazieInspection
def the_callback(data):
# noinspection GrazieInspection
"""
The callback function to display the change in distance
:param data: [report_type = PrivateConstants.DHT, pin number, humidity, temperature timestamp]
if this is an error report:
[report_type = PrivateConstants.DHT, pin number, error value timestamp]
"""
The callback function to display the change in distance
:param data: [report_type = PrivateConstants.DHT, error = 0, pin number,
dht_type, humidity, temperature timestamp]
if this is an error report:
[report_type = PrivateConstants.DHT, error != 0, pin number, dht_type
timestamp]
"""
if data[1]:
# error message
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[4]))
print(f'DHT Error Report:'
f'Pin: {data[2]} Error: {data[3]} Time: {date}')
print(f'DHT Error Report:'
f'Pin: {data[2]} DHT Type: {data[3]} Error: {data[1]} Time: {date}')
else:
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[5]))
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[6]))
print(f'DHT Valid Data Report:'
f'Pin: {data[2]} Humidity: {data[3]} Temperature: {data[4]} Time: {date}')
f'Pin: {data[2]} DHT Type: {data[3]} Humidity: {data[4]} Temperature:'
f' {data[5]} Time: {date}')



def dht(my_board, pin, callback):
def dht(my_board, pin, callback, dht_type):
# noinspection GrazieInspection
"""
Set the pin mode for a DHT 22 device. Results will appear via the
callback.
Set the pin mode for a DHT 22 device. Results will appear via the
callback.
:param my_board: an pymata express instance
:param pin: Arduino pin number
:param callback: The callback function
:param dht_type: 22 or 11
"""

# set the pin mode for the DHT device
my_board.set_pin_mode_dht(pin, callback, dht_type)

:param my_board: an pymata express instance
:param pin: Arduino pin number
:param callback: The callback function
"""

# set the pin mode for the trigger and echo pins
my_board.set_pin_mode_dht(pin, callback)
board = telemetrix.Telemetrix()
try:
dht(board, 8, the_callback, 11)
dht(board, 9, the_callback, 22)
dht(board, 10, the_callback, 22)
dht(board, 11, the_callback, 11)

# wait forever
while True:
try:
time.sleep(.01)
except KeyboardInterrupt:
my_board.shutdown()
board.shutdown()
sys.exit(0)


board = telemetrix.Telemetrix()
try:
dht(board, 9, the_callback)
board.shutdown()
except (KeyboardInterrupt, RuntimeError):
board.shutdown()
sys.exit(0)
Loading

0 comments on commit e1eadbb

Please sign in to comment.