# Web server with MicroPython ###### tags: `python` `web` `boards` `raspberry` ## ESP32 ### Import machine ```python! #set pin2 output(defalut led led = machine.Pin(2,machine.Pin.OUT) led.off() ``` ### configure the wife ```pytho= import network import time networkName = 'zsystem' networkPassword = 'zang670623' station = network.WLAN(network.STA_IF) station.active(True) station.connect(networkName, networkPassword) while station.isconnected() == False: print("Wait to connect") time.sleep(2) print('connect successful') print(station.ifconfig()) ``` ### Configure the socket ```python= import socket socket = socket.socket(socket.AF_INET,socket.SOCK.STREAM) socket.bind(("",80)) #listen to port 80 on any address socket.listen(10) #set max of socket connections ``` socket.AF_INET use IP address 4 socket.SOCK.STREAM use TCP protocol socket.SOCK.DGRAM use UDP protocol ### Web socket Use web socket to communicate with the clients ### Establish server ```python! server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server.bind((LHOST,LPORT)) server.listen(10) ``` ### Get client information ```python= #client information socketClient, address =server.accept() print(f'[*] Connection form {address[0]}:{address[1]} has been edstablished !!!!') #get request request=socketClient.recv(1024) print(request.decode()) ``` ## Code ```python= #set pin import machine led = machine.Pin(2,machine.Pin.OUT) led.off() #Configure the wife import network import time networkName = 'zsystem' networkPassword = 'zang670623' station = network.WLAN(network.STA_IF) station.active(True) station.connect(networkName, networkPassword) while station.isconnected() == False: print("Wait to connect") time.sleep(2) print('connect successful') print(station.ifconfig()) #configure the socket import socket LHOST ='' LPORT =80 server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server.bind((LHOST,LPORT)) server.listen(10) def webPage(): if led.value()==1: ledState="on" print('ledState is on') elif led.value()==0: ledState="off" print('ledState is off') webSite= """ <html> <head> <meta content="width=device-width, initial-scale=1" name="viewport"></meta> </head> <body> <center><h2>ESP32 Web Server in MicroPython </h2></center> <center> <form> <button name="led" type="submit" value="on"> LED ON </button> <button name="led" type="submit" value="off"> LED OFF </button> </form> </center> <center><p>LED is now <strong>""" + ledState + """</strong>.</p></center> </body> </html>""" return webSite while True: #client information socketClient, address =server.accept() print(f'[*] Connection form {address[0]}:{address[1]} has been edstablished !!!!') #get request request=socketClient.recv(1024) print(request.decode()) request = str(request) ledUp = request.find('/?led=on') ledDown = request.find('/?led=off') if ledUp == 6 and led.value() == 0: print('led on') print(str(ledUp)) led.value(1) print("meow") elif ledDown == 6 and led.value() == 1: print('led off') print(str(ledDown)) led.value(0) print("meow") response = webPage() #socketClient send() socketClient.send('HTTP/1.1 200 ok\n') socketClient.send('Content-Type: text/html\n') socketClient.send('Connection: close\n\n') socketClient.sendall(response) ``` Result ![](https://i.imgur.com/K70TY8p.png) led off ![Uploading file..._bdaivm675]() ![](https://i.imgur.com/M9M3AFh.png) led on ![Uploading file..._ncjh1zdcb]() Occur problem When I try to continually click the button which one of both, the boards will disconnect XD ![](https://i.imgur.com/TLbzHOl.png) --- ## Raspberry [MicroWebSrv](https://github.com/jczic/MicroWebSrv2) [Reference Document](https://www.rototron.info/raspberry-pi-esp32-micropython-web-server-tutorial/) IDE connects to the board ![](https://i.imgur.com/O6dSB1D.png) Putty connects to the directory of raspberry ![Uploading file..._9iqc92fwl]() Import the library Use pip packet manager ```bash! pip install network ``` Read-Eval-Print Loop**,簡稱**REPL** ```bash= pip install repl ``` Update packet Manager ```bash= sudo apt update ``` Install the Apache ```bash= sudo apt install apache2 -y ``` Get border IP address Cmd >ifconfig >ip address >hostname -i ![](https://i.imgur.com/HIEn56Z.png) And we could know we connect successfully ## Edit web file WebDefaultPath: /var/www/html ---