Tue, 05 Aug 2014 22:29:22 +0200
Updated conditional defines and dependencies
158 | 1 | /***************************************************************************** |
2 | * Copyright (C) 2014 | |
3 | * | |
4 | * Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
6 | * This file is part of the mbsePi-apps | |
7 | * | |
8 | * This is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * mbsePi-apps is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with thermferm; see the file COPYING. If not, write to the Free | |
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | *****************************************************************************/ | |
22 | ||
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
23 | #include "thermferm.h" |
158 | 24 | #include "devices.h" |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
25 | #include "rc-switch.h" |
158 | 26 | #include "xutil.h" |
27 | ||
28 | ||
29 | extern int debug; | |
30 | extern sys_config Config; | |
31 | extern int my_shutdown; | |
32 | ||
33 | ||
34 | ||
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
35 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
36 | int device_out(char *uuid, int value) |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
37 | { |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
38 | devices_list *device; |
187
3c8bf18fdf42
Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
39 | time_t now; |
3c8bf18fdf42
Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
40 | #ifdef HAVE_WIRINGPI_H |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
41 | int i, rc; |
187
3c8bf18fdf42
Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
42 | char buf[40]; |
3c8bf18fdf42
Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
43 | #endif |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
44 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
45 | if (uuid == NULL) |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
46 | return 0; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
47 | |
185 | 48 | now = time(NULL); |
181
4099412fca09
Fixed double commands to 433 MHz switches, they did not switch.
Michiel Broek <mbroek@mbse.eu>
parents:
179
diff
changeset
|
49 | |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
50 | for (device = Config.devices; device; device = device->next) { |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
51 | if (strcmp(uuid, device->uuid) == 0) { |
185 | 52 | /* |
53 | * Execute command if different then the old value. But also | |
54 | * every 2 minutes because commands can get lost of devices were | |
55 | * disconnected, or with radio problems. | |
56 | */ | |
57 | if ((value != device->value) || (((int)now - (int)device->timestamp) > 120)) { | |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
58 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
59 | #ifdef HAVE_WIRINGPI_H |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
60 | rc = 0; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
61 | if ((device->type == DEVTYPE_RC433) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
62 | enableTransmit(device->gpiopin); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
63 | usleep(10000); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
64 | snprintf(buf, 39, "%s,%d", device->address, value ? 1:0); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
65 | for (i = 0; i < strlen(buf); i++) |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
66 | if (buf[i] == '-') |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
67 | buf[i] = ','; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
68 | rc = toggleSwitch(buf); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
69 | syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
70 | if (debug) |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
71 | fprintf(stdout, "RC433 command %s rc=%d\n", buf, rc); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
72 | usleep(50000); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
73 | disableTransmit(); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
74 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
75 | device->value = value; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
76 | device->timestamp = time(NULL); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
77 | return rc; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
78 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
79 | if ((device->type == DEVTYPE_GPIO) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
80 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
81 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
82 | #endif |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
83 | if ((device->type == DEVTYPE_W1) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) { |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
84 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
85 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
86 | } else { |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
87 | return 0; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
88 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
89 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
90 | } |
185 | 91 | |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
92 | return 0; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
93 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
94 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
95 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
96 | |
158 | 97 | /* |
98 | * Auto detect hotplugged or known to be present devices | |
99 | */ | |
100 | int devices_detect(void) | |
101 | { | |
102 | struct dirent *de; | |
103 | DIR *fd; | |
104 | devices_list *device, *ndev; | |
105 | int found, subdevices, i, rc = 0; | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
106 | char buf[40]; |
158 | 107 | uuid_t uu; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
108 | #ifdef HAVE_WIRINGPI_H |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
109 | int pin; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
110 | #endif |
158 | 111 | |
112 | /* | |
113 | * Scan for 1-wire devices | |
114 | */ | |
115 | if ((fd = opendir((char *)"/sys/bus/w1/devices"))) { | |
116 | while ((de = readdir(fd))) { | |
117 | if (de->d_name[0] != '.') { | |
118 | found = FALSE; | |
119 | for (device = Config.devices; device; device = device->next) { | |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
120 | if (strcmp(device->address,de->d_name) == 0) { |
158 | 121 | found = TRUE; |
122 | break; | |
123 | } | |
124 | } | |
125 | ||
126 | if (found == FALSE) { | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
127 | strncpy(buf, de->d_name, 2); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
128 | buf[2] = '\0'; |
158 | 129 | subdevices = 1; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
130 | if (strcmp(buf, (char *)"29") == 0) |
158 | 131 | subdevices = 8; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
132 | if (strcmp(buf, (char *)"3a") == 0) |
158 | 133 | subdevices = 2; |
134 | for (i = 0; i < subdevices; i++) { | |
135 | ndev = (devices_list *)malloc(sizeof(devices_list)); | |
136 | ndev->next = NULL; | |
137 | ndev->version = 1; | |
138 | ndev->uuid = malloc(37); | |
139 | uuid_generate(uu); | |
140 | uuid_unparse(uu, ndev->uuid); | |
141 | ndev->type = DEVTYPE_W1; | |
142 | ndev->direction = DEVDIR_UNDEF; | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
143 | if (strcmp(buf, (char *)"10") == 0) { |
158 | 144 | ndev->direction = DEVDIR_IN_ANALOG; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
145 | ndev->description = xstrcpy((char *)"DS18S20 Digital thermometer"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
146 | } else if (strcmp(buf, (char *)"28") == 0) { |
158 | 147 | ndev->direction = DEVDIR_IN_ANALOG; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
148 | ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
149 | } else if (strcmp(buf, (char *)"29") == 0) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
150 | ndev->description = xstrcpy((char *)"DS2408 8 Channel addressable switch/LCD"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
151 | } else if (strcmp(buf, (char *)"3a") == 0) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
152 | ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
153 | } else if (strcmp(buf, (char *)"w1") == 0) { |
158 | 154 | ndev->description = xstrcpy((char *)"Master System device"); |
155 | } else { | |
156 | ndev->description = xstrcpy((char *)"Unknown device family "); | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
157 | ndev->description = xstrcat(ndev->description, buf); |
158 | 158 | } |
159 | ndev->value = ndev->inuse = 0; | |
160 | ndev->present = DEVPRESENT_YES; | |
161 | ndev->address = xstrcpy(de->d_name); | |
162 | ndev->subdevice = i; | |
163 | ndev->gpiopin = -1; | |
164 | ndev->comment = xstrcpy((char *)"Auto detected device"); | |
165 | ndev->timestamp = time(NULL); | |
166 | ||
167 | if (Config.devices == NULL) { | |
168 | Config.devices = ndev; | |
169 | } else { | |
170 | for (device = Config.devices; device; device = device->next) { | |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
171 | if (device->next == NULL) { |
158 | 172 | device->next = ndev; |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
173 | break; |
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
174 | } |
158 | 175 | } |
176 | } | |
177 | rc++; | |
178 | } | |
179 | } | |
180 | } | |
181 | } | |
166
c31ea86fec43
Forgot some close functions so tha program ran out of file descriptors. Better loggin for writing the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
164
diff
changeset
|
182 | closedir(fd); |
158 | 183 | } |
184 | ||
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
185 | #ifdef HAVE_WIRINGPI_H |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
186 | if (piBoardRev() == 2) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
187 | /* |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
188 | * Support rev B boards only |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
189 | */ |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
190 | found = FALSE; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
191 | for (device = Config.devices; device; device = device->next) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
192 | if (device->type == DEVTYPE_GPIO) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
193 | found = TRUE; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
194 | break; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
195 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
196 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
197 | |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
198 | if (found == FALSE) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
199 | /* |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
200 | * There were no GPIO devices found. |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
201 | */ |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
202 | subdevices = 12; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
203 | pin = 0; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
204 | for (i = 0; i < subdevices; i++) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
205 | if (i == 8) |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
206 | pin = 17; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
207 | |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
208 | ndev = (devices_list *)malloc(sizeof(devices_list)); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
209 | ndev->next = NULL; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
210 | ndev->version = 1; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
211 | ndev->uuid = malloc(37); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
212 | uuid_generate(uu); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
213 | uuid_unparse(uu, ndev->uuid); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
214 | ndev->type = DEVTYPE_GPIO; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
215 | ndev->value = digitalRead(pin); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
216 | ndev->present = DEVPRESENT_YES; |
163
3d813570a5e3
LIST BUS now displays the new devices table. Adjusted the web screen
Michiel Broek <mbroek@mbse.eu>
parents:
162
diff
changeset
|
217 | ndev->address = xstrcpy((char *)"GPIO"); |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
218 | snprintf(buf, 39, "Raspberry GPIO %d", i); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
219 | ndev->description = xstrcpy(buf); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
220 | ndev->subdevice = i; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
221 | ndev->gpiopin = pin; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
222 | ndev->timestamp = time(NULL); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
223 | if (i == 7) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
224 | ndev->direction = DEVDIR_INTERN; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
225 | ndev->inuse = 1; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
226 | ndev->comment = xstrcpy((char *)"1-Wire bus"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
227 | } else { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
228 | ndev->direction = DEVDIR_IN_BIN; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
229 | ndev->inuse = 0; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
230 | ndev->comment = xstrcpy((char *)"Raspberry GPIO"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
231 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
232 | pin++; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
233 | |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
234 | if (Config.devices == NULL) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
235 | Config.devices = ndev; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
236 | } else { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
237 | for (device = Config.devices; device; device = device->next) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
238 | if (device->next == NULL) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
239 | device->next = ndev; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
240 | break; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
241 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
242 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
243 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
244 | rc++; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
245 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
246 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
247 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
248 | #endif |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
249 | |
158 | 250 | return rc; |
251 | } | |
252 | ||
253 | ||
254 | ||
255 | #ifdef HAVE_WIRINGPI_H | |
256 | PI_THREAD (my_devices_loop) | |
257 | #else | |
258 | void *my_devices_loop(void *threadid) | |
259 | #endif | |
260 | { | |
261 | devices_list *device; | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
262 | char *addr = NULL, line[60], *p = NULL; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
263 | FILE *fp; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
264 | int temp, rc; |
158 | 265 | |
266 | syslog(LOG_NOTICE, "Thread my_devices_loop started"); | |
267 | if (debug) | |
268 | fprintf(stdout, "Thread my_devices_loop started\n"); | |
269 | ||
270 | /* | |
271 | * Loop forever until the external shutdown variable is set. | |
272 | */ | |
273 | for (;;) { | |
274 | ||
275 | /* | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
276 | * Process all devices. |
158 | 277 | */ |
278 | for (device = Config.devices; device; device = device->next) { | |
279 | ||
280 | if (my_shutdown) | |
281 | break; | |
282 | ||
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
283 | switch (device->type) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
284 | case DEVTYPE_W1: |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
285 | if (strncmp(device->address, (char *)"28", 2) == 0) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
286 | /* Read DS18B20 sensor */ |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
287 | addr = xstrcpy((char *)"/sys/bus/w1/devices/"); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
288 | addr = xstrcat(addr, device->address); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
289 | addr = xstrcat(addr, (char *)"/w1_slave"); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
290 | if ((fp = fopen(addr, "r"))) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
291 | if (device->present != DEVPRESENT_YES) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
292 | syslog(LOG_NOTICE, "sensor %s is back", device->address); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
293 | device->present = DEVPRESENT_YES; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
294 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
295 | /* |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
296 | * The output looks like: |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
297 | * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
298 | * 72 01 4b 46 7f ff 0e 10 57 t=23125 |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
299 | */ |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
300 | fgets(line, 50, fp); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
301 | line[strlen(line)-1] = '\0'; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
302 | if ((line[36] == 'Y') && (line[37] == 'E')) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
303 | /* CRC is Ok, continue */ |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
304 | fgets(line, 50, fp); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
305 | line[strlen(line)-1] = '\0'; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
306 | strtok(line, (char *)"="); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
307 | p = strtok(NULL, (char *)"="); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
308 | rc = sscanf(p, "%d", &temp); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
309 | if ((rc == 1) && (device->value != temp)) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
310 | device->value = temp; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
311 | device->timestamp = time(NULL); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
312 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
313 | } else { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
314 | syslog(LOG_NOTICE, "sensor %s CRC error", device->address); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
315 | device->present = DEVPRESENT_ERROR; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
316 | } |
166
c31ea86fec43
Forgot some close functions so tha program ran out of file descriptors. Better loggin for writing the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
164
diff
changeset
|
317 | fclose(fp); |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
318 | } else { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
319 | if (device->present != DEVPRESENT_NO) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
320 | syslog(LOG_NOTICE, "sensor %s is missing", device->address); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
321 | device->present = DEVPRESENT_NO; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
322 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
323 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
324 | free(addr); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
325 | addr = NULL; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
326 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
327 | break; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
328 | default: |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
329 | break; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
330 | } |
158 | 331 | } |
332 | usleep(10000); | |
333 | } | |
334 | ||
335 | syslog(LOG_NOTICE, "Thread my_devices_loop stopped"); | |
336 | if (debug) | |
337 | fprintf(stdout, "Thread my_devices_loop stopped\n"); | |
338 | ||
339 | return 0; | |
340 | } | |
341 | ||
342 | ||
343 |