From a918f8ac71355a219e73550b0f120d093c90c841 Mon Sep 17 00:00:00 2001 From: MrYsLab Date: Sat, 29 May 2021 16:40:16 -0400 Subject: [PATCH 1/3] DHTStable changes --- examples/dht.py | 63 +++++++++------ html/telemetrix/index.html | 162 ++++++++++++++++++++++++------------- telemetrix/telemetrix.py | 57 ++++++++----- 3 files changed, 178 insertions(+), 104 deletions(-) diff --git a/examples/dht.py b/examples/dht.py index b224464..1dea31e 100644 --- a/examples/dht.py +++ b/examples/dht.py @@ -20,43 +20,50 @@ 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 def the_callback(data): """ The callback function to display the change in distance - :param data: [report_type = PrivateConstants.DHT, pin number, humidity, temperature timestamp] + :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, pin number, error value timestamp] + [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}') + 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): """ Set the pin mode for a DHT 22 device. Results will appear via the callback. @@ -64,23 +71,27 @@ def dht(my_board, pin, 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 trigger and echo pins - my_board.set_pin_mode_dht(pin, callback) + # set the pin mode for the DHT device + my_board.set_pin_mode_dht(pin, callback, dht_type) + + +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) diff --git a/html/telemetrix/index.html b/html/telemetrix/index.html index 8f4146a..93762dc 100644 --- a/html/telemetrix/index.html +++ b/html/telemetrix/index.html @@ -3,15 +3,17 @@ - + telemetrix.telemetrix API documentation - - - - + + + + + +
@@ -283,6 +285,10 @@

Module telemetrix.telemetrix

command = [PrivateConstants.ENABLE_ALL_REPORTS] self._send_command(command) + # Have the server reset its data structures + command = [PrivateConstants.RESET] + self._send_command(command) + def _find_arduino(self): """ This method will search all potential serial ports for an Arduino @@ -738,16 +744,20 @@

Module telemetrix.telemetrix

command = [PrivateConstants.I2C_BEGIN, i2c_port] self._send_command(command) - def set_pin_mode_dht(self, pin, callback=None): + def set_pin_mode_dht(self, pin, callback=None, dht_type=22): """ :param pin: connection pin :param callback: callback function - Error Callback: [Callback 0=DHT REPORT, DHT_ERROR=0, PIN, Error Number, Time] + :param dht_type: either 22 for DHT22 or 11 for DHT11 + + Error Callback: [DHT REPORT Type, DHT_ERROR_NUMBER, PIN, DHT_TYPE, Time] - Valid Data Callback: Callback 0=DHT REPORT, DHT_DATA=1, PIN, Humidity, Temperature Time] + Valid Data Callback: DHT REPORT Type, DHT_DATA=, PIN, DHT_TYPE, Humidity, + Temperature, + Time] """ @@ -760,7 +770,10 @@

Module telemetrix.telemetrix

self.dht_callbacks[pin] = callback self.dht_count += 1 - command = [PrivateConstants.DHT_NEW, pin] + if dht_type != 22 and dht_type != 11: + dht_type = 22 + + command = [PrivateConstants.DHT_NEW, pin, dht_type] self._send_command(command) else: if self.shutdown_on_exception: @@ -946,42 +959,50 @@

Module telemetrix.telemetrix

""" This is the dht report handler method. - :param data: data[0] = report sub type - DHT_DATA or DHT_ERROR + :param data: data[0] = report error return + No Errors = 0 + + Checksum Error = 1 + + Timeout Error = 2 + + Invalid Value = 999 data[1] = pin number - data[2] = humidity high order byte or error value if DHT_ERROR + data[2] = dht type 11 or 22 - data[3] = humidity byte 2 + data[3] = humidity positivity flag - data[4] = humidity byte 3 + data[4] = temperature positivity value - data[5] = humidity byte 4 + data[5] = humidity integer - data[6] = temperature high order byte for data + data[6] = humidity fractional value - data[7] = temperature byte 2 + data[7] = temperature integer - data[8] = temperature byte 3 + data[8] = temperature fractional value - data[9] = temperature byte 4 - """ + """ if data[0]: # DHT_ERROR # error report # data[0] = report sub type, data[1] = pin, data[2] = error message if self.dht_callbacks[data[1]]: - # Callback 0=DHT REPORT, DHT_ERROR=0, PIN, Error Number, Time + # Callback 0=DHT REPORT, DHT_ERROR, PIN, Time message = [PrivateConstants.DHT_REPORT, data[0], data[1], data[2], time.time()] self.dht_callbacks[data[1]](message) else: # got valid data DHT_DATA - f_humidity = bytearray(data[2:6]) - f_temperature = bytearray(data[6:]) - message = [PrivateConstants.DHT_REPORT, data[0], data[1], - (struct.unpack('<f', f_humidity))[0], - (struct.unpack('<f', f_temperature))[0], - time.time()] + f_humidity = float(data[5] + data[6] / 100) + if data[3]: + f_humidity *= -1.0 + f_temperature = float(data[7] + data[8] / 100) + if data[4]: + f_temperature *= -1.0 + message = [PrivateConstants.DHT_REPORT, data[0], data[1], data[2], + f_humidity, f_temperature, time.time()] self.dht_callbacks[data[1]](message) @@ -1475,6 +1496,10 @@

Classes

command = [PrivateConstants.ENABLE_ALL_REPORTS] self._send_command(command) + # Have the server reset its data structures + command = [PrivateConstants.RESET] + self._send_command(command) + def _find_arduino(self): """ This method will search all potential serial ports for an Arduino @@ -1930,16 +1955,20 @@

Classes

command = [PrivateConstants.I2C_BEGIN, i2c_port] self._send_command(command) - def set_pin_mode_dht(self, pin, callback=None): + def set_pin_mode_dht(self, pin, callback=None, dht_type=22): """ :param pin: connection pin :param callback: callback function - Error Callback: [Callback 0=DHT REPORT, DHT_ERROR=0, PIN, Error Number, Time] + :param dht_type: either 22 for DHT22 or 11 for DHT11 + + Error Callback: [DHT REPORT Type, DHT_ERROR_NUMBER, PIN, DHT_TYPE, Time] - Valid Data Callback: Callback 0=DHT REPORT, DHT_DATA=1, PIN, Humidity, Temperature Time] + Valid Data Callback: DHT REPORT Type, DHT_DATA=, PIN, DHT_TYPE, Humidity, + Temperature, + Time] """ @@ -1952,7 +1981,10 @@

Classes

self.dht_callbacks[pin] = callback self.dht_count += 1 - command = [PrivateConstants.DHT_NEW, pin] + if dht_type != 22 and dht_type != 11: + dht_type = 22 + + command = [PrivateConstants.DHT_NEW, pin, dht_type] self._send_command(command) else: if self.shutdown_on_exception: @@ -2138,42 +2170,50 @@

Classes

""" This is the dht report handler method. - :param data: data[0] = report sub type - DHT_DATA or DHT_ERROR + :param data: data[0] = report error return + No Errors = 0 + + Checksum Error = 1 + + Timeout Error = 2 + + Invalid Value = 999 data[1] = pin number - data[2] = humidity high order byte or error value if DHT_ERROR + data[2] = dht type 11 or 22 - data[3] = humidity byte 2 + data[3] = humidity positivity flag - data[4] = humidity byte 3 + data[4] = temperature positivity value - data[5] = humidity byte 4 + data[5] = humidity integer - data[6] = temperature high order byte for data + data[6] = humidity fractional value - data[7] = temperature byte 2 + data[7] = temperature integer - data[8] = temperature byte 3 + data[8] = temperature fractional value - data[9] = temperature byte 4 - """ + """ if data[0]: # DHT_ERROR # error report # data[0] = report sub type, data[1] = pin, data[2] = error message if self.dht_callbacks[data[1]]: - # Callback 0=DHT REPORT, DHT_ERROR=0, PIN, Error Number, Time + # Callback 0=DHT REPORT, DHT_ERROR, PIN, Time message = [PrivateConstants.DHT_REPORT, data[0], data[1], data[2], time.time()] self.dht_callbacks[data[1]](message) else: # got valid data DHT_DATA - f_humidity = bytearray(data[2:6]) - f_temperature = bytearray(data[6:]) - message = [PrivateConstants.DHT_REPORT, data[0], data[1], - (struct.unpack('<f', f_humidity))[0], - (struct.unpack('<f', f_temperature))[0], - time.time()] + f_humidity = float(data[5] + data[6] / 100) + if data[3]: + f_humidity *= -1.0 + f_temperature = float(data[7] + data[8] / 100) + if data[4]: + f_temperature *= -1.0 + message = [PrivateConstants.DHT_REPORT, data[0], data[1], data[2], + f_humidity, f_temperature, time.time()] self.dht_callbacks[data[1]](message) @@ -2884,27 +2924,34 @@

Methods

-def set_pin_mode_dht(self, pin, callback=None) +def set_pin_mode_dht(self, pin, callback=None, dht_type=22)

:param pin: connection pin

:param callback: callback function

-

Error Callback: [Callback 0=DHT REPORT, DHT_ERROR=0, PIN, Error Number, Time]

-

Valid Data Callback: Callback 0=DHT REPORT, DHT_DATA=1, PIN, Humidity, Temperature Time]

+

:param dht_type: either 22 for DHT22 or 11 for DHT11

+

Error Callback: [DHT REPORT Type, DHT_ERROR_NUMBER, PIN, DHT_TYPE, Time]

+

Valid Data Callback: DHT REPORT Type, DHT_DATA=, PIN, DHT_TYPE, Humidity, +Temperature, +Time]

Expand source code -
def set_pin_mode_dht(self, pin, callback=None):
+
def set_pin_mode_dht(self, pin, callback=None, dht_type=22):
     """
 
     :param pin: connection pin
 
     :param callback: callback function
 
-    Error Callback: [Callback 0=DHT REPORT, DHT_ERROR=0, PIN, Error Number, Time]
+    :param dht_type: either 22 for DHT22 or 11 for DHT11
 
-    Valid Data Callback: Callback 0=DHT REPORT, DHT_DATA=1, PIN, Humidity, Temperature Time]
+    Error Callback: [DHT REPORT Type, DHT_ERROR_NUMBER, PIN, DHT_TYPE, Time]
+
+    Valid Data Callback: DHT REPORT Type, DHT_DATA=, PIN, DHT_TYPE, Humidity,
+    Temperature,
+    Time]
 
     """
 
@@ -2917,7 +2964,10 @@ 

Methods

self.dht_callbacks[pin] = callback self.dht_count += 1 - command = [PrivateConstants.DHT_NEW, pin] + if dht_type != 22 and dht_type != 11: + dht_type = 22 + + command = [PrivateConstants.DHT_NEW, pin, dht_type] self._send_command(command) else: if self.shutdown_on_exception: @@ -3228,9 +3278,7 @@

-

Generated by pdoc 0.8.1.

+

Generated by pdoc 0.9.2.

- - \ No newline at end of file diff --git a/telemetrix/telemetrix.py b/telemetrix/telemetrix.py index 281f732..440c54d 100644 --- a/telemetrix/telemetrix.py +++ b/telemetrix/telemetrix.py @@ -701,16 +701,20 @@ def set_pin_mode_i2c(self, i2c_port=0): command = [PrivateConstants.I2C_BEGIN, i2c_port] self._send_command(command) - def set_pin_mode_dht(self, pin, callback=None): + def set_pin_mode_dht(self, pin, callback=None, dht_type=22): """ :param pin: connection pin :param callback: callback function - Error Callback: [Callback 0=DHT REPORT, DHT_ERROR=0, PIN, Error Number, Time] + :param dht_type: either 22 for DHT22 or 11 for DHT11 - Valid Data Callback: Callback 0=DHT REPORT, DHT_DATA=1, PIN, Humidity, Temperature Time] + Error Callback: [DHT REPORT Type, DHT_ERROR_NUMBER, PIN, DHT_TYPE, Time] + + Valid Data Callback: DHT REPORT Type, DHT_DATA=, PIN, DHT_TYPE, Humidity, + Temperature, + Time] """ @@ -723,7 +727,10 @@ def set_pin_mode_dht(self, pin, callback=None): self.dht_callbacks[pin] = callback self.dht_count += 1 - command = [PrivateConstants.DHT_NEW, pin] + if dht_type != 22 and dht_type != 11: + dht_type = 22 + + command = [PrivateConstants.DHT_NEW, pin, dht_type] self._send_command(command) else: if self.shutdown_on_exception: @@ -909,42 +916,50 @@ def _dht_report(self, data): """ This is the dht report handler method. - :param data: data[0] = report sub type - DHT_DATA or DHT_ERROR + :param data: data[0] = report error return + No Errors = 0 + + Checksum Error = 1 + + Timeout Error = 2 + + Invalid Value = 999 data[1] = pin number - data[2] = humidity high order byte or error value if DHT_ERROR + data[2] = dht type 11 or 22 - data[3] = humidity byte 2 + data[3] = humidity positivity flag - data[4] = humidity byte 3 + data[4] = temperature positivity value - data[5] = humidity byte 4 + data[5] = humidity integer - data[6] = temperature high order byte for data + data[6] = humidity fractional value - data[7] = temperature byte 2 + data[7] = temperature integer - data[8] = temperature byte 3 + data[8] = temperature fractional value - data[9] = temperature byte 4 - """ + """ if data[0]: # DHT_ERROR # error report # data[0] = report sub type, data[1] = pin, data[2] = error message if self.dht_callbacks[data[1]]: - # Callback 0=DHT REPORT, DHT_ERROR=0, PIN, Error Number, Time + # Callback 0=DHT REPORT, DHT_ERROR, PIN, Time message = [PrivateConstants.DHT_REPORT, data[0], data[1], data[2], time.time()] self.dht_callbacks[data[1]](message) else: # got valid data DHT_DATA - f_humidity = bytearray(data[2:6]) - f_temperature = bytearray(data[6:]) - message = [PrivateConstants.DHT_REPORT, data[0], data[1], - (struct.unpack(' Date: Sun, 30 May 2021 15:14:18 -0400 Subject: [PATCH 2/3] DHTStable changes --- examples/dht.py | 33 +++++++++++++++++--------------- html/telemetrix/index.html | 34 +++++++++++++++++++-------------- telemetrix/private_constants.py | 2 +- telemetrix/telemetrix.py | 17 ++++++++++------- 4 files changed, 49 insertions(+), 37 deletions(-) diff --git a/examples/dht.py b/examples/dht.py index 1dea31e..aa38742 100644 --- a/examples/dht.py +++ b/examples/dht.py @@ -42,15 +42,17 @@ # 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, 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] - """ + 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])) @@ -64,15 +66,16 @@ def the_callback(data): 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. - - :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 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) diff --git a/html/telemetrix/index.html b/html/telemetrix/index.html index 93762dc..df7b2dd 100644 --- a/html/telemetrix/index.html +++ b/html/telemetrix/index.html @@ -59,7 +59,6 @@

