|
1 /***************************************************************************** |
|
2 * Copyright (C) 2014 |
|
3 * |
|
4 * Michiel Broek <mbroek at mbse dot eu> |
|
5 * |
|
6 * This file is part of the mbsePi-apps |
|
7 * |
|
8 * This is free software; you can redistribute it and/or modify it |
|
9 * under the terms of the GNU General Public License as published by the |
|
10 * Free Software Foundation; either version 2, or (at your option) any |
|
11 * later version. |
|
12 * |
|
13 * mbsePi-apps is distributed in the hope that it will be useful, but |
|
14 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 * General Public License for more details. |
|
17 * |
|
18 * You should have received a copy of the GNU General Public License |
|
19 * along with EC-65K; see the file COPYING. If not, write to the Free |
|
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
|
21 *****************************************************************************/ |
|
22 |
|
23 #include "../lib/mbselib.h" |
|
24 #include "sensors.h" |
|
25 |
|
26 #ifdef HAVE_WIRINGPI_H |
|
27 |
|
28 |
|
29 extern char *myhostname; |
|
30 extern bool debug; |
|
31 extern sys_config Config; |
|
32 extern int lcdHandle; |
|
33 extern int lcdupdate; |
|
34 |
|
35 |
|
36 |
|
37 void my_sensors_loop(void) |
|
38 { |
|
39 w1_therm *tmp1, *old1; |
|
40 char *device, line[60], *p = NULL; |
|
41 FILE *fp; |
|
42 int temp, rc, deviation; |
|
43 |
|
44 /* |
|
45 * Here send our 1-wire sensors values |
|
46 */ |
|
47 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { |
|
48 old1 = tmp1->next; |
|
49 |
|
50 /* |
|
51 * Build path and alias topic |
|
52 */ |
|
53 device = xstrcpy((char *)"/sys/bus/w1/devices/"); |
|
54 device = xstrcat(device, tmp1->master); |
|
55 device = xstrcat(device, (char *)"/"); |
|
56 device = xstrcat(device, tmp1->name); |
|
57 device = xstrcat(device, (char *)"/w1_slave"); |
|
58 |
|
59 /* |
|
60 * Read sensor data |
|
61 */ |
|
62 if ((fp = fopen(device, "r"))) { |
|
63 /* |
|
64 * The output looks like: |
|
65 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES |
|
66 * 72 01 4b 46 7f ff 0e 10 57 t=23125 |
|
67 */ |
|
68 fgets(line, 50, fp); |
|
69 line[strlen(line)-1] = '\0'; |
|
70 if ((line[36] == 'Y') && (line[37] == 'E')) { |
|
71 /* |
|
72 * CRC is Ok, continue |
|
73 */ |
|
74 fgets(line, 50, fp); |
|
75 line[strlen(line)-1] = '\0'; |
|
76 strtok(line, (char *)"="); |
|
77 p = strtok(NULL, (char *)"="); |
|
78 rc = sscanf(p, "%d", &temp); |
|
79 if ((rc == 1) && (tmp1->lastval != temp)) { |
|
80 /* |
|
81 * It is possible to have read errors or extreme values. |
|
82 * This can happen with bad connections so we compare the |
|
83 * value with the previous one. If the difference is too |
|
84 * much, we don't send that value. That also means that if |
|
85 * the next value is ok again, it will be marked invalid too. |
|
86 * Maximum error is 20 degrees for now. |
|
87 */ |
|
88 deviation = 20000; |
|
89 if ((tmp1->lastval == 0) || |
|
90 (tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation)))) { |
|
91 /* |
|
92 * Temperature is changed and valid, update and publish this. |
|
93 */ |
|
94 tmp1->update = TRUE; |
|
95 } else { |
|
96 syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, tmp1->lastval, temp); |
|
97 if (debug) { |
|
98 fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, tmp1->lastval, temp); |
|
99 } |
|
100 } |
|
101 tmp1->lastval = temp; |
|
102 lcdupdate = TRUE; |
|
103 } |
|
104 } else { |
|
105 syslog(LOG_NOTICE, "sensor %s/%s CRC error", tmp1->master, tmp1->name); |
|
106 } |
|
107 fclose(fp); |
|
108 tmp1->present = 1; |
|
109 } else { |
|
110 tmp1->present = 0; |
|
111 if (debug) |
|
112 printf("sensor %s is missing\n", tmp1->name); |
|
113 } |
|
114 |
|
115 free(device); |
|
116 device = NULL; |
|
117 } |
|
118 } |
|
119 |
|
120 |
|
121 |
|
122 #endif |