-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathumidade.c
100 lines (81 loc) · 2.19 KB
/
umidade.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// Created by davi on 11/7/17.
//
#include "umidade.h"
#include "mqtt.h"
#include "debug.h"
#include "config.h"
#include <stdlib.h>
#include <stdint.h>
int data[5] = {0, 0, 0, 0, 0};
double read_dht_data() {
int lastState = HIGH;
uint8_t counter = 0;
uint8_t j = 0, i;
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
TRACE("Configurando sensor de umidade\n");
/* pull pin down for 18 milliseconds */
pinMode(DHT_PIN, OUTPUT);
digitalWrite(DHT_PIN, LOW);
delay(18);
/* prepare to read the pin */
pinMode(DHT_PIN, INPUT);
/* detect change and read data */
for (i = 0; i < MAX_TIMINGS; i++) {
counter = 0;
while (digitalRead(DHT_PIN) == lastState) {
counter++;
delayMicroseconds(1);
if (counter == 255) {
break;
}
}
lastState = digitalRead(DHT_PIN);
if (counter == 255)
break;
/* ignore first 3 transitions */
if ((i >= 4) && (i % 2 == 0)) {
/* shove each bit into the storage bytes */
data[j / 8] <<= 1;
if (counter > 16)
data[j / 8] |= 1;
j++;
}
}
/*
* check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
* print it out if data is good
*/
if ((j >= 40) &&
(data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) {
float h = (float) ((data[0] << 8) + data[1]) / 10;
if (h > 100) {
h = data[0]; // for DHT11
}
return h;
}
return 0;
}
void *thread_dht(void *pVoid) {
double h;
sensor_umidade = 1;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
while (1) {
h = read_dht_data();
if (h) {
mqtt_pub_double("umidade", h);
printf("Umidade: %.1f %%\n", h);
}
delay(INTERVALO_UMIDADE);
}
#pragma clang diagnostic pop
}
void *umidade(void *pVoid) {
pthread_t t1 = 0;
printf("Criando thread de umidade\n");
pthread_create(&t1, NULL, thread_dht, NULL);
printf("Iniciando thread de umidade\n");
pthread_join(t1, NULL);
return NULL;
}