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
pymata4.pymata4
pymata4.pymata4
pymata4.pymata4
pymata4.pymata4
pymata4.pymata4
pymata4.pymata4
pymata4.pymata4
pymata4.pymata4
pymata4.pymata4
pymata4.pymata4
pymata4.pymata4
ERROR CODES: If either humidity or temperature value:
- == -1 Configuration Error
- == -2 Checksum Error
- == -3 Timeout Error
-
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
ERROR CODES: If either humidity or temperature value:
- == -1 Configuration Error
- == -2 Checksum Error
- == -3 Timeout Error
-
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
Pym