Temelelektronik.info

Web Socket Micropython ile Node Mcu Mavi Led Yakma

Devre


Bu devre Eylül ayından itibaren 153  defa ziyaret edilmiştir.

Video



https://www.youtube.com/watch?v=ecoDVAQeC44

Açıklama

#boot.py____________________________
try:
    import usocket as socket
except:
    import socket
    
from machine import Pin
import network
import esp
esp.osdebug(None)

import gc
gc.collect()

ssid='HUAWEI P smart 2019'
password='12345687'

station = network.WLAN(network.STA_IF)

station.active(True)

station.connect(ssid,password)

while station.isconnected()==False:
    pass

print ('baglanti kuruldu')
print (station.ifconfig())

relay=Pin(5,Pin.OUT)



#main.py_________________________

import machine
from machine import Pin
led=Pin(2,Pin.OUT)

html = """<!DOCTYPE html>
<html>
    <head> <title>ESP8266 Led Yak Sondur</title> </head>
    <body>
    <p>Led Yakma Sondurme Sayfasi #{}</p>
    <form action="/" method="POST" Content-Type="multipart/form-data" >    
    <input type="text" name="islem"><br><br>
    <input type="submit" value="Gonder" />
    </form>
    </table>
    </body>
</html>""".format(led.value())

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
    try:
        if gc.mem_free() < 102000:
            gc.collect()
        conn, addr = s.accept()
        conn.settimeout(3.0)
        print('Got a connection from %s' % str(addr))
        
#Request_____________________________________
        request = conn.recv(1024)
        conn.settimeout(None)
        request = str(request)
        print('Cevap uzunlugu:'+str(len(request)))
        print('Content = %s' % request)
        on = request.count('Yak')
        off = request.count('Sondur')
        if on > 0:#sayfanin iceriginde donen bilgiye led yak ya da sondur
            print('led ON')
            led.value(0)
        if off > 0:
            print('led OFF')
            led.value(1)

#Response_____________________________________

        
        if led.value()==1:
            response = html.replace("#0","#1")
        if led.value()==0:
            response = html.replace("#1","#0")
        conn.send('HTTP/1.1 200 OK\n')
        conn.send('Content-Type: text/html\n')
        conn.send('Connection: close\n\n')
        conn.sendall(response)
        conn.close()
        
    except OSError as e:
        conn.close()
        print('Connection closed')
 


Proje Videosu:https://www.youtube.com/watch?v=ecoDVAQeC44

Download

WebSocketledyakma.rar