thermferm/one-wire.c

changeset 728
da038d0bed04
parent 724
01e3936f62d4
child 731
8b7c63bddf75
equal deleted inserted replaced
727:78744b7e6021 728:da038d0bed04
60 payload = xstrcat(payload, (char *)"\",\"present\":\""); 60 payload = xstrcat(payload, (char *)"\",\"present\":\"");
61 payload = xstrcat(payload, (char *)DEVPRESENT[dev_w1->present]); 61 payload = xstrcat(payload, (char *)DEVPRESENT[dev_w1->present]);
62 payload = xstrcat(payload, (char *)"\",\"value\":"); 62 payload = xstrcat(payload, (char *)"\",\"value\":");
63 snprintf(vbuf, 63, "%d", dev_w1->value); 63 snprintf(vbuf, 63, "%d", dev_w1->value);
64 payload = xstrcat(payload, vbuf); 64 payload = xstrcat(payload, vbuf);
65 payload = xstrcat(payload, (char *)",\"resolution\":");
66 snprintf(vbuf, 63, "%d", dev_w1->resolution);
67 payload = xstrcat(payload, vbuf);
65 payload = xstrcat(payload, (char *)",\"timestamp\":"); 68 payload = xstrcat(payload, (char *)",\"timestamp\":");
66 snprintf(vbuf, 63, "%ld", (long)dev_w1->timestamp); 69 snprintf(vbuf, 63, "%ld", (long)dev_w1->timestamp);
67 payload = xstrcat(payload, vbuf); 70 payload = xstrcat(payload, vbuf);
68 payload = xstrcat(payload, (char *)"}"); 71 payload = xstrcat(payload, (char *)"}");
69 72
178 found = TRUE; 181 found = TRUE;
179 if (dev_w1->present != DEVPRESENT_YES) { 182 if (dev_w1->present != DEVPRESENT_YES) {
180 syslog(LOG_NOTICE, "One-wire device %s is back", buffer); 183 syslog(LOG_NOTICE, "One-wire device %s is back", buffer);
181 pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]); 184 pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]);
182 dev_w1->present = DEVPRESENT_YES; 185 dev_w1->present = DEVPRESENT_YES;
186 dev_w1->resolution = 0; /* Might be wrong, so reset */
183 dev_w1->timestamp = time(NULL); 187 dev_w1->timestamp = time(NULL);
184 pthread_mutex_unlock(&mutexes[LOCK_ONE_WIRE]); 188 pthread_mutex_unlock(&mutexes[LOCK_ONE_WIRE]);
185 changed = true; 189 changed = true;
186 device_present(dev_w1->address, DEVPRESENT_YES); 190 device_present(dev_w1->address, DEVPRESENT_YES);
187 } 191 }
195 n_w1->address = xstrcpy(buffer); 199 n_w1->address = xstrcpy(buffer);
196 strncpy(n_w1->family, buffer, 2); 200 strncpy(n_w1->family, buffer, 2);
197 n_w1->family[2] = '\0'; 201 n_w1->family[2] = '\0';
198 n_w1->present = DEVPRESENT_YES; 202 n_w1->present = DEVPRESENT_YES;
199 n_w1->value = (strcmp(w1type, (char *)"3a") == 0) ? 3:-1; 203 n_w1->value = (strcmp(w1type, (char *)"3a") == 0) ? 3:-1;
204 n_w1->resolution = 0;
200 n_w1->timestamp = time(NULL); 205 n_w1->timestamp = time(NULL);
201 changed = true; 206 changed = true;
202 207
203 pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]); 208 pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]);
204 if (w1_devices == NULL) { 209 if (w1_devices == NULL) {
248 } 253 }
249 free(devfile); 254 free(devfile);
250 devfile = NULL; 255 devfile = NULL;
251 } 256 }
252 257
258 mDelay(40);
253 SM_PROCEED(Read2413); 259 SM_PROCEED(Read2413);
254 260
255 SM_STATE(Read2413) 261 SM_STATE(Read2413)
256 262
257 if (my_one_wire_shutdown) { 263 if (my_one_wire_shutdown) {
336 } /* for (device = Config.devices; ... */ 342 } /* for (device = Config.devices; ... */
337 } 343 }
338 } 344 }
339 } 345 }
340 346
347 mDelay(40);
341 SM_PROCEED(ReadTemp); 348 SM_PROCEED(ReadTemp);
342 349
343 SM_STATE(ReadTemp) 350 SM_STATE(ReadTemp)
344 351
345 if (my_one_wire_shutdown) { 352 if (my_one_wire_shutdown) {
350 * cur_w1 points to the next not handled device. 357 * cur_w1 points to the next not handled device.
351 */ 358 */
352 if (cur_w1 != NULL) { 359 if (cur_w1 != NULL) {
353 360
354 if (((strcmp(cur_w1->family, (char *)"10") == 0) || (strcmp(cur_w1->family, (char *)"22") == 0) || (strcmp(cur_w1->family, (char *)"28") == 0) || 361 if (((strcmp(cur_w1->family, (char *)"10") == 0) || (strcmp(cur_w1->family, (char *)"22") == 0) || (strcmp(cur_w1->family, (char *)"28") == 0) ||
355 (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) && 362 (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) && (cur_w1->present == DEVPRESENT_YES)) {
356 (cur_w1->present == DEVPRESENT_YES)) { 363
364 /*
365 * Check and correct resolution. Only do this for new sensors
366 * or known sensors that are plugged in again.
367 */
368 if (cur_w1->resolution != W1_TEMP_RESOLUTION) {
369 devfile = xstrcpy((char *)"/sys/bus/w1/devices/");
370 devfile = xstrcat(devfile, cur_w1->address);
371 devfile = xstrcat(devfile, (char *)"/resolution");
372 if ((fp = fopen(devfile, "r+"))) {
373 if ((fgets(buffer, 25, fp))) {
374 sscanf(buffer, "%d", &value);
375 /* If device is removed, value is negative (errno?) */
376 if ((value > 0) && (value != W1_TEMP_RESOLUTION)) {
377 syslog(LOG_NOTICE, "One-wire device %s set resolution from %d to %d", cur_w1->address, value, W1_TEMP_RESOLUTION);
378 fseek(fp, 0L, SEEK_SET);
379 sprintf(buffer, "%d", W1_TEMP_RESOLUTION);
380 fputs(buffer, fp);
381 }
382 cur_w1->resolution = W1_TEMP_RESOLUTION;
383 }
384 fclose(fp);
385 } else {
386 syslog(LOG_NOTICE, "One-wire device %s open: %s", cur_w1->address, strerror(errno));
387 }
388 free(devfile);
389 }
390
391 /*
392 * Set conversion wait time. Skip this??
393 */
394 conv_time = 760;
357 devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); 395 devfile = xstrcpy((char *)"/sys/bus/w1/devices/");
358 devfile = xstrcat(devfile, cur_w1->address); 396 devfile = xstrcat(devfile, cur_w1->address);
359 devfile = xstrcat(devfile, (char *)"/resolution"); 397 devfile = xstrcat(devfile, (char *)"/conv_time");
360 if ((fp = fopen(devfile, "r+"))) { 398 if ((fp = fopen(devfile, "r"))) {
361 if ((fgets(buffer, 25, fp))) { 399 if ((fgets(buffer, 25, fp))) {
362 sscanf(buffer, "%d", &value); 400 sscanf(buffer, "%d", &conv_time);
363 /* If device is removed, value is negative (errno?) */ 401 }
364 if ((value > 0) && (value != W1_TEMP_RESOLUTION)) { 402 fclose(fp);
365 syslog(LOG_NOTICE, "One-wire device %s set resolution from %d to %d", cur_w1->address, value, W1_TEMP_RESOLUTION); 403 }
366 fseek(fp, 0L, SEEK_SET); 404 free(devfile);
367 sprintf(buffer, "%d", W1_TEMP_RESOLUTION); 405
368 fputs(buffer, fp); 406 /*
369 } 407 * Read temperature for one sensor.
370 } 408 * Bulk read does not work for unknown reason.
371 fclose(fp); 409 * It may be because there are also DS2413 devices.
372 free(devfile); 410 */
373 411 devfile = xstrcpy((char *)"/sys/bus/w1/devices/");
374 conv_time = 760; 412 devfile = xstrcat(devfile, cur_w1->address);
375 devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); 413 devfile = xstrcat(devfile, (char *)"/temperature");
376 devfile = xstrcat(devfile, cur_w1->address); 414 if ((fp = fopen(devfile, "r"))) {
377 devfile = xstrcat(devfile, (char *)"/conv_time"); 415 mDelay(conv_time);
378 if ((fp = fopen(devfile, "r"))) { 416 if ((fgets(buffer, 25, fp))) {
379 if ((fgets(buffer, 25, fp))) { 417 sscanf(buffer, "%d", &value);
380 sscanf(buffer, "%d", &conv_time); 418 if (cur_w1->value != value) {
419 cur_w1->timestamp = time(NULL);
420 changed = true;
421 if (debug)
422 syslog(LOG_NOTICE, "One-wire device %s temperature read %d => %d", cur_w1->address, cur_w1->value, value);
381 } 423 }
382 fclose(fp); 424 cur_w1->value = value; /* devices.c will pick this up */
383 } 425 } else {
384 free(devfile); 426 syslog(LOG_NOTICE, "One-wire device %s temperature read error", cur_w1->address);
385 427 }
386 devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); 428 fclose(fp);
387 devfile = xstrcat(devfile, cur_w1->address);
388 devfile = xstrcat(devfile, (char *)"/temperature");
389 if ((fp = fopen(devfile, "r"))) {
390 mDelay(conv_time);
391 if ((fgets(buffer, 25, fp))) {
392 sscanf(buffer, "%d", &value);
393 if (cur_w1->value != value) {
394 cur_w1->timestamp = time(NULL);
395 changed = true;
396 if (debug)
397 syslog(LOG_NOTICE, "One-wire device %s temperature read %d => %d", cur_w1->address, cur_w1->value, value);
398 }
399 cur_w1->value = value; /* devices.c will pick this up */
400 } else {
401 syslog(LOG_NOTICE, "One-wire device %s temperature read error", cur_w1->address);
402 }
403 fclose(fp);
404 }
405
406 } else { 429 } else {
407 syslog(LOG_NOTICE, "One-wire device %s open: %s", cur_w1->address, strerror(errno)); 430 syslog(LOG_NOTICE, "One-wire device %s open: %s", cur_w1->address, strerror(errno));
408 } 431 }
409 free(devfile); 432 free(devfile);
410 devfile = NULL; 433 devfile = NULL;

mercurial