3 * Copyright (C) 2009 Eric Reed
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.
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.
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
20 * Eric Reed <ericr at reedhome.net>
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
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
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.
40 #include "configfile.h"
42 #if HAVE_TERMIOS_H && HAVE_SYS_IOCTL_H && HAVE_MATH_H
44 # include <sys/ioctl.h>
47 # error "No applicable input method."
50 #define EXPECTED_PACKAGE_LENGTH 278
52 #define PKT_BEGIN 0x04
55 #define DEFAULT_DEVICE "/dev/ttyUSB0"
57 static char *conf_device = NULL;
58 static int conf_retries = 0;
62 static const char *config_keys[] =
67 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
69 static int ted_read_value(double *ret_power, double *ret_voltage)
71 unsigned char receive_buffer[300];
72 unsigned char package_buffer[300];
73 char pkt_request[1] = {0xAA};
74 int package_buffer_pos;
77 struct timeval timeout;
86 /* Initialize the input set*/
90 /* Initialize timeout structure, set to 2 seconds */
91 memset (&timeout, 0, sizeof (timeout));
95 /* clear out anything in the buffer */
96 tcflush (fd, TCIFLUSH);
98 status = write (fd, pkt_request, sizeof(pkt_request));
101 ERROR ("ted plugin: swrite failed.");
105 /* Loop until we find the end of the package */
107 package_buffer_pos = 0;
108 while (end_flag == 0)
110 ssize_t receive_buffer_length;
113 /* check for timeout or input error*/
114 status = select (fd + 1, &input, NULL, NULL, &timeout);
115 if (status == 0) /* Timeout */
117 WARNING ("ted plugin: Timeout while waiting for file descriptor "
121 else if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
123 /* Some signal or something. Start over.. */
129 ERROR ("ted plugin: select failed: %s",
130 sstrerror (errno, errbuf, sizeof (errbuf)));
134 receive_buffer_length = read (fd, receive_buffer, sizeof (receive_buffer));
135 if (receive_buffer_length < 0)
138 if ((errno == EAGAIN) || (errno == EINTR))
140 ERROR ("ted plugin: read(2) failed: %s",
141 sstrerror (errno, errbuf, sizeof (errbuf)));
144 else if (receive_buffer_length == 0)
146 /* Should we close the FD in this case? */
147 WARNING ("ted plugin: Received EOF from file descriptor.");
150 else if (((size_t) receive_buffer_length) > sizeof (receive_buffer))
152 ERROR ("ted plugin: read(2) returned invalid value %zi.",
153 receive_buffer_length);
160 * Handle escape sequences in `receive_buffer' and put the
161 * result in `package_buffer'.
163 /* We need to see the begin sequence first. When we receive `ESCAPE
164 * PKT_BEGIN', we set `package_buffer_pos' to zero to signal that
165 * the beginning of the package has been found. */
168 for (i = 0; i < receive_buffer_length; i++)
170 /* Check if previous byte was the escape byte. */
171 if (escape_flag == 1)
174 /* escape escape = single escape */
175 if ((receive_buffer[i] == ESCAPE)
176 && (package_buffer_pos >= 0))
178 package_buffer[package_buffer_pos] = ESCAPE;
179 package_buffer_pos++;
181 else if (receive_buffer[i] == PKT_BEGIN)
183 package_buffer_pos = 0;
185 else if (receive_buffer[i] == PKT_END)
192 DEBUG ("ted plugin: Unknown escaped byte: %#x",
193 (unsigned int) receive_buffer[i]);
196 else if (receive_buffer[i] == ESCAPE)
200 /* if we are in a package add byte to buffer
201 * otherwise throw away */
202 else if (package_buffer_pos >= 0)
204 package_buffer[package_buffer_pos] = receive_buffer[i];
205 package_buffer_pos++;
207 } /* for (i = 0; i < receive_buffer_length; i++) */
208 } /* while (end_flag == 0) */
210 /* Check for errors inside the loop. */
211 if ((end_flag == 0) || (package_buffer_pos != EXPECTED_PACKAGE_LENGTH))
215 * Power is at positions 247 and 248 (LSB first) in [10kW].
216 * Voltage is at positions 251 and 252 (LSB first) in [.1V].
218 * Power is in 10 Watt steps
219 * Voltage is in volts
221 *ret_power = 10.0 * (double) ((((int) package_buffer[248]) * 256)
222 + ((int) package_buffer[247]));
223 *ret_voltage = 0.1 * (double) ((((int) package_buffer[252]) * 256)
224 + ((int) package_buffer[251]));
228 } /* int ted_read_value */
230 static int ted_open_device (void)
233 struct termios options;
238 dev = DEFAULT_DEVICE;
239 if (conf_device != NULL)
242 fd = open (dev, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
245 ERROR ("ted plugin: Unable to open device %s.", dev);
249 /* Get the current options for the port... */
250 tcgetattr(fd, &options);
251 options.c_cflag = B19200 | CS8 | CSTOPB | CREAD | CLOCAL;
252 options.c_iflag = IGNBRK | IGNPAR;
255 options.c_cc[VTIME] = 20;
256 options.c_cc[VMIN] = 250;
258 /* Set the new options for the port... */
259 tcflush(fd, TCIFLUSH);
260 tcsetattr(fd, TCSANOW, &options);
262 INFO ("ted plugin: Successfully opened %s.", dev);
264 } /* int ted_open_device */
266 static void ted_submit (char *type, double value)
269 value_list_t vl = VALUE_LIST_INIT;
271 values[0].gauge = value;
275 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
276 sstrncpy (vl.plugin, "ted", sizeof (vl.plugin));
277 sstrncpy (vl.type, type, sizeof (vl.type));
279 plugin_dispatch_values (&vl);
282 static int ted_config (const char *key, const char *value)
284 if (strcasecmp ("Device", key) == 0)
287 conf_device = sstrdup (value);
289 else if (strcasecmp ("Retries", key) == 0)
296 WARNING ("ted plugin: Invalid retry count: %i", tmp);
303 ERROR ("ted plugin: Unknown config option: %s", key);
308 } /* int ted_config */
310 static int ted_read (void)
317 status = ted_open_device ();
323 for (i = 0; i <= conf_retries; i++)
325 status = ted_read_value (&power, &voltage);
333 ted_submit ("power", power);
334 ted_submit ("voltage", voltage);
339 static int ted_shutdown (void)
348 } /* int ted_shutdown */
350 void module_register (void)
352 plugin_register_config ("ted", ted_config,
353 config_keys, config_keys_num);
354 plugin_register_read ("ted", ted_read);
355 plugin_register_shutdown ("ted", ted_shutdown);
356 } /* void module_register */
358 /* vim: set sw=4 et : */