Full Functional Python Web Interfacekit for SBC

General PhidgetSBC Discussion.
grompot
Phidgetsian
Posts: 14
Joined: Thu Jul 31, 2014 2:15 am
Location: Nylstroom (Modimolle)
Contact:

Full Functional Python Web Interfacekit for SBC

Postby grompot » Tue Feb 17, 2015 4:16 am

I was working on a solar monitoring and management system where all my sensors is connected to the SBC3. In order for the project to save power all the code needed to be on the SBC. I already wrote a python script that can monitor and manage my solar system and I also wrote a python InterfaceKit-full (check the code on the python forum). Both these apps does not have any GUI and need to run in the Linux shell.

I thought, the PhidgetSBC 3 have a web server. I went through the article that control LEDs from a web page http://www.phidgets.com/docs/Web_Page_on_the_SBC and decided I will first experiment with a Full Python Web InterfaceKit using the above article as a guide.

Read through http://www.phidgets.com/docs/Web_Page_on_the_SBC before you start copying my code into your cgi-bin.

I created a directory interfacekit in /var/www/cgi-bin and created a file WebInterfaceKit.py with the following code.

Code: Select all


#! /usr/bin/python

# Troubleshooting
# Make sure the ref to python in line one /usr/bin/python point to your python directory
# Make sure the file WebInterfaceKit.py is in the cgi-bin /var/www/cgi-bin/interfacekit and executable (run chmod -R /var/www/cgi-bin)

from Phidgets.PhidgetException import *
from Phidgets.Events.Events import *
from Phidgets.Devices.InterfaceKit import *
 
import cgi
import cgitb
 
cgitb.enable()
errors = ""
 
# Print the HTML header
print("Content-type: text/html\n\n")
print("<html><title>FULL INTERFACE KIT</title><body>\n")
print('<center><b><font size="16" color="#0000ff">FULL INTERFACEKIT</font></b></center><br>')
print('<font size="4">THIS PROJECT WILL DEVELOP IN A FULL FUNCTIONAL WEB APPLICATION TO CONTROL THE PHIDGETS SBC</font>')
print('<br>&#169 J J Slabbert')
print('<br><a href="mailto:jaco.slabbert@mweb.co.za">jaco.slabbert@mweb.co.za</a><br><br>')

# Create, Open, and Attach the Interface Kit
try:
    ifk = InterfaceKit()
except RuntimeError as e:
    errors = errors + "<h5>Runtime Exception on object creation: " + e.details + "</h5>\n"
try:
    ifk.openPhidget()
except PhidgetException as e:
    errors = errors + "<h5>Phidget Exception on Open: " + e.details + "</h5>\n"
try:
    ifk.waitForAttach(10000)
except PhidgetException as e:
    errors = errors + "<h5>Phidget Exception on Attach: " + e.details + "</h5>\n"
    errors = errors + "<h5>If Phidget is 'Not Physically Attached' it may be in use</h5>\n"

# Generating the Sensor Value Table
print('<b>Analog Sensor Values</b>')
print('<table border="1"><tr><td>Sensor Number</td><td>Sensor Value</td></tr>\n')

for i in range (0,8):
    try:       
        print ('<tr><td>'+str(i)+'</td><td>'+str(ifk.getSensorValue(i))+'</td></tr>\n')
    except:
        pint('<tr><td>'+str(i)+'</td><td>Could not Read Censor Value </td></tr>\n')   
print('</table>')

#Print the Digital Input values
print('<b>Digital Input Values</b>')
print('<table border="1"><tr><td>Input Number</td><td>Input Value</td></tr>\n')

for i in range (0,8):
    try:
        print ('<tr><td>'+str(i)+'</td><td>'+str(ifk.getInputState(i))+'</td></tr>\n')
    except:
        pint('<tr><td>'+str(i)+'</td><td>Could not Read Digital Input State </td></tr>\n')
print('</table>')

#Print the Digital Output States
print('Digital Output States')
print('<table border="1"><tr><td>Output Number</td><td>Output State</td><td>Set True</td><td>Set False</td></tr>\n')

for i in range (0,8):
    try:
        print ('<tr><td>'+str(i)+'</td><td>'+str(ifk.getOutputState(i))+'</td><td><a href="SetTrue'+str(i)+'.py">Set True</a></td><td><a href="SetFalse'+str(i)+'.py">Set False</a></td></tr>\n')
    except:
        pint('<tr><td>'+str(i)+'</td><td>Could not Read Digital Outputt State </td></tr>\n')
