I am a student in university and i am working on a project for my essay.
The task is to read registers and floats from a power analyzer with RS485 connection.
I use a power analyzer, a RS485 to USB adapter and a Raspberry pi 2 model in order to send data to my local emoncms account in raspberry pi.
I found the minimalmodbus files very useful for that job. By now i can read the values from the power analyzer but when i am trying to send the string of data to emoncms, i receive the error below:
root@raspberrypi:/usr/local/lib/python2.7/dist-packages# python test4.py
(1, 4, 13, 225.32798767089844, 225.31370544433594, 225.31173706054688, 50.027103424072266)
Error: Request contains no data via csv, json or data tag
I am very sure that i am so close but i do not have the proper programming background to finish it.
I will be grateful if someone help me a bit.
For that reason i am sending you the python code i created searching the internet for many days.
Thank you!
import minimalmodbus
import serial, string, httplib, time, csv, math
instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 1, mode='rtu')
domain = "localhost"
emoncmspath = "emoncms"
apikey = "my api key"
nodeid = 1
conn = httplib.HTTPConnection(domain)
serial = serial.Serial("/dev/ttyUSB0", 9600, timeout=2)
while True:
Register1 = instrument.read_register(2013, numberOfDecimals=0, functioncode=3, signed=False)
Register2 = instrument.read_register(2014, numberOfDecimals=0, functioncode=3, signed=False)
Register3 = instrument.read_register(2015, numberOfDecimals=0, functioncode=3, signed=False)
Float1 = instrument.read_float(3027, functioncode=3, numberOfRegisters=2)
Float2 = instrument.read_float(3029, functioncode=3, numberOfRegisters=2)
Float3 = instrument.read_float(3031, functioncode=3, numberOfRegisters=2)
Float4 = instrument.read_float(3109, functioncode=3, numberOfRegisters=2)
allvalues = Register1,Register2,Register3,Float1,Float2,Float3,Float4
string = str(allvalues)
#print allvalues
print string
csv_out = open('mycsv.csv', 'wb')
mywriter = csv.writer(csv_out)
rows = zip(allvalues)
mywriter.writerows(rows)
csv_out.close()
#Create csv string
newcsv = ','.join(string)
#Send to emoncms
conn.request("GET", "/"+emoncmspath+"/input/post.json?apikey="+apikey+"&node="+str(nodeid)+"&csv="+newcsv)
response = conn.getresponse()
print response.read()
time.sleep(2)