Skip to content

Commit b6e0e21

Browse files
authored
Merge pull request #214 from PropGit/master
Added Jon McPhalen's DHTxx Temperature/Humidity sensor object.
2 parents 3b1fd6b + 91f081e commit b6e0e21

File tree

5 files changed

+23
-0
lines changed

5 files changed

+23
-0
lines changed

docs/p2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ by Dave Hein
211211
## Recent Activity
212212
##### _Full details [here](https://github.com/parallaxinc/propeller/pulls?q=is%3Aclosed)._
213213

214+
* Added Jon "JonnyMac" McPhalen's [DHTxx Temperature/Humidity](https://github.com/parallaxinc/propeller/tree/master/libraries/community/p2/All/jm_dhtxx) object
214215
* Added object from Greg LaPolla's
215216
* P1 [ADS1118 C](https://github.com/parallaxinc/propeller/tree/master/libraries/community/p1/All/ads1118/C)
216217
* P1 [ADS1118 Spin](https://github.com/parallaxinc/propeller/tree/master/libraries/community/p1/All/ads1118/Spin)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# DHTxx Temperature/Humidity Sensors
2+
3+
By: Jon "JonnyMac" McPhalen
4+
5+
Language: Spin2, PASM2
6+
7+
Created: 14-JAN-2021
8+
9+
Category: sensor
10+
11+
Description:
12+
This object allows the P2 to interface with DHT11 and DHT22/CM2302 sensors for projects requiring the monitoring of temperature and relative humidity. With either sensor type, temperature and humidity is returned in 0.1-degree and 0.1% units.
13+
14+
Code is compatible with Parallax PN 28059 (DHT22/CM2302).
15+
16+
License: MIT (see end of source code)

libraries/community/p2/All/jm_dhtxx/jm_dhtxx.spin2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'' ================================================================================================='''' File....... jm_dhtxx.spin2'' Purpose.... Interface for DHT11/DHT22 Temperature/Humidity Sensors'' Author..... Jon "JonnyMac" McPhalen'' Copyright (c) 2021 Jon McPhalen'' -- see below for terms of use'' E-mail..... jon.mcphalen@gmail.com'' Started....'' Updated.... 14 JAN 2021'''' ================================================================================================={ Wakes and reads data from DHT11/DHT22 or compatible temp/humidity sensor. Resources: -- http://robocraft.ru/files/datasheet/DHT11.pdf -- https://cdn-shop.adafruit.com/datasheets/Digital+humidity+and+temperature+sensor+AM2302.pdf}con { fixed io pins } RX1 = 63 { I } ' programming / debug TX1 = 62 { O } SF_CS = 61 { O } ' serial flash SF_SCK = 60 { O } SF_SDO = 59 { O } SF_SDI = 58 { I }con #true, ON, OFF #false, NO, YES CM2302 = 22 ' device types DHT22 = 22 DHT11 = 11 #0, PU_EXT, PU_INT ' pull-up modes #0, ERR_NONE, ERR_BUS, ERR_RESP, ERR_PKT, ERR_CRC ' error conditionsvar { globals } long dio ' sensor pin long smode ' sensor mode long us1tix ' ticks in 1us long last ' last access time long refresh ' min time between access long errorlevel ' 0 on good read long tempc ' latest readings long humidity byte dht[5] ' packet bytespub null()'' This is not a top-level objectpub start(pin, type, pullup)'' Assign pin to DHTxx sensor'' -- type is 11 or 22'' -- pullup is 0 for external, 1 for internal dio := pin ' save IO pin if (type == 22) ' get type smode := 22 ' set mode for conversions refresh := 2000 ' set minimum refresh timing else smode := 11 refresh := 1000 pinclear(dio) ' remove old settings if (pullup) ' set pull-up mode wrpin(dio, P_HIGH_1K5) else wrpin(dio, P_HIGH_FLOAT) pinh(dio) ' enable pull-up us1tix := (clkfreq / 1_000_000) ' clock ticks in 1us last := getms() - refreshpub read_tempc() : result'' Returns current temperature in 0.1C units if ((getms()-last) >= refresh) ' okay to read sensor new_read() return tempcpub read_tempf() : result'' Returns current temperature in 0.1F units return read_tempc() * 9 / 5 + 32_0pub read_humidity() : result'' Returns relative humidity in 0.1% units if ((getms()-last) >= refresh) new_read() return humiditypub error_level() : result ' for debugging return errorlevelpub p_packet() : result ' for debugging return @dhtpri new_read() : result'' Wake and read sensor last := getms() ' mark last access longfill(@tempc, 0, 2) ' clear results if (smode == 22) errorlevel := read_dht( 2_000, @dht) ' get new data else errorlevel := read_dht(19_000, @dht) if (errorlevel <> ERR_NONE) return' DHT22 mode if (smode == 22) ' convert packet bytes humidity := (dht[0] << 8) | dht[1] tempc := ((dht[2] & $7F) << 8) | dht[3] if (dht[2].[7]) ' check negative temp bit tempc := 0 - tempc return' DHT11 mode' -- convert to 0.1 units to match DHT22 humidity := dht[0] * 10 tempc := dht[2] * 10pri read_dht(wt, p_dat) : result | p, tix1, timeout, t0, t1, idx1, idx2, b, crc'' Wake DHTxx device and read packet'' -- wt is wake time in microseconds'' -- p_dat is pointer to 5-byte data array p, tix1 := dio, us1tix ' copy global vars org.setup rep #2, #5 wrbyte #0, p_dat add p_dat, #1 sub p_dat, #5 ' restore packet pointer qmul tix1, #90 ' calcuate timeout ticks getqx timeout.wake drvl p ' stimulous pulse rep #1, wt waitx tix1 drvh p.check_bus rep #1, #5 ' let bus rise waitx tix1 testp p wc ' verify high if_nc jmp #.bus_error.check_resp0 rep #1, #40 ' let device respond waitx tix1 testp p wc ' verify low if_c jmp #.resp_error.check_resp1 call #.get_pulse ' get response pulse tjz t1, #.resp_error ' abort on timeout qdiv t1, tix1 ' convert to usecs getqx t1 cmp t1, #65 wcz ' check range if_b jmp #.resp_error cmp t1, #95 wcz if_a jmp #.resp_error.get_packet mov idx1, #5 ' get 5 bytes mov crc, #0 ' clear crc.get_byte mov idx2, #8 ' 8 bits per byte.get_bit call #.get_pulse ' meaure pulse tjz t1, #.pkt_error ' check for timeout qdiv t1, tix1 ' convert pulse to usecs getqx t1 cmp t1, #50 wcz ' convert to bit shl b, #1 ' prep for new bit if_a or b, #$01 ' add to byte djnz idx2, #.get_bit ' next bit and b, #$FF ' clean up wrbyte b, p_dat ' write to hub add crc, b ' update crc add p_dat, #1 ' advance hub pointer djnz idx1, #.get_byte ' next byte.check_crc sub crc, b ' remove last byte and crc, #$FF ' isolate low byte cmp crc, b wcz ' check crc if_e mov result, #ERR_NONE ' good crc if_ne mov result, #ERR_CRC ' bad crc jmp #.done' allow up to 90us for rising edge' allow up to 90us for next falling edge.get_pulse getct t0 ' mark start of low period.p0_loop getct t1 ' mark time now subs t1, t0 ' t1 is elapsed ticks cmps t1, timeout wcz ' check for timeout if_a mov t1, #0 ' set to error if_a ret testp p wc ' no timeout, pin high? if_nc jmp #.p0_loop ' if not, try again.get_pulse1 getct t0.p1_loop getct t1 subs t1, t0 cmps t1, timeout wcz if_a mov t1, #0 if_a ret testp p wc if_c jmp #.p1_loop ret' error conditions.pkt_error mov result, #ERR_PKT jmp #.done.resp_error neg result, #ERR_RESP jmp #.done.bus_error neg result, #ERR_BUS.done ret endcon { license }{{ Terms of Use: MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:0aadf99b72d17d361dd319df3d7e6bcddd27732da9a5fa03f79de93ae88e7d56
3+
size 17964

libraries/community/p2/Sensor/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
[JM Click RTC](../All/jm_click_rtc)
88

9+
[JM DHTxx Temperature/Humidity](../All/jm_dhtxx)
10+
911
[JM DS3231 RTC](../All/jm_ds3231)
1012

1113
[JM EZ Analog](../All/jm_ez_analog)

0 commit comments

Comments
 (0)