-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
176 lines (143 loc) · 4.77 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Author: Om Shingare
# Language: MicroPython
# Project Name: ESP32 Simple Web Server
import socket
import network
import machine
import networkcredentials
import time
led = machine.Pin(2, machine.Pin.OUT)
led.off()
sta = network.WLAN(network.STA_IF)
if not sta.isconnected():
print('Connecting to the network...')
sta.active(True)
sta.connect('try', None)
while not sta.isconnected():
pass
print('Network config:', sta.ifconfig())
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80)) # Specify the port number (80 for HTTP)
s.listen(50)
def web_page():
led_state = get_led_state()
html_page = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="Control your ESP32 using a web interface.">
<title>ESP32 Web Server</title>
<meta name="favicon" href="https://omshingare.me/assets/logo-12777f7b.svg">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
<style>
body {{
background-color: #121212;
color: #ffffff;
font-family: 'Roboto', sans-serif;
margin: 0;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}}
.container {{
background-color: #1e1e1e;
border-radius: 10px;
box-shadow: 0 0 10px rgba(255, 255, 255, 0.1);
padding: 20px;
text-align: center;
}}
h2 {{
color: #03a9f4;
}}
button {{
font-size: 18px;
padding: 12px 24px;
margin: 5px;
}}
button.btn-success {{
background-color: #4caf50;
}}
button.btn-danger {{
background-color: #f44336;
}}
button.btn-warning {{
background-color: #ff9800;
}}
p {{
font-size: 20px;
color: #ccc;
}}
</style>
</head>
<body>
<div class="container">
<h2 class="mt-4">ESP32 Web Server</h2>
<p>This web interface allows you to control your ESP32 remotely.</p>
<form class="mt-4">
<button class="btn btn-success" name="LED" type="submit" value="1">LED ON</button>
<button class="btn btn-danger" name="LED" type="submit" value="0">LED OFF</button>
<button class="btn btn-warning" name="LED" type="submit" value="2">LED BLINK</button>
</form>
<p class="mt-4">LED is currently <strong>{led_state}</strong>.</p>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>"""
return html_page
def get_led_state():
if isLedBlinking:
return 'Blinking'
elif led.value() == 1:
return 'ON'
elif led.value() == 0:
return 'OFF'
tim0 = machine.Timer(0)
def handle_callback(timer):
led.value(not led.value())
isLedBlinking = False
while True:
# Socket accept()
conn, addr = s.accept()
print("Got connection from %s" % str(addr))
# Socket receive()
request = conn.recv(1024)
print("")
print("")
print("Content %s" % str(request))
# Socket send()
request = str(request)
led_on = request.find('/?LED=1')
led_off = request.find('/?LED=0')
led_blink = request.find('/?LED=2')
if led_on == 6:
print('LED ON')
print(str(led_on))
led.value(1)
if isLedBlinking:
tim0.deinit()
isLedBlinking = False
elif led_off == 6:
print('LED OFF')
print(str(led_off))
led.value(0)
if isLedBlinking:
tim0.deinit()
isLedBlinking = False
elif led_blink == 6:
print('LED Blinking')
print(str(led_blink))
isLedBlinking = True
tim0.init(period=500, mode=machine.Timer.PERIODIC, callback=handle_callback)
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()