2016-01-16 67 views
0

我在pigpio library的幫助下將我的Somfy sketch移植到Python,這樣我的樹莓派就可以在早晨打開窗簾並在日落後關閉它們。網絡服務器:如何從本地網頁運行python腳本

一切正常,我很滿意。

爲了增加一點互動性,我希望能夠通過樹莓派服務的網頁來控制百葉窗。 這意味着在網頁上按下按鈕時啓動Python腳本。

會有n個盲人,所以在頁面上有3 * n個按鈕(每個盲人{{UP,STOP,DOWN})。 他們可以使用兩個參數(盲人和命令)觸發相同的腳本,或爲每個按鈕觸發不同的腳本(我不介意)。

但我從來沒有建立一個網絡服務器。我幾乎不知道任何HTML,我從來沒有使用CGI,我也不明白它是什麼。

所以,我的問題是:

  1. 什麼是最簡單的(將具備一個頁面和觸發腳本)的Web服務器可以使用嗎?
  2. 要使用哪些HTML代碼?
  3. 最重要的是:如何點擊按鈕/鏈接將啓動一個腳本(可能傳遞兩個參數)?
  4. 如何確保這隻會在本地工作(檢查主機的IP,或在我的智能手機上下載證書,最簡單的路線)?

該腳本位於somfy目錄中。文本文件跟蹤滾動代碼和遠程地址也是如此。也許該頁面可以放在那裏? 如果你真的覺得你需要的代碼,我把它給你,但我不能肯定它是必要的:

def envoi_commande(telco, bouton): 
    checksum = 0 

    with open("somfy/" + telco + ".txt", 'r') as file: 
     data = file.readlines() 

    teleco = int(data[0], 16) 
    code = int(data[1]) 
    data[1] = str(code + 1) 

    print hex(teleco) 
    print code 

    with open("somfy/" + telco + ".txt", 'w') as file: 
     file.writelines(data) 

    pi = pigpio.pi() # connect to 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 much 
    frame[1] = bouton << 4 # Which button did you press? The 4 LSB will be the 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 : ", 
    for octet in frame: 
     print "0x%0.2X" % octet, 
    print "" 

    for i in range(0, 7): 
     checksum = checksum^frame[i]^(frame[i] >> 4) 

    checksum &= 0b1111; # We keep the last 4 bits only 

    frame[1] |= checksum; 

    print "With cks : ", 
    for octet in frame: 
     print "0x%0.2X" % octet, 
    print "" 

    for i in range(1, 7): 
     frame[i] ^= frame[i-1]; 

    print "Obfuscated :", 
    for 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)) 
    for i 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)) 

    for i 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 the frame) 
    for i 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)) 

    for i 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() 

回答

0

因爲你已經移植腳本到Python你可以使用Django

www.djangoproject.com

當按下按鈕時,請在視圖中調用您的python腳本。

讓我補充一些注意事項,其中很多可能取決於其他因素,因爲在完成此任務方面還有一些工作要做。

這是一個很好的學習網站html http://www.w3schools.com/

相關問題