print('</table>')

print ('<br><br>This page does not automaticly refresh')
print ('<br><br><a href="WebInterfaceKit.py">Refresh this page</a>')
print('</body></html>\n')
 
ifk.closePhidget()
exit(0)


Before running this code, make sure python 2.7 is installed. if python 2.7 is not your default python, edit the first line of the above code to point to python2.7.

The first line #! .... is used by the web server to locate the application to execute the python app.

The Phyton app must also be executable (Run chmod 777 -R /var/www/cgi-bin/interfacekit in the shell)

Open http://(your sbc ip)/cgi-bin/interfacekit/WebInterfaceKit.py
You will now see the main page like the image on pinterest.
http://www.pinterest.com/pin/429038301976689989/

It will read censor values, digital input and output states and there is also an option to set digital output states, but to set digital output states, you will first have to go through the next steps.

Create a file with name SetTrue0.py with the following code.

Code: Select all

#! /usr/bin/python

# Troubleshooting
# Make sure the ref to python in line one /usr/bin/python point to your python directory
# Make sure the file .py is in the cgi-bin /var/www/cgi-bin/interfacekit/ and executable (run chmod -R /var/www/cgi-bin)

from Phidgets.PhidgetException import *
from Phidgets.Events.Events import *
from Phidgets.Devices.InterfaceKit import *

import cgi
import cgitb

cgitb.enable()
errors = ""

# Print the HTML header
print("Content-type: text/html\n\n")
print("<html><title>FULL INTERFACE KIT</title><body>\n")
print('<center><b><font size="16" color="#0000ff">FULL INTERFACEKIT</font></b></center><br>')
print('<font size="4">THIS PROJECT WILL DEVELOP IN A FULL FUNCTIONAL WEB APPLICATION TO CONTROL THE PHIDGETS SBC</font>')
print('<br>&#169 J J Slabbert')
print('<br><a href="mailto:jaco.slabbert@mweb.co.za">jaco.slabbert@mweb.co.za</a><br><br>')

# Create, Open, and Attach the Interface Kit
try:
    ifk = InterfaceKit()
except RuntimeError as e:
    errors = errors + "<h5>Runtime Exception on object creation: " + e.details + "</h5>\n"
try:
    ifk.openPhidget()
except PhidgetException as e:
    errors = errors + "<h5>Phidget Exception on Open: " + e.details + "</h5>\n"
try:
    ifk.waitForAttach(10000)
except PhidgetException as e:
    errors = errors + "<h5>Phidget Exception on Attach: " + e.details + "</h5>\n"
    errors = errors + "<h5>If Phidget is 'Not Physically Attached' it may be in use</h5>\n"

try:
    ifk.setOutputState(0,1)
    print('<font size="16">Digital Output 0 set True</font>')
except:
    print('<font size="16">Error in setting Digital output 0 to True</font>')

print('<br><br><a href="WebInterfaceKit.py">Go Back to WebInterfaceKit main page. Do not use the browser back button, since the page will not refresh</a>')

print('</body>')
ifk.closePhidget()
exit()


Now create files SetTrue1.py, SetTrue2.py,..........SetTrue7.py by duplicating (cp in shell) SetTrue0.py and change the line with ifk.setOutputState(0,1) to ifk.setOutputState(?,1) where ? Is 1 to 7

Create a file SetFalse0.py, SetFalse1.py, .....SetFalse7.py with the same code as above, but change the line of ifk.setOutputState(0,1) to ifk.setOutputState(?,0).

All your files must be in the cgi-bin/interfacekit/ and they must be executable.

Feel free to e-mail me. I can reply with all the files

Trouble shooting web applications is difficult. My advise is to execute the code in the python shell for trouble shooting. The python shell will not render the web page, but it may assist in detecting where errors are.

Enjoy the first Full Web Interface Kit for the SBC that I know about!

grompot
Phidgetsian
Posts: 14
Joined: Thu Jul 31, 2014 2:15 am
Location: Nylstroom (Modimolle)
Contact:

Re: Full Functional Python Web Interfacekit for SBC

Postby grompot » Sun Mar 01, 2015 11:19 am


User avatar
mparadis
Site Admin
Posts: 649
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Full Functional Python Web Interfacekit for SBC

Postby mparadis » Mon Mar 02, 2015 8:11 am

Thank you for taking the time to make this guide, I'm sure our users will find it very helpful.


Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests