web2py

Web2Py controlling the lights!

I AM SOOO HAPPY!
Finally, i could control the test LED using my simple Web2py web app. All thanks to Niphlod, Cliff, Anthony, who has replied to my forum threads , and others who have replied to my other forum threads.

I did some slight modification after having a better understanding on URL and arguments. in test.html, there will be 2 buttons, On and Off.

web2py comfort on off
I will issue a URL request upon clicking on the “button” image, so this is how i should write:

<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>

‘test’ – what i want is in the “test” function
args = [‘bulbs’, ‘1’, ‘on’] – starting from index 0 storing ‘bulbs’, index 1 storing ‘1’ and so on.

In default.py, this is the crucial part that i kept on failing:

check for the conditions, remember the double equal sign “==” used for comparison.
because it is in the same function as the open port and login, i do not have to reopen and relogin to the system.

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")

Compared to my previous pathetic noob state here.

Another useful thing i learnt is the troubleshoot tool.
Adding this code

{{=response.toolbar()}}

at the top of the view (test.html), will display a set of useful toolbar. From the request tab, i could easily see the args that i pass in when i clicked on the button image.

web2py toolbar

or simply just

{{=request.args}}
{{=request.vars}}

which will display just the args.
web2py request args

Note that I have just changed the layout :p Got this from web2py layoutsKeep it Simple.
Much things can be done from here! But it’s time for CherryPy! 😀

Web2Py Dispatching, URL and my little progress

Thanks to Anthony who replied to my previous post regarding web2py, i read through the chapters about Dispatching and URL again. Although they aren’t very clear in my head but at least, i get the basic format.

I made some modification to my codes as follow:

in default.py i added:

if request.args(0)=='1':
serialport.write("\x03O!0101\x0D")
else:
serialport.write("\x03O!0100\x0D")

in test.html i added:

<a href = "{{=URL('test', args =['lightstate'], vars = dict(state='1'))}}"><img src ="/Comfort2/static/images/on.jpg" width ="75" height="75"></a>

Web2py Web App - screenshot login

Login page

So i click on the login button, and come to this page. i click on the “on” button and get to this link:

https://10.0.0.132/Comfort2/default/test/lightstate?state=1

Web2py Web App - screenshot

Turn On

Although it does not remain ON but only repeats the log in- turn on – turn off, i’m glad it did not issue me any error. 😡

I was just wondering is it because i use () instead of [] for request.args(0), i changed it to request.args[0] and it returns me this error right at the first page.

<type ‘exceptions.TypeError’> ‘NoneType’ object has no attribute ‘__getitem__’

okay, due to typo: request.arg[0]

changed to request.args[0] and it returns me:

<type ‘exceptions.IndexError’> list index out of range

Again, Niphlod answered my question in the google group. 🙂 i am so thankful for all the kind people who have responded to my questions and explained so many things to me.

Well then, i’m getting there! Looking forward to more replies and i shall continue tomorrow! 🙂

Ranting + web2py

1st of April, Happy April Fool everyone! It’s not a good day to crack some jokes today. I have been rather demotivating lately. Probably has used up all my energy and motivations for the previous project. Although I have moved on to a whole new better team, i feel a bit left out seeing the rest of the team are so excited with the idea while I’m partially dead. Meanwhile, there is another good idea with another good friend of mine. We have talked about it long ago but we were to busy to even discuss about it. Now my mind is depleted of inspirations. Happening currently is another project that I am doing with my sister. Probably, just probably, i was demoralized. Somehow.

I have a good Final Year Project supervisor and therefore i should come up with a good project proposal. Intending to do some social-technopreneur stuffs but not having strong motivations currently.

At work, I’m not moving on fast enough. It’s a total waste of time sitting here, uncertain of what to do and how to do. Self-learning is challenging indeed. But I am glad to have a nice company and colleagues.

Well sorry for the ranting.
Just an update on where am I stuck at web2py. Mapping of URL.

I intend to write a function in default.py with an argument. Not really sure what “argument” is but it seems like something one level down after function. Then, from View, i can pass the variables into the argument. Using “if” and “else”, i can selectively send the correct serial command.
so basically, it is something like, if i click on this image/link, it will send variable=”1″ to the controller. So at the controller, i can check if the receive variable is “1”, it will send this serial command. Else, it will send another one.

Here is part of the codes in default.py

test.lightstate="0"
if test.lightstate == "1":
serialport.write("\x03O!0101\x0D")
else:
serialport.write("\x03O!0100\x0D")
return dict(lightstate)

in test.html

<a href = "{{=URL('test',args =[lightstate], vars={1})}}"><img src ="/Comfort2/static/images/on.jpg" width ="75" height="75"</a>

