Skip to content

Commit 522797c

Browse files
authored
Merge pull request #6 from troublegum/dev
Release 1.3
2 parents ea2653c + 110a369 commit 522797c

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

README.md

+40-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,31 @@ Use Thonny IDE or other IDE for upload your code in ESP8266/ESP32 board.
1111

1212
## Quick start
1313

14+
### Typical Wi-Fi connection code for ESP board
15+
```
16+
import network
17+
18+
wlan_id = "your wi-fi"
19+
wlan_pass = "your password"
20+
21+
wlan = network.WLAN(network.STA_IF)
22+
wlan.active(True)
23+
24+
while not wlan.isconnected():
25+
wlan.connect(wlan_id, wlan_pass)
26+
print("Connected... IP: " + wlan.ifconfig()[0])
27+
```
28+
1429

1530
### Hello world example
1631

32+
Type in browser http://IP_ADDRESS_ESP/ and you will see "HELLO WORLD" text.
33+
1734
```
1835
from micropyserver import MicroPyServer
1936
37+
''' there should be a wi-fi connection code here '''
38+
2039
def hello_world(request):
2140
''' request handler '''
2241
server.send("HELLO WORLD!")
@@ -29,9 +48,14 @@ server.start()
2948
```
3049

3150
### Add some routes
51+
52+
Type in browser http://IP_ADDRESS_ESP/ or http://IP_ADDRESS_ESP/another_action and your will see text "THIS IS INDEX PAGE!" or "THIS IS ANOTHER ACTION!".
53+
3254
```
3355
from micropyserver import MicroPyServer
3456
57+
''' there should be a wi-fi connection code here '''
58+
3559
def show_index(request):
3660
''' main request handler '''
3761
server.send("THIS IS INDEX PAGE!")
@@ -50,10 +74,14 @@ server.start()
5074

5175
### Send JSON response example
5276

77+
Type in browser http://IP_ADDRESS_ESP/ and you will see JSON response.
78+
5379
```
5480
from micropyserver import MicroPyServer
5581
import json
5682
83+
''' there should be a wi-fi connection code here '''
84+
5785
def return_json(request):
5886
''' request handler '''
5987
json_str = json.dumps({"param_one": 1, "param_two": 2})
@@ -69,17 +97,22 @@ server.start()
6997
```
7098

7199
### Access denied example
100+
101+
Type in browser http://IP_ADDRESS_ESP/ and you will see "THIS IS INDEX PAGE!" text or "ACCESS DENIED!" if your IP not equal "127.0.0.1".
102+
72103
```
73104
from micropyserver import MicroPyServer
74105
106+
''' there should be a wi-fi connection code here '''
107+
75108
def show_index(request):
76109
''' main request handler '''
77110
server.send("THIS IS INDEX PAGE!")
78111
79112
def on_request_handler(request, address):
80-
if str(address[0]) != '127.0.0.1':
113+
if str(address[0]) != "127.0.0.1":
81114
server.send("HTTP/1.0 403\r\n\r\n")
82-
server.send('ACCESS DENIED!')
115+
server.send("ACCESS DENIED!")
83116
return False
84117
return True
85118
@@ -95,7 +128,7 @@ server.start()
95128

96129
### Turn ON / OFF a LED example
97130

98-
You can remote control a LED via internet.
131+
You can remote control a LED via internet. Use your browser for on/off LED. Type in browser http://IP_ADDRESS_ESP/on or http://IP_ADDRESS_ESP/off.
99132

100133
![schema](https://habrastorage.org/webt/jb/xu/aj/jbxuaj0nr8fnqllbq27p_vfx3bw.png)
101134

@@ -150,6 +183,8 @@ server.start()
150183
from micropyserver import MicroPyServer
151184
import utils
152185
186+
''' there should be a wi-fi connection code here '''
187+
153188
def hello_world(request):
154189
''' request handler '''
155190
utils.send_response(server, "HELLO WORLD!")
@@ -188,3 +223,5 @@ Set handler on server error - server.on_error(handler)
188223
## Utils methods
189224

190225
Send response to client - utils.send_response(server, response, http_code=200, content_type="text/html", extend_headers=None)
226+
227+
Get HTTP request method - utils.get_request_method(request)

utils.py

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
THE SOFTWARE.
2727
"""
2828

29+
import re
30+
2931
""" HTTP response codes """
3032
HTTP_CODES = {
3133
100: 'Continue',
@@ -98,3 +100,9 @@ def send_response(server, response, http_code=200, content_type="text/html", ext
98100
server.send(header + "\r\n")
99101
server.send("\r\n")
100102
server.send(response)
103+
104+
105+
def get_request_method(request):
106+
""" return http request method """
107+
lines = request.split("\r\n")
108+
return re.search("^([A-Z]+)", lines[0]).group(1)

0 commit comments

Comments
 (0)