Serial Volt/Amp meter

Moderator: Mattk

pterodyne
Fresh Boarder
Fresh Boarder
Posts: 17
Joined: Tue May 01, 2012 7:46 pm

Serial Volt/Amp meter

Post by pterodyne » Fri Jun 08, 2012 8:50 pm

I posted this in another thread a while back, but no responses (wrong forum category maybe?). Im really just looking for a way to read my wattsview http://wattsview.com/ serial DC Amp/Volt meters. I have them attached to the linksys nslu2 via added USB ports and USB serial adapters. I have two of them (ttyUSB0 and ttyUSB1), one measures the voltage/current coming out of the solar array, the other measures the load side. I don't even care about the ohms law calculations that the microcontroller on the wattsview is doing. Just need Amps/Volts

The best I can really do so far is (from SSH):

Code: Select all

cat /dev/ttyUSB0
(or ttyUSB1) will output:

Code: Select all

00016.3V-00013.4A 00218.2W 00557.5WH
Things I have tried. Netcat. I can get a good read via HTTP but don't really know what I am doing. Also played with some scripts and attempted do do a user defined plugin. Using Awk I was able to rewrite the output, but still at a loss as to how to make a sensor.

Here are my awk examples. To rewrite the output so either just volts or amps are displayed

Volts

Code: Select all

awk 'BEGIN {FS = "" } ; { print $5$6$7$8$9 }' /dev/ttyUSB0
This outputs like: 13.7V. THere is probably a simpler way to rewrite the above output

Amps

Code: Select all

awk 'BEGIN {FS = "" } ; { print $14$15$16$17$18 }' /dev/ttyUSB0

And finally, my ham-handed attempt to write a script based on other examples here (Ideally I would rewrite each portion of 00016.3V-00013.4A 00218.2W 00557.5WH so I had V and A as separate sensors:

Code: Select all

#! /bin/sh
#
while :
do
  # pull data filter the needed string
  nc localhost 8082 /dev/stdout 2>/dev/null |  gawk 'BEGIN {FS=""} {print $5$6$7$8$9} {printf "data9 %d\n" }'
  # make sure output gets flushed
  sync
  # wait 300 seconds for next read
  sleep 30
done

Thanks again for any advice, Im getting by just ssh into meteohub and cat-ing the serial port, but Id love to log and chart without having to dump modify and bring into another program like excel. Meteohub seems like it would be a perfect platform for this with most tools needed already present and numerous ways to approach the design. I also have a wmr-100 which is reading the weather fine.. see my page at weather undergound at:

http://www.wunderground.com/weatherstat ... =KCOHARTS4

Thanks!

pinto
Junior Boarder
Junior Boarder
Posts: 23
Joined: Thu Apr 14, 2011 7:33 pm
Contact:

Re: Serial Volt/Amp meter

Post by pinto » Sun Jun 10, 2012 12:26 am

pterodyne wrote:The best I can really do so far is (from SSH):

Code: Select all

cat /dev/ttyUSB0
(or ttyUSB1) will output:

Code: Select all

00016.3V-00013.4A 00218.2W 00557.5WH

Code: Select all

#! /bin/sh
#
while :
do

# Read data from sensors
WhatWeRead=`cat /dev/ttyUSB0`

# Format reading
volt=`echo $WhatWeRead | cut -c -7`

echo data20 $(printf "%0.1f" $(echo "$volt"));
sync
sleep 30
done
do 'man cut' to see how you can isolate the values you need :)

pinto
Junior Boarder
Junior Boarder
Posts: 23
Joined: Thu Apr 14, 2011 7:33 pm
Contact:

Re: Serial Volt/Amp meter

Post by pinto » Sun Jun 10, 2012 9:10 am

This should do it if:
When you do a cat /dev/ttyUSB0
the result is:00016.3V-00013.4A 00218.2W 00557.5WH

Code: Select all

#! /bin/sh
#
while :
do

# Read data from sensors
WhatWeRead=`cat /dev/ttyUSB0`

# Format reading
volt=`echo $WhatWeRead | cut -c -7`
amp=`echo $WhatWeRead | cut -c 10-16`
watt=`echo $WhatWeRead | cut  -c 19-25`
wattH=`echo $WhatWeRead | cut -c 28-34`

echo data20 $(printf "%0.1f" $(echo "$volt"))"\n"data21 $(printf "%0.1f" $(echo "$amp"))"\n"data22 $(printf "%0.1f" $(echo "$watt"))"\n"data23 $(printf "%0.1f" $(echo "$wattH"));
sync
sleep 30
done

Attachments
Knipsel.PNG
the result
Knipsel.PNG (1.37 KiB) Viewed 6998 times

pterodyne
Fresh Boarder
Fresh Boarder
Posts: 17
Joined: Tue May 01, 2012 7:46 pm

Re: Serial Volt/Amp meter

Post by pterodyne » Sun Jun 10, 2012 9:30 am

Awesome! Thanks.. ill try to implement tomorrow

Thanks again!

pterodyne
Fresh Boarder
Fresh Boarder
Posts: 17
Joined: Tue May 01, 2012 7:46 pm

Re: Serial Volt/Amp meter

Post by pterodyne » Mon Jun 11, 2012 4:57 pm

I apologize for not understanding. Here is how I tried to implement:

copied your script to a file /home/meteohub/load.sh
chmod u+x /home/meteohub/load.sh

execute it
/home/metohub/load.sh
After about 30 seconds it comes back "Terminated"
Not sure what that means.

So I try to add as new plugin:
Path /home/meteohub/load.sh
all else to defaults

No new sensors are showing up.

doing a ps shows:
2066 root 636 S < /bin/sh /home/meteohub/load.sh
2067 root 392 S < cat /dev/ttyUSB0

so at least the plugin is loading the script. I also tried to just add it as an "additional sensor ID to be evaluated", but that sensor reads no data

Thanks again for any advice

pterodyne
Fresh Boarder
Fresh Boarder
Posts: 17
Joined: Tue May 01, 2012 7:46 pm

Re: Serial Volt/Amp meter

Post by pterodyne » Mon Jun 11, 2012 5:04 pm

Also, FWIW, doing this does work:

root@sioweather:~$ cat /dev/ttyUSB1 | cut -c -7
-00015.

-00015.

Ill have to play with cut to get exactly what I want extracted, but I see where you were going with that. WAY easier than awk to pick specific characters.

And I also think I understand what your script is doing. Just not sure how the sensors are created. I see the names being defined, but don't really understand how that part works.

pterodyne
Fresh Boarder
Fresh Boarder
Posts: 17
Joined: Tue May 01, 2012 7:46 pm

Re: Serial Volt/Amp meter

Post by pterodyne » Mon Jun 11, 2012 5:17 pm

ARRGGHH.. for a second I thought I had cracked it. I use nano on the meteohub shell, and forgot to do nano -w so the original attempt to implement the script was wrong. I fixed that, but still no new sensors are being implemented. I do see this in the meteohub log though:

logger (11.06.2012 09:13:01): data logger (version 4.9j, build 1219) started.
wmr928fulleval(11.06.2012 09:13:03): recomputation of weather data finished: 279599 records processed in 776 seconds (360 records per second)
logger (11.06.2012 09:13:05): connect station 0 (WMR-100/88 via USB HID).
logger (11.06.2012 09:13:05): connect station 1 (Plug-in via Plug-in).

pterodyne
Fresh Boarder
Fresh Boarder
Posts: 17
Joined: Tue May 01, 2012 7:46 pm

Re: Serial Volt/Amp meter

Post by pterodyne » Mon Jun 11, 2012 6:08 pm

Ok, still no luck, but I rechecked everything possible to make sure there were no syntax errors, or other mistakes.

SO I started working on my version of the script:

Code: Select all

#! /bin/sh
#
while :
do

# Read data from sensors
Load=`cat /dev/ttyUSB0`
Charge=`cat /dev/ttyUSB1`

# Format readings
lvolt=`echo $Load | cut -c 4-8`
lamp=`echo $Load | cut -c 13-17`
cvolt=`echo $Charge | cut -c 4-8`
camp=`echo $Charge | cut -c 13-17`

echo data20 $(printf "%0.1f" $(echo "$lvolt"))"\n"data21 $(printf "%0.1f" $(echo "$lamp"))"\n"data22 $(printf "%0.1f" $(echo "$cvolt"))"\n"data23 $(printf "%0.1f" $(echo "$camp"));
sync
sleep 30
done
Modified to add the second serial wattsview. For some reason it doesn't show cat /dev/ttyUSB1 in the process list, not sure if it can be executed the way Ive done it above..

pinto
Junior Boarder
Junior Boarder
Posts: 23
Joined: Thu Apr 14, 2011 7:33 pm
Contact:

Re: Serial Volt/Amp meter

Post by pinto » Mon Jun 11, 2012 8:45 pm

Note: I tested the script using a file, are you sure cat is not hanging when you use it with /dev/ttyUSB0 from within the script ?
I have never done that.
you can see what shellscripts are doing when you use

Code: Select all

#! /bin/sh -x
and start it manually

pterodyne
Fresh Boarder
Fresh Boarder
Posts: 17
Joined: Tue May 01, 2012 7:46 pm

Re: Serial Volt/Amp meter

Post by pterodyne » Mon Jun 11, 2012 9:29 pm

Here is what I get:

Code: Select all

root@sioweather:/home/meteohub$ sh -x ./load.sh
+ sh -x ./load.sh
+ :
+ cat /dev/ttyUSB0
Seems to hang. BUt I know I can call cat /dev/ttyUSB0 from a shell script as I have done it numerous times. Something to do with loading it into a variable first?

pterodyne
Fresh Boarder
Fresh Boarder
Posts: 17
Joined: Tue May 01, 2012 7:46 pm

Re: Serial Volt/Amp meter

Post by pterodyne » Wed Jun 13, 2012 5:00 pm

Since it seems to hang when in a variable (not sure why, other than you cannot cat /dev/ttyUSB0 more than once, The second session will just hang)
I have tried a different approach.

Using:

Code: Select all

#! /bin/sh

while :
do

(printf "%s" "data20 "; cat /dev/ttyUSB1| cut -c 4-8)

sync
sleep 30
done
I get this result, which is close, but the printf for data20 only displays the first time.

Code: Select all

./charge.sh
data20 015.0

015.0

014.8

015.0
If I can get data20 to repeat, then it would just be a matter of formatting so it is like this for /dev/ttyUSB0

Code: Select all

data20 013.3
data21 04.2
and for /dev/ttyUSB1

Code: Select all

data22 017.0
data23 15.2
The key is that the read on the serial port either needs to close for each value, or the formatting has to be done all in one read from the:

Code: Select all

 00012.6V-00002.1A 00026.7W 00514.0WH
I don't care about the watt hour and watt part of the reading. All I'm ever really looking at is Volts on the load side of things, and AMPS on the charge side of things.

pterodyne
Fresh Boarder
Fresh Boarder
Posts: 17
Joined: Tue May 01, 2012 7:46 pm

Re: Serial Volt/Amp meter

Post by pterodyne » Wed Jun 13, 2012 7:51 pm

Ok I think I cracked it, probably there is a simpler way to write it, but this seems to work:

Code: Select all

awk 'BEGIN {FS = "" } ; { print "data20 " $5$6$7$8 "\n" "data21 " $14$15$16$17 }' /dev/ttyUSB1
The result is:

Code: Select all

