Attempting to code Python to use API data

Posted on
Sun Jan 28, 2018 2:25 pm
towsled offline
Posts: 32
Joined: Jul 19, 2014

Attempting to code Python to use API data

I have an EKM Metering system monitoring my gas and electric and decided that I would try and access the system in Indigo. EKM provides an API (ekmmeters.py) which I thought I would code up in Python (I am very new to coding Python). So far I have installed ekmmeters.py in Xcode and imported the sample code from their manual into Indigo (copied below). It appears that when I attempt to run the basic script (opens and then closes the connection to the meter) it is not recognizing the ekmmeters.py module. I am sure that I am missing something simple but could use help getting pointed in the right direction.

Thanks,
Dan..
____________________________

Code: Select all
import os      # to delete example db before create
import random  # to generate example data
import time    # to support summarizer sample

from ekmmeters import *

my_port_name = "/dev/ttyS0"
my_meter_address = "300001162"

ekm_set_log(ekm_print_log)
port = SerialPort(my_port_name)

if (port.initPort() == True):
    my_meter = V4Meter(my_meter_address)
    my_meter.attachPort(port)
else:
    print "Cannot open port"
    exit()

# Example goes here

port.closePort()

Posted on
Sun Jan 28, 2018 3:11 pm
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Attempting to code Python to use API data

I think we need a little bit more information on how you're trying to proceed. How are you trying to access the script from within Indigo?

If you access your script as a file and have the other file in the same folder, Indigo should be able to recognize it.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Jan 28, 2018 3:24 pm
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Attempting to code Python to use API data

And it depends on where ekmmeters.py is. Is it a package? If so it may be more along the lines of import ekmmeters rather than from ekmmeters import *. You should get an error when you try to load the file if it's not loading properly - what is that error?

Also watch your indents in Python, it's pretty important:

Code: Select all
if (port.initPort() == True):
my_meter = V4Meter(my_meter_address)
my_meter.attachPort(port)
else:
print "Cannot open port"
exit()


Should be more like:
Code: Select all
if (port.initPort() == True):
     my_meter = V4Meter(my_meter_address)
     my_meter.attachPort(port)
else:
     print "Cannot open port"
     exit()

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Sun Jan 28, 2018 3:57 pm
towsled offline
Posts: 32
Joined: Jul 19, 2014

Re: Attempting to code Python to use API data

Thanks for the replies. The framework I am following has the shell listed in the previous note and then specific language inserted at the #Example goes here point. Here is the error I get in the Log when trying to run the script from within Indigo:

_________________
Code: Select all
Script Error                    embedded script: No module named ekmmeters
Script Error                    Exception Traceback (most recent call shown last):

embedded script, line 6, at top level
ImportError: No module named ekmmeters
__________________

The manufacturer provided the following example of language to request data from the meter (below). Results are returned in hex and then must be parsed.

Code: Select all
# Open connection to serial port
# Change /dev/ttyUSB0 to match your system
sp = serial.Serial(
    port='/dev/ttyUSB0',
    baudrate=9600,
    parity=serial.PARITY_EVEN,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.SEVENBITS,
    xonxoff=0,
    timeout=5
)

# Meter Number
meter="000300001184"

# Send Request A to v4 Meter
sp.write("\x2F\x3F"+meter+"\x30\x30\x21\x0D\x0A")

# Get meter response
response = sp.read(255)

# Show response
print(" ".join("{0:02x}".format(ord( c )) for c in response))

# Send close to meter
sp.write("\x01\x42\x30\x03\x75")

# Close connection to serial port
sp.close

Thanks again for the help!
Dan..

Posted on
Sun Jan 28, 2018 4:16 pm
towsled offline
Posts: 32
Joined: Jul 19, 2014

Re: Attempting to code Python to use API data

Point well taken on the spacing and format.. For some reason I loose the spacing when I cut the code out of the compiler window and paste here. :D

Posted on
Sun Jan 28, 2018 4:30 pm
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Attempting to code Python to use API data

If you block select the code segments and then click the Code button above, the spacing and format will be preserved.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Jan 28, 2018 6:39 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Attempting to code Python to use API data

I edited your posts to place the code between code tags so you can see how those work for future posts.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Jan 29, 2018 10:57 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Attempting to code Python to use API data

After giving this some thought, I think I might know what's going on. In Python 2.x (I don't know about all Python 2 versions; for this example, I'm using Indigo 7 and Python 2.7). Consider two files, both of which reside in the same folder:

foo.py
Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-

from foo1 import *

print(u"foo")

foo1.py
Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-

print(u"foo1")

When I run foo.py from the terminal, I get the following result:
Code: Select all
Daves-MacBook-Air:Scripts dave$ python foo.py
foo1
foo

When I run foo.py as an embedded script within Indigo, I get the following result:
Code: Select all
   Script Error                    foo.py: No module named foo1
   Script Error                    Exception Traceback (most recent call shown last):

     foo.py, line 4, at top level
ImportError: No module named foo1


The Python interpreter will search the current folder (the one where the scripts reside) as a part of the search path, but it doesn't appear this is the case when running the script as an embedded script in Indigo. Further, there are rules about how imports work when the folder is considered a Python package (or not).

I believe there are a couple of different ways to handle your case (there are others but I think these are the best): you could write your code as a plugin, place your scripts in a folder included in the default sys.path, or you could use absolute imports as opposed to relative. Here is a nice explanation of the ins and outs of Python imports.

Any Python gurus out there (I'm not one) should feel free to correct anything I've said here.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Jan 29, 2018 11:27 am
racarter online
User avatar
Posts: 468
Joined: Jun 18, 2016
Location: North Yorkshire, UK

Re: Attempting to code Python to use API data

If you want to import from a custom Python file into an embedded or external Python script called from Indigo, try placing the file in the /Library/Python/2.7/site-packages folder. (2.7 works for me but you might need to experiment.)

Posted on
Mon Jan 29, 2018 12:00 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Attempting to code Python to use API data

racarter wrote:
If you want to import from a custom Python file into an embedded or external Python script called from Indigo, try placing the file in the /Library/Python/2.7/site-packages folder. (2.7 works for me but you might need to experiment.)


This is correct for Indigo 7. For Indigo 6 or earlier, it's:

Code: Select all
/Library/Python/2.5/site-packages

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Jan 29, 2018 4:26 pm
towsled offline
Posts: 32
Joined: Jul 19, 2014

Re: Attempting to code Python to use API data

Thanks all for the great advice! I will try the suggestions later on tonight and report back what I find.

Thanks again,
Dan..

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests