ArduCounter

Aus FHEMWiki
Zur Navigation springen Zur Suche springen
ArduCounter
Zweck / Funktion
Count pulses, calculate time between pulses and convert this to readings for e.g. power consumption of Energy meters
Allgemein
Typ Contrib
Details
Dokumentation Thema
Support (Forum) Sonstiges
Modulname 98_ArduCounter.pm
Ersteller StefanStrobel (Forum / Wiki)
Wichtig: sofern vorhanden, gilt im Zweifel immer die (englische) Beschreibung in der commandref!

The ArduCounter module works together with an arduino board connected to FHEM via usb that runs the provided ArduCounter.ino sketch to count impulses from energy meters with an S0 interface or similar devices in configurable intervals.

The arduino board counts impulses on every configured input pin using pin change interrupts. It reports the counted number of impulses per pin together with the time between the first and last impulse per pin.

The module converts this to readings like power consumption and it is not affected by delays or the load of your FHEM server. Counters are configured using attributes that define which Arduino pins should count pulses and in which intervals the counted numbers should be reported from the arduino borad to FHEM.

Availability

The module has been checked in to the contrib area. The corresponding arduino sketch can also be found under contrib in the subdirectory arduino/.

Prerequisites

This module requires Device::SerialPort or Win32::SerialPort module as well as an arduino uno, nano, jeenode or similar device that runs the ArduCounter sketch.

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 The default baudrate of the firmware is 38400 since Version 1.4 of the sketch.

Example:

define AC ArduCounter /dev/ttyUSB2

Configuration of the module

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 the signals to be counted start with rising or falling edges. 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.

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.

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.




Supported readings

pinX
the counted total of impulses for pin X
powerX
the result of (delta count) / (delta time) * factor.
the number of pulses counted during the last reporting interval diveded by the time between the first pulse and the last pulse in the interval, multiplied by a configurabe factor.

Attributes

do_not_notify
...
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 additional readings: timeDiff, countDiff and lastMsg for each pin