Skip to content

Update TempHumidity.py #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 14 additions & 152 deletions Processors/PyBoard/Examples/Grove/TempHumidity Class.py
Original file line number Diff line number Diff line change
@@ -1,163 +1,25 @@
'''
http://wiki.seeedstudio.com/Grove-TemperatureAndHumidity_Sensor/
http://wiki.seeedstudio.com/Grove-Temperature_and_Humidity_Sensor_Pro/

connections:
Ground (B) - GND
Power (R) - 3V3
Ground - GND
Power - 3V3
NC
Signal - pin Y10
'''

def set_max_priority(): pass
def set_default_priority(): pass
from utime import sleep

PULSES_CNT = 41
import dht
import machine, utime
#DHT library from website
d=dht.DHT11(machine.Pin('Y10'))

class DHT(object):
DHT_TYPE = {
'DHT11': '11',
'DHT22': '22'
}
while True:
d.measure()
print("Temp:")
print(d.temperature())
print("Humidity:")
print(d.humidity())
utime.sleep_ms(500)

MAX_CNT = 320

def __init__(self, dht_type, pin):
self.pin = pin
if dht_type != self.DHT_TYPE['DHT11'] and dht_type != self.DHT_TYPE['DHT22']:
print('ERROR: Please use 11|22 as dht type.')
exit(1)
self._dht_type = '11'
self.dht_type = dht_type
GPIO.setup(self.pin, GPIO.OUT)

@property
def dht_type(self):
return self._dht_type

@dht_type.setter
def dht_type(self, type):
self._dht_type = type
self._last_temp = 0.0
self._last_humi = 0.0

def _read(self):
# Send Falling signal to trigger sensor output data
# Wait for 20ms to collect 42 bytes data
GPIO.setup(self.pin, GPIO.OUT)
set_max_priority()

GPIO.output(self.pin, 1)
sleep(.2)

GPIO.output(self.pin, 0)
sleep(.018)

GPIO.setup(self.pin, GPIO.IN)
# a short delay needed
for i in range(10):
pass

# pullup by host 20-40 us
count = 0
while GPIO.input(self.pin):
count += 1
if count > self.MAX_CNT:
# print("pullup by host 20-40us failed")
set_default_priority()
return None, "pullup by host 20-40us failed"

pulse_cnt = [0] * (2 * PULSES_CNT)
fix_crc = False
for i in range(0, PULSES_CNT * 2, 2):
while not GPIO.input(self.pin):
pulse_cnt[i] += 1
if pulse_cnt[i] > self.MAX_CNT:
# print("pulldown by DHT timeout %d" % i)
set_default_priority()
return None, "pulldown by DHT timeout %d" % i

while GPIO.input(self.pin):
pulse_cnt[i + 1] += 1
if pulse_cnt[i + 1] > self.MAX_CNT:
# print("pullup by DHT timeout %d" % (i + 1))
if i == (PULSES_CNT - 1) * 2:
# fix_crc = True
# break
pass
set_default_priority()
return None, "pullup by DHT timeout %d" % i

# back to normal priority
set_default_priority()

total_cnt = 0
for i in range(2, 2 * PULSES_CNT, 2):
total_cnt += pulse_cnt[i]

# Low level ( 50 us) average counter
average_cnt = total_cnt / (PULSES_CNT - 1)
# print("low level average loop = %d" % average_cnt)

data = ''
for i in range(3, 2 * PULSES_CNT, 2):
if pulse_cnt[i] > average_cnt:
data += '1'
else:
data += '0'

data0 = int(data[ 0: 8], 2)
data1 = int(data[ 8:16], 2)
data2 = int(data[16:24], 2)
data3 = int(data[24:32], 2)
data4 = int(data[32:40], 2)

if fix_crc and data4 != ((data0 + data1 + data2 + data3) & 0xFF):
data4 = data4 ^ 0x01
data = data[0: PULSES_CNT - 2] + ('1' if data4 & 0x01 else '0')

if data4 == ((data0 + data1 + data2 + data3) & 0xFF):
if self._dht_type == self.DHT_TYPE['DHT11']:
humi = int(data0)
temp = int(data2)
elif self._dht_type == self.DHT_TYPE['DHT22']:
humi = float(int(data[ 0:16], 2)*0.1)
temp = float(int(data[17:32], 2)*0.2*(0.5-int(data[16], 2)))
else:
# print("checksum error!")
return None, "checksum error!"

return humi, temp

def read(self, retries = 15):
for i in range(retries):
humi, temp = self._read()
if not humi is None:
break
if humi is None:
return self._last_humi, self._last_temp
self._last_humi,self._last_temp = humi, temp
return humi, temp

Grove = DHT


def main():
import sys
import time

if len(sys.argv) < 3:
print('Usage: {} dht_type pin'.format(sys.argv[0]))
sys.exit(1)

typ = sys.argv[1]
sensor = DHT(typ, int(sys.argv[2]))

while True:
humi, temp = sensor.read()
if not humi is None:
print('DHT{0}, humidity {1:.1f}%, temperature {2:.1f}*'.format(sensor.dht_type, humi, temp))
else:
print('DHT{0}, humidity & temperature: {1}'.format(sensor.dht_type, temp))
time.sleep(1)