Module telemetrix.telemetrix

""" import socket -import struct import sys import threading import time @@ -71,10 +70,11 @@

Module telemetrix.telemetrix

# noinspection PyPackageRequirements from serial.tools import list_ports +# noinspection PyUnresolvedReferences from telemetrix.private_constants import PrivateConstants -# noinspection PyPep8,PyMethodMayBeStatic +# noinspection PyPep8,PyMethodMayBeStatic,GrazieInspection,PyBroadException class Telemetrix(threading.Thread): """ This class exposes and implements the telemetrix API. @@ -271,7 +271,7 @@

Module telemetrix.telemetrix

self.shutdown() raise RuntimeError(f'Incorrect Arduino ID: {self.reported_arduino_id}') print('Valid Arduino ID Found.') - # get arduino firmware version and print it + # get telemetrix firmware version and print it print('\nRetrieving Telemetrix4Arduino firmware ID...') self._get_firmware_version() if not self.firmware_version: @@ -281,7 +281,7 @@

Module telemetrix.telemetrix

else: print(f'Telemetrix4Arduino firmware version: {self.firmware_version[0]}.' - f'{self.firmware_version[1]}') + f'{self.firmware_version[1]}.{self.firmware_version[2]}') command = [PrivateConstants.ENABLE_ALL_REPORTS] self._send_command(command) @@ -374,7 +374,7 @@

