From 3104bbec3f59add35872d31f229220019c0f904a Mon Sep 17 00:00:00 2001 From: Martin Sebald Date: Fri, 19 Jan 2018 13:33:41 +0100 Subject: [PATCH] udev and new libsml repo --- sml/50-sml.rules | 1 + sml/README.md | 9 +++++-- sml/sml.rules | 2 +- sml/sml_server.c | 63 ++++++++++++++++++++++++++---------------------- 4 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 sml/50-sml.rules diff --git a/sml/50-sml.rules b/sml/50-sml.rules new file mode 100644 index 0000000..7af114e --- /dev/null +++ b/sml/50-sml.rules @@ -0,0 +1 @@ +SUBSYSTEMS=="usb", KERNEL=="ttyUSB*", ATTRS{manufacturer}=="Silicon Labs", ATTRS{serial}=="00xxxxxx", SYMLINK="sml0" diff --git a/sml/README.md b/sml/README.md index 92acbc3..6bcc3f6 100644 --- a/sml/README.md +++ b/sml/README.md @@ -5,8 +5,13 @@ First of all you need is a eHZ (Elektronischer Haushaltszähler) and an IrDA USB I used several information from the openHAB community, mainly from [this topic](https://community.openhab.org/t/using-a-power-meter-sml-with-openhab/21923/1). -You need to get, modify and compile [libsml](https://github.com/dailab/libsml) from Github liked described [here](https://community.openhab.org/t/using-a-power-meter-sml-with-openhab/21923/1). I included my version of sml_server.c here as it differs a bit. +You need to get, modify and compile [libsml](https://github.com/volkszaehler/libsml) (the [old libsml](https://github.com/dailab/libsml) is dead!) from Github liked described [here](https://community.openhab.org/t/using-a-power-meter-sml-with-openhab/21923/1) and in my comment about the new repository [further down in the thread](https://community.openhab.org/t/using-a-power-meter-sml-with-openhab/21923/12). But I included my version of sml_server.c (pulled and edited 2018/01/19) here as it differs a bit. -* **sml.items**: Put this file into your */etc/openhab2/items*. +If you like to have a fixed device name (e.g. */dev/sml0* instead of */dev/ttyUSB0*) which cannot change (it may if you plug in another USB device), please use the *50-sml.rules* file and modify it. To find our the serial and maybe also different manufacturer of our device use the following command to display USB device properties: +``` +lsusb -v +``` +* **50-sml.rules**: Put this file into your */etc/udev/rules.d* directory and modify it for your specific setup. +* **sml.items**: Put this file into your */etc/openhab2/items* directory. * **sml.rules**: Put this file into your */etc/openhab2/rules* directory and modify it for your specific setup. In my case the compiled sml_server is in */home/openhabian/src/libsml/examples*. You also might need to adjust the device name, in my case it is: */dev/ttyUSB0* * **sml_server.c**: Put this file into the *libsml* subdirectory *src/libsml/examples* and compile *libsml* with this file, not the original one. \ No newline at end of file diff --git a/sml/sml.rules b/sml/sml.rules index dc5ac34..0b7a124 100644 --- a/sml/sml.rules +++ b/sml/sml.rules @@ -3,7 +3,7 @@ rule "EHZ" Time cron "0,30 */1 * * * ?" // this one cycles every 30 seconds. depends on your needs then // getting the payload of the meter - var String meter_payload = executeCommandLine("/home/openhabian/src/libsml/examples/sml_server /dev/ttyUSB0", 5000) // note the argument /dev/ttyUSB0 - your meter-device-id goes here + var String meter_payload = executeCommandLine("/home/openhabian/volkszaehler/libsml/examples/sml_server /dev/ttyUSB0", 5000) // note the argument /dev/ttyUSB0 or /dev/sml0 - your meter-device-id goes here // splitting the payload - first in lines, then getting counterStr and consumptionStr with delimiter "#" val lines = meter_payload.split('\n') val counterStr = lines.get(0).split('#').get(1) diff --git a/sml/sml_server.c b/sml/sml_server.c index f2e15c9..540a282 100644 --- a/sml/sml_server.c +++ b/sml/sml_server.c @@ -2,6 +2,9 @@ // DAI-Labor, TU-Berlin // // This file is part of libSML. +// Thanks to Thomas Binder and Axel (tuxedo) for providing code how to +// print OBIS data (see transport_receiver()). +// https://community.openhab.org/t/using-a-power-meter-sml-with-openhab/21923 // // libSML is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -22,20 +25,30 @@ #include #include #include +#include #include +#include #include #include #include +#include int serial_port_open(const char* device) { int bits; struct termios config; memset(&config, 0, sizeof(config)); + if (!strcmp(device, "-")) + return 0; // read stdin when "-" is given for the device + +#ifdef O_NONBLOCK + int fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK); +#else int fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY); +#endif if (fd < 0) { - printf("error: open(%s): %s\n", device, strerror(errno)); + fprintf(stderr, "error: open(%s): %s\n", device, strerror(errno)); return -1; } @@ -44,38 +57,24 @@ int serial_port_open(const char* device) { bits |= TIOCM_RTS; ioctl(fd, TIOCMSET, &bits); - tcgetattr( fd, &config ) ; + tcgetattr(fd, &config); // set 8-N-1 - config.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); + config.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR + | ICRNL | IXON); config.c_oflag &= ~OPOST; config.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); config.c_cflag &= ~(CSIZE | PARENB | PARODD | CSTOPB); config.c_cflag |= CS8; // set speed to 9600 baud - cfsetispeed( &config, B9600); - cfsetospeed( &config, B9600); + cfsetispeed(&config, B9600); + cfsetospeed(&config, B9600); tcsetattr(fd, TCSANOW, &config); return fd; } -//void transport_receiver(unsigned char *buffer, size_t buffer_len) { -// // the buffer contains the whole message, with transport escape sequences. -// // these escape sequences are stripped here. -// sml_file *file = sml_file_parse(buffer + 8, buffer_len - 16); -// // the sml file is parsed now - -// // read here some values .. - -// // this prints some information about the file -// sml_file_print(file); - -// // free the malloc'd memory -// sml_file_free(file); -//} - void transport_receiver(unsigned char *buffer, size_t buffer_len) { // Danke an Axel (tuxedo) für seinen Beispielcode int i; @@ -117,19 +116,25 @@ void transport_receiver(unsigned char *buffer, size_t buffer_len) { } } -int main(int argc, char **argv) { +int main(int argc, char *argv[]) { // this example assumes that a EDL21 meter sending SML messages via a // serial device. Adjust as needed. - // char *device = "/dev/cu.usbserial"; - char *device=argv[1]; - int fd = serial_port_open(device); + if (argc != 2) { + printf("Usage: %s \n", argv[0]); + printf("device - serial device of connected power meter e.g. /dev/cu.usbserial, or - for stdin\n"); + exit(1); // exit here + } - if (fd > 0) { - // listen on the serial device, this call is blocking. - sml_transport_listen(fd, &transport_receiver); - close(fd); + // open serial port + int fd = serial_port_open(argv[1]); + if (fd < 0) { + printf("Error: failed to open device (%s)\n", argv[1]); + exit(3); } + // listen on the serial device, this call is blocking. + sml_transport_listen(fd, &transport_receiver); + close(fd); + return 0; } -