New emonBase standalone gateway

Hi all.

I've been working on a standalone gateway to add a few features to the current setup.

It seems complete enough for me to be published, although I'd rather have a little feedback before recommending it.

https://github.com/Jerome-github/oem_gateway

It contains the features of the current python or php  gateway, plus new features:

  • It can be parameterized either via emoncms GUI (with current limitations of the interface) or through a configuration file. Useful for those who don't want to install a local emoncms just for the configuration of their gateway. This also allows to add features without the need for complex GUI edition.

  • It can read inputs from the RFM2Pi, but also inputs from the serial port of the form "NodeID val1 val2 ...", or even from a socket. This socket possibility means any application on the same machine or on the network can send data to the gateway. This even solves the inter-applications issue. It is better than writing to a file, in my opinion.

  • Extensibility. By design, it should be easy to create new inputs (data sources) or outputs (another server's API). It uses classes, so a new input (listener) or output (buffer) can be created through inheritance/overriding.

See readme file for more explanations.

I should add a socket use sample, but it's pretty basic and require very few code. Here's a python example:

import socket
HOST = 'raspberrypi'    # The remote host, can be 'localhost' just the same
PORT = 50011            # The port chosen in the gateway config
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('14 15 26\r\n')
s.close()

Currently, each buffer instantiated in the gateway buffers a given amount of data when network is down. This is limited (we don't want to eat up all memory). Next step could be to add the possibility to buffer samples in a file (with care for SD card wearout).

I should be gone for a few weeks. Feel free to send feedback about the design, the documentation, bugs, etc.

 

 

Robert Wall's picture

Re: New emonBase standalone gateway

What hardware is required?

Jérôme's picture

Re: New emonBase standalone gateway

Forgot to mention that. It runs on my Pi.

Technically, it could run on any device that runs python. The RFM2Pi part is designed to work with Martin's card.

It would be useful in particular for people who use the Pi as a base but fulfill any of the following

- don't want to host emoncms on the Pi

- don't want to use the radio but ethernet

- want to use sensors on the Pi itself rather than only on emonTX

I've seen people here with this kind of requirements.

modenet's picture

Re: New emonBase standalone gateway

HI, thanks for your works! I'm using oemgateway after another sd crash.

I use oemgateway to upload data to my local data and to emoncms.org: yesterday I observed (when emoncms.org was down) that oemgateway stopped sending data even to my local server; I had to modify conf to stop upload to emoncms.org to resume pushing data to my local server.

 

 

casestudies's picture

Re: New emonBase standalone gateway

I've been working on using this gateway to push data from a Tweet-A-Watt to emoncms. I've uploaded the main script "wattcher.py" to Github. The other files to make it work can be found at https://github.com/adafruit/Tweet-a-Watt.

https://github.com/casestudies/tweetawatt

The gateway script is running on a Raspberry Pi at the moment. Originally, I tried another computer, but data logging would stop after 6-12 hours. It appeared that wattcher.py would lock up. I had been using this computer in a data logging system I developed a couple years ago. However, wattcher.py has been running on the Pi for over 24 hours now, so I believe the first computer has issues and the problem isn't related to the gateway script.

I had a bit of trouble at first figuring out the data format. Perhaps it's because I'm new to this, but it might help to mention that data should be formatted as an integer (i.e. no decimals) in the socket sendall string. Once I figured out that the data should be sent as an integer and converted back into a float using emoncms, it worked very well. I guess I should have realized this sooner as the data sent from emonTx comes in as an integer as well.

At some point, I'd like to clean up the wattcher script as I've added features to it over time. The ability to easily change the logging rate would be helpful too.

I have noticed the wattcher.py script fails if oem_gateway.py isn't running. I'm going to add some try/except lines in wattcher.py to try to solve that problem soon.

If anyone else is logging data to emoncms using the Tweet-A-Watt project, I'd love to hear more about it!

WindyBoy's picture

Re: New emonBase standalone gateway

Just getting my head around things,

Is the oemGateway a replacement for the existing/default one ? i.e rfm2pigateway.py ?

I wanted to additionally get rfm2pigateway.py to read a csv file at intervals parse the data and create/insert the data as being from another node. Modified/added some code in rfm2pigateway.py, just hacking away trying to see how it would work, dosn't yet and perhaps I'm going about this all wrong ? Heres the code 

# Read csv file created by modcache periodically by cron job
            self.log.debug("NAD NAD *********")

            if (int(self._settings['sendtimeinterval'])):
            if ( 1 == 1) :
                try :
                        cr = csv.reader(open("/home/pi/engclassic.csv","rb"))

                        # Recombine transmitted chars into signed int
                        values = []
                        for row in cr:
                        #print (row[1])
                        # ignore the "success" entry in csv for now
                        if row[1] != "Success":
                        value = int(re.sub('[!.]',"", row[1])) # remove . char
                #       print value
#                       value = int(row[1])
                        if value > 32768:
                                value -= 65536
                        values.append(value)

                        node = 21       # identify the source of data... used later inside emoncms to identify the incomming info
                        self.log.debug("NAD NAD *********")
                        self.log.debug("Node: " + str(node))
                        self.log.debug("Values: " + str(values))

                        # Add data to send buffers
                        values.insert(0,node)
                        for server_buf in self._server_buffers.itervalues():
                                server_buf.add_data(values)
                except:
                        print "Windy bosy new code crashed:", sys.exc_info()[0]

I put that inside the run function

I'm not sure yet if this new modified version is even being called, I restarted 

sudo  /etc/init.d/rfm12piphp restart log

but I'm not sure that is enough ? Anyway I thought I'd ask before I head too much more in the wrong direction.

Thanks

WindyBoy's picture

Re: New emonBase standalone gateway

So the way to really do this is with modules right :)

deeaitch's picture

Re: New emonBase standalone gateway

Awesome, just awesome.  I've been running oemgateway for a couple of weeks now and it is working well.  I changed the code a little to get it to work with a Jeelink and also a little code to send data over a socket to another python program I have running that controls my central heating and hot water using the sensors' data as feedback.

up to now I've been starting it manually just by typing into a terminal window:

python python /home/pi/oem_gateway/oemgateway.py

Now my problem is that I'm going away on business for a whole month and I need to make sure oemgateway starts if the pi reboots due to a power failure so my wife and kids don't freeze!  The trouble is I can't seem to get it to work.  I've got a cron job that says:

@reboot python /home/pi/oem_gateway/oemgateway.py

but it doesn't seem to work.

the next line in crontab:

@reboot python /home/pi/oem_gateway/HeatingTimer.py

works just fine.  Can anyone offer any advice?  I'm no expert but file permissions seem OK.  I'm not sure that I want to go down the route of using Jerome's 'daemon' solution as what I have works fine apart from the auto start on boot.

Cheers.

deeaitch's picture

Re: New emonBase standalone gateway

I figured it out :)  For anyone else interested it should have been:

