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