Emonpi & Socket Interfacer help

Hi - I recently purchased an Emonpi and am very happy with it so far. I have been trying to setup multi-tarrif cost calculations following various threads I found here in the forums like these:

http://openenergymonitor.org/emon/node/6100 and http://openenergymonitor.org/emon/node/10175

I understand the principles of what I need to do, but so far I have not been able to update input values of the node I created to hold the 'currentcost', 'onpeak' and 'offpeak' parameters with any script. In my emonhub conf file I have declared a socket interface like this:

[[Socket]]   # Socket Interfacer
    Type = EmonHubSocketInterfacer
    [[[init_settings]]]
        port_nb = 50824
    [[[runtimesettings]]]

but when I try to post data to it using any of the sample python scripts I have found I get an error returned in my shell saying "socket.error: [Errno 111] Connection refused". If I run a netstat command directly on my EmonPi It does not display my chosen port as 'listening' on the local address/port column (I am presuming it should be displayed here ?). I have tired various different port numbers incase any particular one was used by another application. Which ever one I tired I get the same message. I also made sure to add any declared port to the UFW firewall rule. I have tired running my value posting script locally on the EmonPi and also from remote machines (with the HOST parameter updated accordingly) but always get the same error.

Any ideas what I might be doing wrong? Is there something different or unusual here because its the low-write emoncms running on the PI (shop bought unaltered emonpi with low-write-v8.5)

Is there any other method of updating 'input' values? I have read the HTTP JSON posting method is not yet implemented on the PI but that was an older post - is this still accurate?

Any help or advice greatly appreciated.

paddyb's picture

Re: Emonpi & Socket Interfacer help

I should have shown the script I am using when attempting to update the values.. As far as I can see it should work based on the many other examples I have found scattered around the forum.


#!/usr/bin/python
import socket
# "frame" string starting with a node id followed by values$
frame = '17 19550 1 0'
# Parameters #
# HOST: hostname or IP address of emonHub
HOST = 'localhost'
# PORT: port number to the emonHub interfacer
PORT = 50824
# Code #
# Append line ending
frame = frame + '\r\n'
# Send frame of data
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send(frame)
s.close()

 

pb66's picture

Re: Emonpi & Socket Interfacer help

What is logged in emonhub.log when emonhub is (re)started? 

Does it show the socket interfacer as successfully created or are there any errors, you may need to set loglevel = DEBUG in emonhub.conf to get the full picture.

Your conf and script look ok to me, I would of said check the port is enabled in ufw but you've done that, did you restart (re-enable) ufw after adding the new rules?

Paul

paddyb's picture

Re: Emonpi & Socket Interfacer help

Hi - thank you very much for helping me. I did the restart of emonhub as suggested and see this in the log actually:

"ERROR MainThread Unable to create 'Socket' interfacer: global name 'socket' is not defined"

I then changed the name of my interfacer to Test_Skt incase the original title I chose of 'socket' in case it clashed with some kind of namespace or reserverd keywords but I get the same error message:

INFO     MainThread Creating EmonHubSocketInterfacer 'Test_Skt' 
DEBUG    MainThread Opening socket on port 50824
ERROR    MainThread Unable to create 'Test_Skt' interfacer: global name 'socket' is not defined

Does this mean anything to anyone? 

 

pb66's picture

Re: Emonpi & Socket Interfacer help

Try adding "import socket" as the first line to ~/emonhub/src/emonhub_interfacer.py

Paul

paddyb's picture

Re: Emonpi & Socket Interfacer help

I found that file one folder deeper than you said, in "/home/pi/emonhub/src/interfacers", but it already has import socket in it. It was the fifth item in the import list. I moved it to the top anyway but still get the same "global name 'socket' is not defined" message

 

pb66's picture

Re: Emonpi & Socket Interfacer help

Sorry my mistake I gave you the "other" location by mistake I had both the "emon-pi" variant and original emonhub files open to compare. I have not used a socket interfacer on emonPi yet, but I know they work fine on the genuine emonhub as I use them frequently.

Your reply has confused me somewhat as you say this is on a emonPi, correct?

The suggestion to add that import came from comparing the files as above.

The import is clearly missing from the emon-pi variant (see here) and it can be correctly found as the 5th import on the original version (see here). in which case I'm thrown as the location says emon-pi variant but the import entry contradicts that, no wonder you are having troubles, Have you changed anything or can you shed any light on the confusion?

Paul

pb66's picture

Re: Emonpi & Socket Interfacer help

I think I sussed it! 

The file you should be looking at is /home/pi/emonhub/src/interfacers/EmonHubSocketInterfacer.py, when the interfacers were split out into seperate files in that version the import must of got left behind in /home/pi/emonhub/src/interfacers/emonhub_interfacer.py.

Paul

paddyb's picture

Re: Emonpi & Socket Interfacer help

Hi Paul,

I can confirm I am definitely using a genuine EmonPi, in its lovely aluminum case, bought from the OEM shop. I also definitely have the files u pointed to in your first link to the the emon-pi variant. 

I am very much a python amature, but in the file EmonHubSocketInterfacer.py it does import emonhub_interfacer.py. I assumed when it does that, that it kind-of inherited any required imports required or called by the first file? (could be mistaken).

Anyways, just to try something, I added the serial, time, datetime, logging, socket & select imports to the EmonHubSocketInterfacer.py file and rebooted. Now it does create the socket, but when I run my script to update the values, the script executes without errors so it thinks it sent the values to a socket who listened, but on the input page they don't actually update. The instant I execute my script the log starts to fill with this message:

WARNING MainThread Test_Sock2 thread is dead

 

pb66's picture

Re: Emonpi & Socket Interfacer help

You would think the python imports would work like that (many users do), I expected that behaviour too when I first started using python, however no, that's not how python works unfortunately, You can "apparently" find or access the inherited imports by using something like importedparentmodule.moduleparentimported.function (eg something like s = emonhub_interfacer.serial.serial(port, baud)) but it is frowned upon and considered wrong, in fact I have only read one instance of it it the literally hundreds of posts I read on imports whilst trying to fathom the "quirky" import system.

Each module should import all its own stuff, this doesn't open another instance of the imported modules, just links to the existing instance if there is one.

As for the "thread is dead" I'm afraid I do not have a clue :-(, I have only ever used sockets in the original emonHub and they have never given cause to do any debugging so I'm not really sure what you can check, but I will post anything I think of and if i get time i will take a closer look.

Paul

paddyb's picture

Re: Emonpi & Socket Interfacer help

Thanks for the explanation and all the help so far. I have kinda given up on getting this to work on my EmonPi as I have managed to complete my goal of multi tariff recording on emoncms.org instead (also thanks to your help in a different thread :) )

If a new version is released for EmonPi I will upgrade to it and try again. I guess for now if any of the devs see this they could mark this feature as needing improvement in future versions.

Comment viewing options

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