網頁控制LED-micropython

 網頁控制LED-micropython

因為是用內建的LED所以不用接任何杜邦線

程式碼

from machine import Pin
import networksocket

ssid="CTK"
password="ctk674011"
led=Pin(2Pin.OUT)
ledState="OFF"
def sw_page():
    if led.value() == 1:
        ledState="ON"
    else:
        ledState="OFF"
    html="""
            <html>
        <head>
            <title>ESP32 網頁伺服器控制練習</title>
            <meta name="viewport" content="width=device-width, initial￾
            scale=1">
            <link rel="icon" href="data:,">
            <style>
                html {
                    text-align: center;
                    font-family: Arial;
                    display: inline-block;
                    margin: auto;}
                .button {
                    background-color: #e0e252; /* Green */
                    border: none;
                    color: white;
                    padding: 15px 35px;
                    text-align: center;
                    text-decoration: none;
                    display: inline-block;
                    font-size: 16px;
                }
                .button2 {
                    background-color: #b0b1a2; /* Green */
                    padding: 15px 32px;
                }
            </style>
        </head>

        <body>
            <h1>ESP32 網頁伺服器控制練習</h1>
            <p>GPIO-2 狀態: <strong>""" + ledState + """</strong></p>
            <p><a href="/on"><button class="button">ON</button></a></p>
            <p><a href="/off"><button class="button button2">OFF</button></a></p>
        </body>

        </html>
    """
    return html
wifi=network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssidpassword)
while wifi.isconnected()==False:
    pass
print("connected sccessful!")
print("IP: {}".format(wifi.ifconfig()))

s=socket.socket(socket.AF_INETsocket.SOCK_STREAM)
s.bind((''80))
s.listen(5)

while True:
    connaddr = s.accept()
    print("got a connect from %s"%str(addr))
    request = conn.recv(1024)
    request = str(request)
    print("context = %s"%request)
    led_on = request.find("/on")
    led_off = request.find("/off")
    if led_on == 6:
        led.value(1)
        print("ON")
    if led_off == 6:
        led.value(0)
        print("OFF")
    respones = sw_page()
    conn.send("HTTP/1.1 200 OK\n")
    conn.send("Context-Type: text/html\n")
    conn.send("Connection: close\n\n")
    conn.sendall(respones)
    conn.close()


解析

匯入腳位和wifi和套接的模組
from machine import Pin
import networksocket

wifi連線設定
ssid="基地台名稱"
password="基地台密碼"
wifi=network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssidpassword)
while wifi.isconnected()==False:
    pass
print("connected sccessful!")
print("IP: {}".format(wifi.ifconfig()))

主要這裡是前端網頁,if和else是在控制網頁的開啓就顯示ON,否則顯示OFF
def sw_page():
    if led.value() == 1:
        ledState="ON"
    else:
        ledState="OFF"
    html="""
            <html>
        <head>
            <title>ESP32 網頁伺服器控制練習</title>
            <meta name="viewport" content="width=device-width, initial￾
            scale=1">
            <link rel="icon" href="data:,">
            <style>
                html {
                    text-align: center;
                    font-family: Arial;
                    display: inline-block;
                    margin: auto;}
                .button {
                    background-color: #e0e252; /* Green */
                    border: none;
                    color: white;
                    padding: 15px 35px;
                    text-align: center;
                    text-decoration: none;
                    display: inline-block;
                    font-size: 16px;
                }
                .button2 {
                    background-color: #b0b1a2; /* Green */
                    padding: 15px 32px;
                }
            </style>
        </head>

        <body>
            <h1>ESP32 網頁伺服器控制練習</h1>
            <p>GPIO-2 狀態: <strong>""" + ledState + """</strong></p>
            <p><a href="/on"><button class="button">ON</button></a></p>
            <p><a href="/off"><button class="button button2">OFF</button></a></p>
        </body>

        </html>
    """
    return html
以下是套接
s=socket.socket(socket.AF_INETsocket.SOCK_STREAM)
s.bind((''80))
s.listen(5)

while True:
    connaddr = s.accept()
    print("got a connect from %s"%str(addr))
    request = conn.recv(1024)
    request = str(request)
    print("context = %s"%request)
    led_on = request.find("/on")
    led_off = request.find("/off")
    if led_on == 6:
        led.value(1)
        print("ON")
    if led_off == 6:
        led.value(0)
        print("OFF")
    respones = sw_page()
    conn.send("HTTP/1.1 200 OK\n")
    conn.send("Context-Type: text/html\n")
    conn.send("Connection: close\n\n")
    conn.sendall(respones)
    conn.close()






















留言

這個網誌中的熱門文章

天氣光環時鐘+農曆提醒(NODE-RED)專題作品-micropython