Fieldlines.com: The Otherpower discussion board

Logged in users => User Diaries => Topic started by: Simen on April 06, 2013, 06:06:58 AM

Title: Monitoring the Tristar TS-xx with a PI.
Post by: Simen on April 06, 2013, 06:06:58 AM
I just bought a Tristar TS-60, because i've added 200W of solar panels to my bank, and blew my old Picaxe dumpload controller due to too much load. ;)

Since the Tristar comes with a rs232-port, i just had to try to communicate with it, and i wanted to use a Raspberry Pi to do it.
Now, i'm no programming genius, but with a good deal of searching the net, studying documentations, and some 'borrowing' of code, i actually managed to get data out of the Tristar; make sense of it, and put it on a 'live' webpage... ;)

http://mwlmf.net/monitor/monitor.py (http://mwlmf.net/monitor/monitor.py)
(Refreshes every 15 sec.)

The Tristar uses the Modbus protocol to communicate, and i found a free library for Python called pymodbus which installed on the Pi without problems. I'm using a 'Prolific pl2303' based usb to rs232 cable between the Pi and the Tristar, which the Pi accepted without any fuss.
The Pi uses around 3.5W, which i find acceptable for a full computer. ;) It runs directly from my bank via a 12V to 5V usb plug.

The Python script on the Pi gets data from the Tristar every 5 sec., and stores the data in a textfile directly on my webserver, where another Python script picks it up and creates a webpage every 15 sec.

I can publish the scripts here, if there are any interest. :)

