I am currently using this script below on a RPi to query my solar charge controller, via the GPIO Tx and Rx pins.
#!/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:
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,