Sat, 31 May 2014 12:00:40 +0200
Code fix
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 | |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
41
diff
changeset
|
23 | #include "thermferm.h" |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
24 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
25 | #ifdef HAVE_WIRINGPI_H |
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 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | extern bool debug; |
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 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
33 | |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
34 | PI_THREAD (my_sensors_loop) |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
35 | { |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
36 | w1_therm *tmp1, *old1; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | char *device, line[60], *p = NULL; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
38 | FILE *fp; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
39 | int temp, rc, deviation; |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
41 | 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
|
42 | if (debug) |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
43 | 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
|
44 | |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
45 | /* |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
46 | * Loop forever until the external shutdown variable is set. |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
47 | */ |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
48 | for (;;) { |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
49 | /* |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
50 | * Here send our 1-wire sensors values |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
51 | */ |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
52 | for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
53 | old1 = tmp1->next; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
54 | |
40
dafbbd5e9922
Prepared unix socket communication
Michiel Broek <mbroek@mbse.eu>
parents:
27
diff
changeset
|
55 | if (my_shutdown) { |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
56 | 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
|
57 | if (debug) |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
58 | 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
|
59 | return 0; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
60 | } |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
61 | |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
62 | /* |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
63 | * Build path to the on-wire sensor |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
64 | */ |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
65 | device = xstrcpy((char *)"/sys/bus/w1/devices/"); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
66 | device = xstrcat(device, tmp1->master); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
67 | device = xstrcat(device, (char *)"/"); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
68 | device = xstrcat(device, tmp1->name); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
69 | device = xstrcat(device, (char *)"/w1_slave"); |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
70 | |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
71 | /* |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
72 | * Read sensor data |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
73 | */ |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
74 | if ((fp = fopen(device, "r"))) { |
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 | * The output looks like: |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
77 | * 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
|
78 | * 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
|
79 | */ |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
80 | fgets(line, 50, fp); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
81 | line[strlen(line)-1] = '\0'; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
82 | if ((line[36] == 'Y') && (line[37] == 'E')) { |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
83 | /* |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
84 | * CRC is Ok, continue |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
85 | */ |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
86 | fgets(line, 50, fp); |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
87 | line[strlen(line)-1] = '\0'; |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
88 | strtok(line, (char *)"="); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
89 | p = strtok(NULL, (char *)"="); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
90 | rc = sscanf(p, "%d", &temp); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
91 | if ((rc == 1) && (tmp1->lastval != temp)) { |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
92 | /* |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
93 | * 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
|
94 | * 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
|
95 | * 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
|
96 | * 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
|
97 | * 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
|
98 | * Maximum error is 20 degrees for now. |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
99 | */ |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
100 | deviation = 20000; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
101 | if ( (tmp1->lastval == 0) || |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
102 | (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
|
103 | /* |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
104 | * Temperature is changed and valid, set flag. |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
105 | */ |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
106 | tmp1->update = TRUE; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
107 | } else { |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
108 | 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
|
109 | if (debug) { |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
110 | 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
|
111 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
112 | } |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
113 | tmp1->lastval = temp; |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
114 | } |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
115 | } else { |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
116 | 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
|
117 | } |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
118 | fclose(fp); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
119 | tmp1->present = 1; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
120 | } else { |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
121 | tmp1->present = 0; |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
122 | if (debug) |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
123 | 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
|
124 | } |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
125 | |
27
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
126 | free(device); |
4703cc10b99a
Sensor reading is done in a thread
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
127 | device = NULL; |
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 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
130 | } |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
131 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
132 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
133 | |
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
134 | #endif |