1 /**
2 * collectd - src/ted.c
3 * Copyright (C) 2009 Eric Reed
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Eric Reed <ericr at reedhome.net>
21 *
22 * This is a collectd module for The Energy Detective: A low-cost whole
23 * house energy monitoring system. For more information on TED, see
24 * http://theenergydetective.com
25 *
26 * This module was not created by Energy, Inc. nor is it supported by
27 * them in any way. It was created using information from two sources:
28 * David Satterfield's TED module for Misterhouse, and Micah Dowty's TED
29 * Python Module.
30 *
31 * This has only tested with the model 1001 RDU, with
32 * firmware version 9.01U. The USB port is uses the very common FTDI
33 * USB-to-serial chip, so the RDU will show up as a serial device on
34 * Windows, Mac OS, or Linux.
35 **/
37 #include "collectd.h"
38 #include "common.h"
39 #include "plugin.h"
40 #include "configfile.h"
42 #if HAVE_TERMIOS_H && HAVE_SYS_IOCTL_H && HAVE_MATH_H
43 # include <termios.h>
44 # include <sys/ioctl.h>
45 # include <math.h>
46 #else
47 # error "No applicable input method."
48 #endif
50 #define PKT_LENGTH 278
51 #define MAX_PKT 300
52 #define ESCAPE 0x10
53 #define PKT_BEGIN 0x04
54 #define PKT_END 0x03
56 #define DEFAULT_DEVICE "/dev/ttyUSB "
58 static char *device = NULL;
59 static int fd = -1;
61 static const char *config_keys[] = { "Device" };
62 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
64 static int ted_read_value(double *kw, double *voltage)
65 {
66 unsigned char sResult[MAX_PKT];
67 unsigned char package_buffer[MAX_PKT];
68 char sResultByte;
69 int status;
70 int byte;
71 int package_length;
72 int escape_flag;
73 int end_flag;
74 int sResultnum;
75 struct timeval timeout;
76 char pkt_request[1] = {0xAA};
77 fd_set input;
79 int retry = 3; /* sometimes we receive garbadge */
81 /* Initialize the input set*/
82 FD_ZERO(&input);
83 FD_SET(fd, &input);
85 /* Initialize timout structure, set to 1 second */
86 do
87 {
88 timeout.tv_sec = 2;
89 timeout.tv_usec = 0;
90 escape_flag = 0;
91 end_flag = 0;
92 package_length = 0;
93 /* clear out anything in the buffer */
94 tcflush(fd, TCIFLUSH);
96 status = write (fd, pkt_request,sizeof(pkt_request));
97 DEBUG ("status of write %d",status);
98 if (status < 0)
99 {
100 ERROR ("ted plugin: swrite failed.");
101 return (-1);
102 }
105 /* Loop until we find the end of the package */
106 while (end_flag == 0)
107 {
108 /* check for timeout or input error*/
109 status = select(fd+1, &input, NULL, NULL, &timeout);
110 if (status == 0) /* Timeout */
111 {
112 INFO ("Timeout");
113 break;
114 }
115 else if ((status == -1) && ((errno == EAGAIN) || (errno == EINTR)))
116 {
117 DEBUG ("Not Ready");
118 continue;
119 }
120 else if (status == -1)
121 {
122 char errbuf[1024];
123 ERROR ("ted plugin: select failed: %s",
124 sstrerror (errno, errbuf, sizeof (errbuf)));
125 break;
126 }
128 else
129 {
130 /* find out how may bytes are in the input buffer*/
131 ioctl(fd, FIONREAD, &byte);
132 DEBUG ("bytes in buffer %d",byte);
133 if (byte <= 0)
134 {
135 continue;
136 }
138 sResultnum = read(fd, sResult, MAX_PKT);
139 DEBUG ("bytes read %d",sResultnum);
141 /*
142 * packet filter loop
143 */
144 for (byte=0; byte< sResultnum; byte++)
145 {
146 sResultByte = sResult[byte];
147 /* was byte before escape */
148 if (escape_flag == 1)
149 {
150 escape_flag = 0;
151 /* escape escape = single escape */
152 if ((sResultByte==ESCAPE) & (package_length > 0))
153 {
154 package_buffer[package_length] = ESCAPE;
155 package_length++;
156 }
157 else if (sResultByte==PKT_BEGIN)
158 {
159 package_length=0;
160 }
161 else if (sResultByte==PKT_END)
162 {
163 end_flag = 1;
164 break;
165 }
166 }
167 else if (sResultByte == ESCAPE)
168 {
169 escape_flag = 1;
170 }
171 /* if we are in a package add byte to buffer
172 * otherwise throw away */
173 else if (package_length >= 0)
174 {
175 package_buffer[package_length] = sResultByte;
176 package_length++;
177 }
178 }
179 }
180 }
181 DEBUG ("read package_length %d",package_length);
183 if (package_length != PKT_LENGTH)
184 {
185 INFO ("Not the correct package");
186 /* delay until next package is loaded into TED TED is updated once
187 * per second */
188 usleep (1000000);
189 continue;
190 }
192 /* part of the info in the package get KiloWatts at char 247 and 248
193 * get Voltage at char 251 and 252 */
194 *kw = ((package_buffer[248] * 256) + package_buffer[247])*10.0;
195 DEBUG ("kw %f",*kw);
196 *voltage = ((package_buffer[252] * 256) + package_buffer[251])/10.0;
197 DEBUG ("voltage %f",*voltage);
198 return (0); /* value received */
201 } while (--retry);
203 return (-2); /* no value received */
204 } /* int ted_read_value */
206 static int ted_init (void)
207 {
208 int i;
209 double kw;
210 double voltage;
212 if (device == NULL)
213 {
214 device = DEFAULT_DEVICE;
215 }
217 for (i = 0; i < 10; i++)
218 {
219 device[strlen(device)-1] = i + '0';
221 if ((fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) <= 0)
222 {
223 DEBUG ("No device at fd %d", fd);
224 close (fd);
225 continue;
226 }
227 struct termios options;
228 /* Get the current options for the port... */
229 tcgetattr(fd, &options);
230 options.c_cflag = B19200 | CS8 | CSTOPB | CREAD | CLOCAL;
231 options.c_iflag = IGNBRK | IGNPAR;
232 options.c_oflag = 0;
233 options.c_lflag = 0;
234 options.c_cc[VTIME] = 20;
235 options.c_cc[VMIN] = 250;
237 /* Set the new options for the port... */
238 tcflush(fd, TCIFLUSH);
239 tcsetattr(fd, TCSANOW, &options);
241 if (ted_read_value (&kw,&voltage) != 0)
242 {
243 DEBUG ("No device at fd %d", fd);
244 close (fd);
245 continue;
246 }
248 INFO ("ted plugin: Device found at %s", device);
249 return (0);
250 }
252 ERROR ("ted plugin: No device found");
253 return (-1);
254 }
256 static void ted_submit (char *type_instance, double value)
257 {
258 value_t values[1];
259 value_list_t vl = VALUE_LIST_INIT;
261 values[0].gauge = value;
263 vl.values = values;
264 vl.values_len = 1;
265 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
266 sstrncpy (vl.plugin, "ted", sizeof (vl.plugin));
267 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
268 sstrncpy (vl.type, "ted", sizeof (vl.type));
270 plugin_dispatch_values (&vl);
271 }
273 static int ted_config (const char *key, const char *value)
274 {
275 if (strcasecmp ("Device", key) != 0)
276 {
277 return (-1);
278 }
280 sfree (device);
281 device = sstrdup (value);
282 return (0);
283 } /* int ted_config */
285 static int ted_read (void)
286 {
287 double kw;
288 double voltage;
290 if (fd < 0)
291 return (-1);
293 if (ted_read_value (&kw,&voltage) != 0)
294 return (-1);
296 ted_submit ("ted_kw", kw);
297 ted_submit ("ted_voltage", voltage);
298 return (0);
299 } /* int ted_read */
301 static int ted_shutdown (void)
302 {
303 if (fd >= 0)
304 {
305 close (fd);
306 fd = -1;
307 }
309 return (0);
310 } /* int ted_shutdown */
312 void module_register (void)
313 {
314 plugin_register_config ("ted", ted_config,
315 config_keys, config_keys_num);
316 plugin_register_init ("ted", ted_init);
317 plugin_register_read ("ted", ted_read);
318 plugin_register_shutdown ("ted", ted_shutdown);
319 } /* void module_register */
321 /* vim: set sw=4 et : */