@reboot python python /home/pi/oem_gateway/oemgateway.py --config-file /home/pi/oem_gateway/oemgateway.conf

Doh!!  I am a total newb but loving it :)

 

Jérôme's picture

Re: New emonBase standalone gateway

@deeaitch: I like the daemon solution, with its cool features (start/restart/stop/status) but your solution will do just as well, I believe.

@WindyBoy: Sorry, I didn't see your message. I've been and still am a bit busy, and I don't have the time to look into this right now. I wanted to add a feature to get data from a text file. I ended up not doing it not only because of my sloppiness, but because of the SD Card wearing off when written on too much. I preferred a socket input.

You can do what you want several ways:

- create a Listener that can read your file (have it inherit from another listener and override read and process_frame methods)

- create a third party program that reads your file and sends to a listener via a socket

- modify your program creating the csv file to have it write to a socket instead (my favorite choice)

 

attila's picture

Re: New emonBase standalone gateway

This Gateway looks great! It might solve my problem to load data from PVI AEC PRO (Meteocontrol datalogger) to EmonCMS,

 

is there any news about this project?

 

thanks

Jérôme's picture

Re: New emonBase standalone gateway

See the github page. And the bug list for known issues and feature requests.

It is working and distributed on a ready-to-go SD Card in the solution known as "rock solid gateway".

deeaitch's picture

Re: New emonBase standalone gateway

I want to send data to a socket and have it transmitted by my Jeelink.  I've been using code similar to OemGatewayRFM2PiListenerRepeater_client.py to send the data on port 50012.

I was expecting this data to be transmitted by the Jeelink.  But it doesn't seem to work.  I think that port 50012 is not open in the gateway.  Jerome can you confirm?

In my conf file there is no mention of a repeater

# This listener manages the RFM2Pi module
[[RFM2Pi]]
type = OemGatewayRFM2PiListener
[[[init_settings]]]
com_port = /dev/ttyAMA0
[[[runtime_settings]]]
sgroup = 210
frequency = 4
baseid = 15
sendtimeinterval = 0
# This listener gets data from a socket
[[Socket]]
type = OemGatewaySocketListener
[[[init_settings]]]
port_nb = 50011
[[[runtime_settings]]]

How do I add to my config file to send data from socket 50012 to my Jeelink??

Cheers.

pb66's picture

Re: New emonBase standalone gateway

You need to replace your existing  "OemGatewayRFM2PiListener" with the "OemGatewayRFM2PiListenerRepeater" only one instance can exist and have access to the rfm2pi, unless you are adding a second device ie jeelink and a rfm2pi.In the "OemGatewayRFM2PiListenerRepeater" settings is where you specify (open) the port 50012.

If you have a firewall installed (eg UFW) you will need to allow it there too.

Paul

 

deeaitch's picture

Re: New emonBase standalone gateway

Thanks for the advice.

I was running an old version of the gateway so had to download the new version then replicate all the customisations I'd made to the code. But the repeater is now working. 

Next i built a little jeenode plug to interface to the electric gates on my driveway. Now I can open the gates using my phone using UDP. I also provided inputs so i can see if the gates are open/closed and a push button to act as a doorbell. The inputs need work but on the whole I'm very pleased. I might use another digital input to notify me if theres any post in the box.

Next on the agenda is a Tasker app to automatically open the gates as i approach.

Comment viewing options

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