Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
Fix DHT11 support
Browse files Browse the repository at this point in the history
  • Loading branch information
MrYsLab committed May 22, 2020
1 parent f7e59c7 commit 73729af
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pymata4/private_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class PrivateConstants:
SYSEX_REALTIME = 0x7F # MIDI Reserved for realtime messages

# reserved for PyMata
PYMATA_EXPRESS_THREADED_VERSION = "1.4"
PYMATA_EXPRESS_THREADED_VERSION = "1.5"

# each byte represents a digital port
# and its value contains the current port settings
Expand Down
31 changes: 23 additions & 8 deletions pymata4/pymata4.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ def dht_read(self, pin):
"""
return self.digital_pins[pin].current_value[0], \
self.digital_pins[pin].current_value[1], \
self.digital_pins[pin].event_time
self.digital_pins[pin].current_value[1], \
self.digital_pins[pin].event_time

def digital_read(self, pin):
"""
Expand Down Expand Up @@ -1481,13 +1481,28 @@ def _dht_read_response(self, data):
# if data read correctly process and return

if data[10] == 0:
humidity = (((data[2] & 0x7f) + (data[3] << 7)) * 256 +
((data[4] & 0x7f) + (data[5] << 7))) * 0.1
temperature = (((data[6] & 0x7f) + (data[7] << 7) & 0x7F) * 256 +
((data[8] & 0x7f) + (data[9] << 7))) * 0.1
# dht 22
if data[1] == 22:
humidity = (((data[2] & 0x7f) + (data[3] << 7)) * 256 +
((data[4] & 0x7f) + (data[5] << 7))) * 0.1
temperature = (((data[6] & 0x7f) + (data[7] << 7) & 0x7F) * 256 +
((data[8] & 0x7f) + (data[9] << 7))) * 0.1
# dht 11
elif data[2] == 11:
humidity = (((data[2] & 0x7f) + (data[3] << 7)) +
((data[4] & 0x7f) + (data[5] << 7))) * 0.1
temperature = (((data[6] & 0x7f) + (data[7] << 7) & 0x7F) +
((data[8] & 0x7f) + (data[9] << 7))) * 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[8] == 1:
# Checksum Error
humidity = temperature = -2
Expand Down Expand Up @@ -1915,5 +1930,5 @@ def _tcp_receiver(self):
try:
payload = self.sock.recv(1)
self.the_deque.append(ord(payload))
except:
pass
except Exception:
pass
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
packages=['pymata4'],
install_requires=['pyserial'],

version='1.4',
version='1.5',
description="A Python Protocol Abstraction Library For Arduino Firmata",
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 73729af

Please sign in to comment.