21 *****************************************************************************/ |
21 *****************************************************************************/ |
22 |
22 |
23 #include "thermferm.h" |
23 #include "thermferm.h" |
24 #include "devices.h" |
24 #include "devices.h" |
25 #include "rc-switch.h" |
25 #include "rc-switch.h" |
|
26 #include "panel.h" |
26 #include "xutil.h" |
27 #include "xutil.h" |
27 |
28 |
28 |
29 |
29 extern int debug; |
30 extern int debug; |
30 extern sys_config Config; |
31 extern sys_config Config; |
31 extern int my_shutdown; |
32 extern int my_shutdown; |
32 |
33 |
|
34 |
|
35 #ifdef HAVE_WIRINGPI_H |
|
36 |
|
37 #define MAXTIMINGS 100 |
|
38 |
|
39 int dht11_pin = -1; |
|
40 int dht11_temperature = -1; |
|
41 int dht11_humidity = -1; |
|
42 int dht11_valid = FALSE; |
|
43 int dht11_t_offset = 0; |
|
44 int dht11_h_offset = 0; |
|
45 |
|
46 |
|
47 static uint8_t sizecvt(const int read_value) { |
|
48 /* |
|
49 * digitalRead() and friends from wiringpi are defined as returning a value |
|
50 * < 256. However, they are returned as int() types. This is a safety function |
|
51 */ |
|
52 if (read_value > 255 || read_value < 0) { |
|
53 syslog(LOG_NOTICE, "invalid data from wiringPi library"); |
|
54 } |
|
55 |
|
56 return (uint8_t)read_value; |
|
57 } |
|
58 |
|
59 |
|
60 |
|
61 /* |
|
62 * DHT11 sensor read. This should be used in a thread loop. |
|
63 */ |
|
64 void dht11Read(void) { |
|
65 int tries = 5; |
|
66 unsigned short got_correct_data = 0; |
|
67 |
|
68 if (dht11_pin == -1) |
|
69 return; |
|
70 |
|
71 while (tries && !got_correct_data) { |
|
72 uint8_t laststate = HIGH; |
|
73 uint8_t counter = 0; |
|
74 uint8_t j = 0, i = 0; |
|
75 int dht11_dat[5] = {0,0,0,0,0}; |
|
76 |
|
77 /* |
|
78 * Select output mode to send the start signal. |
|
79 */ |
|
80 pinMode(dht11_pin, OUTPUT); |
|
81 digitalWrite(dht11_pin, HIGH); |
|
82 usleep(1000); |
|
83 |
|
84 /* |
|
85 * Low for at least 18 milliseconds |
|
86 */ |
|
87 digitalWrite(dht11_pin, LOW); |
|
88 usleep(20000); |
|
89 digitalWrite(dht11_pin, HIGH); |
|
90 pinMode(dht11_pin, INPUT); |
|
91 |
|
92 /* |
|
93 * Detect change and read data |
|
94 */ |
|
95 for (i=0; i<MAXTIMINGS; i++) { |
|
96 counter = 0; |
|
97 delayMicroseconds(10); |
|
98 while (sizecvt(digitalRead(dht11_pin)) == laststate) { |
|
99 counter++; |
|
100 delayMicroseconds(1); |
|
101 if (counter == 255) { |
|
102 break; |
|
103 } |
|
104 } |
|
105 laststate = sizecvt(digitalRead(dht11_pin)); |
|
106 |
|
107 if (counter == 255) |
|
108 break; |
|
109 |
|
110 /* |
|
111 * ignore first 3 transitions |
|
112 */ |
|
113 if ((i >= 4) && (i%2 == 0)) { |
|
114 |
|
115 // shove each bit into the storage bytes |
|
116 dht11_dat[(int)((double)j/8)] <<= 1; |
|
117 if (counter > 16) |
|
118 dht11_dat[(int)((double)j/8)] |= 1; |
|
119 j++; |
|
120 } |
|
121 } |
|
122 |
|
123 /* |
|
124 * If there is no sensor, j = 0 |
|
125 */ |
|
126 if ((counter == 255) && (j == 0)) { |
|
127 if (dht11_temperature != -1) { |
|
128 syslog(LOG_NOTICE, "dht11 sensor disappeared"); |
|
129 } else { |
|
130 syslog(LOG_NOTICE, "dht11 sensor not present"); |
|
131 } |
|
132 dht11_temperature = -1; |
|
133 dht11_humidity = -1; |
|
134 dht11_valid = FALSE; |
|
135 return; |
|
136 } |
|
137 |
|
138 /* |
|
139 * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte |
|
140 * print it out if data is good |
|
141 */ |
|
142 if ((j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF))) { |
|
143 got_correct_data = 1; |
|
144 |
|
145 int h = dht11_dat[0] + dht11_dat[1]; |
|
146 int t = (dht11_dat[2] & 0x7F) + dht11_dat[3]; |
|
147 t += dht11_t_offset; |
|
148 h += dht11_h_offset; |
|
149 |
|
150 if ((dht11_dat[2] & 0x80) != 0) |
|
151 t *= -1; |
|
152 |
|
153 dht11_temperature = t; |
|
154 dht11_humidity = h; |
|
155 dht11_valid = TRUE; |
|
156 } else { |
|
157 tries--; |
|
158 if (tries == 0) |
|
159 syslog(LOG_INFO, "dht11 data checksum was wrong 5 times"); |
|
160 usleep(100000); |
|
161 } |
|
162 } |
|
163 } |
|
164 |
|
165 #endif |
33 |
166 |
34 |
167 |
35 |
168 |
36 int device_out(char *uuid, int value) |
169 int device_out(char *uuid, int value) |
37 { |
170 { |
218 snprintf(buf, 39, "Raspberry GPIO %d", i); |
351 snprintf(buf, 39, "Raspberry GPIO %d", i); |
219 ndev->description = xstrcpy(buf); |
352 ndev->description = xstrcpy(buf); |
220 ndev->subdevice = i; |
353 ndev->subdevice = i; |
221 ndev->gpiopin = pin; |
354 ndev->gpiopin = pin; |
222 ndev->timestamp = time(NULL); |
355 ndev->timestamp = time(NULL); |
223 if (i == 7) { |
356 if (i == PANEL_LED) { |
|
357 ndev->direction = DEVDIR_OUT_BIN; |
|
358 ndev->inuse = 1; |
|
359 ndev->comment = xstrcpy((char *)"Frontpanel LED"); |
|
360 } else if (i == PANEL_ENTER) { |
|
361 ndev->direction = DEVDIR_IN_BIN; |
|
362 ndev->inuse = 1; |
|
363 ndev->comment = xstrcpy((char *)"Frontpanel Enter key"); |
|
364 } else if (i == PANEL_DOWN) { |
|
365 ndev->direction = DEVDIR_IN_BIN; |
|
366 ndev->inuse = 1; |
|
367 ndev->comment = xstrcpy((char *)"Frontpanel Down key"); |
|
368 } else if (i == PANEL_UP) { |
|
369 ndev->direction = DEVDIR_IN_BIN; |
|
370 ndev->inuse = 1; |
|
371 ndev->comment = xstrcpy((char *)"Frontpanel Up key"); |
|
372 } else if (i == 7) { |
224 ndev->direction = DEVDIR_INTERN; |
373 ndev->direction = DEVDIR_INTERN; |
225 ndev->inuse = 1; |
374 ndev->inuse = 1; |
226 ndev->comment = xstrcpy((char *)"1-Wire bus"); |
375 ndev->comment = xstrcpy((char *)"1-Wire bus"); |
227 } else { |
376 } else { |
228 ndev->direction = DEVDIR_IN_BIN; |
377 ndev->direction = DEVDIR_IN_BIN; |