Here's a pic of the Tristar and the Pi, and some other of my mess...
(https://www.fieldlines.com/proxy.php?request=http%3A%2F%2Fmwlmf.net%2Falbums%2Fsolcelle%2F100_3434.thumb.jpg&hash=7e175e3da191b0e27ef5c63be91bcc08d693495b)

Since my basement (where my 4x Rolls S-530 batteries are) keeps a cool temperature all year (10C-16C), i really should get the external temp sensor for the Tristar, but for now, i've set the Absorption stage to 14.8V to compensate for the cool temp.

But i do wonder about the behavior of the Tristar; it doesn't seem to get out of Absorption and over to Float at all?! It tapers down to approx. 5A charge, and stays there for the rest of the day. Now, i do have around 2A continuous load on the battery during daytime, but should that be enough for the Tristar not getting over to float? Or do i need a proper equalize? ;)
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: bob golding on April 06, 2013, 07:23:31 AM
thanks for that will have a play when i get time. as regards  the tristar never getting out of float, i have similar batteries to you the 460 amp hour ones. i have 8 of them wired for 24 volts. i have noticed they very rarely get out of absorption also. i normally have 750 watts of solar and 500 watts of wind turbine on the system. no wind at the moment as the turbine  needs a bit of work. i have a backup  genny to equalise the batteries every now and again. i haven't noticed any major problems over the last 4/5 years? cant remember when i brought them. but they are around that age. the genny is also broken at the moment as well so totally reliant on solar at the moment. lets hope the sun keeps shining :)
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Simen on April 06, 2013, 08:49:36 AM
To quote from the Rolls Battery Manual; ( http://support.rollsbattery.com/helpdesk/attachments/1073177 )

Quote
Absorption Charge
Arguably, the most important part of the charge cycle is the absorption charge. Since the bulk charge only recharges the battery bank to an 80% level, the absorption charge completes the charging cycle. Most chargers on the market have a timer that allows the user to adjust the duration for the required time to return the battery to full charge. In order to set the correct time, a simple calculation is required. With the help of the 20 AH capacity, you can figure out the remaining charge required for the battery bank.

As stated previously, the bulk charge brings the bank to an 80% charging level. The remaining 20% of the capacity is a function of time and current. The charger will maintain current level until the bulk set point has been reached, then the charger switches to the absorption timer. The current levels start decreasing as the internal resistance in the battery increases. Assume over the time of the absorption charge that 50% of your maximum charge current will be available (this is factored into the equation). 0.42 = (20%/50%) + 5%. 5% is added due to losses.

Absorption Charge time
Where : T = 0.42 x C /I
T = ABSORPTION CHARGE TIME
C = 20 hr RATED CAPACITY (of the Bank)
I = Charging Current ( 10% of C20 max)
0.42 = ( 20%/50%) + 5% (5% is added due to losses)

Example
2 Banks of 6 V S-530 models
20 hr rate = 400 x (2 strings) = 800 ah
I = 10% of 800ah = 80 amps. If charger is 60 amps max, 60 is used
T = 0.42 x 800/80 = 4.2 hrs OR T = 0.42 x 800/60 = 5.6 hrs

This would mean that these batteries require a considerable time to absorb properly.
The S-4000 series are a strange breed i think. For example, after a charge, they fall down to 12.5V-12.4V very fast under load, but they stays there for a long time...
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: bob golding on April 06, 2013, 10:38:28 AM
to add to the somewhat unusual charging characteristics they also seem to have a lower SG than most batteries as well. i just  make sure they are topped up with water and  test the SG occasionally never seems to get to fully charged state on a normal hydrometer. as long as they are all around the same SG they seem to be happy. well mine are anyway. i give them a  good charge from the genny every month or so. just to be safe.

sorry forgot to ask can you post some more details of your PI project. haven't played with one yet but have friends who have.
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Simen on April 06, 2013, 11:45:09 AM
sorry forgot to ask can you post some more details of your PI project. haven't played with one yet but have friends who have.

What do you want to know? About the Pi itself, or the communication part?

Spec etc. about the Pi can be found on the raspberrypi.org site. All one needs to get it going are the Pi itself and a usb-cable for powering the device, and a 2GB SD-card (or larger). If one want to use it as a standard Linux machine, the os offered at the Pi site works well, and have good support for programming in Python. There are also specific MediaPlayer os available, for example XBMC, which makes the Pi able to play full hd films etc.

Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Simen on April 06, 2013, 04:56:47 PM
About the logging; there was two packages i had to install on the Pi to get the communication up and running; 'python-serial' and 'python-pymodbus'

Now, i'm no Python programmer, so there are bits and pieces that's probably unnecessary, and not always neat; besides, it's not all my work... ;)
Code: [Select]
#!/usr/bin/env python

import time
a = 2
counter = 0

# import the server implementation
from pymodbus.client.sync import ModbusSerialClient as ModbusClient

# configure the client logging
import logging
logging.basicConfig()
log = logging.getLogger('./modbus.error')
log.setLevel(logging.ERROR)

# choose the serial client
client = ModbusClient(method='rtu', port='/dev/ttyUSB0', baudrate=9600, timeout=1)
client.connect()

# Define the State list
state = ['Start', 'Night Check', 'Disconnected', 'Night', 'Fault!', 'BulkCharge', 'Absorption', 'FloatCharge', 'Equalizing']

while a > 1:

#read the registers from logical address 0 to 30.
  rr = client.read_holding_registers(0,30,1)

#scaling
  batt_scale = 96.667
  i_scale = 66.667
  array_scale = 139.15

#the stuff we want
  battsV = ( rr.registers[9] * 96.667) / 32768
  chargeI = ( rr.registers[11] * float(i_scale )) / (2**15)
  arrayV = ( rr.registers[10] * float(array_scale )) / (2**15)
  statenum = rr.registers[27]
  pwmDuty = rr.registers[28] /2.55
  regTemp = rr.registers[14]
  powerIn = battsV * chargeI
  ampH = ( rr.registers[17] + rr.registers[18] ) * 0.1

 

#debug
#  print "Battery Voltage: %.2f" % battsV
#  print "Battery Charge Current: %.2f" % chargeI
#  print "Array Voltage: %.2f" % arrayV
#  print "PWM Duty: %.2f" % pwmDuty
#  print "Control State: %.2f" % state
#  print "Controller Temp: %.2f" % regTemp
#  print "Power in: %.2f" % powerIn
#  print "Ah: %.2f" % ampH

  out = "V:%.2f" % battsV + " A:%.3f" % chargeI + " AV:%.2f" % arrayV + " D:%.2f" % pwmDuty + " S:" + state[statenum] + " CT:%.2f" % regTemp + " P:%.2f" % powerIn + " AH:%.2f" % ampH + "\n"
  fil = open('/mnt/dumpdata.txt', 'w')
  fil.write(out)
  fil.close()
  counter += 1

  if counter == 24:
    tid = time.ctime()
    out = str(tid) + ",%.2f" % battsV + ",%.3f" % chargeI + ",%.2f" % arrayV + ",%.2f" % pwmDuty + "," + state[statenum]  + ",%.2f" % regTemp + ",%.2f" % powerIn + ",%.2f" % ampH + "\n"
    fil = open('/mnt/dumplog.csv', 'a')
    fil.write(out)
    fil.close()
    counter = 0

  time.sleep(5)

# close the client
client.close()

print "done"

In short, the code above opens the comport, gets the data, process and format the numbers, and writes it out to a textfile (dumpdata.txt) every 5 second. It also append the current data to another textfile i called 'dumplog.csv' every 2 minute.
The folder '/mnt' where i saves the files, are a network mount to a folder on my webserver on another machine.
The data in the textfile looks like this:

# V:14.80 A:5.93 AV:16.43 D:75.29 S:Absorption CT:18.00 P:87.79

On my webserver, i've got the script that creates the webpage:
Code: [Select]
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# enable debugging
import sys
import StringIO
import time

# Set http Headere...
sys.stdout.write("Content-type: text/html\r\n\r\n")

# Les inn fila
file = open('./dumpdata.txt')
rawData = file.read()
file.close()

# Klargjør output variabler
v = 0.00
a = 0.00
av = 0.00
d = 0.00
s = 0
ct = 0.00
p = 0.00
ah = 0.00

# Splitt linja i en liste med ett element for hver space
elements = rawData.split()

# loop gjennom alle elementer i lista
for element in elements:

        # Split enkeltelement pÃ¥ ":"
        value = element.split(":")

        #Sjekk hvilket element vi jobber med, og do the right thing for hver av dem
        if value[0] == "V":
                v = value[1]
        if value[0] == "A":
                a = value[1]
        if value[0] == "AV":
                av = value[1]
        if value[0] == "D":
                d = value[1]
        if value[0] == "S":
                s = value[1]
        if value[0] == "CT":
                ct = value[1]
        if value[0] == "P":
                p = value[1]
        if value[0] == "AH":
                ah = value[1]

tid = time.ctime()

# Sett opp html documentet
sys.stdout.write("<html>")

sys.stdout.write("<head>")
sys.stdout.write("<meta http-equiv='refresh' content='15' />")
sys.stdout.write("<title>BatteriStatus</title>")
sys.stdout.write("<link rel='stylesheet' type='text/css' href='styles.css'>")
sys.stdout.write("</head>")

sys.stdout.write("<body>")
sys.stdout.write("Status for my 12V, 1060Ah battery bank (4x Rolls S-4000 6V, 530Ah). <br>")
sys.stdout.write("Charged with approx. 700W of solar panels, connected to a <b>Tristar TS-60 controller</b>.<br><br>")

# Spytt ut en HTML tabell.
sys.stdout.write("<table border='1'>")
sys.stdout.write("<tr>")
sys.stdout.write("<th>Volt</th><th>Amp</th><th>Array</th><th>PWMDuty</th><th>Control State</th><th>Controller Temp</th><th>Power</th>")
sys.stdout.write("</tr>")
sys.stdout.write("<tr>")
sys.stdout.write("<td>" + str(v) + "V</td>")
sys.stdout.write("<td>" + str(a) + "A</td>")
sys.stdout.write("<td>" + str(av) + "V</td>")
sys.stdout.write("<td>" + str(d) + "%</td>")
sys.stdout.write("<td>" + str(s) + "</td>")
sys.stdout.write("<td>" + str(ct) + "&deg;C</td>")
sys.stdout.write("<td>" + str(p) + "W</td>")

sys.stdout.write("</tr>")
sys.stdout.write("</table>")

sys.stdout.write(str(tid))
sys.stdout.write("<br> <br>")

sys.stdout.write("Total Ah since 3 April 2013: <b>" + str(ah) + "Ah</b>")
sys.stdout.write("<br> <br>")

sys.stdout.write("Cornelius. ;) <br>")

sys.stdout.write("</body>")
sys.stdout.write("</html>")

It reads the Dumpdata.txt file and splits the line of data into single fields and assign them to variables, and builds up a simple webpage with the data in a table.

As mentioned in the first post; i burned out my previous controller - or rather; i burned the fets, but before i did so, i used the Pi to get data from the PicAxe, and write it in similar manner, but the code to get the data was much simpler. (less data too ;) )
The tricky part with the picaxe was to get the picaxe's usb-cable to work with the Pi, but there was a simple solution, once i found it. ;) (The Pi recognized the usb to serial chip used in the cable, but not the producer (or something like that), so the solution was some definitions in a startup config file.)

That covers the most of it, i think. :)
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: bob golding on April 06, 2013, 05:21:02 PM
thanks for that most helpful.
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: OperaHouse on April 06, 2013, 06:44:39 PM
I think a lot of people feel cosmic angst over that battery condition.  That condition is a myth reserved for backup systems that hardly get used.  Unless the generating side is overly designed a lot of us don't get much out of bulk charge.
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: ghurd on April 06, 2013, 07:33:00 PM
I think a lot of people feel cosmic angst over that battery condition.  That condition is a myth reserved for backup systems that hardly get used.  Unless the generating side is overly designed a lot of us don't get much out of bulk charge.

I 'THINK' I agree with that.
With PV or wind, often can not get there in a day with available resources anyway.
What is 'perfect on paper' is not always best, or possible with available resources.
G-
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: SparWeb on April 06, 2013, 07:46:41 PM
Simen,
Fascinating work.
I just don't seem to make any headway with Python.  I've been struggling for months to make a satisfactory program to read from a device on a serial port and display the data; I just seem to take one step forward and two steps back.  Python is definitely a programer's language.  Even the tutorials are so thoroughly soaked with notes for developpers that a beginner like me can't read them.  I'm still more comfortable with BASIC.

I'm looking forward to reading through the rest of your work.
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: SparWeb on April 06, 2013, 07:57:38 PM
Well, I should be using Py-modbus shouldn't I?  It would certainly be easier than building the same commands from Py-serial.
Thanks for posting the code!  It's full of ideas that will help me, perhaps even rescue my abandoned Python code.
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Simen on April 07, 2013, 01:35:26 AM
Sparweb;
Do note that you can only use Pymodbus when talking to devices that speaks the modbus protocol. :)

I am also a Basic guy; been that since mid-80's. ;) When they thought that Object Oriented Programming (OOP) was a good way to go, i fell off the ride... I do see the benefits of OOP, but i still struggle with it... ;)

For interested; here's the python code i used to read data from the Picaxe;
Code: [Select]
import serial
import time
ser = serial.Serial("/dev/ttyUSB0", 4800, xonxoff=False, rtscts=False, dsrdtr=False,  timeout=6)
ser.open()
a = 2
counter = 0
ser.flushInput()
while a > 1:
    tekst= ser.readline()
    out = str(tekst)
    out = out.replace("b'", "")
    out = out.replace("\\r\\n'", "")
    fil = open('/mnt/dumpdata.txt', 'w')
    fil.write(out)
    ser.flushInput()
    fil.close()
    counter += 1
    if counter == 24:
      currtime = time.ctime()
      log = open('/mnt/dumplog.txt', 'a')
      outlog = str(currtime) + ' ' + out + '\n'
      log.write(outlog)
      log.close()
      counter = 0

No remarks in the code though, but that code are my work, so ask... ;)
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: bob golding on April 07, 2013, 08:11:35 AM
I think a lot of people feel cosmic angst over that battery condition.  That condition is a myth reserved for backup systems that hardly get used.  Unless the generating side is overly designed a lot of us don't get much out of bulk charge.

I 'THINK' I agree with that.
With PV or wind, often can not get there in a day with available resources anyway.
What is 'perfect on paper' is not always best, or possible with available resources.
G-

as i said in an earlier post, after 4/5 years apart from  reduced output in the winter due to the cold conditions the battery bank is in i haven't noticed any significant degradation in my bank. of course since i installed it  lots has changed anyway.  i have  rebuilt the wind turbine with fewer magnets as they have disappeared due to rust. changed the air gap changed the blades,and last year added  500 watts of solar.
 my loads have also reduced due to lower power lighting as well. the only way i can tell the  health of the batteries is to test them with a hydrometer to check they are all in more or less the same state. since i have added the solar the tri star controller seems to stay in the charging state,red and yellow lights on,  a lot more that when i just had the wind turbine. this makes sense as the charging is more even. but trying to measure all this is not easy, hence the interest in simens  data logger.
 think as long as you don't let the water level get too low there is not much to worry about with those batteries,.
as long as you have some way of giving an equalization charge every now and again. living off grid without a backup  could be worry though. i don't think they like just being charged at low current  constantly without a boost every now and again. the rolls manual says they should have a eq charge monthly. i do mine around every 6 weeks or so. i have a 50 amp charger running off a generator and boost charge them for around 6 hours. even then to get the dump load to kick in takes along time. as i have 2 banks of 24 volts i sometimes just charge  the banks separably if the batteries have got low enough to trip the inverter. this doent happen much with bothe the wind turbine and solar both working. usually happens when we get days  and days of sea fog no sun no wind. 
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: SparWeb on April 07, 2013, 09:12:46 PM
Simen,
More code:  Thanks again.

I see a pattern in what you're doing already, and if there's any knowledge you can share on the subject of USB serial communications versus COM ports I'd appreciate anything you can tell me.
When I was attempting to read about it on StackOverflow or from the Python tutorials, I just got lost.  They were not talking to my level of knowledge.

All of the Python programs I've tried to write  used this to open the serial port:

Code: [Select]
    ser1 = serial.Serial(port='COM5',baudrate=115200, timeout=0)
    ser1.flush()
    line = ser1.readline()                          # Read one line from Serial port (accelerometer)
    ser1.close

This is an excerpt of code, with a lot of other business stripped out, just to show you just the commands concerned with the serial port.  The device connected to the COM 5 port is sending numbers every 1/2 second, always in hexadecimal format, always with a carriage return between them.  I've checked the code a hundred times and the hex to decimal conversion is correct, and there are even provisions to strip extra characters in case the data stream has noise or is interrupted.

Is there anything you can see there that would cause the data read from the port to be corrupted?  Half the time when I run the program it works and half the time it fails.  This is where I was getting frustrated.

BTW: I now have my programs working FLAWLESSLY in BASIC today, after only a few days of programming, and starting from scratch too. 
No pressure on you to solve this thing I have against python, and if you throw your hands up, then welcome to the club!

Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Simen on April 08, 2013, 01:58:20 AM
I agree with you on the pyserial documentation; they threw out examples without explaining properly what happens etc. Not nice to beginners...  ???

Instead of 'serl.flush()', try 'serl.flushInput()'. flushInput() clears the comports inputbuffer; making sure the data you read are fresh from the device (flush() doesn't. ;) )

Just for fun; before you try the flushInput(), insert the following code after your 'flush()':
Code: [Select]
x = serl.inWaiting()
print("Number of data in queue; " + x)

Run your program, and then change the flush() to flushInput() to see the difference. :) (The buffer would probably contain a good deal of data if you doesn't use the flushInput(), especially if you do this inside a loop.

That flush()/flushInput() caused me a headache too... ;)

Do you get a complete set of data before it separates them with a carriage return?

The 'readLine()' reads everything from the port until it receives a '\n' (newline)
If \n doesn't terminate a complete dataset, and you know the number of bytes you want to read, you could use 'read([number of bytes])' in blocking mode (timeout = 1 in opening the port). In non-blocking more (timeout = 0), it would just read the specified number of bytes from the buffer.) - (I found the latter one more unstable...)


Edit;
Make sure that the baudrate are correct; 115200 are pretty fast, and devices such as controllers use often lower rates. The Tristar for example, uses 9600 baud; the Picaxe 08M2 use 4800.

Edit2;
with the flushInput(), you might get an incomplete dataset when you read it the first time, but next time should be complete. This would only be a problem if you read the dataset only once, and not continuously. If you need the data only once, you could read the data twice and discard the first set.
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Bruce S on April 08, 2013, 08:35:02 AM
Just received the first raspberry pi :) looks interesting.
This post helps too! Glad I'm not the only who got lost in the python world!
Basic, I can work with
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: SparWeb on April 09, 2013, 01:08:02 AM
Simen,

Thanks for all the advice.

I will try the flushinput() to start.  You would think "flush()" alone would flush the serial buffer if it's appended to "ser1.flush()", but this is Python after all...   some eager beaver wrote a separate function for that.

I've encountered the process of "read it once, throw that away, read it a second time".  That is a technique recommended in the PICAXE manual when using readADC commands.  Once again our experience overlaps, but you've made discoveries that I haven't.

The device on the COM port is an Arduino, so yes, it's zipping along at 115200 baud.  Because I'm using an Arduino, I have control over the format of the data it sends out.  Currently I have it sending the 3 values of acceleration from an accelerometer, x, y, z (of course).  The values are separated by commas and followed by a CR.  I was able to write code to parse that.  ...when the data actually comes in without errors, the program could parse it, I should say.  When errors crept in, it became harder to deal with the mess.  I'd much rather work on having clean input data!

I also have a device that I want to make work in conjunction with the Arduino, bringing the data into two serial ports at the same time.  It also operates at 115200 baud, and it's the one sending in the hex formatted data.  Making the two devices feed data into the computer simultaneously is the ultimate goal.

Last night I wrote a bunch more BASIC code for the alternate program, and got more of the user interface to work.  That was a much more rewarding process, where I read the documentation (JBasic, if you're curious) organized the commands that I'd need to format the new graphics window, and the controls to go into it, layed them out in the existing program (which is already doing the serial read inputs and calculations), and bang: it worked on THE FIRST TRY.  I went to bed early last night, feeling very smug.

I'll get back to Python, I promise, if only to show it won't win over me. 
(It will probably perform much better than a free BASIC compiler will, if I finish the code)

A little inspiration from you is probably all I need.

-Steve

PS a teacher in my son's elementary school is experimenting with a Raspberry PI.  His current project is to give it control over a remote control car.  He and I snooped on the RC signals with my pocket-oscilloscope, and he's now able to transmit signals directly from the PI, which he's equipped with a small antenna.  Later he wants to try lots of things like pre-programmed courses etc.

Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Simen on April 09, 2013, 01:51:06 AM
The pyserial documentation are a bit fuzzy for us novices when it explain flushInput and flush; the flushinput flushes the serial buffer, while the flush command flushes 'file-like objects'. I think the 'readline()' are a file-like object? So, not the same as the serial input buffer.

Regarding the baud speed; when sending data at 115200 baud, there's a greater chance for errors creeping into the datastream, due to noise/interference from the environment through the cable etc. If you are plagued with errors/noise, and have control over the sending unit; try to lower the baudrate. Since you send only a few bytes of data, a lower speed at, let's say 19200 would still give you the data fast enough to receive and process the data, and be ready for next set of data.

If Jbasic does the job for you; why not stick to it? :) I chose Python because the full environment was already installed in the Pi, and i wanted to get a fingernail inside a new language. ;) My 'old' language are Clarion's Basic; Clarion is a database oriented RAD tool. :)
My brother are a professional programmer, and he uses whatever language that suits the job; often mixing 4-5 different langages in one job/program. I'm a bit jealous of him about that.  :P
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Simen on April 11, 2013, 04:36:12 AM
I suddenly saw the need to add some error-checking in my code... :o

Since the script writes the data received from the Tristar to a file in a folder which is mapped to another computer, there's a weakness if the network connection fails. The script would just stop and exit if there was an error writing the files.

The Pi are connected to a switch which get its power from a small 150W inverter, and that inverter cuts the power if the voltage in are over 15.1V, which happens now and then here, due to temperature compensated charging and equalizing.

So, i found the 'try:' - 'except:' - 'else:' methods useful.

Here's the original codepiece without the error checking.:
Code: [Select]
    fil = open('/mnt/dumpdata.txt', 'w')
    fil.write(out)
    fil.close()
    counter += 1

And here are the same, but with error checking - and logging...

Code: [Select]
  try:
    fil = open('/mnt/dumpdata.txt', 'w')
    fil.write(out)
  except IOError, Argument:
    tid = time.ctime()
    Err = tid + " *** Could not write to DumpDATA.txt: " + str(Argument)
    ErrLog = open('./errorlog', 'a')
    ErrLog.write(Err)
    ErrLog.close()
  else:
    fil.close()
    counter += 1

The code traps the error, and adds an entry into a local 'errorlog' file, so one can see what the error was remotely without having a screen connected.
Here's an example of an error written in the 'errorlog' file:
Code: [Select]
Thu Apr 11 10:12:50 2013 *** Could not write to DumpDATA.txt: [Errno 112] Host is down: '/mnt/dumpdata.txt'

Just wanted you to know... ;)
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: SparWeb on April 11, 2013, 02:47:28 PM
Not dealing with errors is the #1 problem that programs get into trouble, so good thing you've found a way to deal with shut-downs.
Last night I discovered that my code needs a contingency for the rare event that the load goes negative.
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: DamonHD on April 11, 2013, 03:01:14 PM
Dealing gracefully with the unexpected, ie not being too brittle in strange cases, is what I aim for in anything that has to "just work" for a while.

