Wed, 23 Jul 2014 21:34:02 +0200
Removed sorting of 1-Wire devices because the busy indicator doesn't dissapear
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1 | /***************************************************************************** |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
2 | * Copyright (C) 2014 |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | * Michiel Broek <mbroek at mbse dot eu> |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
5 | * |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
6 | * This file is part of the mbsePi-apps |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | * |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | * This is free software; you can redistribute it and/or modify it |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | * under the terms of the GNU General Public License as published by the |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | * Free Software Foundation; either version 2, or (at your option) any |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | * later version. |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | * |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | * mbsePi-apps is distributed in the hope that it will be useful, but |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | * General Public License for more details. |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | * |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | * You should have received a copy of the GNU General Public License |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
19 | * along with EC-65K; see the file COPYING. If not, write to the Free |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
21 | *****************************************************************************/ |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
22 | |
106 | 23 | #include "sensors.h" |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
41
diff
changeset
|
24 | #include "thermferm.h" |
106 | 25 | #include "xutil.h" |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | |
75
4b976601737d
Writes a basic xml configuration next to the plain ascii config file
Michiel Broek <mbroek@mbse.eu>
parents:
74
diff
changeset
|
28 | extern int debug; |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
29 | extern sys_config Config; |
40
dafbbd5e9922
Prepared unix socket communication
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
30 | extern int my_shutdown; |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
31 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
32 | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
51
diff
changeset
|
33 | #ifdef HAVE_WIRINGPI_H |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
34 | PI_THREAD (my_sensors_loop) |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
51
diff
changeset
|
35 | #else |
74 | 36 | void *my_sensors_loop(void *threadid) |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
51
diff
changeset
|
37 | #endif |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
38 | { |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
39 | w1_therm *tmp1, *old1; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | char *device, line[60], *p = NULL; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
41 | FILE *fp; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
42 | int temp, rc, deviation; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
44 | syslog(LOG_NOTICE, "Thread my_sensors_loop started"); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
45 | if (debug) |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
46 | fprintf(stdout, "Thread my_sensors_loop started\n"); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
47 | |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
48 | /* |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
49 | * Loop forever until the external shutdown variable is set. |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
50 | */ |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
51 | for (;;) { |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
51
diff
changeset
|
52 | |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
53 | /* |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
54 | * Here send our 1-wire sensors values |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
55 | */ |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
56 | for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
57 | old1 = tmp1->next; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
58 | |
40
dafbbd5e9922
Prepared unix socket communication
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
59 | if (my_shutdown) { |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
60 | syslog(LOG_NOTICE, "Thread my_sensors_loop stopped"); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
61 | if (debug) |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
62 | fprintf(stdout, "Thread my_sensors_loop stopped\n"); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
63 | return 0; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
64 | } |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
65 | |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
66 | /* |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
67 | * Build path to the on-wire sensor |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
68 | */ |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
69 | device = xstrcpy((char *)"/sys/bus/w1/devices/"); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
70 | device = xstrcat(device, tmp1->master); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
71 | device = xstrcat(device, (char *)"/"); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
72 | device = xstrcat(device, tmp1->name); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
73 | device = xstrcat(device, (char *)"/w1_slave"); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
74 | |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
75 | /* |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
76 | * Read sensor data |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
77 | */ |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
78 | if ((fp = fopen(device, "r"))) { |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
79 | /* |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
80 | * The output looks like: |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
81 | * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
82 | * 72 01 4b 46 7f ff 0e 10 57 t=23125 |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
83 | */ |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
84 | fgets(line, 50, fp); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
85 | line[strlen(line)-1] = '\0'; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
86 | if ((line[36] == 'Y') && (line[37] == 'E')) { |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
87 | /* |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
88 | * CRC is Ok, continue |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
89 | */ |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
90 | fgets(line, 50, fp); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
91 | line[strlen(line)-1] = '\0'; |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
92 | strtok(line, (char *)"="); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
93 | p = strtok(NULL, (char *)"="); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
94 | rc = sscanf(p, "%d", &temp); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
95 | if ((rc == 1) && (tmp1->lastval != temp)) { |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
96 | /* |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
97 | * It is possible to have read errors or extreme values. |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
98 | * This can happen with bad connections so we compare the |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
99 | * value with the previous one. If the difference is too |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
100 | * much, we don't send that value. That also means that if |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
101 | * the next value is ok again, it will be marked invalid too. |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
102 | * Maximum error is 20 degrees for now. |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
103 | */ |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
104 | deviation = 20000; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
105 | if ( (tmp1->lastval == 0) || |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
106 | (tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation))) ) { |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
107 | /* |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
108 | * Temperature is changed and valid, set flag. |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
109 | */ |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
110 | tmp1->update = TRUE; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
111 | } else { |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
112 | syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, tmp1->lastval, temp); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
113 | if (debug) { |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
114 | fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, tmp1->lastval, temp); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
115 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
116 | } |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
117 | tmp1->lastval = temp; |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
118 | } |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
119 | } else { |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
120 | syslog(LOG_NOTICE, "sensor %s/%s CRC error", tmp1->master, tmp1->name); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
121 | } |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
122 | fclose(fp); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
123 | tmp1->present = 1; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
124 | } else { |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
125 | tmp1->present = 0; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
126 | if (debug) |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
127 | printf("sensor %s is missing\n", tmp1->name); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
128 | } |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
129 | |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
130 | free(device); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
131 | device = NULL; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
132 | } |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
75
diff
changeset
|
133 | usleep(10000); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
134 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
135 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
136 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
137 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
138 |