Tue, 05 Aug 2014 11:57:05 +0200
Fixed double commands to 433 MHz switches, they did not switch.
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; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
39 | char buf[40]; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
40 | int i, rc; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
41 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
42 | 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
|
43 | return 0; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
44 | |
181
4099412fca09
Fixed double commands to 433 MHz switches, they did not switch.
Michiel Broek <mbroek@mbse.eu>
parents:
179
diff
changeset
|
45 | if (debug) |
4099412fca09
Fixed double commands to 433 MHz switches, they did not switch.
Michiel Broek <mbroek@mbse.eu>
parents:
179
diff
changeset
|
46 | fprintf(stdout, "device_out(%s, %d)\n", uuid, value); |
4099412fca09
Fixed double commands to 433 MHz switches, they did not switch.
Michiel Broek <mbroek@mbse.eu>
parents:
179
diff
changeset
|
47 | |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
48 | 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
|
49 | if (strcmp(uuid, device->uuid) == 0) { |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
50 | if (value != device->value) { |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
51 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
52 | #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
|
53 | rc = 0; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
54 | 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
|
55 | 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
|
56 | usleep(10000); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
57 | 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
|
58 | 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
|
59 | 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
|
60 | buf[i] = ','; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
61 | 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
|
62 | 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
|
63 | if (debug) |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
64 | 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
|
65 | usleep(50000); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
66 | disableTransmit(); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
67 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
68 | 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
|
69 | 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
|
70 | return rc; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
71 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
72 | 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
|
73 | |
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 | #endif |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
76 | 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
|
77 | |
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 | } else { |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
80 | return 0; |
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 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
83 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
84 | return 0; |
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 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
87 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
88 | |
158 | 89 | /* |
90 | * Auto detect hotplugged or known to be present devices | |
91 | */ | |
92 | int devices_detect(void) | |
93 | { | |
94 | struct dirent *de; | |
95 | DIR *fd; | |
96 | devices_list *device, *ndev; | |
97 | int found, subdevices, i, rc = 0; | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
98 | char buf[40]; |
158 | 99 | uuid_t uu; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
100 | #ifdef HAVE_WIRINGPI_H |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
101 | int pin; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
102 | #endif |
158 | 103 | |
104 | /* | |
105 | * Scan for 1-wire devices | |
106 | */ | |
107 | if ((fd = opendir((char *)"/sys/bus/w1/devices"))) { | |
108 | while ((de = readdir(fd))) { | |
109 | if (de->d_name[0] != '.') { | |
110 | found = FALSE; | |
111 | for (device = Config.devices; device; device = device->next) { | |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
112 | if (strcmp(device->address,de->d_name) == 0) { |
158 | 113 | found = TRUE; |
114 | break; | |
115 | } | |
116 | } | |
117 | ||
118 | if (found == FALSE) { | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
119 | strncpy(buf, de->d_name, 2); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
120 | buf[2] = '\0'; |
158 | 121 | subdevices = 1; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
122 | if (strcmp(buf, (char *)"29") == 0) |
158 | 123 | subdevices = 8; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
124 | if (strcmp(buf, (char *)"3a") == 0) |
158 | 125 | subdevices = 2; |
126 | for (i = 0; i < subdevices; i++) { | |
127 | ndev = (devices_list *)malloc(sizeof(devices_list)); | |
128 | ndev->next = NULL; | |
129 | ndev->version = 1; | |
130 | ndev->uuid = malloc(37); | |
131 | uuid_generate(uu); | |
132 | uuid_unparse(uu, ndev->uuid); | |
133 | ndev->type = DEVTYPE_W1; | |
134 | ndev->direction = DEVDIR_UNDEF; | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
135 | if (strcmp(buf, (char *)"10") == 0) { |
158 | 136 | ndev->direction = DEVDIR_IN_ANALOG; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
137 | ndev->description = xstrcpy((char *)"DS18S20 Digital thermometer"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
138 | } else if (strcmp(buf, (char *)"28") == 0) { |
158 | 139 | ndev->direction = DEVDIR_IN_ANALOG; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
140 | ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
141 | } else if (strcmp(buf, (char *)"29") == 0) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
142 | ndev->description = xstrcpy((char *)"DS2408 8 Channel addressable switch/LCD"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
143 | } else if (strcmp(buf, (char *)"3a") == 0) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
144 | ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
145 | } else if (strcmp(buf, (char *)"w1") == 0) { |
158 | 146 | ndev->description = xstrcpy((char *)"Master System device"); |
147 | } else { | |
148 | ndev->description = xstrcpy((char *)"Unknown device family "); | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
149 | ndev->description = xstrcat(ndev->description, buf); |
158 | 150 | } |
151 | ndev->value = ndev->inuse = 0; | |
152 | ndev->present = DEVPRESENT_YES; | |
153 | ndev->address = xstrcpy(de->d_name); | |
154 | ndev->subdevice = i; | |
155 | ndev->gpiopin = -1; | |
156 | ndev->comment = xstrcpy((char *)"Auto detected device"); | |
157 | ndev->timestamp = time(NULL); | |
158 | ||
159 | if (Config.devices == NULL) { | |
160 | Config.devices = ndev; | |
161 | } else { | |
162 | for (device = Config.devices; device; device = device->next) { | |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
163 | if (device->next == NULL) { |
158 | 164 | device->next = ndev; |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
165 | break; |
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
166 | } |
158 | 167 | } |
168 | } | |
169 | rc++; | |
170 | } | |
171 | } | |
172 | } | |
173 | } | |
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
|
174 | closedir(fd); |
158 | 175 | } |
176 | ||
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
177 | #ifdef HAVE_WIRINGPI_H |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
178 | if (piBoardRev() == 2) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
179 | /* |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
180 | * Support rev B boards only |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
181 | */ |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
182 | found = FALSE; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
183 | for (device = Config.devices; device; device = device->next) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
184 | if (device->type == DEVTYPE_GPIO) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
185 | found = TRUE; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
186 | break; |
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 | } |
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 | if (found == FALSE) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
191 | /* |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
192 | * There were no GPIO devices found. |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
193 | */ |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
194 | subdevices = 12; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
195 | pin = 0; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
196 | for (i = 0; i < subdevices; i++) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
197 | if (i == 8) |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
198 | pin = 17; |
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 | ndev = (devices_list *)malloc(sizeof(devices_list)); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
201 | ndev->next = NULL; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
202 | ndev->version = 1; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
203 | ndev->uuid = malloc(37); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
204 | uuid_generate(uu); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
205 | uuid_unparse(uu, ndev->uuid); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
206 | ndev->type = DEVTYPE_GPIO; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
207 | ndev->value = digitalRead(pin); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
208 | 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
|
209 | ndev->address = xstrcpy((char *)"GPIO"); |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
210 | snprintf(buf, 39, "Raspberry GPIO %d", i); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
211 | ndev->description = xstrcpy(buf); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
212 | ndev->subdevice = i; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
213 | ndev->gpiopin = pin; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
214 | ndev->timestamp = time(NULL); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
215 | if (i == 7) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
216 | ndev->direction = DEVDIR_INTERN; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
217 | ndev->inuse = 1; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
218 | ndev->comment = xstrcpy((char *)"1-Wire bus"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
219 | } else { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
220 | ndev->direction = DEVDIR_IN_BIN; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
221 | ndev->inuse = 0; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
222 | ndev->comment = xstrcpy((char *)"Raspberry GPIO"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
223 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
224 | pin++; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
225 | |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
226 | if (Config.devices == NULL) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
227 | Config.devices = ndev; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
228 | } else { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
229 | for (device = Config.devices; device; device = device->next) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
230 | if (device->next == NULL) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
231 | device->next = ndev; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
232 | break; |
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 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
235 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
236 | rc++; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
237 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
238 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
239 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
240 | #endif |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
241 | |
158 | 242 | return rc; |
243 | } | |
244 | ||
245 | ||
246 | ||
247 | #ifdef HAVE_WIRINGPI_H | |
248 | PI_THREAD (my_devices_loop) | |
249 | #else | |
250 | void *my_devices_loop(void *threadid) | |
251 | #endif | |
252 | { | |
253 | devices_list *device; | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
254 | char *addr = NULL, line[60], *p = NULL; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
255 | FILE *fp; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
256 | int temp, rc; |
158 | 257 | |
258 | syslog(LOG_NOTICE, "Thread my_devices_loop started"); | |
259 | if (debug) | |
260 | fprintf(stdout, "Thread my_devices_loop started\n"); | |
261 | ||
262 | /* | |
263 | * Loop forever until the external shutdown variable is set. | |
264 | */ | |
265 | for (;;) { | |
266 | ||
267 | /* | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
268 | * Process all devices. |
158 | 269 | */ |
270 | for (device = Config.devices; device; device = device->next) { | |
271 | ||
272 | if (my_shutdown) | |
273 | break; | |
274 | ||
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
275 | switch (device->type) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
276 | case DEVTYPE_W1: |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
277 | 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
|
278 | /* Read DS18B20 sensor */ |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
279 | addr = xstrcpy((char *)"/sys/bus/w1/devices/"); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
280 | addr = xstrcat(addr, device->address); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
281 | addr = xstrcat(addr, (char *)"/w1_slave"); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
282 | if ((fp = fopen(addr, "r"))) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
283 | if (device->present != DEVPRESENT_YES) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
284 | 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
|
285 | device->present = DEVPRESENT_YES; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
286 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
287 | /* |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
288 | * The output looks like: |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
289 | * 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
|
290 | * 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
|
291 | */ |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
292 | fgets(line, 50, fp); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
293 | line[strlen(line)-1] = '\0'; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
294 | if ((line[36] == 'Y') && (line[37] == 'E')) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
295 | /* CRC is Ok, continue */ |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
296 | fgets(line, 50, fp); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
297 | line[strlen(line)-1] = '\0'; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
298 | strtok(line, (char *)"="); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
299 | p = strtok(NULL, (char *)"="); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
300 | rc = sscanf(p, "%d", &temp); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
301 | if ((rc == 1) && (device->value != temp)) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
302 | device->value = temp; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
303 | device->timestamp = time(NULL); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
304 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
305 | } else { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
306 | 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
|
307 | device->present = DEVPRESENT_ERROR; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
308 | } |
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
|
309 | fclose(fp); |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
310 | } else { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
311 | if (device->present != DEVPRESENT_NO) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
312 | 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
|
313 | device->present = DEVPRESENT_NO; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
314 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
315 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
316 | free(addr); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
317 | addr = NULL; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
318 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
319 | break; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
320 | default: |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
321 | break; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
322 | } |
158 | 323 | } |
324 | usleep(10000); | |
325 | } | |
326 | ||
327 | syslog(LOG_NOTICE, "Thread my_devices_loop stopped"); | |
328 | if (debug) | |
329 | fprintf(stdout, "Thread my_devices_loop stopped\n"); | |
330 | ||
331 | return 0; | |
332 | } | |
333 | ||
334 | ||
335 |