the error that i get:

<type 'exceptions.NameError'> global name 'lightstate' is not defined

Okay, so how should i define that?

WEIRD Web2py

Morning Monday.

So i was renown for being a “Spoiler” because i always cant’ wait to tell my sisters what’s going to happen in the drama series. 😡 So, i spoilt quite a few things last week including my sd card and SIM card. My Web2py web app couldn’t work as well.

I asked around in the forum and Google group, desperately finding answers. Then, i reconnect everything and hey, where is the error? ARE YOU SERIOUS? i logged into the Comfort system from my web2py app?! Then, i added another serial command to turn on the light… it turned on. i was shocked. Seriously, totally have no idea what is going on. I didn’t change anything, did I?

Anyway, now i will start hesitating between web2py and cherrypy, again.

Update on cherrypy progress, i manage to get the keypad out but have not able to use it to let user to sign in.

Update on my KDrama, YES, i have finished watching “Rooftop Prince” and it is extremely nice. I cried non-stop towards the end. Such a wonderful and touching story. Will update on it in a post next time! T_T

And Thank God for Easter Day. ❤ Gloria! Glory to the Lord! 😉

Hello web2py

I have been lazying out on Raspberry Pi recently. Stuck at getting the web app on web2py to open the port at ttyAMA0. However it works fine on CherryPy.

Why. Why. Why.

Would it be better to just focus on CherryPy? :/ but i think Web2Py is newer and probably better and easier!
That is if and only if i am determined enough to finish reading (and digesting) through the 583 pages web2py manual. But it is so tiring to read it through the screen and time-wasting to study during working hours. If only i have an iPad or something.

Wait, did i just think of the Amazon Kindle or something? Haha, right. I should do something meaningful in the train while on my way to work, rather than sleeping. How about knitting? (That’s too outstanding. You probably wouldn’t want the whole pack of Sardine (we call the commuters that) to stare at you and start guessing your age.)

Not sure if it is a good idea to learn web2py and cherrypy concurrently. just hope that Mr Massimo can solve my problems soon.

Websites KIV:
Your first CherryPy application

CherryPy Tutorial

The CherryPy Documentation

Work related stuffs to do tomorrow:
– testing Z-wave responses (forgot to do it today!!!)

Understood  some Z-Wave problems today.

Other stuffs to do tonight:
– unofficial strategy planning with new team
– reply business emails
– EH, FINISH UP MY PROJECT REPORT!

– revamp website too.

Web2py Web App Progress

Today is more fruitful than yesterday.
I found out an awesome function in the web2py Administrative Interface. It is the “New Application Wizard” which will guide you through creating the most basic webpage.

Also, i just realized that there is a much better web2py tutorial here: http://www.web2py.com/books/default/chapter/29/03

So today i created a skeleton of my web app using the application wizard. Then i edited the layout.html, index.html, and default.py. I created default/test.html in accordance to the new function test() created in default.py. Also, i uploaded some pictures.

here is a screenshot:
web2py web app

Click on the light button to turn on

 

wev2py webapp

Flash a “welcome home” and the light turned on

 

here are some codes used:
in controller/default.py

def test():
    import serial
    import time
    response.flash=T(“welcome home!”)

in view/default/index.html

{{extend ‘layout.html’}}

<h1>Welcome to <strong>Cytech Comfort</strong></h1><h2>A new way to control your smart home</h2>

<img src = “/Comfort2/static/images/comfort.jpg” width=”268″ height=”79″>
<br>
<a href = “{{=URL(c=’default’, f=’test’)}}”><img src =”/Comfort2/static/images/off.jpg” width =”75″ height=”75″</a>

in view/default/test.html

{{extend ‘layout.html’}}
<h1>Welcome to <strong>Cytech Comfort</strong></h1><h2>A new way to control your smart home</h2>

<img src = “/Comfort2/static/images/comfort.jpg” width=”268″ height=”79″>
<br>
<img src = “/Comfort2/static/images/on.jpg” width =”75″ height =”75″>
<p>the lights are turned on</p>

However i could not proceed to send the serial commands (which is my main purpose!!) to the device i wish to control through Raspberry Pi.

i added the following codes to def test()
try:
serialport= serial.Serial(“/dev/ttyAMA0”, 9600, timeout=1)
except RuntimeError:
print “Error opening port”
quit()

It issued me a ticket:
<class ‘serial.serialutil.SerialException’> could not open port /dev/ttyAMA0: [Errno 13] Permission denied: ‘/dev/ttyAMA0’

Argh, headache. Sore eyes. 😦

Progress!

