ESP32 MicroPython Socket
Step 1: Why? The Purpose.
Control the built-in led on ESP32 dev board, via webpage remotely.
Step 2: How? The Process.
Create socketTest.py file inside progFiles folder.
Edit the main.py file to run the WebServer.
Check the functionality of webpage in your mobile or laptop.
Step 3: What? The Result.
Verify "What we Get" by "Why we are Doing".
1: Create socketTest.py file inside progFiles folder.
a) Start rshell with notepad --> Change directory to /pyboard/progFiles
--> edit socketTest.py (Case 1: file already present. System will simply open the existing file with notepad)
(Case 2: file is not present. System will create the file and open the blank file for edit)
b) Copy and paste the following code in socketTest.py. Save the file and close notepad.
#author robofun.in
#indentation = 2 space
from machine import Pin
try:
import usocket as socket
except:
import socket
p2led = Pin(2, Pin.OUT)
def web_page():
if p2led.value() == 1:
gpio_state="ON"
else:
gpio_state="OFF"
html = """
<html>
<head>
<title>ESP32 MicroPython Socket</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<style>
html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}.button{display: inline-block; background-color: #e7bd3b; border: none; border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
.button2{background-color: #4286f4;}
</style>
</head>
<body>
<h1>ESP32 MicroPython Socket</h1>
<p>
GPIO state: <strong>""" + gpio_state + """</strong>
</p>
<p>
<a href="/?p2led=on">
<button class="button">ON</button>
</a>
</p>
<p>
<a href="/?p2led=off">
<button class="button button2">OFF</button>
</a>
</p>
</body>
</html>"""
return html
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
request = str(request)
print('Content = %s' % request)
p2led_on = request.find('/?p2led=on')
p2led_off = request.find('/?p2led=off')
if p2led_on == 6:
print('LED ON')
p2led.value(1)
if p2led_off == 6:
print('LED OFF')
p2led.value(0)
response = web_page()
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()
2: Create/Edit main.py file inside pyboard folder.
Change directory to /pyboard
--> edit main.py (Case 1: file already present. System will simply open the existing file with notepad)
(Case 2: file is not present. System will create the file and open the blank file for edit)
a) Copy and paste the following code main.py. Save the file and close notepad.
#author robofun.in
#indentation = 2 space
import sys
sys.path.append('/prog')
import esp
esp.osdebug(None)
import gc
gc.collect()
import network
ssid = 'Your WiFi name'
password = 'Your WiFi password'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
print('Connection successful')
print(station.ifconfig())
#import firstBlinkTest
import socketTest
b) Start repl and press the EN (reset) button on ESP32 dev board.
At the end of booting process, you can see the message "Connection Successful" and network details like ip: 192.168.1.101 etc.
3: Check the functionality of webpage in your mobile or laptop.
a) Open a new tab and enter the ip address obtained during previous operation.
b) Press the "ON" switch in the browser.
c) Press the "OFF" switch in the browser.