Rgds

Damon
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Simen on April 11, 2013, 03:56:46 PM
I see there's other places in the code that could do with some error-trapping... I do have a tendency to just wanting to get the code to work, and not make it 'safer' until something fails... ::) But then again; Python are new to me, so i'm learning as i go. ;)

One thing would be to include a timestamp with the data, and use that timestamp on the generated webpage. The time currently displayed on the webpage are when the webpage was generated, not when the data was retrieved, which would be more interesting. ;)

Since this is a program that runs unattended on a Linux machine without a screen or keyboard; only network access, it's important that it can take care of itself, and tell me with a log what went wrong. :)
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: bob golding on April 12, 2013, 11:51:45 AM
hi simen,

i have taken the liberty of passing your code ion to some  people who run a hackspace for people interested in raspberry Pi and python to see what they think of it. hope you don't mind. will get back to you in a couple of weeks after they have had a look at it and passed comment. i am away for a couple of weeks so wont be able to provide any input till i get back. i left them the link to this site so you may hear from someone.
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Bruce S on April 13, 2013, 12:07:49 PM
Bob;
If you could let us (GMs) know what possible user names they may use, we could be on the lookout for them.
ATM there is still quite a lot of spammers trying to get in so at times I kill 20+ request at a time.
Damon or I could be notified via email as well , we could then go looking for those specific user names in the request area.
Safe Travels
Bruce S
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: bob golding on April 13, 2013, 01:22:26 PM
hi bruce,

most likely to will be something like david monk or marcus @openshed .org  or something like that.  i just printed this thread and left it with them. will report back when i return. as i have a tristar it would be  useful to play with a PI, not that i am short of things to play with at the moment. yurt needs finishing, turbine needs new blades, bio diesel plant needs finishing, finish the prototype WMO stove, put parts for the centrifuge together. the list goes on. tidy workspace. well that one can wait a bit......

cheers
bob
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Simen on April 13, 2013, 04:19:45 PM
It's nice to get comments from anyone who knows better than oneself. ;)

I had a breakdown of my network today, and it took me all day to figure out that it was the main network cable which connected several switches to my gateway that was bad... ::) In that process, i found that adding a '\n' (linebreak) to the errorlog-line, was a good idéa... ;)
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Treehouse on August 21, 2013, 01:34:14 PM
I know this is an older post.. :)

I just got a new tristar ts-60, and had to try this out as I didn't get the display for it.. Having never used Python I managed to get this up and running on my son's netbook running windows. I changed the dumpdata.txt to dumpdata.html and saved it on my shared drive in the house. All is working! However my son's not too keen on me stealing his netbook. So I got to thinking..lol Is there a way to do something like this on an android tablet? and perhaps also have it display the data on the screen as well?  I know nothing about android programing, but would like to know if it's possible before I try it. 
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: SparWeb on August 22, 2013, 10:30:03 AM
Provided that the tablet has the necessary cable port (ie. USB), that you have a Python compiler for Android, and that you can spare your tablet for this purpose.
A short script in the HTML file will tell the browser to refresh the screen every minute or so (you choose the interval).
Aren't "netbook" computers a lot cheaper than tablets?  I'd rather turn a netbook into a desk ornament than a tablet computer.
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Treehouse on August 22, 2013, 01:02:12 PM
I just got an android tablet from Canadian tire for 69.99 comes with a OTG cable. The cheapest netbook I could find was $150.  If it doesn't work it's going to turn into a Christmas present, LOL I will let you know if I can get it to work with the serial to usb cable..
 
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: brocktice on May 05, 2014, 01:04:12 PM
I have a few ts-mppt-60s in the field for my WISP, and I've been trying to figure out how to monitor them with pymodbus, but never had time to really dig in and learn it. With some adaptation I've got your script working great. Would you mind if I started a github project to share it and any ongoing improvements with others? I'll be adding a munin plugin in the next few days, also. Thanks so much for posting your script!
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Simen on May 05, 2014, 03:58:07 PM
Go ahead and share, Brocktice; that's why i posted it here in the first place... ;)
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: brocktice on May 05, 2014, 06:19:10 PM
Just checking, I'll post a link here once I do it, let me know how you'd like to be cited. Thanks!
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: brocktice on May 05, 2014, 07:58:41 PM
Here's the github repo

