summaryrefslogtreecommitdiffstatshomepage
path: root/esp8266/scripts
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-05-26 17:13:03 +0100
committerDamien George <damien.p.george@gmail.com>2016-05-26 17:13:03 +0100
commit7ebfe09fbd5e8ce00ae811aea1f2d84dfc73a577 (patch)
treea385c61c078c70b1ab58395cfaa347a86b3978d9 /esp8266/scripts
parent45f3416816fd5d717887e3efdde4d3ce3ef2ff38 (diff)
downloadmicropython-7ebfe09fbd5e8ce00ae811aea1f2d84dfc73a577.tar.gz
micropython-7ebfe09fbd5e8ce00ae811aea1f2d84dfc73a577.zip
esp8266: Add dht.py script for high-level control of DHT11/DHT22 sensor.
TODO: should go in a more port-neutral place, like drivers/dht, but at the moment in relies on specific esp module.
Diffstat (limited to 'esp8266/scripts')
-rw-r--r--esp8266/scripts/dht.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/esp8266/scripts/dht.py b/esp8266/scripts/dht.py
new file mode 100644
index 0000000000..9a69e7e07e
--- /dev/null
+++ b/esp8266/scripts/dht.py
@@ -0,0 +1,32 @@
+# DHT11/DHT22 driver for MicroPython on ESP8266
+# MIT license; Copyright (c) 2016 Damien P. George
+
+import esp
+
+class DHTBase:
+ def __init__(self, pin):
+ self.pin = pin
+ self.buf = bytearray(5)
+
+ def measure(self):
+ buf = self.buf
+ esp.dht_readinto(self.pin, buf)
+ if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xff != buf[4]:
+ raise Exception("checksum error")
+
+class DHT11(DHTBase):
+ def humidity(self):
+ return self.buf[0]
+
+ def temperature(self):
+ return self.buf[2]
+
+class DHT22(DHTBase):
+ def humidity(self):
+ return (self.buf[0] << 8 | self.buf[1]) * 0.1
+
+ def temperature(self):
+ t = ((self.buf[2] & 0x7f) << 8 | self.buf[3]) * 0.1
+ if self.buf[2] & 0x80:
+ t = -t
+ return t