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