33 |
33 |
34 |
34 |
35 |
35 |
36 int read_w1_28(char *address, int *val) |
36 int read_w1_28(char *address, int *val) |
37 { |
37 { |
38 char *device, line[60], *p = NULL; |
38 devices_list *device; |
39 FILE *fp; |
39 int tmp; |
40 int rc = 0; |
|
41 |
40 |
42 device = xstrcpy((char *)"/sys/bus/w1/devices/"); |
41 for (device = Config.devices; device; device = device->next) { |
43 device = xstrcat(device, address); |
42 if (strcmp(address, device->uuid) == 0) { |
44 device = xstrcat(device, (char *)"/w1_slave"); |
43 tmp = device->value; |
45 |
44 *val = tmp; |
46 /* |
45 return device->present; |
47 * Read sensor data |
|
48 */ |
|
49 if ((fp = fopen(device, "r"))) { |
|
50 /* |
|
51 * The output looks like: |
|
52 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES |
|
53 * 72 01 4b 46 7f ff 0e 10 57 t=23125 |
|
54 */ |
|
55 fgets(line, 50, fp); |
|
56 line[strlen(line)-1] = '\0'; |
|
57 if ((line[36] == 'Y') && (line[37] == 'E')) { |
|
58 /* |
|
59 * CRC is Ok, continue |
|
60 */ |
|
61 fgets(line, 50, fp); |
|
62 line[strlen(line)-1] = '\0'; |
|
63 strtok(line, (char *)"="); |
|
64 p = strtok(NULL, (char *)"="); |
|
65 rc = sscanf(p, "%d", val); |
|
66 if (rc != 1) { |
|
67 syslog(LOG_NOTICE, "sensor %s data parse error", address); |
|
68 rc = 3; |
|
69 } else { |
|
70 rc = 0; |
|
71 } |
|
72 } else { |
|
73 syslog(LOG_NOTICE, "sensor %s CRC error", address); |
|
74 rc = 1; |
|
75 } |
46 } |
76 fclose(fp); |
|
77 } else { |
|
78 syslog(LOG_NOTICE, "sensor %s is missing", address); |
|
79 rc = 2; |
|
80 } |
47 } |
81 |
48 |
82 free(device); |
49 return 1; |
83 device = NULL; |
|
84 |
|
85 return rc; |
|
86 } |
50 } |
87 |
51 |
88 |
52 |
89 |
53 |
90 #ifdef HAVE_WIRINGPI_H |
54 #ifdef HAVE_WIRINGPI_H |
137 if (my_shutdown) |
101 if (my_shutdown) |
138 break; |
102 break; |
139 |
103 |
140 if (unit->air_address) { |
104 if (unit->air_address) { |
141 rc = read_w1_28(unit->air_address, &temp); |
105 rc = read_w1_28(unit->air_address, &temp); |
142 if (rc == 0) { |
106 if (rc == 2) { |
143 /* |
107 /* |
144 * It is possible to have read errors or extreme values. |
108 * It is possible to have read errors or extreme values. |
145 * This can happen with bad connections so we compare the |
109 * This can happen with bad connections so we compare the |
146 * value with the previous one. If the difference is too |
110 * value with the previous one. If the difference is too |
147 * much, we don't send that value. That also means that if |
111 * much, we don't send that value. That also means that if |
157 syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->air_temperature, temp); |
121 syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->air_temperature, temp); |
158 if (debug) { |
122 if (debug) { |
159 fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->air_temperature, temp); |
123 fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->air_temperature, temp); |
160 } |
124 } |
161 } |
125 } |
162 } else if (rc == 2) { |
126 } else if (rc == 3) { |
163 unit->air_state = 1; |
127 unit->air_state = 1; |
164 } else { |
128 } else { |
165 unit->air_state = 2; |
129 unit->air_state = 2; |
166 } |
130 } |
167 } |
131 } |
168 if (my_shutdown) |
132 if (my_shutdown) |
169 break; |
133 break; |
170 |
134 |
171 if (unit->beer_address) { |
135 if (unit->beer_address) { |
172 rc = read_w1_28(unit->beer_address, &temp); |
136 rc = read_w1_28(unit->beer_address, &temp); |
173 if (rc == 0) { |
137 if (rc == 2) { |
174 deviation = 20000; |
138 deviation = 20000; |
175 if ((unit->beer_temperature == 0) || |
139 if ((unit->beer_temperature == 0) || |
176 (unit->beer_temperature && (temp > (int)unit->beer_temperature - deviation) && (temp < ((int)unit->beer_temperature + deviation)))) { |
140 (unit->beer_temperature && (temp > (int)unit->beer_temperature - deviation) && (temp < ((int)unit->beer_temperature + deviation)))) { |
177 unit->beer_temperature = temp; |
141 unit->beer_temperature = temp; |
178 unit->beer_state = 0; |
142 unit->beer_state = 0; |
180 syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->beer_temperature, temp); |
144 syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->beer_temperature, temp); |
181 if (debug) { |
145 if (debug) { |
182 fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->beer_temperature, temp); |
146 fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->beer_temperature, temp); |
183 } |
147 } |
184 } |
148 } |
185 } else if (rc == 2) { |
149 } else if (rc == 3) { |
186 unit->beer_state = 1; |
150 unit->beer_state = 1; |
187 } else { |
151 } else { |
188 unit->beer_state = 2; |
152 unit->beer_state = 2; |
189 } |
153 } |
190 } |
154 } |