Module telemetrix.telemetrix

:param pin: arduino pin number - :param value: pin value (maximum 16 bitrs) + :param value: pin value (maximum 16 bits) """ value_msb = value >> 8 @@ -1025,10 +1025,13 @@

Module telemetrix.telemetrix

def _firmware_message(self, data): """ Telemetrix4Arduino firmware version message - :param data: data[0] = major number, data[1] = minor number + + :param data: data[0] = major number, data[1] = minor number. + + data[2] = patch number """ - self.firmware_version = [data[0], data[1]] + self.firmware_version = [data[0], data[1], data[2]] def _i2c_read_report(self, data): """ @@ -1482,7 +1485,7 @@

Classes

self.shutdown() raise RuntimeError(f'Incorrect Arduino ID: {self.reported_arduino_id}') print('Valid Arduino ID Found.') - # get arduino firmware version and print it + # get telemetrix firmware version and print it print('\nRetrieving Telemetrix4Arduino firmware ID...') self._get_firmware_version() if not self.firmware_version: @@ -1492,7 +1495,7 @@

Classes

else: print(f'Telemetrix4Arduino firmware version: {self.firmware_version[0]}.' - f'{self.firmware_version[1]}') + f'{self.firmware_version[1]}.{self.firmware_version[2]}') command = [PrivateConstants.ENABLE_ALL_REPORTS] self._send_command(command) @@ -1585,7 +1588,7 @@

Classes

:param pin: arduino pin number - :param value: pin value (maximum 16 bitrs) + :param value: pin value (maximum 16 bits) """ value_msb = value >> 8 @@ -2236,10 +2239,13 @@

Classes

def _firmware_message(self, data): """ Telemetrix4Arduino firmware version message - :param data: data[0] = major number, data[1] = minor number + + :param data: data[0] = major number, data[1] = minor number. + + data[2] = patch number """ - self.firmware_version = [data[0], data[1]] + self.firmware_version = [data[0], data[1], data[2]] def _i2c_read_report(self, data): """ @@ -2473,7 +2479,7 @@

Methods

Set the specified pin to the specified value.

:param pin: arduino pin number

-

:param value: pin value (maximum 16 bitrs)

+

:param value: pin value (maximum 16 bits)

Expand source code @@ -2484,7 +2490,7 @@

Methods

:param pin: arduino pin number - :param value: pin value (maximum 16 bitrs) + :param value: pin value (maximum 16 bits) """ value_msb = value >> 8 diff --git a/telemetrix/private_constants.py b/telemetrix/private_constants.py index 7f0e8c9..0f862e3 100644 --- a/telemetrix/private_constants.py +++ b/telemetrix/private_constants.py @@ -58,7 +58,7 @@ class PrivateConstants: DEBUG_PRINT = 99 - TELEMETRIX_VERSION = "1.5" + TELEMETRIX_VERSION = "1.6" # reporting control REPORTING_DISABLE_ALL = 0 diff --git a/telemetrix/telemetrix.py b/telemetrix/telemetrix.py index 440c54d..8188013 100644 --- a/telemetrix/telemetrix.py +++ b/telemetrix/telemetrix.py @@ -16,7 +16,6 @@ """ import socket -import struct import sys import threading import time @@ -28,10 +27,11 @@ # noinspection PyPackageRequirements from serial.tools import list_ports +# noinspection PyUnresolvedReferences from telemetrix.private_constants import PrivateConstants -# noinspection PyPep8,PyMethodMayBeStatic +# noinspection PyPep8,PyMethodMayBeStatic,GrazieInspection,PyBroadException class Telemetrix(threading.Thread): """ This class exposes and implements the telemetrix API. @@ -228,7 +228,7 @@ def __init__(self, com_port=None, arduino_instance_id=1, self.shutdown() raise RuntimeError(f'Incorrect Arduino ID: {self.reported_arduino_id}') print('Valid Arduino ID Found.') - # get arduino firmware version and print it + # get telemetrix firmware version and print it print('\nRetrieving Telemetrix4Arduino firmware ID...') self._get_firmware_version() if not self.firmware_version: @@ -238,7 +238,7 @@ def __init__(self, com_port=None, arduino_instance_id=1, else: print(f'Telemetrix4Arduino firmware version: {self.firmware_version[0]}.' - f'{self.firmware_version[1]}') + f'{self.firmware_version[1]}.{self.firmware_version[2]}') command = [PrivateConstants.ENABLE_ALL_REPORTS] self._send_command(command) @@ -331,7 +331,7 @@ def analog_write(self, pin, value): :param pin: arduino pin number - :param value: pin value (maximum 16 bitrs) + :param value: pin value (maximum 16 bits) """ value_msb = value >> 8 @@ -982,10 +982,13 @@ def _digital_message(self, data): def _firmware_message(self, data): """ Telemetrix4Arduino firmware version message - :param data: data[0] = major number, data[1] = minor number + + :param data: data[0] = major number, data[1] = minor number. + + data[2] = patch number """ - self.firmware_version = [data[0], data[1]] + self.firmware_version = [data[0], data[1], data[2]] def _i2c_read_report(self, data): """ From 756158f88b441ac5fd1b8bffa08617038c947b8e Mon Sep 17 00:00:00 2001 From: MrYsLab Date: Sun, 30 May 2021 15:17:01 -0400 Subject: [PATCH 3/3] DHTStable changes --- examples/dht.py | 7 +-- telemetrix/telemetrix.py | 111 +++++++++++++++++++++++++-------------- 2 files changed, 77 insertions(+), 41 deletions(-) diff --git a/examples/dht.py b/examples/dht.py index aa38742..5254b26 100644 --- a/examples/dht.py +++ b/examples/dht.py @@ -17,6 +17,7 @@ import sys import time + from telemetrix import telemetrix """ @@ -56,7 +57,7 @@ def the_callback(data): if data[1]: # error message date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[4])) - print(f'DHT Error Report:' + 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[6])) @@ -83,8 +84,8 @@ def dht(my_board, pin, callback, dht_type): board = telemetrix.Telemetrix() try: - dht(board, 8, the_callback, 11) - dht(board, 9, the_callback, 22) + dht(board, 8, the_callback, 11) + dht(board, 9, the_callback, 22) dht(board, 10, the_callback, 22) dht(board, 11, the_callback, 11) diff --git a/telemetrix/telemetrix.py b/telemetrix/telemetrix.py index 8188013..4f08dd6 100644 --- a/telemetrix/telemetrix.py +++ b/telemetrix/telemetrix.py @@ -119,17 +119,27 @@ def __init__(self, com_port=None, arduino_instance_id=1, self.report_dispatch = {} # To add a command to the command dispatch table, append here. - self.report_dispatch.update({PrivateConstants.LOOP_COMMAND: self._report_loop_data}) - self.report_dispatch.update({PrivateConstants.DEBUG_PRINT: self._report_debug_data}) - self.report_dispatch.update({PrivateConstants.DIGITAL_REPORT: self._digital_message}) - self.report_dispatch.update({PrivateConstants.ANALOG_REPORT: self._analog_message}) - self.report_dispatch.update({PrivateConstants.FIRMWARE_REPORT: self._firmware_message}) + self.report_dispatch.update( + {PrivateConstants.LOOP_COMMAND: self._report_loop_data}) + self.report_dispatch.update( + {PrivateConstants.DEBUG_PRINT: self._report_debug_data}) + self.report_dispatch.update( + {PrivateConstants.DIGITAL_REPORT: self._digital_message}) + self.report_dispatch.update( + {PrivateConstants.ANALOG_REPORT: self._analog_message}) + self.report_dispatch.update( + {PrivateConstants.FIRMWARE_REPORT: self._firmware_message}) self.report_dispatch.update({PrivateConstants.I_AM_HERE_REPORT: self._i_am_here}) - self.report_dispatch.update({PrivateConstants.SERVO_UNAVAILABLE: self._servo_unavailable}) - self.report_dispatch.update({PrivateConstants.I2C_READ_REPORT: self._i2c_read_report}) - self.report_dispatch.update({PrivateConstants.I2C_TOO_FEW_BYTES_RCVD: self._i2c_too_few}) - self.report_dispatch.update({PrivateConstants.I2C_TOO_MANY_BYTES_RCVD: self._i2c_too_many}) - self.report_dispatch.update({PrivateConstants.SONAR_DISTANCE: self._sonar_distance_report}) + self.report_dispatch.update( + {PrivateConstants.SERVO_UNAVAILABLE: self._servo_unavailable}) + self.report_dispatch.update( + {PrivateConstants.I2C_READ_REPORT: self._i2c_read_report}) + self.report_dispatch.update( + {PrivateConstants.I2C_TOO_FEW_BYTES_RCVD: self._i2c_too_few}) + self.report_dispatch.update( + {PrivateConstants.I2C_TOO_MANY_BYTES_RCVD: self._i2c_too_many}) + self.report_dispatch.update( + {PrivateConstants.SONAR_DISTANCE: self._sonar_distance_report}) self.report_dispatch.update({PrivateConstants.DHT_REPORT: self._dht_report}) # dictionaries to store the callbacks for each pin @@ -201,7 +211,8 @@ def __init__(self, com_port=None, arduino_instance_id=1, self.shutdown() if self.serial_port: - print(f"Arduino compatible device found and connected to {self.serial_port.port}") + print( + f"Arduino compatible device found and connected to {self.serial_port.port}") self.serial_port.reset_input_buffer() self.serial_port.reset_output_buffer() @@ -276,8 +287,9 @@ def _find_arduino(self): # clear out any possible data in the input buffer # wait for arduino to reset - print(f'\nWaiting {self.arduino_wait} seconds(arduino_wait) for Arduino devices to ' - 'reset...') + print( + f'\nWaiting {self.arduino_wait} seconds(arduino_wait) for Arduino devices to ' + 'reset...') # temporary for testing time.sleep(self.arduino_wait) @@ -296,8 +308,9 @@ def _manual_open(self): self.serial_port = serial.Serial(self.com_port, 115200, timeout=1, writeTimeout=0) - print(f'\nWaiting {self.arduino_wait} seconds(arduino_wait) for Arduino devices to ' - 'reset...') + print( + f'\nWaiting {self.arduino_wait} seconds(arduino_wait) for Arduino devices to ' + 'reset...') self._run_threads() time.sleep(self.arduino_wait) @@ -315,7 +328,8 @@ def _manual_open(self): if not self.firmware_version: if self.shutdown_on_exception: self.shutdown() - raise RuntimeError(f'Telemetrix4Arduino Sketch Firmware Version Not Found') + raise RuntimeError( + f'Telemetrix4Arduino Sketch Firmware Version Not Found') else: print(f'Telemetrix4Arduino firmware version: {self.firmware_version[0]}.' @@ -356,7 +370,8 @@ def disable_all_reporting(self): """ Disable reporting for all digital and analog input pins """ - command = [PrivateConstants.MODIFY_REPORTING, PrivateConstants.REPORTING_DISABLE_ALL, 0] + command = [PrivateConstants.MODIFY_REPORTING, + PrivateConstants.REPORTING_DISABLE_ALL, 0] self._send_command(command) def disable_analog_reporting(self, pin): @@ -366,7 +381,8 @@ def disable_analog_reporting(self, pin): :param pin: Analog pin number. For example for A0, the number is 0. """ - command = [PrivateConstants.MODIFY_REPORTING, PrivateConstants.REPORTING_ANALOG_DISABLE, pin] + command = [PrivateConstants.MODIFY_REPORTING, + PrivateConstants.REPORTING_ANALOG_DISABLE, pin] self._send_command(command) def disable_digital_reporting(self, pin): @@ -376,7 +392,8 @@ def disable_digital_reporting(self, pin): :param pin: Pin number. """ - command = [PrivateConstants.MODIFY_REPORTING, PrivateConstants.REPORTING_DIGITAL_DISABLE, pin] + command = [PrivateConstants.MODIFY_REPORTING, + PrivateConstants.REPORTING_DIGITAL_DISABLE, pin] self._send_command(command) def enable_analog_reporting(self, pin): @@ -387,7 +404,8 @@ def enable_analog_reporting(self, pin): """ - command = [PrivateConstants.MODIFY_REPORTING, PrivateConstants.REPORTING_ANALOG_ENABLE, pin] + command = [PrivateConstants.MODIFY_REPORTING, + PrivateConstants.REPORTING_ANALOG_ENABLE, pin] self._send_command(command) def enable_digital_reporting(self, pin): @@ -397,7 +415,8 @@ def enable_digital_reporting(self, pin): :param pin: Pin number. """ - command = [PrivateConstants.MODIFY_REPORTING, PrivateConstants.REPORTING_DIGITAL_ENABLE, pin] + command = [PrivateConstants.MODIFY_REPORTING, + PrivateConstants.REPORTING_DIGITAL_ENABLE, pin] self._send_command(command) def _get_arduino_id(self): @@ -476,7 +495,8 @@ def i2c_read_restart_transmission(self, address, register, """ - self._i2c_read_request(address, register, number_of_bytes, stop_transmission=False, + self._i2c_read_request(address, register, number_of_bytes, + stop_transmission=False, callback=callback, i2c_port=i2c_port) def _i2c_read_request(self, address, register, number_of_bytes, @@ -501,13 +521,15 @@ def _i2c_read_request(self, address, register, number_of_bytes, if not self.i2c_1_active: if self.shutdown_on_exception: self.shutdown() - raise RuntimeError('I2C Read: set_pin_mode i2c never called for i2c port 1.') + raise RuntimeError( + 'I2C Read: set_pin_mode i2c never called for i2c port 1.') if i2c_port: if not self.i2c_2_active: if self.shutdown_on_exception: self.shutdown() - raise RuntimeError('I2C Read: set_pin_mode i2c never called for i2c port 2.') + raise RuntimeError( + 'I2C Read: set_pin_mode i2c never called for i2c port 2.') if not callback: if self.shutdown_on_exception: @@ -549,13 +571,15 @@ def i2c_write(self, address, args, i2c_port=0): if not self.i2c_1_active: if self.shutdown_on_exception: self.shutdown() - raise RuntimeError('I2C Write: set_pin_mode i2c never called for i2c port 1.') + raise RuntimeError( + 'I2C Write: set_pin_mode i2c never called for i2c port 1.') if i2c_port: if not self.i2c_2_active: if self.shutdown_on_exception: self.shutdown() - raise RuntimeError('I2C Write: set_pin_mode i2c never called for i2c port 2.') + raise RuntimeError( + 'I2C Write: set_pin_mode i2c never called for i2c port 2.') command = [PrivateConstants.I2C_WRITE, len(args), address, i2c_port] @@ -735,7 +759,8 @@ def set_pin_mode_dht(self, pin, callback=None, dht_type=22): else: if self.shutdown_on_exception: self.shutdown() - raise RuntimeError(f'Maximum Number Of DHTs Exceeded - set_pin_mode_dht fails for pin {pin}') + raise RuntimeError( + f'Maximum Number Of DHTs Exceeded - set_pin_mode_dht fails for pin {pin}') # noinspection PyRedundantParentheses def set_pin_mode_servo(self, pin_number, min_pulse=544, max_pulse=2400): @@ -785,7 +810,8 @@ def set_pin_mode_sonar(self, trigger_pin, echo_pin, else: if self.shutdown_on_exception: self.shutdown() - raise RuntimeError(f'Maximum Number Of Sonars Exceeded - set_pin_mode_sonar fails for pin {trigger_pin}') + raise RuntimeError( + f'Maximum Number Of Sonars Exceeded - set_pin_mode_sonar fails for pin {trigger_pin}') def servo_write(self, pin_number, angle): """ @@ -841,16 +867,20 @@ def _set_pin_mode(self, pin_number, pin_state, differential=0, callback=None): 'pin state:', pin_state)) if pin_state == PrivateConstants.AT_INPUT: - command = [PrivateConstants.SET_PIN_MODE, pin_number, PrivateConstants.AT_INPUT, 1] + command = [PrivateConstants.SET_PIN_MODE, pin_number, + PrivateConstants.AT_INPUT, 1] elif pin_state == PrivateConstants.AT_INPUT_PULLUP: - command = [PrivateConstants.SET_PIN_MODE, pin_number, PrivateConstants.AT_INPUT_PULLUP, 1] + command = [PrivateConstants.SET_PIN_MODE, pin_number, + PrivateConstants.AT_INPUT_PULLUP, 1] elif pin_state == PrivateConstants.AT_OUTPUT: - command = [PrivateConstants.SET_PIN_MODE, pin_number, PrivateConstants.AT_OUTPUT] + command = [PrivateConstants.SET_PIN_MODE, pin_number, + PrivateConstants.AT_OUTPUT] elif pin_state == PrivateConstants.AT_ANALOG: - command = [PrivateConstants.SET_PIN_MODE, pin_number, PrivateConstants.AT_ANALOG, + command = [PrivateConstants.SET_PIN_MODE, pin_number, + PrivateConstants.AT_ANALOG, differential >> 8, differential & 0xff, 1] else: if self.shutdown_on_exception: @@ -948,7 +978,8 @@ def _dht_report(self, data): # data[0] = report sub type, data[1] = pin, data[2] = error message if self.dht_callbacks[data[1]]: # Callback 0=DHT REPORT, DHT_ERROR, PIN, Time - message = [PrivateConstants.DHT_REPORT, data[0], data[1], data[2], time.time()] + message = [PrivateConstants.DHT_REPORT, data[0], data[1], data[2], + time.time()] self.dht_callbacks[data[1]](message) else: # got valid data DHT_DATA @@ -959,7 +990,7 @@ def _dht_report(self, data): if data[4]: f_temperature *= -1.0 message = [PrivateConstants.DHT_REPORT, data[0], data[1], data[2], - f_humidity, f_temperature, time.time()] + f_humidity, f_temperature, time.time()] self.dht_callbacks[data[1]](message) @@ -1023,7 +1054,8 @@ def _i2c_too_few(self, data): """ if self.shutdown_on_exception: self.shutdown() - raise RuntimeError(f'i2c too few bytes received from i2c port {data[0]} i2c address {data[1]}') + raise RuntimeError( + f'i2c too few bytes received from i2c port {data[0]} i2c address {data[1]}') def _i2c_too_many(self, data): """ @@ -1033,7 +1065,8 @@ def _i2c_too_many(self, data): """ if self.shutdown_on_exception: self.shutdown() - raise RuntimeError(f'i2c too many bytes received from i2c port {data[0]} i2c address {data[1]}') + raise RuntimeError( + f'i2c too many bytes received from i2c port {data[0]} i2c address {data[1]}') def _i_am_here(self, data): """ @@ -1093,7 +1126,8 @@ def _servo_unavailable(self, report): """ if self.shutdown_on_exception: self.shutdown() - raise RuntimeError(f'Servo Attach For Pin {report[0]} Failed: No Available Servos') + raise RuntimeError( + f'Servo Attach For Pin {report[0]} Failed: No Available Servos') def _sonar_distance_report(self, report): """ @@ -1161,7 +1195,8 @@ def _reporter(self): else: if self.shutdown_on_exception: self.shutdown() - raise RuntimeError('A report with a packet length of zero was received.') + raise RuntimeError( + 'A report with a packet length of zero was received.') else: time.sleep(self.sleep_tune)