coolers/sensors.c

changeset 27
4703cc10b99a
parent 26
9322c619c525
child 40
dafbbd5e9922
equal deleted inserted replaced
26:9322c619c525 27:4703cc10b99a
24 #include "sensors.h" 24 #include "sensors.h"
25 25
26 #ifdef HAVE_WIRINGPI_H 26 #ifdef HAVE_WIRINGPI_H
27 27
28 28
29 extern char *myhostname;
30 extern bool debug; 29 extern bool debug;
31 extern sys_config Config; 30 extern sys_config Config;
32 extern int lcdHandle; 31 extern int shutdown;
33 extern int lcdupdate;
34 32
35 33
36 34
37 void my_sensors_loop(void) 35 PI_THREAD (my_sensors_loop)
38 { 36 {
39 w1_therm *tmp1, *old1; 37 w1_therm *tmp1, *old1;
40 char *device, line[60], *p = NULL; 38 char *device, line[60], *p = NULL;
41 FILE *fp; 39 FILE *fp;
42 int temp, rc, deviation; 40 int temp, rc, deviation;
43 41
42 syslog(LOG_NOTICE, "Thread my_sensors_loop started");
43 if (debug)
44 fprintf(stdout, "Thread my_sensors_loop started\n");
45
44 /* 46 /*
45 * Here send our 1-wire sensors values 47 * Loop forever until the external shutdown variable is set.
46 */ 48 */
47 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { 49 for (;;) {
48 old1 = tmp1->next; 50 /*
51 * Here send our 1-wire sensors values
52 */
53 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
54 old1 = tmp1->next;
49 55
50 /* 56 if (shutdown) {
51 * Build path and alias topic 57 syslog(LOG_NOTICE, "Thread my_sensors_loop stopped");
52 */ 58 if (debug)
53 device = xstrcpy((char *)"/sys/bus/w1/devices/"); 59 fprintf(stdout, "Thread my_sensors_loop stopped\n");
54 device = xstrcat(device, tmp1->master); 60 return 0;
55 device = xstrcat(device, (char *)"/"); 61 }
56 device = xstrcat(device, tmp1->name);
57 device = xstrcat(device, (char *)"/w1_slave");
58 62
59 /* 63 /*
60 * Read sensor data 64 * Build path to the on-wire sensor
61 */ 65 */
62 if ((fp = fopen(device, "r"))) { 66 device = xstrcpy((char *)"/sys/bus/w1/devices/");
67 device = xstrcat(device, tmp1->master);
68 device = xstrcat(device, (char *)"/");
69 device = xstrcat(device, tmp1->name);
70 device = xstrcat(device, (char *)"/w1_slave");
71
72 /*
73 * Read sensor data
74 */
75 if ((fp = fopen(device, "r"))) {
76 /*
77 * The output looks like:
78 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
79 * 72 01 4b 46 7f ff 0e 10 57 t=23125
80 */
81 fgets(line, 50, fp);
82 line[strlen(line)-1] = '\0';
83 if ((line[36] == 'Y') && (line[37] == 'E')) {
63 /* 84 /*
64 * The output looks like: 85 * CRC is Ok, continue
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 */ 86 */
68 fgets(line, 50, fp); 87 fgets(line, 50, fp);
69 line[strlen(line)-1] = '\0'; 88 line[strlen(line)-1] = '\0';
70 if ((line[36] == 'Y') && (line[37] == 'E')) { 89 strtok(line, (char *)"=");
90 p = strtok(NULL, (char *)"=");
91 rc = sscanf(p, "%d", &temp);
92 if ((rc == 1) && (tmp1->lastval != temp)) {
71 /* 93 /*
72 * CRC is Ok, continue 94 * It is possible to have read errors or extreme values.
95 * This can happen with bad connections so we compare the
96 * value with the previous one. If the difference is too
97 * much, we don't send that value. That also means that if
98 * the next value is ok again, it will be marked invalid too.
99 * Maximum error is 20 degrees for now.
73 */ 100 */
74 fgets(line, 50, fp); 101 deviation = 20000;
75 line[strlen(line)-1] = '\0'; 102 if ( (tmp1->lastval == 0) ||
76 strtok(line, (char *)"="); 103 (tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation))) ) {
77 p = strtok(NULL, (char *)"=");
78 rc = sscanf(p, "%d", &temp);
79 if ((rc == 1) && (tmp1->lastval != temp)) {
80 /* 104 /*
81 * It is possible to have read errors or extreme values. 105 * Temperature is changed and valid, set flag.
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 */ 106 */
88 deviation = 20000; 107 tmp1->update = TRUE;
89 if ((tmp1->lastval == 0) || 108 } else {
90 (tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation)))) { 109 syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, tmp1->lastval, temp);
91 /* 110 if (debug) {
92 * Temperature is changed and valid, update and publish this. 111 fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, tmp1->lastval, temp);
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 } 112 }
101 tmp1->lastval = temp;
102 lcdupdate = TRUE;
103 } 113 }
104 } else { 114 tmp1->lastval = temp;
105 syslog(LOG_NOTICE, "sensor %s/%s CRC error", tmp1->master, tmp1->name);
106 } 115 }
107 fclose(fp); 116 } else {
108 tmp1->present = 1; 117 syslog(LOG_NOTICE, "sensor %s/%s CRC error", tmp1->master, tmp1->name);
109 } else { 118 }
110 tmp1->present = 0; 119 fclose(fp);
111 if (debug) 120 tmp1->present = 1;
112 printf("sensor %s is missing\n", tmp1->name); 121 } else {
113 } 122 tmp1->present = 0;
123 if (debug)
124 printf("sensor %s is missing\n", tmp1->name);
125 }
114 126
115 free(device); 127 free(device);
116 device = NULL; 128 device = NULL;
129 }
117 } 130 }
118 } 131 }
119 132
120 133
121 134

mercurial