Emoncms Serial RF12b

Hi, I have Emoncms modular version installed on Raspberry Pi  with Martin's RF12b module & "raspberrypi_run.php" script it is receiving from multi-emontx nodes for last week no issues. I would like to use Python script instead, only reason because I'm learning Python.

Based on Glyn's https://github.com/openenergymonitor/EmoncmsPythonLink/blob/master/pylink.py  and with the serial changed to /dev/ttyAMA0 . The serialtest.py works ok in geany I'm receiving CSV for both emontx and emonglcd, problem is when I try to use pylink.py to post to emoncms localhost, I get NameError: Name 'conn' is not defined.

It has something to do with line 11 below:

conn.request("GET", "/emoncms/input/post?csv="+ser.readline())

tried below as it works in input api test on panel (and many others)

Thanks Peter

 

 

 

 

 

ukmoose's picture

Re: Emoncms Serial RF12b

Peter,  I'm guessing its actually earlier in the script ( e.g. line 3) as it's basically saying you've not used conn before and therefore I don't know what to do with it.

Perhaps you could post the entire script you are using?

 

PeterN's picture

Re: Emoncms Serial RF12b

Hi, Code is based on pyserial https://github.com/openenergymonitor/EmoncmsPythonLink

 

Script

import serial, sys

import httplib

conn = httplib.HTTPConnection("localhost")

ser = serial.Serial('/dev/ttyAMA0', 9600)

while 1:

sys.stdout.write(ser.readline()) 

conn.request("GET", "/emoncms/input/post?csv="+ser.readline())

response = conn.getresponse()

In bold is my changes.

Thanks Peter

 

PeterN's picture

Re: Emoncms Serial RF12b

LXTerminal  after running script in geany

20 196 9
Traceback (most recent call last):
  File "pyserial.py", line 12, in <module>
    connn.request("GET", "/emoncms/input/post?csv=" +ser.readline())
NameError: name 'connn' is not defined

------------------
(program exited with code: 1)
Press return to continue

mharizanov's picture

Re: Emoncms Serial RF12b

I see the error message states "connn" while the code example declares "conn" (one less 'n'), could that be the reason?

PeterN's picture

Re: Emoncms Serial RF12b

Thanks again Martin, sorted error  and lesson learned. Its running in geany now and shows data in LXTerminal. Not updating as feed yet. Any suggestion on where to start posting to emoncms, can you please share working example if available, I thought link below would show as CSV in emoncms inputs but no luck.

conn.request("GET", "/emoncms/input/post?csv="+ser.readline())

I assume after above hurdle I will need to program in my python how to threat for each node as there is no typedef struct in raspberry pi .

Cheers

Peter

P.S. some additional info

LXTerminal sample for Node10 - emontx  (typedef struct { int power1, power2, power3, Vrms, Temperature, powerFactor; }) and Node 20 - emonglcd (Temperature)

10 120 2 1 0 9 0 210 101 150 3 89 0

20 2 10

ukmoose's picture

Re: Emoncms Serial RF12b

 

Peter,

Glad you found the typo ;)

I use a slightly different way of doing it in python (but the results the same)

opener = urllib.FancyURLopener({})
   f = opener.open("http://server/path/api/post?apikey=apikey4&node=" + id1 + id2.zfill(2) + "&json={Temperature:"+ temperature_str +",Humidity:"+ str(int(humidity,16)) + ",Battery:"+ str(battery) +  "}")
   f.read()

 

From that you should be able to se how I pass the variables for the node into the string.  Its 3 digits long because I wanted to be able to differentiate internal and external temps from the node ID  ( I use 1xx for internal and 4xx for external)

(zfill(2) - gives 2 digits and will include leading zeros)

 

PeterN's picture

Re: Emoncms Serial RF12b

Hi, Thanks a mil. Before I start to make sense of above. What release of python are you using? I'm using 2.7 and wondering should I upgrade to 3. Second question, are you using latest emontx csv.

Thanks

Peter

ukmoose's picture

Re: Emoncms Serial RF12b

I'm using Python 2.7.3rc2.  My thinking was to stick with a version where if I google for an example, the example is likely to work :-). 

 

My setup for the above is actually cheap temperature (433MHz) sensors from Clas Ohlson going to a rfxtrx433 from rfxcom thats hooked up to my RaspPi.

On the RaspPi I have a Python script, that reads the output of the serial splits it up into different variables, and then as per Glyn's script appends them onto the end of a URL and as python "reads" or "gets" the webpage it passes the variables to emoncms.  

 

I'm using the json import, but cannot remember why I chose it!

The code hopefully gives you 1 way of getting data for different nodes into emoncms using python.  Good luck!  

 

 

galen's picture

Re: Emoncms Serial RF12b

@ ukmoose
Hello,

I want to implement emoncms with usb rfxtrx433 and raspberry pi

I see you did the same thing
is it possible to share the python script?

I am not a god of programming

it would help me

thank you in advance

mharizanov's picture

Re: Emoncms Serial RF12b

Galen,

you may want to take a look at this script: https://github.com/Jerome-github/oem_gateway

 

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.