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 PKT_LENGTH 278
53 #define PKT_BEGIN 0x04
56 #define DEFAULT_DEVICE "/dev/ttyUSB "
58 static char *device = NULL;
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)
66 unsigned char sResult[MAX_PKT];
67 unsigned char package_buffer[MAX_PKT];
75 struct timeval timeout;
76 char pkt_request[1] = {0xAA};
79 int retry = 3; /* sometimes we receive garbadge */
81 /* Initialize the input set*/
85 /* Initialize timout structure, set to 1 second */
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);
100 ERROR ("ted plugin: swrite failed.");
105 /* Loop until we find the end of the package */
106 while (end_flag == 0)
108 /* check for timeout or input error*/
109 status = select(fd+1, &input, NULL, NULL, &timeout);
110 if (status == 0) /* Timeout */
115 else if ((status == -1) && ((errno == EAGAIN) || (errno == EINTR)))
120 else if (status == -1)
123 ERROR ("ted plugin: select failed: %s",
124 sstrerror (errno, errbuf, sizeof (errbuf)));
130 /* find out how may bytes are in the input buffer*/
131 ioctl(fd, FIONREAD, &byte);
132 DEBUG ("bytes in buffer %d",byte);
138 sResultnum = read(fd, sResult, MAX_PKT);
139 DEBUG ("bytes read %d",sResultnum);
144 for (byte=0; byte< sResultnum; byte++)
146 sResultByte = sResult[byte];
147 /* was byte before escape */
148 if (escape_flag == 1)
151 /* escape escape = single escape */
152 if ((sResultByte==ESCAPE) & (package_length > 0))
154 package_buffer[package_length] = ESCAPE;
157 else if (sResultByte==PKT_BEGIN)
161 else if (sResultByte==PKT_END)
167 else if (sResultByte == ESCAPE)
171 /* if we are in a package add byte to buffer
172 * otherwise throw away */
173 else if (package_length >= 0)
175 package_buffer[package_length] = sResultByte;
181 DEBUG ("read package_length %d",package_length);
183 if (package_length != PKT_LENGTH)
185 INFO ("Not the correct package");
186 /* delay until next package is loaded into TED TED is updated once
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;
196 *voltage = ((package_buffer[252] * 256) + package_buffer[251])/10.0;
197 DEBUG ("voltage %f",*voltage);
198 return (0); /* value received */
203 return (-2); /* no value received */
204 } /* int ted_read_value */
206 static int ted_init (void)
214 device = DEFAULT_DEVICE;
217 for (i = 0; i < 10; i++)
219 device[strlen(device)-1] = i + '0';
221 if ((fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) <= 0)
223 DEBUG ("No device at fd %d", fd);
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;
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)
243 DEBUG ("No device at fd %d", fd);
248 INFO ("ted plugin: Device found at %s", device);
252 ERROR ("ted plugin: No device found");
256 static void ted_submit (char *type_instance, double value)
259 value_list_t vl = VALUE_LIST_INIT;
261 values[0].gauge = value;
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);
273 static int ted_config (const char *key, const char *value)
275 if (strcasecmp ("Device", key) != 0)
281 device = sstrdup (value);
283 } /* int ted_config */
285 static int ted_read (void)
293 if (ted_read_value (&kw,&voltage) != 0)
296 ted_submit ("ted_kw", kw);
297 ted_submit ("ted_voltage", voltage);
301 static int ted_shutdown (void)
310 } /* int ted_shutdown */
312 void module_register (void)
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 : */