String data to Emoncms

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:


[email protected]:/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)

Why do you want to send it as CSV? You can send it directly without CSV formatting.

Here is an example of my weather station and how I send values via python:
import urllib
import httplib2
from urllib.request import urlopen

url = “http://swiss-solar-log.ch:85/input/post.json?apikey=”+apikey+ “&node=”+nodeid + “&json={” + “Avg_Windspeed:”+str(avg_windspeed)+ ‘,’+ “Wind_Direction:”+str(wind_direction) +’,’+ “Gust_Windspeed:”+str(gust_windspeed) +’,’+ “Temperature:”+str(temperature) +’,’+ “outdoor_humidity:”+str(outdoor_humidity) +’,’+ “barometer:”+str(barometer) +’,’+ “daily_rainfall:”+str(daily_rainfall) +’,’+ “current_windchill:”+str(current_windchill) +’,’+ “current_humindex:”+str(current_humindex) +’,’+ “cloud_height:”+str(cloud_height) +"}"
print (url)
f = urlopen(url)

This way you can assign Headers/Titles to the data so that you know what your are seeing in the input area. I use a PERL script to read out a Siemens S7 PLC from our water treatment of our town. There it makes a lot of sense as there are a lot of bits showing the status of UV, Turbidity etc.

You can do the same with your conn.request way.
To solve your request I would say a simple + is missing at thend before the bracket:
conn.request(“GET”, “/”+emoncmspath+"/input/post.json?apikey="+apikey+"&node="+str(nodeid)+"&csv="+newcsv+)
Or just print out the conn.request and verify how the link looks in the console. I have that too in my way with the print (url)

Dear Andreas, thank you for your reply and help.
a)The extra + in the end, causes an invalid syntax error.
b) I used the url way to solve it by adding the code below
url = “http://localhost/emoncms/input/post.json?apikey=”+apikey+ “&node=”+nodeid + “&json={” +str(allvalues)+"}"
The error message i get is:

File “myfile.py”, line 41, in
url = “http://localhost/emoncms/input/post.json?apikey=”+apikey+ “&node=”+nodeid + “&json={” +str(allvalues)+"}"
TypeError: cannot concatenate ‘str’ and ‘int’ objects

Is there any possibility of having wrong type of data in my string?
If i print my values i take this:
(1, 4, 13, 225.72996520996094, 225.72549438476562, 225.7203826904297, 49.992347717285156)
Int of 16bits and Floats of 32 bits.

Try url = “http://localhost/emoncms/input/post.json?apikey=”+apikey+ “&node=”+str(nodeid) + “&json={” +str(allvalues)+"}" the node id “1” is an integer. .

Hi Lazaros

I defined the nodeid in the beginning. I use text as nodeid as I have sometimes up to 50-60 nodes and just having numbers is a little bit annoying over time.

I defined it for my weather script like this in the beginning of the script:
nodeid = ‘Liedertswil_Wetter’

Just as a reminder. Sending the allvalues will give you a list with 7 entries labeled with Key 1 to Key 7. If you can remember all the time which key is which value than this is fine. Otherwise you can use as shown above the possibility to assign a name to the value:

emoncmskey = ‘your_api_key’
nodeid = ‘Modbus_or_whatever_you_like’
url = “http://localhost/emoncms/input/post.json?apikey=”+apikey+ “&node=”+nodeid + “&json={” + “Register1Name:”+str(Register1)+ ‘,’+ “Register2Name:”+str(Register2) +’,’+ “Register3Name:”+str(Register3) +’,’+ “Float1Name:”+str(Float1) +’,’+ “Float2Name:”+str(Float2) +’,’+ “Float3Name:”+str(Float3) +’,’+ “Float4Name:”+str(Float4) +"}"

Something like this… :slight_smile:
Andi

Dear friends!

I would like to thank you all for your help. The problem has been solved.
I follow the instruction both of Andreas and pb66.

For example:
In order to send the 3 phase voltages i use the code below.
url = “http://localhost/emoncms/input/post.json?apikey=”+apikey+ “&node=”+str(nodeid) + “&json={” + “V1-N:”+str(Float1)+’,’+ “V2-N:”+str(Float2)+’,’+ “V3-N:”+str(Float3)+"}"

“&node=”+str(nodeid) was the first “secret”
and the total command structure the other.

Once again, thank you all!