OpenSprinkler › Forums › Hardware Questions › OpenSprinkler Pi (OSPi) › OSPi interval program – ospi_addon problems
- This topic is empty.
-
AuthorPosts
-
July 23, 2013 at 2:47 am #22547
LLMoonMemberI am trying to add a custom page using the new ospi_addon stub that was added in July 10 version. Has anyone else successfully used this to add functionality to interval program? If the custom class is defined in the ospi.py file everything works fine, but moving it to ospi_addon.py creates errors.
When accessing the custom page from a browser, I get errors that indicate my class (‘view_wellpressure’) is not found. Here’s what I see on the terminal:
Starting main loop
http://0.0.0.0:8080/
192.168.1.18:58721 – – [22/Jul/2013 20:56:31] “HTTP/1.1 GET /” – 200 OK
Traceback (most recent call last):
File “/home/pi/OSPi/web/application.py”, line 239, in process
return self.handle()
File “/home/pi/OSPi/web/application.py”, line 230, in handle
return self._delegate(fn, self.fvars, args)
File “/home/pi/OSPi/web/application.py”, line 419, in _delegate
cls = fvars[f]
KeyError: u’view_wellpressure’I think my custom class is not found in fvars[]
For completeness, here is the content of ospi_addon.py
#!/usr/bin/python
import ospi
from smbus import SMBus
#### Add any new page urls here ####
ospi.urls.extend() # example: ()
#### add new functions and classes here ####
class view_wellpressure:
"""open well page"""
def GET(self):
wellpg = 'n'
wellpg += data('meta')+'n'
wellpg += 'n'
wellpg += 'n'
wellpg += ''
return wellpg
print "initializing sensor variables"
bus = SMBus(1)
SENSOR_ADDRESS = 0x48
CH_PRESSURE = 0x01
CH_WELLPOWER = 0x00
def readPressure():
bus.write_byte(SENSOR_ADDRESS,CH_PRESSURE) #set the channel
time.sleep(0.01)
result = bus.read_byte(SENSOR_ADDRESS) #throw away first byte
result = bus.read_byte(SENSOR_ADDRESS)
result = (0.388*result) - 9.32 #calculate pressure using slope .388
return int(result)
def readWellPower():
bus.write_byte(SENSOR_ADDRESS,CH_WELLPOWER) #set the channel
time.sleep(0.01)
result = bus.read_byte(SENSOR_ADDRESS) #throw away first byte
result = bus.read_byte(SENSOR_ADDRESS)
if result > 100:
result = "ON"
else:
result = "OFF"
return result
I appreciate any advice, as I’m relatively new to Python and Javascript. Learning alot from this project and this forum!
July 23, 2013 at 11:27 am #24903
paintycanParticipantIt was missing from the examples, but you need to include the file name in the ospi.urls.extend statement. It should look like this:
ospi.urls.extend()
That worked for me, at least.
July 23, 2013 at 11:47 am #24904
djagerifParticipantAlso, if you want to reference something back in the ospi.py script then do the following:
custpg += ‘n’
July 24, 2013 at 12:18 am #24905
LLMoonMemberThanks very much, that resolved the errors and page is loading properly. Seems quite obvious now. I was treating python import like a C “include” statement, and forgot that objects need to be referenced to their source module.
-
AuthorPosts
- You must be logged in to reply to this topic.
OpenSprinkler › Forums › Hardware Questions › OpenSprinkler Pi (OSPi) › OSPi interval program – ospi_addon problems