Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TP lora COOC
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Monitor
Incidents
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LANGLAIS Charlotte
TP lora COOC
Commits
f5705842
Commit
f5705842
authored
3 years ago
by
LANGLAIS Charlotte
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
2ffc23d9
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
DHT11/lib/dht.py
+80
-0
80 additions, 0 deletions
DHT11/lib/dht.py
with
80 additions
and
0 deletions
DHT11/lib/dht.py
0 → 100644
+
80
−
0
View file @
f5705842
import
time
import
pycom
from
machine
import
enable_irq
,
disable_irq
,
Pin
#Classe appelée pour le retour des données
class
DTHResult
:
ERR_NO_ERROR
=
0
ERR_MISSING_DATA
=
1
ERR_CRC
=
2
error_code
=
ERR_NO_ERROR
temperature
=
-
1
humidity
=
-
1
def
__init__
(
self
,
error_code
,
temperature
,
humidity
):
self
.
error_code
=
error_code
self
.
temperature
=
temperature
self
.
humidity
=
humidity
def
is_valid
(
self
):
return
self
.
error_code
==
DTHResult
.
ERR_NO_ERROR
#Classe de calcul des valeurs du capteur
class
DTH
:
def
__init__
(
self
,
pin
):
self
.
__pin
=
Pin
(
pin
,
mode
=
Pin
.
OPEN_DRAIN
)
self
.
__pin
(
1
)
time
.
sleep
(
1.0
)
def
read
(
self
):
self
.
__send_and_sleep
(
0
,
0.019
)
data
=
pycom
.
pulses_get
(
self
.
__pin
,
100
)
self
.
__pin
.
init
(
Pin
.
OPEN_DRAIN
)
self
.
__pin
(
1
)
bits
=
[]
for
a
,
b
in
data
:
if
a
==
1
and
18
<=
b
<=
28
:
bits
.
append
(
0
)
if
a
==
1
and
65
<=
b
<=
75
:
bits
.
append
(
1
)
if
len
(
bits
)
!=
40
:
return
DTHResult
(
DTHResult
.
ERR_MISSING_DATA
,
0
,
0
)
# Calcul des bytes
the_bytes
=
self
.
__bits_to_bytes
(
bits
)
# calcul checksum et test
checksum
=
self
.
__calculate_checksum
(
the_bytes
)
if
the_bytes
[
4
]
!=
checksum
:
return
DTHResult
(
DTHResult
.
ERR_CRC
,
0
,
0
)
# Retour des données
[
int_rh
,
dec_rh
,
int_t
,
dec_t
,
csum
]
=
the_bytes
rh
=
int_rh
#dht11 20% ~ 90%
t
=
int_t
#dht11 0..50°C
return
DTHResult
(
DTHResult
.
ERR_NO_ERROR
,
t
,
rh
)
def
__send_and_sleep
(
self
,
output
,
mysleep
):
self
.
__pin
(
output
)
time
.
sleep
(
mysleep
)
def
__bits_to_bytes
(
self
,
bits
):
the_bytes
=
[]
byte
=
0
for
i
in
range
(
0
,
len
(
bits
)):
byte
=
byte
<<
1
if
(
bits
[
i
]):
byte
=
byte
|
1
else
:
byte
=
byte
|
0
if
((
i
+
1
)
%
8
==
0
):
the_bytes
.
append
(
byte
)
byte
=
0
return
the_bytes
def
__calculate_checksum
(
self
,
the_bytes
):
return
the_bytes
[
0
]
+
the_bytes
[
1
]
+
the_bytes
[
2
]
+
the_bytes
[
3
]
&
255
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment