;
+ close FILE;
+ $log .= "--- AVRDUDE ---------------------------------------------------------------------------------\n";
+ $log .= $logText;
+ $log .= "--- AVRDUDE ---------------------------------------------------------------------------------\n\n";
+ }
+ else {
+ $log .= "WARNING: avrdude created no log file\n\n";
+ }
+ DevIo_OpenDev($hash, 0, 0);
+ $log .= "$name opened\n";
+ }
+ return $log;
+ }
+ return undef;
+}
+
+
+# GET command
+#########################################################################
+sub ArduCounter_Get($@)
+{
+ my ( $hash, @a ) = @_;
+ return "\"set ArduCounter\" needs at least one argument" if ( @a < 2 );
+ my $name = shift @a;
+ my $attr = shift @a;
+
+ if(!defined($ArduCounter_gets{$attr})) {
+ my @cList = keys %ArduCounter_gets;
+ return "Unknown argument $attr, choose one of " . join(" ", @cList);
+ }
+
+ if(!$hash->{FD}) {
+ Log3 $name, 4, "$name: Get called but device is disconnected";
+ return ("Get called but device is disconnected", undef);
+ }
+
+ if (AttrVal($name, "disable", undef)) {
+ Log3 $name, 4, "$name: get called but device is disabled";
+ return;
+ }
+
+ if ($attr eq "info") {
+ Log3 $name, 3, "$name: Sending info command to device";
+ ArduCounter_Write( $hash, "s");
+ my ($err, $msg) = ArduCounter_ReadAnswer($hash, 'Next report in [0-9]+ Milliseconds');
+ # todo: test adding \n to regex to make sure we got the whole respose string
+
+ return ($err ? $err : $msg);
+ }
+
+ return undef;
+}
+
+
+######################################
+sub ArduCounter_HandleVersion($$)
+{
+ my ($hash, $line) = @_;
+ my $name = $hash->{NAME};
+ if ($line =~ / V([\d\.]+)/) {
+ my $version = $1;
+ if ($version < "1.7") {
+ $version .= " - not compatible with this Module version - please flash new sketch";
+ Log3 $name, 3, "$name: device reported outdated Arducounter Firmware - please update!";
+ }
+ $hash->{VersionFirmware} = $version;
+ Log3 $name, 4, "$name: device reported firmware $version";
+ }
+}
+
+
+#########################################################################
+sub ArduCounter_Parse($)
+{
+ my ($hash) = @_;
+ my $name = $hash->{NAME};
+ my $retStr = "";
+
+ my @lines = split /\n/, $hash->{buffer};
+ foreach my $line (@lines) {
+ # Log3 $name, 5, "$name: Parse line: $line";
+ if ($line =~ 'R([\d]+) C([\d]+) D([\d]+) T([\d]+)( N[\d]+)?( X[\d]+)?( F[\d]+)?(L [\d]+)?( A[\d]+)?')
+ {
+ my $pin = $1;
+ my $count = $2;
+ my $diff = $3;
+ my $time = $4;
+ my $deTime = ($5 ? substr($5, 2) / 1000 : "");
+ my $reject = ($6 ? substr($6, 2) : "");
+ my $first = ($7 ? substr($7, 2) : "");
+ my $last = ($8 ? substr($8, 2) : "");
+ my $avgLen = ($9 ? substr($9, 2) : "");
+
+ my $factor = AttrVal($name, "readingFactor$pin", AttrVal($name, "factor", 1000));
+ my $rcname = AttrVal($name, "readingNameCount$pin", "pin$pin");
+ my $rpname = AttrVal($name, "readingNamePower$pin", "power$pin");
+ my $lName = AttrVal($name, "readingNamePower$pin", AttrVal($name, "readingNameCount$pin", "pin$pin"));
+
+ my $chIdx = 0;
+ my $now = gettimeofday();
+ my $sTime = $now - $time/1000; # start of observation interval (~first pulse)
+ my $fSTime = FmtDateTime($sTime); # formatted
+ my $fSdTim = FmtTime($sTime); # only time formatted
+
+ my $eTime = $now; # now / end of observation interval
+ my $fETime = FmtDateTime($eTime); # formatted
+ my $fEdTim = FmtTime($eTime); # only time formatted
+ my $power = sprintf ("%.3f", ($time ? $diff/$time/1000*3600*$factor : 0));
+
+
+ Log3 $name, 4, "$name: Pin $pin ($lName) count $count (diff $diff) in " .
+ sprintf("%.3f", $time/1000) . "s" .
+ ((defined($reject) && $reject ne "") ? ", reject $reject" : "") .
+ ($avgLen ? ", Avg Len ${avgLen}ms" : "") .
+ ", result $power";
+ Log3 $name, 5, "$name: interval $fSdTim until $fEdTim" .
+ ($first ? ", First at $first" : "") .
+ ($last ? ", Last at $last" : "");
+
+
+ readingsBeginUpdate($hash);
+ if (AttrVal($name, "readingStartTime$pin", 0)) {
+ $hash->{".updateTime"} = $sTime;
+ $hash->{".updateTimestamp"} = $fSTime;
+ Log3 $name, 5, "$name: readingStartTime$pin specified: setting reading timestamp to $fSdTim";
+ Log3 $name, 5, "$name: set readings $rpname to $power, timeDiff$pin to $time and countDiff$pin to $diff";
+ readingsBulkUpdate($hash, $rpname, $power) if ($time);
+ $hash->{CHANGETIME}[$chIdx++] = $fSTime; # Intervall start
+ $hash->{".updateTime"} = $eTime;
+ $hash->{".updateTimestamp"} = $fETime;
+
+ readingsBulkUpdate($hash, $rcname, $count);
+ $hash->{CHANGETIME}[$chIdx++] = $fETime;
+
+ if (AttrVal($name, "verboseReadings$pin", 0)) {
+ readingsBulkUpdate($hash, "timeDiff$pin", $time);
+ $hash->{CHANGETIME}[$chIdx++] = $fETime;
+ readingsBulkUpdate($hash, "countDiff$pin", $diff);
+ $hash->{CHANGETIME}[$chIdx++] = $fETime;
+ readingsBulkUpdate($hash, "lastMsg$pin", $line);
+ $hash->{CHANGETIME}[$chIdx++] = $fETime;
+ if (defined($reject)) {
+ readingsBulkUpdate($hash, "reject$pin", $reject);
+ $hash->{CHANGETIME}[$chIdx++] = $fETime;
+ }
+ }
+ } else {
+ Log3 $name, 5, "$name: set readings $rpname to $power, timeDiff$pin to $time and countDiff$pin to $diff";
+ readingsBulkUpdate($hash, $rpname, $power) if ($time);
+ $eTime = time_str2num(ReadingsTimestamp ($name, $rpname, 0));
+ readingsBulkUpdate($hash, $rcname, $count);
+ if (AttrVal($name, "verboseReadings$pin", 0)) {
+ readingsBulkUpdate($hash, "timeDiff$pin", $time);
+ readingsBulkUpdate($hash, "countDiff$pin", $diff);
+ readingsBulkUpdate($hash, "lastMsg$pin", $line);
+ if (defined($reject)) {
+ readingsBulkUpdate($hash, "reject$pin", $reject);
+ }
+ }
+ }
+ readingsEndUpdate($hash, 1);
+
+ if ($deTime) {
+ if (defined ($hash->{'.DeTOff'}) && $hash->{'.LastDeT'}) {
+ if ($deTime >= $hash->{'.LastDeT'}) {
+ $hash->{'.Drift2'} = ($now - $hash->{'.DeTOff'}) - $deTime;
+ } else {
+ $hash->{'.DeTOff'} = $now - $deTime;
+ Log3 $name, 5, "$name: device clock wrapped (now $deTime, before $hash->{'.LastDeT'}). New offset is $hash->{'.DeTOff'}";
+ }
+ } else {
+ $hash->{'.DeTOff'} = $now - $deTime;
+ $hash->{'.Drift2'} = 0;
+ $hash->{'.DriftStart'} = $now;
+ Log3 $name, 5, "$name: Initialize clock offset to $hash->{'.DeTOff'}";
+ }
+ $hash->{'.LastDeT'} = $deTime;
+ }
+
+ my $drTime = ($now - $hash->{'.DriftStart'});
+ Log3 $name, 5, "$name: Device Time $deTime" .
+ #", Offset " . sprintf("%.3f", $hash->{'.DeTOff'}/1000) .
+ ", Drift " . sprintf("%.3f", $hash->{'.Drift2'}) .
+ "s in " . sprintf("%.3f", $drTime) . "s" .
+ ($drTime > 0 ? ", " . sprintf("%.2f", $hash->{'.Drift2'} / $drTime * 100) . "%" : "");
+
+ if (!$hash->{Initialized}) {
+ Log3 $name, 3, "$name: device reported count";
+ if (!$hash->{WaitForHello}) {
+ ArduCounter_SendHello("direct:$name");
+ }
+ $hash->{Initialized} = 1;
+ RemoveInternalTimer ("sendHello:$name");
+ }
+
+ } elsif ($line =~ /ArduCounter V([\d\.]+).?Hello/) { # response to h(ello)
+ Log3 $name, 3, "$name: device replied to hello, V$1";
+ ArduCounter_HandleVersion($hash, $line);
+ $hash->{Initialized} = 1;
+ ArduCounter_ConfigureDevice($hash) if ($hash->{WaitForHello});
+
+ delete $hash->{WaitForHello};
+ RemoveInternalTimer ("hwait:$name");
+ RemoveInternalTimer ("sendHello:$name");
+
+ } elsif ($line =~ /Status: ArduCounter V([\d\.]+)/) { # response to s(how)
+ $retStr .= "\n" if ($retStr);
+ $retStr .= $line;
+ ArduCounter_HandleVersion($hash, $line);
+
+ #todo: remove here?
+ delete $hash->{WaitForHello};
+ RemoveInternalTimer ("hwait:$name"); # dont wait for hello reply if already sent
+ RemoveInternalTimer ("sendHello:$name"); # Hello not needed anymore if not sent yet
+
+
+ } elsif ($line =~ /ArduCounter V([\d\.]+).?Started/) { # setup message
+ Log3 $name, 3, "$name: device sent setup message, V$1";
+ ArduCounter_HandleVersion($hash, $line);
+ $hash->{Initialized} = 1;
+ ArduCounter_ConfigureDevice($hash);
+
+ delete $hash->{WaitForHello};
+ RemoveInternalTimer ("hwait:$name"); # dont wait for hello reply if already sent
+ RemoveInternalTimer ("sendHello:$name"); # Hello not needed anymore if not sent yet
+
+ } elsif ($line =~ /V([\d\.]+).?Setup done/) { # old setup message
+ Log3 $name, 3, "$name: device is flashed with an old and incompatible firmware : $1";
+ Log3 $name, 3, "$name: please use set $name flash to update";
+ ArduCounter_HandleVersion($hash, $line);
+
+ } elsif ($line =~ /^M (.*)/) {
+ $retStr .= "\n" if ($retStr);
+ $retStr .= $1;
+ Log3 $name, 3, "$name: device: $1";
+ } elsif ($line =~ /^[\s\n]*$/) {
+ # blank line - ignore
+ } else {
+ Log3 $name, 3, "$name: unparseable message from device: $line";
+ }
+ }
+ $hash->{buffer} = "";
+ return $retStr;
+}
+
+
+
+#########################################################################
+# called from the global loop, when the select for hash->{FD} reports data
+sub ArduCounter_Read($)
+{
+ my ($hash) = @_;
+ my $name = $hash->{NAME};
+ my ($pin, $count, $diff, $power, $time, $reject, $msg);
+
+ # read from serial device
+ my $buf = DevIo_SimpleRead($hash);
+ return if (!defined($buf) );
+
+ $hash->{buffer} .= $buf;
+ my $end = chop $buf;
+ #Log3 $name, 5, "$name: Read: current buffer content: " . $hash->{buffer};
+
+ # did we already get a full frame?
+ return if ($end ne "\n");
+ ArduCounter_Parse($hash);
+}
+
+
+
+#####################################
+# Called from get / set to get a direct answer
+# called with logical device hash
+sub
+ArduCounter_ReadAnswer($$)
+{
+ my ($hash, $expect) = @_;
+ my $name = $hash->{NAME};
+ my $rin = '';
+ my $msgBuf = '';
+ my $to = AttrVal($name, "timeout", 2);
+ my $buf;
+
+ Log3 $name, 5, "$name: ReadAnswer called";
+
+ for(;;) {
+
+ if($^O =~ m/Win/ && $hash->{USBDev}) {
+ $hash->{USBDev}->read_const_time($to*1000); # set timeout (ms)
+ $buf = $hash->{USBDev}->read(999);
+ if(length($buf) == 0) {
+ Log3 $name, 3, "$name: Timeout in ReadAnswer";
+ return ("Timeout reading answer", undef)
+ }
+ } else {
+ if(!$hash->{FD}) {
+ Log3 $name, 3, "$name: Device lost in ReadAnswer";
+ return ("Device lost when reading answer", undef);
+ }
+
+ vec($rin, $hash->{FD}, 1) = 1; # setze entsprechendes Bit in rin
+ my $nfound = select($rin, undef, undef, $to);
+ if($nfound < 0) {
+ next if ($! == EAGAIN() || $! == EINTR() || $! == 0);
+ my $err = $!;
+ DevIo_Disconnected($hash);
+ Log3 $name, 3, "$name: ReadAnswer error: $err";
+ return("ReadAnswer error: $err", undef);
+ }
+ if($nfound == 0) {
+ Log3 $name, 3, "$name: Timeout2 in ReadAnswer";
+ return ("Timeout reading answer", undef);
+ }
+
+ $buf = DevIo_SimpleRead($hash);
+ if(!defined($buf)) {
+ Log3 $name, 3, "$name: ReadAnswer got no data";
+ return ("No data", undef);
+ }
+ }
+
+ if($buf) {
+ #Log3 $name, 5, "$name: ReadAnswer got: $buf";
+ $hash->{buffer} .= $buf;
+ }
+
+ my $end = chop $buf;
+ #Log3 $name, 5, "$name: Current buffer content: " . $hash->{buffer};
+ next if ($end ne "\n");
+
+
+ $msgBuf .= "\n" if ($msgBuf);
+ $msgBuf .= ArduCounter_Parse($hash);
+
+ #Log3 $name, 5, "$name: ReadAnswer msgBuf: " . $msgBuf;
+ if ($msgBuf =~ $expect) {
+ Log3 $name, 5, "$name: ReadAnswer matched $expect";
+ return (undef, $msgBuf);
+ }
+ }
+ return ("no Data", undef);
+}
+
+
+
+#
+# copied from other FHEM modules
+#########################################################################
+sub ArduCounter_Ready($)
+{
+ my ($hash) = @_;
+ my $name = $hash->{NAME};
+
+ if (AttrVal($name, "disable", undef)) {
+ return;
+ }
+
+ # try to reopen if state is disconnected
+ if ( $hash->{STATE} eq "disconnected" ) {
+ #Log3 $name, 3, "$name: ReadyFN tries to open"; # debug
+ DevIo_OpenDev( $hash, 1, undef );
+ if ($hash->{FD} && !$hash->{Initialized}) {
+ Log3 $name, 3, "$name: device not initialized yet, set timer to send h(ello";
+ my $now = gettimeofday();
+ RemoveInternalTimer ("sendHello:$name");
+ my $helloDelay = AttrVal($name, "helloSendDelay", 3);
+ InternalTimer($now+$helloDelay, "ArduCounter_SendHello", "sendHello:$name", 0);
+ }
+ return;
+ }
+
+ # This is relevant for windows/USB only
+ my $po = $hash->{USBDev};
+ if ($po) {
+ my ( $BlockingFlags, $InBytes, $OutBytes, $ErrorFlags ) = $po->status;
+ return ( $InBytes > 0 );
+ }
+}
+
+
+1;
+
+
+=pod
+=item device
+=item summary Module for consumption counter based on an arduino with the ArduCounter sketch
+=item summary_DE Modul für Strom / Wasserzähler auf Arduino-Basis mit ArduCounter Sketch
+=begin html
+
+
+ArduCounter
+
+
+ This module implements an Interface to an Arduino based counter for pulses on any input pin of an Arduino Uno, Nano or similar device like a Jeenode. The typical use case is an S0-Interface on an energy meter
+ Counters are configured with attributes that define which Arduino pins should count pulses and in which intervals the Arduino board should report the current counts.
+ The Arduino sketch that works with this module uses pin change interrupts so it can efficiently count pulses on all available input pins.
+
+ Prerequisites
+
+
+ -
+ This module requires an Arduino uno, nano, Jeenode or similar device running the ArduCounter sketch provided with this module
+
+
+
+
+
+ Define
+
+
+ define <name> ArduCounter <device>
+
+ <device> specifies the serial port to communicate with the Arduino.
+
+ The name of the serial-device depends on your distribution.
+ You can also specify a baudrate if the device name contains the @
+ character, e.g.: /dev/ttyUSB0@38400
+ The default baudrate of the ArduCounter firmware is 38400 since Version 1.4
+
+ Example:
+
+ define AC ArduCounter /dev/ttyUSB2@38400
+
+
+
+
+ Configuration of ArduCounter counters
+
+ Specify the pins where impulses should be counted e.g. as attr AC pinX falling pullup 30
+ The X in pinX can be an Arduino pin number with or without the letter D e.g. pin4, pinD5, pin6, pinD7 ...
+ After the pin you can define if rising or falling edges of the signals should be counted. The optional keyword pullup activates the pullup resistor for the given Arduino Pin.
+ The last argument is also optional and specifies a minimal pulse length in milliseconds. In this case the first argument (e.g. falling) means that an impulse starts with a falling edge from 1 to 0 and ends when the signal changes back from 0 to 1.
+
+ Example:
+
+ define AC ArduCounter /dev/ttyUSB2
+ attr AC factor 1000
+ attr AC interval 60 300
+ attr AC pinD4 falling pullup
+ attr AC pinD5 falling pullup 30
+ attr AC pinD6 rising
+
+ this defines three counters connected to the pins D4, D5 and D5.
+ D4 and D5 have their pullup resistors activated and the impulse draws the pins to zero.
+ For D4 every falling edge of the signal (when the input changes from 1 to 0) is counted.
+ For D5 the arduino measures the time in milliseconds between the falling edge and the rising edge. If this time is longer than the specified 30 milliseconds then the impulse is counted. If the time is shorter then this impulse is regarded as noise and added to a separate reject counter.
+ For pin D6 the ardiono counts every time when the signal changes from 0 to 1.
+ The ArduCounter sketch which must be loaded on the Arduino implements this using pin change interrupts,
+ so all avilable input pins can be used, not only the ones that support normal interrupts.
+
+
+
+
+ Set-Commands
+
+ - raw
+ send the value to the Arduino board so you can directly talk to the sketch using its commands.
+ This is not needed for normal operation but might be useful sometimes for debugging
+ - flash
+ flashes the ArduCounter firmware ArduCounter.hex from the fhem subdirectory FHEM/firmware
+ onto the device. This command needs avrdude to be installed. The attribute flashCommand specidies how avrdude is called. If it is not modifed then the module sets it to avrdude -p atmega328P -c arduino -P [PORT] -D -U flash:w:[HEXFILE] 2>[LOGFILE]
+ This setting should work for a standard installation and the placeholders are automatically replaced when
+ the command is used. So normally there is no need to modify this attribute.
+ Depending on your specific Arduino board however, you might need to insert -b 57600
in the flash Command.
+
+ - reset
+ reopens the arduino device and sends a command to it which causes a reinitialize and reset of the counters. Then the module resends the attribute configuration / definition of the pins to the device.
+
+
+
+ Get-Commands
+
+ - info
+ send a command to the Arduino board to get current counts.
+ This is not needed for normal operation but might be useful sometimes for debugging
+
+
+
+ Attributes
+
+ - do_not_notify
+ - readingFnAttributes
+
+ - pin.*
+ Define a pin of the Arduino board as input. This attribute expects either
+ rising
, falling
or change
, followed by an optional pullup
and an optional number as value.
+ If a number is specified, the arduino will track rising and falling edges of each impulse and measure the length of a pulse in milliseconds. The number specified here is the minimal length of a pulse and a pause before a pulse. If one is too small, the pulse is not counted but added to a separate reject counter.
+ - interval normal max min mincout
+ Defines the parameters that affect the way counting and reporting works.
+ This Attribute expects at least two and a maximum of four numbers as value. The first is the normal interval, the second the maximal interval, the third is a minimal interval and the fourth is a minimal pulse count.
+
+ In the usual operation mode (when the normal interval is smaller than the maximum interval),
+ the Arduino board just counts and remembers the time between the first impulse and the last impulse for each pin.
+ After the normal interval is elapsed the Arduino board reports the count and time for those pins where impulses were encountered.
+ This means that even though the normal interval might be 10 seconds, the reported time difference can be
+ something different because it observed impulses as starting and ending point.
+ The Power (e.g. for energy meters) is the calculated based of the counted impulses and the time between the first and the last impulse.
+ For the next interval, the starting time will be the time of the last impulse in the previous
+ reporting period and the time difference will be taken up to the last impulse before the reporting
+ interval has elapsed.
+
+ The second, third and fourth numbers (maximum, minimal interval and minimal count) exist for the special case when the pulse frequency is very low and the reporting time is comparatively short.
+ For example if the normal interval (first number) is 60 seconds and the device counts only one impulse in 90 seconds, the the calculated power reading will jump up and down and will give ugly numbers.
+ By adjusting the other numbers of this attribute this can be avoided.
+ In case in the normal interval the observed impulses are encountered in a time difference that is smaller than the third number (minimal interval) or if the number of impulses counted is smaller than the
+ fourth number (minimal count) then the reporting is delayed until the maximum interval has elapsed or the above conditions have changed after another normal interval.
+ This way the counter will report a higher number of pulses counted and a larger time difference back to fhem.
+
+ If this is seems too complicated and you prefer a simple and constant reporting interval, then you can set the normal interval and the mximum interval to the same number. This changes the operation mode of the counter to just count during this normal and maximum interval and report the count. In this case the reported time difference is always the reporting interval and not the measured time between the real impulses.
+ - factor
+ Define a multiplicator for calculating the power from the impulse count and the time between the first and the last impulse
+
+ - readingNameCount[0-9]+
+ Change the name of the counter reading pinX to something more meaningful.
+ - readingNamePower[0-9]+
+ Change the name of the power reading powerX to something more meaningful.
+ - readingFactor[0-9]+
+ Override the factor attribute for this individual pin.
+ - readingStartTime[0-9]+
+ Allow the reading time stamp to be set to the beginning of measuring intervals
+ - verboseReadings[0-9]+
+ create readings timeDiff, countDiff and lastMsg for each pin
+
+
+ Readings / Events
+
+ The module creates at least the following readings and events for each defined pin:
+ - pin.*
+ the current count at this pin
+ - power.*
+ the current calculated power at this pin
+ Most reading names can be customized with attribues and many more readings can be generated by setting the attribute verboseReadings[0-9]+ to 1.
+
+
+
+
+=end html
+=cut
+
diff --git a/fhem/contrib/98_ArduCounter.pm b/fhem/contrib/98_ArduCounter.pm
deleted file mode 100755
index 3fa4ab42f..000000000
--- a/fhem/contrib/98_ArduCounter.pm
+++ /dev/null
@@ -1,469 +0,0 @@
-############################################################################
-# $Id$
-# fhem Modul für Impulszähler auf Basis von Arduino mit ArduCounter Sketch
-#
-# This file is part of fhem.
-#
-# Fhem is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 2 of the License, or
-# (at your option) any later version.
-#
-# Fhem is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with fhem. If not, see .
-#
-##############################################################################
-# Changelog:
-#
-# 2014-2-4 initial version
-# 2014-3-12 added documentation
-# 2015-02-08 renamed ACNT to ArduCounter
-# 2016-01-01 added attributes for reading names
-#
-
-package main;
-
-use strict;
-use warnings;
-use Time::HiRes qw(gettimeofday);
-
-my %ArduCounter_sets = (
- "raw" => ""
-);
-
-my %ArduCounter_gets = (
- "info" => ""
-);
-
-
-#
-# FHEM module intitialisation
-# defines the functions to be called from FHEM
-#########################################################################
-sub ArduCounter_Initialize($)
-{
- my ($hash) = @_;
-
- require "$attr{global}{modpath}/FHEM/DevIo.pm";
-
- $hash->{ReadFn} = "ArduCounter_Read";
- $hash->{ReadyFn} = "ArduCounter_Ready";
- $hash->{DefFn} = "ArduCounter_Define";
- $hash->{UndefFn} = "ArduCounter_Undef";
- $hash->{GetFn} = "ArduCounter_Get";
- $hash->{SetFn} = "ArduCounter_Set";
- $hash->{AttrFn} = "ArduCounter_Attr";
- $hash->{AttrList} =
- 'pin.* ' .
- "interval " .
- "factor " .
- "readingNameCount[0-9]+ " .
- "readingNamePower[0-9]+ " .
- "do_not_notify:1,0 " . $readingFnAttributes;
-}
-
-#
-# Define command
-######################################################################### #
-sub ArduCounter_Define($$)
-{
- my ( $hash, $def ) = @_;
- my @a = split( "[ \t][ \t]*", $def );
-
- return "wrong syntax: define ArduCounter devicename\@speed"
- if ( @a < 3 );
-
- DevIo_CloseDev($hash);
- my $name = $a[0];
- my $dev = $a[2];
-
- $hash->{buffer} = "";
- $hash->{DeviceName} = $dev;
-
- my $ret = DevIo_OpenDev( $hash, 0, 0);
- return $ret;
-}
-
-
-sub ArduCounter_InitDev($)
-{
- my ($hash) = @_;
- my $name = $hash->{NAME};
-
- $hash->{STATE} = "Initialized";
- # now talking to Arduino device is possible
- DevIo_SimpleWrite( $hash, "int 60 300\n", 0 ); # default 1 to 5 minutes
-
- # now that the Arduino device reported "setup done"
- # send attributes to arduino device. Just call ArduCounter_Attr again,
- # now with state "Initialized"
- while (my ($attr, $val) = each(%{$attr{$name}})) {
- if ($attr =~ "pin|del|interval") {
- Log3 $name, 3, "$name: InitDev calls Attr with $attr $val";
- ArduCounter_Attr("set", $name, $attr, $val);
- }
- }
-}
-
-
-#
-# undefine command when device is deleted
-#########################################################################
-sub ArduCounter_Undef($$)
-{
- my ( $hash, $arg ) = @_;
- DevIo_CloseDev($hash);
- return undef;
-}
-
-# Wrap write to IODEV in case device is not initialized yet
-#########################################################################
-sub
-ArduCounter_Write($$)
-{
- my ( $hash, $line ) = @_;
- my $name = $hash->{NAME};
- if ($line) {
- Log3 $name, 4, "$name: Write called with $line";
- } else {
- Log3 $name, 5, "$name: Write called from timer, State = $hash->{STATE}";
- delete $hash->{TimerSet};
- }
- if ($hash->{STATE} eq "Initialized") {
- if ($hash->{WriteWaiting}) {
- DevIo_SimpleWrite( $hash, $hash->{WriteWaiting}, 0 );
- Log3 $name, 4, "$name: Write: wrote waiting commands to device";
- delete $hash->{WriteWaiting};
- }
- DevIo_SimpleWrite( $hash, "$line\n", 0 ) if ($line);
- } else {
- # Device not initialized yet - add to WaitingBuffer
- if ($line) {
- if ($hash->{WriteWaiting}) {
- $hash->{WriteWaiting} .= "$line\n";
- } else {
- $hash->{WriteWaiting} = "$line\n";
- }
- }
- if (!$hash->{TimerSet}) {
- InternalTimer(gettimeofday()+1, "ArduCounter_Write", $hash, 0);
- }
- $hash->{TimerSet} = 1;
- }
-}
-
-
-# Attr command
-#########################################################################
-sub
-ArduCounter_Attr(@)
-{
- my ($cmd,$name,$aName,$aVal) = @_;
- # $cmd can be "del" or "set"
- # $name is device name
- # aName and aVal are Attribute name and value
-
- my $hash = $defs{$name};
- Log3 $name, 4, "$name: Attr called with @_";
- if ($cmd eq "set") {
- if ($aName =~ 'pin.*') {
- if ($aName !~ 'pin([dD]?\d+)') {
- Log3 $name, 3, "$name: Invalid pin name in attr $name $aName $aVal";
- return "Invalid pin name $aName";
- }
- my $pin = $1;
- if ($aVal =~ '(rising|falling|change)( pullup)?') {
- my $opt = "";
- if ($aVal =~ 'rising') {$opt = "r"}
- elsif ($aVal =~ 'falling') {$opt = "f"}
- elsif ($aVal =~ 'change') {$opt = "c"}
- if ($aVal =~ 'pull') {$opt .= " p"}
- ArduCounter_Write( $hash, "add $pin $opt")
- if ($hash->{STATE} eq "Initialized");
- } else {
- Log3 $name, 3, "$name: Invalid value in attr $name $aName $aVal";
- return "Invalid Value $aVal";
- }
- } elsif ($aName eq "interval") {
- if ($aVal =~ '^(\d+) (\d+)$') {
- my $min = $1;
- my $max = $2;
- if ($min < 1 || $min > 3600 || $max < $min || $max > 3600) {
- Log3 $name, 3, "$name: Invalid value in attr $name $aName $aVal";
- return "Invalid Value $aVal";
- }
- ArduCounter_Write( $hash, "int $aVal")
- if ($hash->{STATE} eq "Initialized");
- } else {
- Log3 $name, 3, "$name: Invalid value in attr $name $aName $aVal";
- return "Invalid Value $aVal";
- }
- } elsif ($aName eq "factor") {
- if ($aVal =~ '^(\d+)$') {
- } else {
- Log3 $name, 3, "$name: Invalid value in attr $name $aName $aVal";
- return "Invalid Value $aVal";
- }
- }
- } elsif ($cmd eq "del") {
- if ($aName =~ 'pin.*') {
- if ($aName !~ 'pin([dD]?\d+)') {
- Log3 $name, 3, "$name: Invalid pin name in attr $name $aName $aVal";
- return "Invalid pin name $aName";
- }
- my $pin = $1;
- ArduCounter_Write( $hash, "rem $pin")
- if ($hash->{STATE} eq "Initialized");
- }
- }
- return undef;
-}
-
-
-# SET command
-#########################################################################
-sub ArduCounter_Set($@)
-{
- my ( $hash, @a ) = @_;
- return "\"set ArduCounter\" needs at least one argument" if ( @a < 2 );
-
- # @a is an array with DeviceName, SetName, Rest of Set Line
- my $name = shift @a;
- my $attr = shift @a;
- my $arg = join("", @a);
-
- if(!defined($ArduCounter_sets{$attr})) {
- my @cList = keys %ArduCounter_sets;
- return "Unknown argument $attr, choose one of " . join(" ", @cList);
- }
-
- if ($attr eq "raw") {
- DevIo_SimpleWrite( $hash, "$arg\n", 0 );
- }
-
- return undef;
-}
-
-# GET command
-#########################################################################
-sub ArduCounter_Get($@)
-{
- my ( $hash, @a ) = @_;
- return "\"set ArduCounter\" needs at least one argument" if ( @a < 2 );
-
- # @a is an array with DeviceName, GetName
- my $name = shift @a;
- my $attr = shift @a;
-
- if(!defined($ArduCounter_gets{$attr})) {
- my @cList = keys %ArduCounter_gets;
- return "Unknown argument $attr, choose one of " . join(" ", @cList);
- }
-
- if ($attr eq "info") {
- DevIo_SimpleWrite( $hash, "show\n", 0 );
- return "sent show command to get info - watch fhem log";
- }
-
- return undef;
-}
-
-
-#########################################################################
-# called from the global loop, when the select for hash->{FD} reports data
-sub ArduCounter_Read($)
-{
- my ($hash) = @_;
- my $name = $hash->{NAME};
- my ($pin, $count, $diff, $power, $time, $factor);
-
- # read from serial device
- my $buf = DevIo_SimpleRead($hash);
- return "" if ( !defined($buf) );
-
- $hash->{buffer} .= $buf;
- my $end = chop $buf;
- Log3 $name, 5, "$name: Current buffer content: " . $hash->{buffer};
-
- # did we already get a full frame?
- return if ($end ne "\n");
-
- readingsBeginUpdate($hash);
-
- my @lines = split /\n/, $hash->{buffer};
- foreach my $line (@lines) {
- if ($line =~ 'R([\d]+) C([\d]+) D([\d]+) T([\d]+)')
- {
- $pin = $1;
- $count = $2;
- $diff = $3;
- $time = $4;
- if (defined ($attr{$name}{factor})) {
- $factor = $attr{$name}{factor};
- } else {
- $factor = 1000;
- }
- Log3 $name, 4, "$name: Read match msg: Pin $pin count $count (diff $diff) in $time Millis";
- my $rcname = AttrVal($name, "readingNameCount$pin", "pin$pin");
- my $rpname = AttrVal($name, "readingNamePower$pin", "power$pin");
- readingsBulkUpdate($hash, $rcname, sprintf ("%.3f", $count) );
- if ($time) {
- readingsBulkUpdate($hash, $rpname, sprintf ("%.3f", $diff/$time/1000*3600*$factor) );
- }
- } elsif ($line =~ '(ArduCounter V[\d\.]+.?) Setup done') {
- readingsBulkUpdate($hash, "version", $1);
- Log3 $name, 3, "$name: Arduino reported setup done - sending init cmds";
- ArduCounter_InitDev($hash);
- } else {
- Log3 $name, 3, "$name: " . $line;
- }
- }
- readingsEndUpdate( $hash, 1 );
- $hash->{buffer} = "";
- return "";
-}
-
-#
-# copied from other FHEM modules
-#########################################################################
-sub ArduCounter_Ready($)
-{
- my ($hash) = @_;
-
- # try to reopen if state is disconnected
- return DevIo_OpenDev( $hash, 1, undef )
- if ( $hash->{STATE} eq "disconnected" );
-
- # This is relevant for windows/USB only
- my $po = $hash->{USBDev};
- my ( $BlockingFlags, $InBytes, $OutBytes, $ErrorFlags ) = $po->status;
- return ( $InBytes > 0 );
-}
-
-
-1;
-
-
-=pod
-=begin html
-
-
-ArduCounter
-
-
- This module implements an Interface to an Arduino based counter for pulses on any input pin of an Arduino Uno, Nano or similar device like a Jeenode. The typical use case is an S0-Interface on an energy meter
- Counters are configured with attributes that define which Arduino pins should count pulses and in which intervals the Arduino board should report the current counts.
- The Arduino sketch that works with this module uses pin change interrupts so it can efficiently count pulses on all available input pins.
-
- Prerequisites
-
-
- -
- This module requires an Arduino uno, nano, Jeenode or similar device running the ArduCounter sketch provided with this module
-
-
-
-
-
- Define
-
-
- define <name> ArduCounter <device>
-
- <device> specifies the serial port to communicate with the Arduino.
-
- The name of the serial-device depends on your distribution.
- You can also specify a baudrate if the device name contains the @
- character, e.g.: /dev/ttyUSB0@9600
-
- Example:
-
- define AC ArduCounter /dev/ttyUSB2@9600
-
-
-
-
- Configuration of ArduCounter counters
-
- Specify the pins where S0 interfaces are connected to as attr AC pinX rising pullup
- The X in pinX can be an Arduino pin number with or without the letter D e.g. pin4, pin5, pinD4, pinD6 ...
- After the pin ypu can define if rising or falling edges of the signals should be counted. The optional keyword pullup
- activates the pullup resistor for the given Arduino Pin.
-
- Example:
-
- define AC ArduCounter /dev/ttyUSB2@9600
- attr AC factor 1000
- attr AC interval 60 300
- attr AC pinD4 rising pullup
- attr AC pinD5 rising pullup
-
- this defines two counters connected to the pins D4 and D5, each with the pullup resistor activated.
- Impulses will be counted when the signal changes from 0 to 1.
- The ArduCounter sketch which must be loaded on the Arduino implements this using pin change interrupts,
- so all avilable input pins can be used.
-
-
-
-
- Set-Commands
-
- - raw
- send the value to the Arduino board so you can directly talk to the sketch using its commands.
- This is not needed for normal operation but might be useful sometimes for debugging
-
-
-
- Get-Commands
-
- - info
- send the internal command show
to the Arduino board to get current counts.
- This is not needed for normal operation but might be useful sometimes for debugging
-
-
-
- Attributes
-
- - do_not_notify
- - readingFnAttributes
-
- - pin.*
- Define a pin of the Arduino board as input. This attribute expects either
- rising
, falling
or change
as value, followed by
- on optional pullup
.
- - interval
- Define the reporting interval after which the Arduino board should hand over the count and the time from first to last impulse per pin.
- This Attribute expects two numbers as value. The first is the minimal interval, the second the maximal interval.
- Nothing is reported during the minimal interval. The Arduino board just counts and reemembers the time between the first impulse and the last impulse for each pin.
- After the minimal interval the Arduino board reports count and time for those pins where impulses were encountered.
- If no impulses were encountered, the pin is not reported until the second interval is over.
- The default intervals are 60 seconds as minimal time and 5 minutes as maximum interval.
- - factor
- Define a multiplicator for calculating the power from the impulse count and the time between the first and the last impulse
-
- - readingNameCount[0-9]+
- Change the name of the counter reading pinX to something more meaningful.
- - readingNamePower[0-9]+
- Change the name of the power reading powerX to something more meaningful.
-
-
-
- Readings / Events
-
- The module creates the following readings and events for each defined pin:
- - pin.*
- the current count at this pin
- - power.*
- the current calculated power at this pin
-
-
-
-
-=end html
-=cut
-