Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ce7ed2

Browse files
committed
[network] Complete
1 parent b00fc58 commit 2ce7ed2

17 files changed

Lines changed: 416 additions & 170 deletions

network/README.md

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,51 @@
1+
# Usage
12

3+
## network
24

5+
- [network_esp32.py](./network_esp32.py)
6+
- [network_espat.py](./network_espat.py)
7+
- [network_espw5k.py](./network_espw5k.py)
38

4-
## Use ESP32 as WiFi module
9+
> 使用 MaixPy IDE 的菜单功能【发送文件到板子】即可作为一个类库使用。
510
6-
* Download latest firmware of esp32 [here](https://github.com/sipeed/Maixduino_esp32_fimware/releases)
7-
* Upload firmware to ESP32 by [esptool](https://github.com/espressif/esptool) or other burn tools, e.g.
8-
```
9-
pip install esptool
10-
esptool.py --chip esp32 --port /dev/ttyUSB1 erase_flash
11-
esptool.py --chip esp32 --port /dev/ttyUSB1 --baud 1500000 write_flash -z 0x0000 maixduino_esp32_firmware_v1.4.0.bin
11+
Use the MaixPy IDE's menu functionality [send files to the board] as a class library.
12+
13+
```python
14+
from network_esp32 import wifi
15+
16+
from network_espat import wifi
17+
18+
from network_w5k import wlan
1219
```
1320

14-
* Then try demo
21+
## example
22+
23+
> 使用网卡连接到网络后才能进行以下操作。
24+
25+
Using the network card to connect to the network to make the following operation.
26+
27+
- [demo_esp32_ap_scan.py](./demo_esp32_ap_scan.py)
28+
- [demo_esp32_ping.py](./demo_esp32_ping.py)
29+
30+
- [demo_espat_ap_scan.py](./demo_espat_ap_scan.py)
31+
32+
- [demo_socket_tcp_client.py](./demo_socket_tcp_client.py)
33+
- [demo_socket_tcp_server.py](./demo_socket_tcp_server.py)
34+
35+
- [demo_socket_udp_client.py](./demo_socket_udp_client.py)
36+
- [demo_socket_udp_server.py](./demo_socket_udp_server.py)
37+
38+
- [demo_socket_mqtt.py](./demo_socket_mqtt.py)
39+
40+
- [demo_http_get_jpg.py](./demo_http_get_jpg.py)
41+
- [demo_socket_https.py](./demo_socket_https.py)
42+
43+
- [demo_socket_send_pic.py](./demo_socket_send_pic.py)
44+
- [demo_socket_pic_server.py](./demo_socket_pic_server.py)
45+
46+
## other
47+
48+
- [demo_espat_ap_test.py](./demo_espat_ap_test.py)
49+
- [espat_upgrade.py](./espat_upgrade.py)
1550

51+
- [demo_esp32_read_adc.py](./demo_esp32_read_adc.py)

network/demo_espat_ap_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This file is part of MaixPY
2+
# Copyright (c) sipeed.com
3+
#
4+
# Licensed under the MIT license:
5+
# http://www.opensource.org/licenses/mit-license.php
6+
#
7+
8+
from network_espat import wifi
9+
wifi.reset()
10+
11+
print(wifi.at_cmd("AT\r\n"))
12+
print(wifi.at_cmd("AT+GMR\r\n"))
13+
14+
'''
15+
>>> reset...
16+
b'\r\n\r\nOK\r\n'
17+
b'AT version:1.1.0.0(May 11 2016 18:09:56)\r\nSDK version:1.5.4(baaeaebb)\r\ncompile time:May 20 2016 15:06:44\r\nOK\r\n'
18+
MicroPython v0.5.1-136-g039f72b6c-dirty on 2020-11-18; Sipeed_M1 with kendryte-k210
19+
Type "help()" for more information.
20+
>>>
21+
'''

network/demo_http_get_jpg.py

Lines changed: 104 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,61 @@
1-
import usocket, network, time
2-
import lcd, image
3-
from Maix import GPIO
4-
from machine import UART
5-
from fpioa_manager import fm
6-
from board import board_info
1+
2+
# This file is part of MaixPY
3+
# Copyright (c) sipeed.com
4+
#
5+
# Licensed under the MIT license:
6+
# http://www.opensource.org/licenses/mit-license.php
7+
#
8+
9+
SSID = "Sipeed_2.4G"
10+
PASW = "xxxxxxxx"
11+
12+
13+
def enable_esp32():
14+
from network_esp32 import wifi
15+
if wifi.isconnected() == False:
16+
for i in range(5):
17+
try:
18+
# Running within 3 seconds of power-up can cause an SD load error
19+
# wifi.reset(is_hard=False)
20+
wifi.reset(is_hard=True)
21+
print('try AT connect wifi...')
22+
wifi.connect(SSID, PASW)
23+
if wifi.isconnected():
24+
break
25+
except Exception as e:
26+
print(e)
27+
print('network state:', wifi.isconnected(), wifi.ifconfig())
28+
29+
30+
enable_esp32()
31+
32+
33+
def enable_espat():
34+
from network_espat import wifi
35+
if wifi.isconnected() == False:
36+
for i in range(5):
37+
try:
38+
# Running within 3 seconds of power-up can cause an SD load error
39+
# wifi.reset(is_hard=False)
40+
wifi.reset()
41+
print('try AT connect wifi...')
42+
wifi.connect(SSID, PASW)
43+
if wifi.isconnected():
44+
break
45+
except Exception as e:
46+
print(e)
47+
print('network state:', wifi.isconnected(), wifi.ifconfig())
48+
49+
#enable_espat()
50+
51+
# from network_w5k import wlan
52+
753

854
try:
9-
import usocket as _socket
10-
except:
11-
import _socket
12-
try:
13-
import ussl as ssl
55+
import usocket as socket
1456
except:
15-
import ssl
16-
17-
18-
# for new MaixGO board, if not, remove it
19-
fm.register(0, fm.fpioa.GPIOHS1, force=True)
20-
wifi_io0_en=GPIO(GPIO.GPIOHS1, GPIO.OUT)
21-
wifi_io0_en.value(0)
22-
23-
# En SEP8285
24-
fm.register(8, fm.fpioa.GPIOHS0, force=True)
25-
wifi_en=GPIO(GPIO.GPIOHS0,GPIO.OUT)
26-
fm.register(board_info.WIFI_RX,fm.fpioa.UART2_TX, force=True)
27-
fm.register(board_info.WIFI_TX,fm.fpioa.UART2_RX, force=True)
28-
29-
def wifi_enable(en):
30-
global wifi_en
31-
wifi_en.value(en)
32-
33-
def wifi_reset():
34-
global uart
35-
wifi_enable(0)
36-
time.sleep_ms(200)
37-
wifi_enable(1)
38-
time.sleep(2)
39-
uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096)
40-
tmp = uart.read()
41-
uart.write("AT+UART_CUR=921600,8,1,0,0\r\n")
42-
print(uart.read())
43-
uart = UART(UART.UART2,921600,timeout=1000, read_buf_len=10240) # important! baudrate too low or read_buf_len too small will loose data
44-
uart.write("AT\r\n")
45-
tmp = uart.read()
46-
print(tmp)
47-
if not tmp.endswith("OK\r\n"):
48-
print("reset fail")
49-
return None
50-
try:
51-
nic = network.ESP8285(uart)
52-
except Exception:
53-
return None
54-
return nic
55-
56-
nic = wifi_reset()
57-
if not nic:
58-
raise Exception("WiFi init fail")
59-
60-
nic.connect("Sipeed_2.4G", "passwd")
61-
nic.ifconfig()
57+
import socket
58+
6259

6360
class Response:
6461

@@ -116,14 +113,14 @@ def request(method, url, data=None, json=None, headers={}, stream=None, parse_he
116113
host, port = host.split(":", 1)
117114
port = int(port)
118115

119-
ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)
116+
ai = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)
120117
ai = ai[0]
121118

122119
resp_d = None
123120
if parse_headers is not False:
124121
resp_d = {}
125122

126-
s = usocket.socket(ai[0], ai[1], ai[2])
123+
s = socket.socket(ai[0], ai[1], ai[2])
127124
try:
128125
s.connect(ai[-1])
129126
if proto == "https:":
@@ -196,40 +193,83 @@ def request(method, url, data=None, json=None, headers={}, stream=None, parse_he
196193
def head(url, **kw):
197194
return request("HEAD", url, **kw)
198195

196+
199197
def get(url, **kw):
200198
return request("GET", url, **kw)
201199

200+
202201
def post(url, **kw):
203202
return request("POST", url, **kw)
204203

204+
205205
def put(url, **kw):
206206
return request("PUT", url, **kw)
207207

208+
208209
def patch(url, **kw):
209210
return request("PATCH", url, **kw)
210211

212+
211213
def delete(url, **kw):
212214
return request("DELETE", url, **kw)
213215

214-
headers ={
216+
217+
headers = {
215218
"User-Agent": "MaixPy"
216219
}
217220

218221
res = get("http://static.sipeed.com/example/MaixPy.jpg", headers=headers)
219222
print("response:", res.status_code)
220223
content = res.content
221-
print("get img, length:{}, should be:{}".format(len(content), int(res.headers['Content-Length'])))
224+
print("get img, length:{}, should be:{}".format(
225+
len(content), int(res.headers['Content-Length'])))
222226

223-
if len(content)!= int(res.headers['Content-Length']):
227+
if len(content) != int(res.headers['Content-Length']):
224228
print("download img fail, not complete, try again")
225229
else:
226230
print("save to /flash/MaixPy.jpg")
227-
f = open("/flash/MaixPy.jpg","wb")
231+
f = open("/flash/MaixPy.jpg", "wb")
228232
f.write(content)
229233
f.close()
230234
del content
231235
print("save ok")
232236
print("display")
237+
import lcd
238+
import image
233239
img = image.Image("/flash/MaixPy.jpg")
234240
lcd.init()
235241
lcd.display(img)
242+
243+
'''
244+
MicroPython v0.5.1-136-g039f72b6c-dirty on 2020-11-18; Sipeed_M1 with kendryte-k210
245+
Type "help()" for more information.
246+
>>> network state: True ('192.168.0.143', '255.255.255.0', '192.168.0.1', '0', '0', 'b0:b9:8a:5b:be:7f', 'Sipeed_2.4G')
247+
248+
Traceback (most recent call last):
249+
File "<stdin>", line 220, in <module>
250+
File "<stdin>", line 197, in get
251+
File "<stdin>", line 179, in request
252+
File "<stdin>", line 124, in request
253+
OSError: -1
254+
MicroPython v0.5.1-136-g039f72b6c-dirty on 2020-11-18; Sipeed_M1 with kendryte-k210
255+
Type "help()" for more information.
256+
>>> network state: True ('192.168.0.143', '255.255.255.0', '192.168.0.1', '0', '0', 'b0:b9:8a:5b:be:7f', 'Sipeed_2.4G')
257+
[MaixPy] get_host_byname | get_host_byname failed
258+
259+
Traceback (most recent call last):
260+
File "<stdin>", line 220, in <module>
261+
File "<stdin>", line 197, in get
262+
File "<stdin>", line 115, in request
263+
OSError: [Errno 22] EINVAL
264+
MicroPython v0.5.1-136-g039f72b6c-dirty on 2020-11-18; Sipeed_M1 with kendryte-k210
265+
Type "help()" for more information.
266+
>>> network state: True ('192.168.0.143', '255.255.255.0', '192.168.0.1', '0', '0', 'b0:b9:8a:5b:be:7f', 'Sipeed_2.4G')
267+
response: 200
268+
get img, length:50611, should be:50611
269+
save to /flash/MaixPy.jpg
270+
save ok
271+
display
272+
MicroPython v0.5.1-136-g039f72b6c-dirty on 2020-11-18; Sipeed_M1 with kendryte-k210
273+
Type "help()" for more information.
274+
>>>
275+
'''

network/demo_socket_https.png

-82.5 KB
Binary file not shown.

network/demo_socket_https.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
# http://www.opensource.org/licenses/mit-license.php
66
#
77

8-
# Uasge see readme.md
9-
# from network_esp8285 import wifi
10-
# from network_w5k import wlan
11-
128
SSID = "Sipeed_2.4G"
13-
PASW = "Sipeed123."
9+
PASW = "xxxxxxxx"
1410

1511
def enable_esp32():
1612
from network_esp32 import wifi
@@ -30,8 +26,8 @@ def enable_esp32():
3026

3127
enable_esp32()
3228

33-
def enable_esp8285():
34-
from network_esp8285 import wifi
29+
def enable_espat():
30+
from network_espat import wifi
3531
if wifi.isconnected() == False:
3632
for i in range(5):
3733
try:
@@ -46,7 +42,9 @@ def enable_esp8285():
4642
print(e)
4743
print('network state:', wifi.isconnected(), wifi.ifconfig())
4844

49-
#enable_esp8285()
45+
#enable_espat()
46+
47+
# from network_w5k import wlan
5048

5149
try:
5250
import usocket as socket

network/demo_socket_mqtt.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11

22
# Refer to https://github.com/micropython/micropython-lib/tree/master/umqtt.simple
3-
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@
1313
1414
## pic_server.py
1515
16-
局域网图传 server
16+
LAN map transmission server
1717
18-
- 安装依赖
18+
- Installation dependency
1919
20-
更新 pip
20+
update pip
2121
2222
```shell
2323
# python -m pip install --upgrade pip
24-
python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple # 国内清华源,加速下载安装
24+
python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple # Domestic Tsinghua source
2525
```
2626
27-
安装 pygame
27+
update pygame
2828
2929
```shell
3030
# pip3 install pygame
31-
pip3 install pygame -i https://pypi.tuna.tsinghua.edu.cn/simple # 国内清华源,加速下载安装
31+
pip3 install pygame -i https://pypi.tuna.tsinghua.edu.cn/simple # Domestic Tsinghua source
3232
```
3333
34-
- 运行 server
34+
- run server
3535
3636
```shell
37-
python3 pic_serve.py
37+
python3 demo_socket_pic_server.py
3838
```
3939
'''
4040

0 commit comments

Comments
 (0)