WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Python Script to Query Solar MPPT Charge Controller  (Read 3643 times)

Offline yeme

  • Newbie
  • *
  • Posts: 21
Python Script to Query Solar MPPT Charge Controller
« on: August 30, 2016, 10:05:20 AM »
I am currently using this script below on a RPi to query my solar charge controller, via the GPIO Tx and Rx pins.

Code: [Select]
#!/usr/bin/env python
import csv
import time
import json
import serial
import sys
from tracer import Tracer, TracerSerial, QueryCommand
import httplib, urllib   # http and url libs used for HTTP POSTs
import socket            # socket used to get host name/IP


def queryTracer():
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout = 1)

tracer = Tracer(0x16)
t_ser = TracerSerial(tracer, ser)
query = QueryCommand()
t_ser.send_command(query)
result = t_ser.receive_result()

current_time = time.localtime()


print time.strftime('%Y-%m-%dT%H:%M:%S',current_time)
print "Battery V\t %02.02f" % result.batt_voltage
print "Panel V  \t %02.02f" % result.pv_voltage
print "Load A   \t %02.02f" % result.load_amps
print "Charge A \t %02.02f" % result.charge_current

with open('tracer.csv', 'a') as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    csvwriter.writerow([time.strftime('%d.%m.%Y %H:%M:%S',current_time)] +
[result.batt_voltage] +
[result.pv_voltage] +
[result.load_amps] +
[result.charge_current]
)

#################
## Phant Stuff ##
#################
server = "data.sparkfun.com" # base URL of your feed
publicKey = "*******************" # public key, everyone can see this
privateKey = "******************* # private key, only you should know
fields = ["battery", "panel", "load", "charge"] # Your feed's data fields

data = {} # Create empty set, then fill in with our three fields:
        data[fields[0]] = result.batt_voltage
        data[fields[1]] = result.pv_voltage
        data[fields[2]] = result.load_amps
data[fields[3]] = result.charge_current
        params = urllib.urlencode(data)

        # Now we need to set up our headers:
        headers = {} # start with an empty set
        headers["Content-Type"] = "application/x-www-form-urlencoded"
        headers["Connection"] = "close"
        headers["Content-Length"] = len(params) # length of data
        headers["Phant-Private-Key"] = privateKey # private key header

        # Now we initiate a connection, and post the data
        c = httplib.HTTPConnection(server)
        c.request("POST", "/input/" + publicKey + ".txt", params, headers)
        r = c.getresponse() # Get the server's response and print it
        print r.status, r.reason

   
while True:
        queryTracer()
        time.sleep(30)


The RPi setup requires me to disable the serial startup (uart=0 in /boot/config.txt) for this script to work (so I commented out the serial startup line in /opt/bootlocal.sh on the piCore Pi). The script pulls voltage and amp readings from my off-grid solar setup and uploads them to an IOT website.  When I run this script on my piCore Pi, I get the following error:

Code: [Select]
tc@box:~$ python logVoltages.py
Traceback (most recent call last):
  File "logVoltages.py", line 6, in <module>
    import serial
ImportError: No module named serial
tc@box:~$

I do not know how to proceed and would like some help and direction to get this working.  I believe python is working correctly as I am successfully serving pages using the Simple HTTP server on this piCore (7.0) setup.

Thanks,




Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14832
Re: Python Script to Query Solar MPPT Charge Controller
« Reply #1 on: August 30, 2016, 10:44:17 AM »
It looks like you need pyserial

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Python Script to Query Solar MPPT Charge Controller
« Reply #2 on: August 30, 2016, 10:59:02 AM »
Yes, install pyserial.tcz which is for Python 2.7 For 3.4 or 3.5 you must install it from source.

Also, please remove the serial console setting in /mnt/mmcblk0p1/cmdline.txt or cmdline3.txt if you are using pi-Core-8.0 and RPi3
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline yeme

  • Newbie
  • *
  • Posts: 21
Re: Python Script to Query Solar MPPT Charge Controller
« Reply #3 on: August 30, 2016, 11:56:22 AM »
Thanks, that took care of my serial error.  I was unaware of pyserial.

I'll be back as I progress.

Thanks


Offline yeme

  • Newbie
  • *
  • Posts: 21
Re: Python Script to Query Solar MPPT Charge Controller
« Reply #4 on: August 30, 2016, 02:18:19 PM »
All is working.  I also needed to compile a file called "__init__.py" after removing the previous "pyc" version.

Thanks again