207 * Loop forever until the external shutdown variable is set. |
207 * Loop forever until the external shutdown variable is set. |
208 */ |
208 */ |
209 for (;;) { |
209 for (;;) { |
210 |
210 |
211 /* |
211 /* |
212 * Here send our 1-wire sensors values |
212 * Process all devices. |
213 */ |
213 */ |
214 for (device = Config.devices; device; device = device->next) { |
214 for (device = Config.devices; device; device = device->next) { |
215 |
215 |
216 if (my_shutdown) |
216 if (my_shutdown) |
217 break; |
217 break; |
218 |
218 |
219 /* |
219 switch (device->type) { |
220 * Build path to the on-wire sensor |
220 case DEVTYPE_W1: |
221 */ |
221 if (strncmp(device->address, (char *)"28", 2) == 0) { |
222 // device = xstrcpy((char *)"/sys/bus/w1/devices/"); |
222 /* Read DS18B20 sensor */ |
223 // device = xstrcat(device, tmp1->master); |
223 addr = xstrcpy((char *)"/sys/bus/w1/devices/"); |
224 // device = xstrcat(device, (char *)"/"); |
224 addr = xstrcat(addr, device->address); |
225 // device = xstrcat(device, tmp1->name); |
225 addr = xstrcat(addr, (char *)"/w1_slave"); |
226 // device = xstrcat(device, (char *)"/w1_slave"); |
226 if ((fp = fopen(addr, "r"))) { |
227 |
227 if (device->present != DEVPRESENT_YES) { |
228 /* |
228 syslog(LOG_NOTICE, "sensor %s is back", device->address); |
229 * Read sensor data |
229 device->present = DEVPRESENT_YES; |
230 */ |
230 } |
231 // if ((fp = fopen(device, "r"))) { |
231 /* |
232 /* |
232 * The output looks like: |
233 * The output looks like: |
233 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES |
234 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES |
234 * 72 01 4b 46 7f ff 0e 10 57 t=23125 |
235 * 72 01 4b 46 7f ff 0e 10 57 t=23125 |
235 */ |
236 */ |
236 fgets(line, 50, fp); |
237 // fgets(line, 50, fp); |
237 line[strlen(line)-1] = '\0'; |
238 // line[strlen(line)-1] = '\0'; |
238 if ((line[36] == 'Y') && (line[37] == 'E')) { |
239 // if ((line[36] == 'Y') && (line[37] == 'E')) { |
239 /* CRC is Ok, continue */ |
240 /* |
240 fgets(line, 50, fp); |
241 * CRC is Ok, continue |
241 line[strlen(line)-1] = '\0'; |
242 */ |
242 strtok(line, (char *)"="); |
243 // fgets(line, 50, fp); |
243 p = strtok(NULL, (char *)"="); |
244 // line[strlen(line)-1] = '\0'; |
244 rc = sscanf(p, "%d", &temp); |
245 // strtok(line, (char *)"="); |
245 if ((rc == 1) && (device->value != temp)) { |
246 // p = strtok(NULL, (char *)"="); |
246 device->value = temp; |
247 // rc = sscanf(p, "%d", &temp); |
247 device->timestamp = time(NULL); |
248 // if ((rc == 1) && (tmp1->lastval != temp)) { |
248 } |
249 /* |
249 } else { |
250 * It is possible to have read errors or extreme values. |
250 syslog(LOG_NOTICE, "sensor %s CRC error", device->address); |
251 * This can happen with bad connections so we compare the |
251 device->present = DEVPRESENT_ERROR; |
252 * value with the previous one. If the difference is too |
252 } |
253 * much, we don't send that value. That also means that if |
253 } else { |
254 * the next value is ok again, it will be marked invalid too. |
254 if (device->present != DEVPRESENT_NO) { |
255 * Maximum error is 20 degrees for now. |
255 syslog(LOG_NOTICE, "sensor %s is missing", device->address); |
256 */ |
256 device->present = DEVPRESENT_NO; |
257 // deviation = 20000; |
257 } |
258 // if ( (tmp1->lastval == 0) || |
258 } |
259 // (tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation))) ) { |
259 free(addr); |
260 /* |
260 addr = NULL; |
261 * Temperature is changed and valid, set flag. |
261 } |
262 */ |
262 break; |
263 // tmp1->update = TRUE; |
263 default: |
264 // } else { |
264 break; |
265 // syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, tmp1->lastval, temp); |
265 } |
266 // if (debug) { |
|
267 // fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, tmp1->lastval, temp); |
|
268 // } |
|
269 // } |
|
270 // tmp1->lastval = temp; |
|
271 // } |
|
272 // } else { |
|
273 // syslog(LOG_NOTICE, "sensor %s/%s CRC error", tmp1->master, tmp1->name); |
|
274 // } |
|
275 // fclose(fp); |
|
276 // tmp1->present = 1; |
|
277 // } else { |
|
278 // tmp1->present = 0; |
|
279 // if (debug) |
|
280 // printf("sensor %s is missing\n", tmp1->name); |
|
281 // } |
|
282 |
|
283 // free(device); |
|
284 // device = NULL; |
|
285 } |
266 } |
286 usleep(10000); |
267 usleep(10000); |
287 } |
268 } |
288 |
269 |
289 syslog(LOG_NOTICE, "Thread my_devices_loop stopped"); |
270 syslog(LOG_NOTICE, "Thread my_devices_loop stopped"); |