data20 15.5
data21 12.5
data20
data21
data20 15.8
data21 12.5
data20
data21
The blank lines seem to be ignored by meteohub. Next I need to figure out how to multiply this result by 100 as meteohub sensor page seems to show
Type # ID Name Last Signal Sensor Data
unknown 20 1 sec 0.12
unknown 21 1 sec 0.02
Learnin is fun! not really frustrating, glad to know what Ive learned so far.

pinto
Junior Boarder
Junior Boarder
Posts: 23
Joined: Thu Apr 14, 2011 7:33 pm
Contact:

Re: Serial Volt/Amp meter

Post by pinto » Wed Jun 13, 2012 9:07 pm

did find another way using grep:
-m 1 :stop after 1 match
and we know there is allways a "V" in the string

Code: Select all

#! /bin/sh
#
while :
do
WhatWeRead=$(grep -m 1 V /dev/ttyUSB0)

# Format reading
volt=`echo $WhatWeRead | cut -c -7`
amp=`echo $WhatWeRead | cut -c 10-16`
watt=`echo $WhatWeRead | cut  -c 19-25`
wattH=`echo $WhatWeRead | cut -c 28-34`
echo data20 $(printf "%0.1f" $(echo "$volt"))"\n"data21 $(printf "%0.1f" $(echo "$amp"))"\n"data22 $(printf "%0.1f" $(echo "$watt"))"\n"data23 $(printf "%0.1f" $(echo "$wattH"));
sync
sleep 30
done
This script is tested reading from /dev/ttyUSB0, reading from an Arduino that I programmed to send the string "00012.6V-00002.1A 00026.7W 00514.0WH".
So,if you want to give this a try ??

pterodyne
Fresh Boarder
Fresh Boarder
Posts: 17
Joined: Tue May 01, 2012 7:46 pm

Re: Serial Volt/Amp meter

Post by pterodyne » Wed Jun 13, 2012 9:44 pm

trying right now..

pterodyne
Fresh Boarder
Fresh Boarder
Posts: 17
Joined: Tue May 01, 2012 7:46 pm

Re: Serial Volt/Amp meter

Post by pterodyne » Wed Jun 13, 2012 9:51 pm

this is what I get from the command line

Code: Select all

root@sioweather:/home/meteohub$ sh -x load.sh
+ :
+ grep -m 1 V /dev/ttyUSB0
grep: invalid option -- m
BusyBox v1.2.1 (2007.12.25-11:17+0000) multi-call binary

Usage: grep [-ihHnqvsEABC] PATTERN [FILEs...]

Search for PATTERN in each FILE or standard input.

Options:
        -H      prefix output lines with filename where match was found
        -h      suppress the prefixing filename on output
        -i      ignore case distinctions
        -l      list names of files that match
        -L      list names of files that do not match
        -n      print line number with output lines
        -q      be quiet. Returns 0 if PATTERN was found, 1 otherwise
        -v      select non-matching lines
        -s      suppress file open/read error messages
        -c      only print count of matching lines
        -f      read PATTERN from file
        -e      PATTERN is a regular expression
        -F      PATTERN is a set of newline-separated strings
        -E      PATTERN is an extended regular expression
        -A      print NUM lines of trailing context
        -B      print NUM lines of leading context
        -C      print NUM lines of output context

+ WhatWeRead=
+ echo
+ cut -c -7
+ volt=
+ echo
+ cut -c 10-16
+ amp=
+ echo
+ cut -c 19-25
+ watt=
+ echo
+ cut -c 28-34
+ wattH=
+ echo
+ printf %0.1f
+ echo
+ printf %0.1f
+ echo
+ printf %0.1f
+ echo
+ printf %0.1f
+ echo data20 0.0\ndata21 0.0\ndata22 0.0\ndata23 0.0
data20 0.0\ndata21 0.0\ndata22 0.0\ndata23 0.0
+ sync
+ sleep 30

Post Reply