Personal tools

Difference between revisions of "S0 serial reader"

From PhotoVoltaic Logger new generation

Jump to: navigation, search
m (Energy meter)
m (KKoPV moved page S0 over USB reader to S0 serial reader)
 
(64 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Abstract ==
+
{{TOCright}}[[Category:Hardware]][[Category:Software]]
 
+
To read S0 impulses from e.g. energy meters, a program and a [[S0 reader script|script]] to store this into PVLng is included.
This is a part of the [http://github.com/k-ko/PVLng-scripts PVLng-scripts repository].
 
 
 
Scripts located in S0/
 
 
 
To read S0 impulses from e.g. energy meters, a program and a script to store this into PVLng is included.
 
  
 
== Hardware requirements ==
 
== Hardware requirements ==
Line 11: Line 6:
 
=== Energy meter ===
 
=== Energy meter ===
  
[[File:B-Watt.png|right]]
+
You need a energy meter with S0 interface, best for 3 phases.
  
You need a energy meter with S0 interface, best for 3 phases.
+
[[File:B-Watt.png|240px]]
  
 
[http://www.amazon.de/gp/product/B0032ON5YE/ref=oh_details_o06_s00_i00 B-Watt Swissnox 3x5(80A)]
 
[http://www.amazon.de/gp/product/B0032ON5YE/ref=oh_details_o06_s00_i00 B-Watt Swissnox 3x5(80A)]
Line 21: Line 16:
 
If your computer not more have a serial port (like mine), you need a USB serial adapter.
 
If your computer not more have a serial port (like mine), you need a USB serial adapter.
  
The best choice is an adapter with a FTDI / FT232RL chip set.
+
The best choice is an adapter with a FTDI or a FT232RL chip set.
  
 
[[File:DigitusDA70156.png]]
 
[[File:DigitusDA70156.png]]
  
 
[http://www.amazon.com/Digitus-DA-70156-USB-Seriell-Adapter/dp/B0030IT780/ Digitus DA-70156 USB to Serial Adapter, USB 2.0]
 
[http://www.amazon.com/Digitus-DA-70156-USB-Seriell-Adapter/dp/B0030IT780/ Digitus DA-70156 USB to Serial Adapter, USB 2.0]
 +
 +
During prototyping a gender changer (female/female) is '''recommended'''.
  
 
Connect your S0 port like this to your serial interface:
 
Connect your S0 port like this to your serial interface:
  
 
[[File:S0SerialPins.png]]
 
[[File:S0SerialPins.png]]
 
During prototyping is a gender changer (female/female) recommended.
 
  
 
Or you recycle an old serial mouse cable...
 
Or you recycle an old serial mouse cable...
Line 39: Line 34:
 
=== USB to TTL Serial Cable ===
 
=== USB to TTL Serial Cable ===
  
[[File:FTDI-FT232-USBtoTTL.jpg|300px|right]]
+
I found also a bit cheaper possibility, a USB to TTL Adapter with at least 6 pins in various forms.
  
I found also a bit cheaper possibility, a USB to TTL Adapter with 6 pins.
+
This cable is originally used for Arduino compatible boards but it works perfect also for S0 signals.
  
This cable is originally used for Arduino compatible boards but it works perfect also for S0 signals.
+
[[File:FTDI-FT232-USBtoTTL.jpg|280px]] [[File:FTDI-FT232-USBtoTTL-2.jpg|280px]]
 +
{{Note|'''''Please note:''''' You need a cable with '''6 pins''', not only '''4 pins''', although we need only 2 of them!}}
  
'''''Please note:'''''
+
Mostly I found these pin - wire associations:
* You need a cable with '''6 pins''', not only '''4 pins''', although we need only 2 of them!
 
  
{| class="wikitable"
+
<tt>
 +
{|
 
|-
 
|-
! Wire !! Signal
+
| Wire: || &nbsp; || Red  || &nbsp; || Black || &nbsp; || '''''White''''' || &nbsp; || Green || &nbsp; || '''''Yellow''''' || &nbsp; || Blue
 
|-
 
|-
| Red || 5V
+
| Signal: || || 5V || || GND || || '''''RXD''''' || || TXD || || '''''RTS''''' || || CTS
 +
|}
 +
</tt>
 +
 
 +
[[File:FTDI-Breakout1.jpg|240px]] [[File:FTDI-Breakout2.jpg|240px]] [[File:FTDI-Breakout3.jpg|240px]]
 +
 
 +
You can find them on [http://search.ebay.de/FTDI+USB+TTL+6+Pin eBay] for abt. $7, shipping from China to Germany took abt. 3 weeks.
 +
 
 +
{{Note|Double check to have the '''RTS''' signal included!}}
 +
 
 +
=== Test ===
 +
 
 +
<small>(adapted from [[Volkszähler:hardware/controllers/s0-an-rs232|Volkszähler Wiki]])</small>
 +
 
 +
You should test your setup, to make sure, the signals will be correct detected. A simple test can be done with [http://unixhelp.ed.ac.uk/CGI/man-cgi?stty stty].
 +
 
 +
* Configure your device (50 Baud, return direct every character)
 +
 
 +
# stty time 1 min 1 -icanon < /dev/'''ttyUSB0'''
 +
 
 +
* Read each character and create a timestamp:
 +
 
 +
# strace -ttt -e read cat < /dev/'''ttyUSB0''' > /dev/null
 +
 
 +
* With a bit more code you can better check the results
 +
 
 +
# (strace -ttt -e read cat < /dev/'''ttyUSB0''' > /dev/null) 2>&1 | \
 +
  awk 'NR==1{t0=$1; next}{t=$1; print 3600e3 /(t-t1)/'''2000''', t-t0, t-t1, $0; t1=t}'
 +
 
 +
Adjust the '''device''' and the '''2000''' to the impulse rate of '''your''' watt meter!
 +
 
 +
 
 +
{| class="wikitable"
 
|-
 
|-
| Black || GND
+
! Column !! Result
 
|-
 
|-
| '''White''' || '''RXD'''
+
| 1 || Power in watt
 
|-
 
|-
| Green || TXD
+
| 2 || Current time in seconds
 
|-
 
|-
| '''Yellow''' || '''RTS'''
+
| 3 || Time difference since last impulse
 
|-
 
|-
| Blue || CTS
+
| 4 || Original trace output with timestamp
 
|}
 
|}
 
You can find them on [http://search.ebay.de/FTDI+USB+TTL+6+Pin eBay], shipping from China to Germany took abt. 3 weeks.
 
  
 
== Channel definition ==
 
== Channel definition ==
Line 73: Line 99:
 
[[File:S0PowerSensorChannel.png]]
 
[[File:S0PowerSensorChannel.png]]
  
You should also define a [[Fixed name for USB device|fixed name]] for your device.
+
It is '''strongly recommended''' to define a [[Fixed name for USB device|fixed name]] for your device!
  
== Mode of operation ==
+
== Software ==
 +
 
 +
The software for storing the power data is splited into 2 parts
 +
 
 +
=== 1. Daemon ===
  
 
An energy meter with S0 interface sends depending of the actual power usage an amount of impulses per kilo watt hour.
 
An energy meter with S0 interface sends depending of the actual power usage an amount of impulses per kilo watt hour.
Line 81: Line 111:
 
A common frequency is 1000 or 2000 per kilo watt hour, or 800 as for our energy meter.
 
A common frequency is 1000 or 2000 per kilo watt hour, or 800 as for our energy meter.
  
The S0 binary listen the device and detects the time between these impulses. It converts them to power usage in watt.
+
The [https://github.com/KKoPV/PVLng-scripts/tree/master/S0/bin daemon] runs in Background and detects these impulses. It converts them to power usage in watt.
 
 
== Installation ==
 
  
$ cd S0/bin
+
These power values are written into log file.
$ ./compile
 
  
== Configuration ==
+
Compile the daemon for your system like this, it will also "strip" the binary afterwards.
  
Please refer to <tt>S0.conf.dist</tt> to configure your environment regarding interfaces and channels.
+
# cd S0/bin
 +
# ./compile
  
$ cp S0.conf.dist S0.conf
+
Afterwards just test it to make sure all works fine.
  
== Test ==
+
=== Test ===
  
To test your configuration, run
+
# ./S0
 +
 +
Usage: bin/S0 -d <device> [options]
 +
 +
Options:
 +
 +
        -d <device>        Serial port device, required
 +
        -r <resolution>    Impulses per kWh (kilo watt hour), default 1000
 +
        -f <format>        Output format, must fit printf(), default "%f"
 +
        -l <filename>      Log file, default /tmp/S0.log
 +
        -v                  Verbosity level: info
 +
        -vv                Verbosity level: debug
 +
        -F                  Don't daemonize, run in foreground
 +
        -h                  Show this help
  
$ ./S0.sh -tv S0.conf
+
If program runs in foreground, measuring data are written to <tt>stdout</tt> instead
 +
of log file.
  
== Run with cron ==
+
### Run for test very verbose in foreground
 +
# ./S0 -d /dev/usb-ftdi-1 -r 800 -Fvv
 +
[22-Mar 22:35:18][-d ]  Device    : /dev/usb-ftdi-1
 +
[22-Mar 22:35:18][-r ]  Resolution: 800
 +
[22-Mar 22:35:18][-f ]  Format    : %f
 +
[22-Mar 22:35:18][-l ]  Log file  : /tmp/S0.log
 +
[22-Mar 22:35:18][-v ]  Verbosity : 2
 +
[22-Mar 22:35:18][-F ]  Foreground: yes
 +
[22-Mar 22:35:18]      Device /dev/usb-ftdi-1 opened
 +
[22-Mar 22:35:18]      Start listening
 +
[22-Mar 22:35:18]      Press Ctrl+C to abort ...
 +
[22-Mar 22:35:18]      Skip 1st impulse
 +
[22-Mar 22:35:23]      1395524123.582 - 976.773414
 +
[22-Mar 22:35:28]      1395524128.188 - 976.974449
 +
[22-Mar 22:35:32]      1395524132.802 - 975.276735
 +
[22-Mar 22:35:37]      1395524137.393 - 980.170497
 +
[22-Mar 22:35:41]      1395524141.984 - 980.166868
 +
[22-Mar 22:35:46]      1395524146.588 - 977.398846
 +
[22-Mar 22:35:51]      1395524151.210 - 973.802969
 +
Ctrl+C
  
Edit your crontab with
+
'''1<sup>st</sup> value''' is the float timestamp and the '''2<sup>nd</sup> value''' is the actual watt usage.
  
$ crontab -e
+
The 1<sup>st</sup> impulse will be skipped and used as defined starting timestamp.
  
and add the following lines
+
=== 2. Shell script ===
  
# run S0 reader each 2 minutes
+
The [[S0 reader script]] run each minute via cron, analyses the log file and calculates the average.
*/2 * * * * /path/to/PVLng-sctipts/S0/S0.sh S0.conf
 
  
[[Category:Scripts]]
+
This average value will then pushed to the PVLng API.

Latest revision as of 14:42, 23 October 2016

To read S0 impulses from e.g. energy meters, a program and a script to store this into PVLng is included.

Hardware requirements

Energy meter

You need a energy meter with S0 interface, best for 3 phases.

B-Watt.png

B-Watt Swissnox 3x5(80A)

Serial port adapter

If your computer not more have a serial port (like mine), you need a USB serial adapter.

The best choice is an adapter with a FTDI or a FT232RL chip set.

DigitusDA70156.png

Digitus DA-70156 USB to Serial Adapter, USB 2.0

During prototyping a gender changer (female/female) is recommended.

Connect your S0 port like this to your serial interface:

S0SerialPins.png

Or you recycle an old serial mouse cable...

S0SerialMouse.png

USB to TTL Serial Cable

I found also a bit cheaper possibility, a USB to TTL Adapter with at least 6 pins in various forms.

This cable is originally used for Arduino compatible boards but it works perfect also for S0 signals.

FTDI-FT232-USBtoTTL.jpg FTDI-FT232-USBtoTTL-2.jpg

Please note: You need a cable with 6 pins, not only 4 pins, although we need only 2 of them!

Mostly I found these pin - wire associations:

Wire:   Red   Black   White   Green   Yellow   Blue
Signal: 5V GND RXD TXD RTS CTS

FTDI-Breakout1.jpg FTDI-Breakout2.jpg FTDI-Breakout3.jpg

You can find them on eBay for abt. $7, shipping from China to Germany took abt. 3 weeks.

Double check to have the RTS signal included!

Test

(adapted from Volkszähler Wiki)

You should test your setup, to make sure, the signals will be correct detected. A simple test can be done with stty.

  • Configure your device (50 Baud, return direct every character)
# stty time 1 min 1 -icanon < /dev/ttyUSB0
  • Read each character and create a timestamp:
# strace -ttt -e read cat < /dev/ttyUSB0 > /dev/null
  • With a bit more code you can better check the results
# (strace -ttt -e read cat < /dev/ttyUSB0 > /dev/null) 2>&1 | \
  awk 'NR==1{t0=$1; next}{t=$1; print 3600e3 /(t-t1)/2000, t-t0, t-t1, $0; t1=t}'

Adjust the device and the 2000 to the impulse rate of your watt meter!


Column Result
1 Power in watt
2 Current time in seconds
3 Time difference since last impulse
4 Original trace output with timestamp

Channel definition

Please put the device where your energy meter is connected to into the channel attribute like this:

S0PowerSensorChannel.png

It is strongly recommended to define a fixed name for your device!

Software

The software for storing the power data is splited into 2 parts

1. Daemon

An energy meter with S0 interface sends depending of the actual power usage an amount of impulses per kilo watt hour.

A common frequency is 1000 or 2000 per kilo watt hour, or 800 as for our energy meter.

The daemon runs in Background and detects these impulses. It converts them to power usage in watt.

These power values are written into log file.

Compile the daemon for your system like this, it will also "strip" the binary afterwards.

# cd S0/bin
# ./compile

Afterwards just test it to make sure all works fine.

Test

# ./S0

Usage: bin/S0 -d <device> [options]

Options:

        -d <device>         Serial port device, required
        -r <resolution>     Impulses per kWh (kilo watt hour), default 1000
        -f <format>         Output format, must fit printf(), default "%f"
        -l <filename>       Log file, default /tmp/S0.log
        -v                  Verbosity level: info
        -vv                 Verbosity level: debug
        -F                  Don't daemonize, run in foreground
        -h                  Show this help

If program runs in foreground, measuring data are written to stdout instead of log file.

### Run for test very verbose in foreground
# ./S0 -d /dev/usb-ftdi-1 -r 800 -Fvv
[22-Mar 22:35:18][-d ]  Device    : /dev/usb-ftdi-1
[22-Mar 22:35:18][-r ]  Resolution: 800
[22-Mar 22:35:18][-f ]  Format    : %f
[22-Mar 22:35:18][-l ]  Log file  : /tmp/S0.log
[22-Mar 22:35:18][-v ]  Verbosity : 2
[22-Mar 22:35:18][-F ]  Foreground: yes
[22-Mar 22:35:18]       Device /dev/usb-ftdi-1 opened
[22-Mar 22:35:18]       Start listening
[22-Mar 22:35:18]       Press Ctrl+C to abort ...
[22-Mar 22:35:18]       Skip 1st impulse
[22-Mar 22:35:23]       1395524123.582 - 976.773414
[22-Mar 22:35:28]       1395524128.188 - 976.974449
[22-Mar 22:35:32]       1395524132.802 - 975.276735
[22-Mar 22:35:37]       1395524137.393 - 980.170497
[22-Mar 22:35:41]       1395524141.984 - 980.166868
[22-Mar 22:35:46]       1395524146.588 - 977.398846
[22-Mar 22:35:51]       1395524151.210 - 973.802969
Ctrl+C

1st value is the float timestamp and the 2nd value is the actual watt usage.

The 1st impulse will be skipped and used as defined starting timestamp.

2. Shell script

The S0 reader script run each minute via cron, analyses the log file and calculates the average.

This average value will then pushed to the PVLng API.