Thanks Niphlod, Cliff, and Anthony for your helpful replies. :D
I have just successfully controlled a LED lamp after understanding the URL
and arguments. I am so happy!!
here are my codes for your inspection :D
in test.html
<a href = "{{=URL('test', args =['bulb','1', 'on'])}}"><img src
="/Comfort2/static/images/on.jpg" width ="75" height="75"></a>
<a href = "{{=URL('test', args =['bulb','1', 'off'])}}"><img src
="/Comfort2/static/images/off.jpg" width ="75" height="75"></a>
in default.py
if request.args(1)== '1' and request.args(2)=='on':
serialport.write("\x03O!0101\x0D")
elif request.args(1)== '1' and request.args(2)=='off':
serialport.write("\x03O!0100\x0D")
Attached is a work-in-progress cherrypy script. it has the jQuery keypad (i
got it from here http://jsfiddle.net/pjaaar/6Zh2V/ ). i was wondering if i
could use jquery in web2py, the web page may seem more interactive. But i
don't mind doing 2 different interfaces for cherrypy and web2py. :D
Afterall, learning all these are getting more exciting.
--
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
#!/usr/bin/python
"""
Simple example to show how to use cherrypy with jquery and jquery mobile.
"""
import cherrypy
class HelloWorld:
""" Sample request handler class. """
@cherrypy.expose
def index(self):
return '''<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0;" />
<meta name="viewport" content="width=device-width"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>
</title>
<link rel="stylesheet" href="static/jquery.mobile-1.2.0.min.css" />
<style>
/* App custom styles */
</style>
<script src="static/jquery.min.js">
</script>
<script src="static/jquery.mobile-1.2.0.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
var login_code = "";
$("#login_div").hide();
$('#myInput').click(function(){
$('#n_keypad').fadeToggle('fast');
});
$('.numero').click(function(){
if (!isNaN($('#myInput').val())) {
if (parseInt($('#myInput').val()) == 0) {
$('#myInput').val($(this).text());
} else {
$('#myInput').val($('#myInput').val() + $(this).text());
}
}
});
$("#login").click(function(){
$("#login_div").fadeToggle('fast');
});
$('.done').click(function(){
$('#n_keypad').hide('fast');
login_code = $('#myInput').val();
$.post( '/start' , {passcode:login_code.toString()});
$("#login_div").hide('fast');
});
$('.del').click(function(){
$('#myInput').val($('#myInput').val().substring(0,$('#myInput').val().length - 1));
});
$('.clear').click(function(){
$('#myInput').val('');
});
$('.zero').click(function(){
if (!isNaN($('#myInput').val())) {
if (parseInt($('#myInput').val()) != 0) {
$('#myInput').val($('#myInput').val() + $(this).text());
}
}
});
});
</script>
</head>
<body style="overflow: hidden;overflow-x:hidden;">
<div data-role="page" data-theme="a" id="page1">
<div data-theme="a" data-role="header" data-position="">
<h5>
Comfort Web Remote
</h5>
</div>
<div data-role="content">
<div class="ui-grid-b">
<div class="ui-block-a">
<button type="button" id="start" data-role="button" data-transition="fade" >
Start
</button>
</div>
<div class="ui-block-b">
<button type="button" id="enter" data-role="button" data-transition="fade">
Enter
</button>
</div>
<div class="ui-block-c">
<button type="button" id="esc" data-role="button" data-transition="fade">
Esc
</button>
</div>
<div class="ui-block-a">
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<label for="power">
</label>
<select name="power" id="power" data-theme="a" data-role="slider">
<option value="off">
Off
</option>
<option value="on">
On
</option>
</select>
</fieldset>
</div>
</div>
</div>
<div class="ui-grid-c">
<div class="ui-block-a">
<button type="button" id="login" data-role="button" data-transition="fade" >
LOGIN
</button>
</div>
<div class="ui-block-a" id="login_div">
<input style="background: white; color: black;" type="text" readonly="readonly" id="myInput"/>
<table class="ui-bar-a" id="n_keypad" style="display: none; -khtml-user-select: none;">
<tr>
<td><a data-role="button" data-theme="b" class="numero">7</a></td>
<td><a data-role="button" data-theme="b" class="numero">8</a></td>
<td><a data-role="button" data-theme="b" class="numero">9</a></td>
<td><a data-role="button" data-theme="e" class="del">Del</a></td>
</tr>
<tr>
<td><a data-role="button" data-theme="b" class="numero">4</a></td>
<td><a data-role="button" data-theme="b" class="numero">5</a></td>
<td><a data-role="button" data-theme="b" class="numero">6</a></td>
<td><a data-role="button" data-theme="e" class="clear">Clear</a></td>
</tr>
<tr>
<td><a data-role="button" data-theme="b" class="numero">1</a></td>
<td><a data-role="button" data-theme="b" class="numero">2</a></td>
<td><a data-role="button" data-theme="b" class="numero">3</a></td>
<td><a data-role="button" data-theme="e"> </a></td>
</tr>
<tr>
<td><a data-role="button" data-theme="e" class="numero">*</a></td>
<td><a data-role="button" data-theme="b" class="zero">0</a></td>
<td><a data-role="button" data-theme="e" class="numero">#</a></td>
<td><a data-role="button" data-theme="e" class="done">Done</a></td>
</tr>
</table>
</div>
</div>
<script>
//App custom javascript
</script>
</body>
</html>
'''
@cherrypy.expose
def request(self, **data):
# Then to access the data do the following
#print data
key = data['key_pressed'].lower()
if key == "start":
print "start"
elif key == "enter":
#arm
serialport= serial.Serial ("/dev/ttyAMA0", 9600, timeout=0.5)
serialport.write("\x03M!031234\x0D")
powerstat=serialport.readlines(1)
print powerstat
time.sleep(1)
elif key == "power_on":
#switch on light
serialport= serial.Serial ("/dev/ttyAMA0", 9600, timeout=0.5)
serialport.write("\x03O!0101\x0D")
powerstat=serialport.readlines(1)
print powerstat
time.sleep(1)
elif key == "esc":
#disarm
serialport= serial.Serial ("/dev/ttyAMA0", 9600, timeout=0.5)
serialport.write("\x03M!001234\x0D")
powerstat=serialport.readlines(1)
print powerstat
time.sleep(1)
elif key == "power_off":
#switch off light
serialport= serial.Serial ("/dev/ttyAMA0", 9600, timeout=0.5)
serialport.write("\x03O!0100\x0D")
powerstat=serialport.readlines(1)
print powerstat
time.sleep(1)
else:
print key
@cherrypy.expose
def start (self, **data):
passcode = data.get("passcode", None)
print "logging in using passcode %s"%passcode
# this is where you should add your device send code
import serial
import time
serialport=serial.Serial ("/dev/ttyAMA0", 9600, timeout=0.5)
serialport.write("\x03LI%s\x0D"%passcode)
#i would like to write "\x03LI1234\x0D"
reply=serialport.readlines(1)
print reply
time.sleep(1)
import os.path
tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')
if __name__ == '__main__':
# CherryPy always starts with app.root when trying to map request URIs
# to objects, so we need to mount a request handler root. A request
# to '/' will be mapped to HelloWorld().index().
cherrypy.quickstart(HelloWorld(), config=tutconf)
else:
# This branch is for the test suite; you can ignore it.
cherrypy.tree.mount(HelloWorld(), config=tutconf)