diff --git a/examples/dht.py b/examples/dht.py index 27b1052..cc41e0a 100644 --- a/examples/dht.py +++ b/examples/dht.py @@ -41,15 +41,17 @@ def the_callback(data): This will print the pin number, its reported value and the date and time when the change occurred - :param data: [pin, current reported value, pin_mode, timestamp] + :param data: [report_type, pin, dht_type, error_value, + humidity, temperature, timestamp] """ - tlist = time.localtime(data[5]) - ftime = f'{tlist.tm_year}-{tlist.tm_mon:02}-{tlist.tm_mday:02} ' \ - f'{tlist.tm_hour:02}:{tlist.tm_min:0}:{tlist.tm_sec:02}' + if not data[3]: + tlist = time.localtime(data[6]) + ftime = f'{tlist.tm_year}-{tlist.tm_mon:02}-{tlist.tm_mday:02} ' \ + f'{tlist.tm_hour:02}:{tlist.tm_min:0}:{tlist.tm_sec:02}' - print(f'Pin: {data[1]} DHT Type: {data[2]} Humidity:{data[3]}, ' - f'Temperature: {data[4]} Timestamp: {ftime}') + print(f'Pin: {data[1]} DHT Type: {data[2]} Humidity:{data[4]}, ' + f'Temperature: {data[5]} Timestamp: {ftime}') def dht(my_board, callback=None): @@ -63,8 +65,11 @@ def dht(my_board, callback=None): """ # set the pin mode - for pin 6 differential is set explicitly - my_board.set_pin_mode_dht(6, sensor_type=22, differential=.01, callback=callback) - my_board.set_pin_mode_dht(7, sensor_type=11, callback=callback) + my_board.set_pin_mode_dht(8, sensor_type=11, differential=.05, callback=callback) + my_board.set_pin_mode_dht(9, sensor_type=22, differential=.05, callback=callback) + + my_board.set_pin_mode_dht(10, sensor_type=22, differential=.05, callback=callback) + my_board.set_pin_mode_dht(11, sensor_type=11, differential=.05, callback=callback) # a flag to change the differential value after the first 5 seconds changed = False @@ -72,27 +77,53 @@ def dht(my_board, callback=None): try: time.sleep(POLL_TIME) - # poll the first DHT - value = board.dht_read(6) + # poll the first dht + value = board.dht_read(8) # format the time string and then print the data tlist = time.localtime(value[2]) ftime = f'{tlist.tm_year}-{tlist.tm_mon:02}-{tlist.tm_mday:02} ' \ f'{tlist.tm_hour:02}:{tlist.tm_min:0}:{tlist.tm_sec:02}' - print(f'poll pin 6: humidity={value[0]} temp={value[1]} ' + print(f'poll pin 8: humidity={value[0]} temp={value[1]} ' f'time of last report: {ftime}') # poll the second DHT and print the values - value = board.dht_read(7) + value = board.dht_read(9) tlist = time.localtime(value[2]) ftime = f'{tlist.tm_year}-{tlist.tm_mon:02}-{tlist.tm_mday:02} ' \ f'{tlist.tm_hour:02}:{tlist.tm_min:0}:{tlist.tm_sec:02}' - print(f'poll pin 7: humidity={value[0]} temp={value[1]} ' + print(f'poll pin 9: humidity={value[0]} temp={value[1]} ' + f'time of last report: {ftime}') + + # poll the third dht + + value = board.dht_read(10) + + # format the time string and then print the data + tlist = time.localtime(value[2]) + ftime = f'{tlist.tm_year}-{tlist.tm_mon:02}-{tlist.tm_mday:02} ' \ + f'{tlist.tm_hour:02}:{tlist.tm_min:0}:{tlist.tm_sec:02}' + print(f'poll pin 10: humidity={value[0]} temp={value[1]} ' + f'time of last report: {ftime}') + + # poll the fourth DHT + value = board.dht_read(11) + tlist = time.localtime(value[2]) + ftime = f'{tlist.tm_year}-{tlist.tm_mon:02}-{tlist.tm_mday:02} ' \ + f'{tlist.tm_hour:02}:{tlist.tm_min:0}:{tlist.tm_sec:02}' + print(f'poll pin 11: humidity={value[0]} temp={value[1]} ' f'time of last report: {ftime}') if not changed: # explicitly change the differential values - my_board.set_pin_mode_dht(6, sensor_type=22, differential=20.0, callback=callback) - my_board.set_pin_mode_dht(7, sensor_type=11, differential=2.0, callback=callback) + + my_board.set_pin_mode_dht(8, sensor_type=11, differential=2.0, + callback=callback) + my_board.set_pin_mode_dht(9, sensor_type=22, differential=20.0, + callback=callback) + my_board.set_pin_mode_dht(10, sensor_type=22, differential=20.0, + callback=callback) + my_board.set_pin_mode_dht(11, sensor_type=11, differential=2.0, + callback=callback) changed = True except KeyboardInterrupt: board.shutdown() diff --git a/html/pymata4/index.html b/html/pymata4/index.html index 8117828..9f6af13 100644 --- a/html/pymata4/index.html +++ b/html/pymata4/index.html @@ -3,15 +3,17 @@ - + pymata4.pymata4 API documentation - - - - + + + + + +
@@ -35,6 +37,8 @@

Module pymata4.pymata4

Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

+

DHT support courtesy of Martyn Wheeler +Based on the DHTNew library - https://github.com/RobTillaart/DHTNew

Expand source code @@ -54,6 +58,9 @@

Module pymata4.pymata4

You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + DHT support courtesy of Martyn Wheeler + Based on the DHTNew library - https://github.com/RobTillaart/DHTNew """ from collections import deque @@ -318,7 +325,8 @@

Module pymata4.pymata4

if self.using_firmata_express: version_number = firmware_version[0:3] if version_number != PrivateConstants.FIRMATA_EXPRESS_VERSION: - raise RuntimeError(f'You must use FirmataExpress version 1.1. Version Found = {version_number}') + raise RuntimeError(f'You must use FirmataExpress version 1.2. ' + f'Version Found = {version_number}') print(f'Arduino Firmware ID: {firmware_version}') except TypeError: print('\nIs your serial cable plugged in and do you have the ' @@ -413,6 +421,7 @@

Module pymata4.pymata4

# wait until the END_SYSEX comes back i_am_here = self.serial_port.read_until(b'\xf7') + if not i_am_here: continue @@ -431,6 +440,7 @@

Module pymata4.pymata4

if i_am_here[2] == self.arduino_instance_id: self.using_firmata_express = True self.com_port = self.serial_port + return except KeyboardInterrupt: raise RuntimeError('User Hit Control-C') @@ -491,10 +501,8 @@

Module pymata4.pymata4

:return: A list = [humidity, temperature time_stamp] - ERROR CODES: If either humidity or temperature value: - == -1 Configuration Error - == -2 Checksum Error - == -3 Timeout Error + NOTE: If an error was returned, the humidity and temperture + are set to 0.0. """ return self.digital_pins[pin].current_value[0], \ @@ -811,7 +819,8 @@

Module pymata4.pymata4

:param address: i2c device address - :param register: i2c register + :param register: i2c register (or None if no register + selection is needed) :param number_of_bytes: number of bytes to be read @@ -874,7 +883,7 @@

Module pymata4.pymata4

data = [address, read_type, register & 0x7f, (register >> 7) & 0x7f, number_of_bytes & 0x7f, (number_of_bytes >> 7) & 0x7f] else: - data = [address, read_type, + data = [address, read_type, number_of_bytes & 0x7f, (number_of_bytes >> 7) & 0x7f] self._send_sysex(PrivateConstants.I2C_REQUEST, data) @@ -1084,7 +1093,7 @@

Module pymata4.pymata4

:param pin_number: digital pin number on arduino. :param sensor_type: type of dht sensor - Valid values = DHT11, DHT12, DHT22, DHT21, AM2301 + Valid values = DHT11, DHT22, :param differential: This value needs to be met for a callback to be invoked. @@ -1093,14 +1102,21 @@

Module pymata4.pymata4

callback: returns a data list: - [pin_type, pin_number, DHT type, humidity value, temperature raw_time_stamp] + [pin_type, pin_number, DHT type, validation flag, humidity value, temperature + raw_time_stamp] The pin_type for DHT input pins = 15 - ERROR CODES: If either humidity or temperature value: - == -1 Configuration Error - == -2 Checksum Error - == -3 Timeout Error + Validation Flag Values: + + No Errors = 0 + + Checksum Error = 1 + + Timeout Error = 2 + + Invalid Value = 999 + """ # if the pin is not currently associated with a DHT device @@ -1498,17 +1514,8 @@

Module pymata4.pymata4

""" Process the dht response message. - Values are calculated using: - humidity = (_bits[0] * 256 + _bits[1]) * 0.1 - - temperature = ((_bits[2] & 0x7F) * 256 + _bits[3]) * 0.1 - error codes: - 0 - OK - 1 - DHTLIB_ERROR_TIMEOUT - 2 - Checksum error - - :param: data - array of 9 7bit bytes ending with the error status + :param: data: [Report Type, pin, dht_type, validation_flag, humidity, temperature] """ # get the time of the report time_stamp = time.time() @@ -1520,49 +1527,15 @@

Module pymata4.pymata4

reply_data.append(pin) dht_type = data[1] reply_data.append(dht_type) - humidity = None - temperature = None - self.digital_pins[pin].event_time = time_stamp + humidity = temperature = 0 - if data[7] == 1: # data[9] is config flag - if data[10] != 0: - self.dht_sensor_error = True - humidity = temperature = -1 - # return - else: - # if data read correctly process and return - - if data[6] == 0: - # dht 22 - if data[1] == 22: - humidity = (data[2] * 256 + data[3]) * 0.1 - temperature = ((data[4] & 0x7F) * 256 + data[5]) * 0.1 - # dht 11 - elif data[1] == 11: - humidity = (data[2]) + (data[3]) * 0.1 - temperature = (data[4]) + (data[5]) * 0.1 - else: - raise RuntimeError(f'Unknown DHT Sensor type reported: {data[2]}') - - humidity = round(humidity, 2) - temperature = round(temperature, 2) - - # check for negative temperature - if data[6] & 0x80: - temperature = -temperature - - elif data[7] == 1: - # Checksum Error - humidity = temperature = -2 - self.dht_sensor_error = True - elif data[7] == 2: - # Timeout Error - humidity = temperature = -3 - self.dht_sensor_error = True - # since we initialize - if humidity is None: - return + if data[2] == 0: # all is well + humidity = float(data[3] + data[4] / 10) + temperature = float(data[5] + data[6] / 10) + + self.digital_pins[pin].event_time = time_stamp + reply_data.append(data[2]) reply_data.append(humidity) reply_data.append(temperature) reply_data.append(time_stamp) @@ -2273,7 +2246,8 @@

Classes

if self.using_firmata_express: version_number = firmware_version[0:3] if version_number != PrivateConstants.FIRMATA_EXPRESS_VERSION: - raise RuntimeError(f'You must use FirmataExpress version 1.1. Version Found = {version_number}') + raise RuntimeError(f'You must use FirmataExpress version 1.2. ' + f'Version Found = {version_number}') print(f'Arduino Firmware ID: {firmware_version}') except TypeError: print('\nIs your serial cable plugged in and do you have the ' @@ -2368,6 +2342,7 @@

Classes

# wait until the END_SYSEX comes back i_am_here = self.serial_port.read_until(b'\xf7') + if not i_am_here: continue @@ -2386,6 +2361,7 @@

Classes

if i_am_here[2] == self.arduino_instance_id: self.using_firmata_express = True self.com_port = self.serial_port + return except KeyboardInterrupt: raise RuntimeError('User Hit Control-C') @@ -2446,10 +2422,8 @@

Classes

:return: A list = [humidity, temperature time_stamp] - ERROR CODES: If either humidity or temperature value: - == -1 Configuration Error - == -2 Checksum Error - == -3 Timeout Error + NOTE: If an error was returned, the humidity and temperture + are set to 0.0. """ return self.digital_pins[pin].current_value[0], \ @@ -2766,7 +2740,8 @@

Classes

:param address: i2c device address - :param register: i2c register + :param register: i2c register (or None if no register + selection is needed) :param number_of_bytes: number of bytes to be read @@ -2829,7 +2804,7 @@

Classes

data = [address, read_type, register & 0x7f, (register >> 7) & 0x7f, number_of_bytes & 0x7f, (number_of_bytes >> 7) & 0x7f] else: - data = [address, read_type, + data = [address, read_type, number_of_bytes & 0x7f, (number_of_bytes >> 7) & 0x7f] self._send_sysex(PrivateConstants.I2C_REQUEST, data) @@ -3039,7 +3014,7 @@

Classes

:param pin_number: digital pin number on arduino. :param sensor_type: type of dht sensor - Valid values = DHT11, DHT12, DHT22, DHT21, AM2301 + Valid values = DHT11, DHT22, :param differential: This value needs to be met for a callback to be invoked. @@ -3048,14 +3023,21 @@

Classes

callback: returns a data list: - [pin_type, pin_number, DHT type, humidity value, temperature raw_time_stamp] + [pin_type, pin_number, DHT type, validation flag, humidity value, temperature + raw_time_stamp] The pin_type for DHT input pins = 15 - ERROR CODES: If either humidity or temperature value: - == -1 Configuration Error - == -2 Checksum Error - == -3 Timeout Error + Validation Flag Values: + + No Errors = 0 + + Checksum Error = 1 + + Timeout Error = 2 + + Invalid Value = 999 + """ # if the pin is not currently associated with a DHT device @@ -3453,17 +3435,8 @@

Classes

""" Process the dht response message. - Values are calculated using: - humidity = (_bits[0] * 256 + _bits[1]) * 0.1 - temperature = ((_bits[2] & 0x7F) * 256 + _bits[3]) * 0.1 - - error codes: - 0 - OK - 1 - DHTLIB_ERROR_TIMEOUT - 2 - Checksum error - - :param: data - array of 9 7bit bytes ending with the error status + :param: data: [Report Type, pin, dht_type, validation_flag, humidity, temperature] """ # get the time of the report time_stamp = time.time() @@ -3475,49 +3448,15 @@

Classes

reply_data.append(pin) dht_type = data[1] reply_data.append(dht_type) - humidity = None - temperature = None - self.digital_pins[pin].event_time = time_stamp + humidity = temperature = 0 - if data[7] == 1: # data[9] is config flag - if data[10] != 0: - self.dht_sensor_error = True - humidity = temperature = -1 - # return - else: - # if data read correctly process and return - - if data[6] == 0: - # dht 22 - if data[1] == 22: - humidity = (data[2] * 256 + data[3]) * 0.1 - temperature = ((data[4] & 0x7F) * 256 + data[5]) * 0.1 - # dht 11 - elif data[1] == 11: - humidity = (data[2]) + (data[3]) * 0.1 - temperature = (data[4]) + (data[5]) * 0.1 - else: - raise RuntimeError(f'Unknown DHT Sensor type reported: {data[2]}') - - humidity = round(humidity, 2) - temperature = round(temperature, 2) - - # check for negative temperature - if data[6] & 0x80: - temperature = -temperature - - elif data[7] == 1: - # Checksum Error - humidity = temperature = -2 - self.dht_sensor_error = True - elif data[7] == 2: - # Timeout Error - humidity = temperature = -3 - self.dht_sensor_error = True - # since we initialize - if humidity is None: - return + if data[2] == 0: # all is well + humidity = float(data[3] + data[4] / 10) + temperature = float(data[5] + data[6] / 10) + + self.digital_pins[pin].event_time = time_stamp + reply_data.append(data[2]) reply_data.append(humidity) reply_data.append(temperature) reply_data.append(time_stamp) @@ -3978,11 +3917,8 @@

Methods

:param pin: digital pin number

:return: A list = [humidity, temperature time_stamp]

-
     ERROR CODES: If either humidity or temperature value:
-                  == -1 Configuration Error
-                  == -2 Checksum Error
-                  == -3 Timeout Error
-
+

NOTE: If an error was returned, the humidity and temperture +are set to 0.0.

Expand source code @@ -3995,10 +3931,8 @@

Methods

:return: A list = [humidity, temperature time_stamp] - ERROR CODES: If either humidity or temperature value: - == -1 Configuration Error - == -2 Checksum Error - == -3 Timeout Error + NOTE: If an error was returned, the humidity and temperture + are set to 0.0. """ return self.digital_pins[pin].current_value[0], \ @@ -4524,7 +4458,8 @@

Methods

the i2c device. This restarts the transmission after the read. It is required for some i2c devices such as the MMA8452Q accelerometer.

:param address: i2c device address

-

:param register: i2c register

+

:param register: i2c register (or None if no register +selection is needed)

:param number_of_bytes: number of bytes to be read

:param callback: Optional callback function to report i2c data as a result of read command

@@ -4546,7 +4481,8 @@

Methods

:param address: i2c device address - :param register: i2c register + :param register: i2c register (or None if no register + selection is needed) :param number_of_bytes: number of bytes to be read @@ -4889,18 +4825,19 @@

Methods

Up to 6 DHT sensors are supported

:param pin_number: digital pin number on arduino.

:param sensor_type: type of dht sensor -Valid values = DHT11, DHT12, DHT22, DHT21, AM2301

+Valid values = DHT11, DHT22,

:param differential: This value needs to be met for a callback to be invoked.

:param callback: callback function

callback: returns a data list:

-

[pin_type, pin_number, DHT type, humidity value, temperature raw_time_stamp]

+

[pin_type, pin_number, DHT type, validation flag, humidity value, temperature +raw_time_stamp]

The pin_type for DHT input pins = 15

-
    ERROR CODES: If either humidity or temperature value:
-                  == -1 Configuration Error
-                  == -2 Checksum Error
-                  == -3 Timeout Error
-
+

Validation Flag Values:

+

No Errors = 0

+

Checksum Error = 1

+

Timeout Error = 2

+

Invalid Value = 999

Expand source code @@ -4913,7 +4850,7 @@

Methods

:param pin_number: digital pin number on arduino. :param sensor_type: type of dht sensor - Valid values = DHT11, DHT12, DHT22, DHT21, AM2301 + Valid values = DHT11, DHT22, :param differential: This value needs to be met for a callback to be invoked. @@ -4922,14 +4859,21 @@

Methods

callback: returns a data list: - [pin_type, pin_number, DHT type, humidity value, temperature raw_time_stamp] + [pin_type, pin_number, DHT type, validation flag, humidity value, temperature + raw_time_stamp] The pin_type for DHT input pins = 15 - ERROR CODES: If either humidity or temperature value: - == -1 Configuration Error - == -2 Checksum Error - == -3 Timeout Error + Validation Flag Values: + + No Errors = 0 + + Checksum Error = 1 + + Timeout Error = 2 + + Invalid Value = 999 + """ # if the pin is not currently associated with a DHT device @@ -5477,9 +5421,7 @@

Pym

- - \ No newline at end of file diff --git a/html/pymata4/pin_data.html b/html/pymata4/pin_data.html index 098e917..94685b2 100644 --- a/html/pymata4/pin_data.html +++ b/html/pymata4/pin_data.html @@ -3,15 +3,17 @@ - + pymata4.pin_data API documentation - - - - + + + + + +
@@ -326,9 +328,7 @@

P

- - \ No newline at end of file diff --git a/html/pymata4/private_constants.html b/html/pymata4/private_constants.html index 530a490..53c785c 100644 --- a/html/pymata4/private_constants.html +++ b/html/pymata4/private_constants.html @@ -3,15 +3,17 @@ - + pymata4.private_constants API documentation - - - - + + + + + +
@@ -111,10 +113,10 @@

Module pymata4.private_constants

SYSEX_REALTIME = 0x7F # MIDI Reserved for realtime messages # reserved for PyMata - PYMATA_EXPRESS_THREADED_VERSION = "1.6" + PYMATA_EXPRESS_THREADED_VERSION = "1.11" # matching FirmataExpress Version Number - FIRMATA_EXPRESS_VERSION = "1.1" + FIRMATA_EXPRESS_VERSION = "1.2" # each byte represents a digital port # and its value contains the current port settings @@ -248,10 +250,10 @@

Classes

SYSEX_REALTIME = 0x7F # MIDI Reserved for realtime messages # reserved for PyMata - PYMATA_EXPRESS_THREADED_VERSION = "1.6" + PYMATA_EXPRESS_THREADED_VERSION = "1.11" # matching FirmataExpress Version Number - FIRMATA_EXPRESS_VERSION = "1.1" + FIRMATA_EXPRESS_VERSION = "1.2" # each byte represents a digital port # and its value contains the current port settings @@ -708,9 +710,7 @@

-

Generated by pdoc 0.8.1.

+

Generated by pdoc 0.9.2.

- - \ No newline at end of file diff --git a/pymata4/private_constants.py b/pymata4/private_constants.py index e3bd764..a0b5729 100644 --- a/pymata4/private_constants.py +++ b/pymata4/private_constants.py @@ -70,10 +70,10 @@ class PrivateConstants: SYSEX_REALTIME = 0x7F # MIDI Reserved for realtime messages # reserved for PyMata - PYMATA_EXPRESS_THREADED_VERSION = "1.10" + PYMATA_EXPRESS_THREADED_VERSION = "1.11" # matching FirmataExpress Version Number - FIRMATA_EXPRESS_VERSION = "1.1" + FIRMATA_EXPRESS_VERSION = "1.2" # each byte represents a digital port # and its value contains the current port settings diff --git a/pymata4/pymata4.py b/pymata4/pymata4.py index 1ca2ba7..a5ce099 100644 --- a/pymata4/pymata4.py +++ b/pymata4/pymata4.py @@ -280,7 +280,8 @@ def __init__(self, com_port=None, baud_rate=115200, if self.using_firmata_express: version_number = firmware_version[0:3] if version_number != PrivateConstants.FIRMATA_EXPRESS_VERSION: - raise RuntimeError(f'You must use FirmataExpress version 1.1. Version Found = {version_number}') + raise RuntimeError(f'You must use FirmataExpress version 1.2. ' + f'Version Found = {version_number}') print(f'Arduino Firmware ID: {firmware_version}') except TypeError: print('\nIs your serial cable plugged in and do you have the ' @@ -455,10 +456,8 @@ def dht_read(self, pin): :return: A list = [humidity, temperature time_stamp] - ERROR CODES: If either humidity or temperature value: - == -1 Configuration Error - == -2 Checksum Error - == -3 Timeout Error + NOTE: If an error was returned, the humidity and temperture + are set to 0.0. """ return self.digital_pins[pin].current_value[0], \ @@ -839,7 +838,7 @@ def _i2c_read_request(self, address, register, number_of_bytes, read_type, data = [address, read_type, register & 0x7f, (register >> 7) & 0x7f, number_of_bytes & 0x7f, (number_of_bytes >> 7) & 0x7f] else: - data = [address, read_type, + data = [address, read_type, number_of_bytes & 0x7f, (number_of_bytes >> 7) & 0x7f] self._send_sysex(PrivateConstants.I2C_REQUEST, data) @@ -1049,7 +1048,7 @@ def set_pin_mode_dht(self, pin_number, sensor_type=22, differential=.1, callback :param pin_number: digital pin number on arduino. :param sensor_type: type of dht sensor - Valid values = DHT11, DHT12, DHT22, DHT21, AM2301 + Valid values = DHT11, DHT22, :param differential: This value needs to be met for a callback to be invoked. @@ -1058,14 +1057,21 @@ def set_pin_mode_dht(self, pin_number, sensor_type=22, differential=.1, callback callback: returns a data list: - [pin_type, pin_number, DHT type, humidity value, temperature raw_time_stamp] + [pin_type, pin_number, DHT type, validation flag, humidity value, temperature + raw_time_stamp] The pin_type for DHT input pins = 15 - ERROR CODES: If either humidity or temperature value: - == -1 Configuration Error - == -2 Checksum Error - == -3 Timeout Error + Validation Flag Values: + + No Errors = 0 + + Checksum Error = 1 + + Timeout Error = 2 + + Invalid Value = 999 + """ # if the pin is not currently associated with a DHT device @@ -1463,17 +1469,9 @@ def _dht_read_response(self, data): """ Process the dht response message. - Values are calculated using: - humidity = (_bits[0] * 256 + _bits[1]) * 0.1 - - temperature = ((_bits[2] & 0x7F) * 256 + _bits[3]) * 0.1 - - error codes: - 0 - OK - 1 - DHTLIB_ERROR_TIMEOUT - 2 - Checksum error - :param: data - array of 9 7bit bytes ending with the error status + :param: data: [pin, dht_type, validation_flag, humidity_positivity_flag, + temperature_positivity_flag, humidity, temperature] """ # get the time of the report time_stamp = time.time() @@ -1485,49 +1483,19 @@ def _dht_read_response(self, data): reply_data.append(pin) dht_type = data[1] reply_data.append(dht_type) - humidity = None - temperature = None - self.digital_pins[pin].event_time = time_stamp + humidity = temperature = 0 - if data[7] == 1: # data[9] is config flag - if data[10] != 0: - self.dht_sensor_error = True - humidity = temperature = -1 - # return - else: - # if data read correctly process and return - - if data[6] == 0: - # dht 22 - if data[1] == 22: - humidity = (data[2] * 256 + data[3]) * 0.1 - temperature = ((data[4] & 0x7F) * 256 + data[5]) * 0.1 - # dht 11 - elif data[1] == 11: - humidity = (data[2]) + (data[3]) * 0.1 - temperature = (data[4]) + (data[5]) * 0.1 - else: - raise RuntimeError(f'Unknown DHT Sensor type reported: {data[2]}') - - humidity = round(humidity, 2) - temperature = round(temperature, 2) - - # check for negative temperature - if data[6] & 0x80: - temperature = -temperature - - elif data[7] == 1: - # Checksum Error - humidity = temperature = -2 - self.dht_sensor_error = True - elif data[7] == 2: - # Timeout Error - humidity = temperature = -3 - self.dht_sensor_error = True - # since we initialize - if humidity is None: - return + if data[2] == 0: # all is well + humidity = float(data[5] + data[6] / 100) + if data[3]: + humidity *= -1.0 + temperature = float(data[7] + data[8] / 100) + if data[4]: + temperature *= -1.0 + + self.digital_pins[pin].event_time = time_stamp + reply_data.append(data[2]) reply_data.append(humidity) reply_data.append(temperature) reply_data.append(time_stamp) diff --git a/setup.py b/setup.py index fee498e..b8bc332 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ packages=['pymata4'], install_requires=['pyserial'], - version='1.10', + version='1.11', description="A Python Protocol Abstraction Library For Arduino Firmata", long_description=long_description, long_description_content_type='text/markdown',