raspberry pi - Webserver: how to run a python script from a local webpage -
i ported somfy sketch python of pigpio library, raspberry pi can open blinds in morning , close them after sunset.
everything works , i'm happy it.
to add bit of interactivity, able control blinds via webpage served raspberry pi. this mean launching python script when button pressed on webpage.
there n blinds, 3*n buttons ({up, stop, down} every blind) on page. either trigger same script 2 arguments (the blind , command) or trigger different script each button (i don't mind).
but i've never set webserver. barely know html , i've never used cgi, nor precisely understand is.
so, questions are:
- what's simplest (will have deliver one page , trigger scripts) webserver can use?
- what html code use?
- the important: how click on button/link start script (possibly passing 2 arguments)?
- how make sure work locally (checking ip of host, or downloading certificate on smartphone, easiest route)?
the script in somfy directory. same goes text files keeping track of rolling code , remote address. maybe page placed there well? if feel need code, i'm giving i'm not sure it's necessary:
def envoi_commande(telco, bouton): checksum = 0 open("somfy/" + telco + ".txt", 'r') file: data = file.readlines() teleco = int(data[0], 16) code = int(data[1]) data[1] = str(code + 1) print hex(teleco) print code open("somfy/" + telco + ".txt", 'w') file: file.writelines(data) pi = pigpio.pi() # connect pi if not pi.connected: exit() pi.wave_add_new() pi.set_mode(txgpio, pigpio.output) print "remote : " + "0x%0.2x" % teleco print "button : " + "0x%0.2x" % bouton print "rolling code : " + str(code) print "" frame[0] = 0xa7; # encryption key. doesn't matter frame[1] = bouton << 4 # button did press? 4 lsb checksum frame[2] = code >> 8 # rolling code (big endian) frame[3] = (code & 0xff) # rolling code frame[4] = teleco >> 16 # remote address frame[5] = ((teleco >> 8) & 0xff) # remote address frame[6] = (teleco & 0xff) # remote address print "frame : ", octet in frame: print "0x%0.2x" % octet, print "" in range(0, 7): checksum = checksum ^ frame[i] ^ (frame[i] >> 4) checksum &= 0b1111; # keep last 4 bits frame[1] |= checksum; print "with cks : ", octet in frame: print "0x%0.2x" % octet, print "" in range(1, 7): frame[i] ^= frame[i-1]; print "obfuscated :", octet in frame: print "0x%0.2x" % octet, print "" wf=[] wf.append(pigpio.pulse(1<<txgpio, 0, 9415)) wf.append(pigpio.pulse(0, 1<<txgpio, 89565)) in range(2): wf.append(pigpio.pulse(1<<txgpio, 0, 2560)) wf.append(pigpio.pulse(0, 1<<txgpio, 2560)) wf.append(pigpio.pulse(1<<txgpio, 0, 4550)) wf.append(pigpio.pulse(0, 1<<txgpio, 640)) in range (0, 56): if ((frame[i/8] >> (7 - (i%8))) & 1): wf.append(pigpio.pulse(0, 1<<txgpio, 640)) wf.append(pigpio.pulse(1<<txgpio, 0, 640)) else: wf.append(pigpio.pulse(1<<txgpio, 0, 640)) wf.append(pigpio.pulse(0, 1<<txgpio, 640)) wf.append(pigpio.pulse(0, 1<<txgpio, 30415)) #2 (i repeat frame) in range(7): wf.append(pigpio.pulse(1<<txgpio, 0, 2560)) wf.append(pigpio.pulse(0, 1<<txgpio, 2560)) wf.append(pigpio.pulse(1<<txgpio, 0, 4550)) wf.append(pigpio.pulse(0, 1<<txgpio, 640)) in range (0, 56): if ((frame[i/8] >> (7 - (i%8))) & 1): wf.append(pigpio.pulse(0, 1<<txgpio, 640)) wf.append(pigpio.pulse(1<<txgpio, 0, 640)) else: wf.append(pigpio.pulse(1<<txgpio, 0, 640)) wf.append(pigpio.pulse(0, 1<<txgpio, 640)) wf.append(pigpio.pulse(0, 1<<txgpio, 30415)) pi.wave_add_generic(wf) wid = pi.wave_create() pi.wave_send_once(wid) while pi.wave_tx_busy(): pass pi.wave_delete(wid) pi.stop()
because have ported script python use django
www.djangoproject.com
and when button pressed call python script within view.
let me add number of caveats in lot of depend on other factors there bit of work in completing task.
this site learning html http://www.w3schools.com/
Comments
Post a Comment