Can any Raspberry Pi users out there or web people help? I Want to use webpage to control Neopixels plugged into a Raspberry Pi. The webpage will have slider controls for R, G, B & overall brightness. I was initially thinking of using cgi-bin scripts using python, but I want the colour to change as the slider moves so I don't know if that would be responsive enough.
Reading around, there seem to be many python-based solutions* so I could use cgi scripts, web sockets, webpy, flask, danjo or others. Anyone got a recommendation of which way to go. Lightweight and no security will do.
*I want to use python if possible as that's what I'm most familiar with ATM, with a bit of Javascript as required.
Any advice appreciated.
Steve
Hi Steve,
It's pretty trivial to set up a web server in Python directly, so you could knock up a very simple server which both serves your web page and responds to JavaScript/Ajax requests from it. Something like this (slightly pseudo-code)...
#!/usr/bin/env python
import urlparse from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class Handler(BaseHTTPRequestHandler):
""" A simple Python web server """
def do_GET(self):
"""Handle HTTP Get requests"""
urlpath = urlparse.urlparse(self.path) url = urlpath.path
if url eq "/myledwebpage.html"
self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write("<html>my web content and javascript here</html>)
elsif url eq "/myajaxhandler":
# get your LED state from URL params self.send_response(200) GPIO.setStuffHere()
else:
self.send_response(403) # forbidden self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write("Forbidden from " + path)
if __name__ == "__main__":
print('Starting HTTP server on port 8080") server = HTTPServer(("", 8080), Handler) server.serve_forever()
On 11/10/17 18:28, steve-ALUG@hst.me.uk wrote:
Can any Raspberry Pi users out there or web people help? I Want to use webpage to control Neopixels plugged into a Raspberry Pi. The webpage will have slider controls for R, G, B & overall brightness. I was initially thinking of using cgi-bin scripts using python, but I want the colour to change as the slider moves so I don't know if that would be responsive enough.
Reading around, there seem to be many python-based solutions* so I could use cgi scripts, web sockets, webpy, flask, danjo or others. Anyone got a recommendation of which way to go. Lightweight and no security will do.
*I want to use python if possible as that's what I'm most familiar with ATM, with a bit of Javascript as required.
Any advice appreciated.
Steve
main@lists.alug.org.uk http://www.alug.org.uk/ https://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
On 12/10/17 10:04, Simon Ransome wrote:
Hi Steve,
It's pretty trivial to set up a web server in Python directly, so you could knock up a very simple server which both serves your web page and responds to JavaScript/Ajax requests from it. Something like this (slightly pseudo-code)...
Thank you Simon.
I'll have a try. I'll also have to have a google for Ajax as I'm not at all familiar with it.
Cheers Steve
On 12/10/17 10:04, Simon Ransome wrote: [SNIP]
elsif url eq "/myajaxhandler":
# get your LED state from URL params self.send_response(200) GPIO.setStuffHere()
So I guess to call this I'd just to a HTTP "Get" to a URL "http://The.IP.Address.xx/myajaxhandler?Some=Params,Here=etc"
And then decode the params & bob's my uncle?
Steve
On 12/10/17 15:36, steve-ALUG@hst.me.uk wrote:
On 12/10/17 10:04, Simon Ransome wrote: [SNIP]
elsif url eq "/myajaxhandler": # get your LED state from URL params self.send_response(200) GPIO.setStuffHere()
So I guess to call this I'd just to a HTTP "Get" to a URL "http://The.IP.Address.xx/myajaxhandler?Some=Params,Here=etc"
And then decode the params & bob's my uncle?
Yes, exactly that. Your onChange or other event handler on the sliders responds to the slide event. You would then build a URL with the extra parameters containing the values of the various sliders, and use that as the basis for an XmlHTTPRequest (which is the Ajax bit). If you wanted to be a bit more cross-browser you could use an Ajax helper from jQuery or some other JavaScript library.
Your server then decodes it (urlparse can do that) and in theory your JavaScript doesn't even need to bother with handling the response, but you may want to add some sort of throttling (like waiting for any current requests to complete) if your sliders generate too many events.
There's a simple example of the Ajax call here: https://www.w3schools.com/xml/ajax_intro.asp
Simon
On 12/10/17 16:25, Simon Ransome wrote: [SNIP]
There's a simple example of the Ajax call here: https://www.w3schools.com/xml/ajax_intro.asp
Simon
Cheers Simon
Steve