OHHH MYYYYY GOD! Thank you!
After struggling for a few days, unable to find a solution to Python – Raspberry Pi – Web2py (this triangle love, urgh), i have a little progress today!

Yesterday, i was able to log in to the home automation system through python right after my boss asked me about it. it is a bit ashamed when i told him, i’m sure it doesnt work but it did work when i tried it again.

So, is \x03 and is \x0D so here is my code in python console:

>>>import serial
>>>serialport = serial.Serial(“/dev/ttyAMA0”, 9600, timeout=0.5)
>>>serialport.write(“\x03LI1234\x0D”) #LI1234 is the default log in
[‘\x03LU01\x0D’] # it returns “user 1 logged in”

Now what? writing this into my web2py application!

I seriously have no idea how to start and the documentation on web2py is 500 over pages and how am i suppose to find the exact information i need? in fact, i am not sure what information i need. I requested some notes from my senior (a national professional Archer, wow!) regarding “Web Applications Design”. Argh, it’s a pity that my prince charming hasn’t take that module :/

Okay, called myself an EEE undergraduate, who has zero knowledge in Java, Python, HTML, CSS, PHP, SQL, Apache, etc huh?

I doubt myself.

Anyway, i struggled for the past 8 hours, fighting off the sleeping bugs (but failed, i dozed off when we are meeting a customer who was introducing his new products), and finally, i did something.

I can run a python script!

I failed for countless of time and i gave up testing it on web2py browser. So i created a simple python script… i realized a few points to note:
1. improper indentation (4 spaces ok!)
2. time is somehow important?
3. #!usr/bin/python at the top of the page
4. run in root

Here it is!

#!/usr/bin/python
import serial
import time
print "welcome to my test"
time.sleep(1)

try:
serialport=serial.Serial(“/dev/ttyAMA0”, 9600, timeout=0.5)
except RuntimeError:
print “error opening port”
quit()

serialport.write(“\x03LI1234\x0D”)
response=serialport.readlines(1)
print response
time.sleep(1)

Save it in home/pi, open up terminal:

$ sudo su
# chmod a+x test1.py
# ./test1.py

welcome to my test
[‘\x03LU01\x0D’]

woooohoooo 😀 log in success!

web2py Web App

It’s time to play around in web2py!
After having the web2py files in your computer, start your Raspberry Pi

server. On your browser, go to the server address (https://10.0.0.132:8000)
Log into Administrative Interface.
start a New Simpe Application.

Application Name: HomeControlTest

After that, it will appear in the “installed applications” list. Click edit to

view all the files templates. Scroll down to “static” and upload 2 files (on

and off switch image). rename the files to images/on.jpg and

images/off.jpg so that the images will be in the “images” folder.

Now what?
stuck for the rest of the day.

R-Pi Server up and running again! (change to static IP)

Fresh in the morning, i reinstall web2py from Web2Py webpage to my home directory ( /home/pi) and run the following:

cd /home/pi/web2py
sudo python2.6 web2py.py

I can start server at 127.0.0.1 (localhost) but not my public IP. It encountered an error:

ERROR:Rocket.Errors.Port8000:Socket 10.0.0.189:8000 in use by other process and it won't share

Meh, can’t find any solution from the web.

So i set my Raspberry Pi IP address to static such as follow:
(ref: Using a Static IP Address)

$ sudo nano /etc/network/interfaces

Change the line that reads

iface eth0 inet dhcp

to

iface eth0 inet static

Below this line enter the desired settings:

address 10.0.0.132 # a static IP address of your choice
netmask 255.255.255.0 # from here onwards, it depends on your router settings
network 10.0.0.0
broadcast 10.0.0.255
gateway 10.0.0.100

For the network mask, broadcast address and gateway, type:

$ ifconfig

or

ipconfig

in your same network window command prompt.

CTRL+O to save and CTRL+X to exit.

Then from your same network computer, try to ping to the new static address you just set by typing in the command prompt:

ping

You can do the same thing on Raspberry Pi as well and ping to your same network computer.

Then i reinstall web2py using the following codes which does not require me to run the web2py.py everytime i want to start the server:

sudo su

wget http://web2py.googlecode.com/hg/scripts/setup-web2py-ubuntu.sh
chmod +x setup-web2py-ubuntu.sh

./setup-web2py-ubuntu.sh

Remember to set an admin password when prompted!
Then go to a browser and type:
https://
for my case, https://10.0.0.132
then click “Admnistrative Interface” and it will prompt you for a your password!
And there you go! server up and running! But i am still wondering why must I install web2py… by right, after reinstalling apache, it should give me “It works!” or something. :/