https://github.com/brocktice/tristar-python-modbus

BTW the captcha on this site is super annoying...
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: DamonHD on May 06, 2014, 01:39:57 AM
Sorry about the CAPTCHA, I hate them too.  You'll stop being prompted for one soon, and they're less annoying than the deluge of SPAM that the mods had to manually clear up.  And a mod is still personally screening each new account.

Rgds

Damon
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: brocktice on July 25, 2014, 12:37:19 PM
I just migrated my monitoring to a fresh server, and a new version (1.2) of pymodbus was involved. It required one change to the script, which has now been added and pushed to the git repo, if anyone needs it.
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: brocktice on June 13, 2015, 01:37:27 PM
We just deployed my older TS-45 (non-MPPT version) in the field with a raspberry pi monitoring. I've added a munin module for the serial version since the TS-45 has only serial, no network monitoring.

I'm going to work on unifying the code so you just specify whether you're using serial or TCP, right now there's a lot of duplication, but the different models use different registers.

Thanks again to everyone for this forum and topic -- it's been hugely helpful to us.
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Simen on June 13, 2015, 02:55:13 PM
I'm still using my original code - although, it stops without any explanation sometimes , but since it isn't 'mission critical', i haven't bothered to do something about it. :)

I also had to move my domain to another server without Python support on the web server, so the link in my first post in this thread doesn't work anymore... I still run it on a local server, since it's here at home it's interesting for me anyway... ;)
Title: Re: Monitoring the Tristar TS-xx with a PI.
Post by: Sajor87 on February 20, 2018, 10:18:27 PM
Hello!

This thread saved my life, so here's what I did. Morningstar Tristar MPPT 60 with a raspberry pi via LAN and twitter client to keep me updated.

from __future__ import division
from pymodbus.client.sync import ModbusTcpClient
import schedule
import time
import tweepy


CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_KEY = ""
ACCESS_SECRET = ""


def get_data():
    client = ModbusTcpClient("") #<--- local ip of tristar
    client.connect()
    rr = client.read_holding_registers(0, 60, unit=1)
    client.close()
    return rr.registers


def handle_data():
    data = get_data()
    charge_state = ["START", "NIGHT_CHECK", "DISCONNECT", "NIGHT",
                    "FAULT", "MPPT", "ABSORPTION", "FLOAT", "EQUALIZE", "SLAVE"]

    alarms = ["RTS open", "RTS shorted", "RTS disconnected", "Heatsink temp sensor open",
              "Heatsink temp sensor shorted", "High temperature current limit", "Current limit",
              "Current offset", "Battery sense out of range", "Battery sense disconnected",
              "Uncalibrated", "RTS miswire", "High voltage disconnect", "Undefined",
              "system miswire", "MOSFET open", "P12 voltage off", "High input voltage current limit",
              "ADC input max", "Controller was reset", "Alarm 21", "Alarm 22", "Alarm 23", "Alarm 24"]

    faults = ["overcurrent", "FETs shorted", "software bug", "battery HVD", "array HVD",
              "settings switch changed", "custom settings edit", "RTS shorted", "RTS disconnected",
              "EEPROM retry limit", "Reserved", " Slave Control Timeout",
              "Fault 13", "Fault 14", "Fault 15", "Fault 16"]
    if data:

        volt_scaling = data[0]
        current_scaling = data[2]
        battery_voltage = round(data[38] / volt_scaling, 2)
        charging_current = round(data[39] / current_scaling, 2)
        charging_state = data[50]
        target_voltage = data[51]
        output_power = data[58]
        battery_temp = data[36]
        total_kwh = data[56]
        alarm = data[46]
        fault = data[44]

        text = "Battery {} V, temp {} C, power {} W, state {}. Total kWh {}.".format(
            battery_voltage, battery_temp, output_power, charge_state[charging_state], total_kwh)

        return text


def publish():
    try:
        text = handle_data()
        if text:
            auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
            auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
            api = tweepy.API(auth)
            api.update_status(text)
    except:
        pass


schedule.every(10).minutes.do(publish)


while True:
    schedule.run_pending()
    time.sleep(1)

Twitter: HolboxCalamar10

Hope it is all self-explanatory.

Regards,
Oscar