|
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 | + |
7 | 53 |
|
8 | 54 | try: |
9 | | - import usocket as _socket |
10 | | -except: |
11 | | - import _socket |
12 | | -try: |
13 | | - import ussl as ssl |
| 55 | + import usocket as socket |
14 | 56 | 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 | + |
62 | 59 |
|
63 | 60 | class Response: |
64 | 61 |
|
@@ -116,14 +113,14 @@ def request(method, url, data=None, json=None, headers={}, stream=None, parse_he |
116 | 113 | host, port = host.split(":", 1) |
117 | 114 | port = int(port) |
118 | 115 |
|
119 | | - ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM) |
| 116 | + ai = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM) |
120 | 117 | ai = ai[0] |
121 | 118 |
|
122 | 119 | resp_d = None |
123 | 120 | if parse_headers is not False: |
124 | 121 | resp_d = {} |
125 | 122 |
|
126 | | - s = usocket.socket(ai[0], ai[1], ai[2]) |
| 123 | + s = socket.socket(ai[0], ai[1], ai[2]) |
127 | 124 | try: |
128 | 125 | s.connect(ai[-1]) |
129 | 126 | if proto == "https:": |
@@ -196,40 +193,83 @@ def request(method, url, data=None, json=None, headers={}, stream=None, parse_he |
196 | 193 | def head(url, **kw): |
197 | 194 | return request("HEAD", url, **kw) |
198 | 195 |
|
| 196 | + |
199 | 197 | def get(url, **kw): |
200 | 198 | return request("GET", url, **kw) |
201 | 199 |
|
| 200 | + |
202 | 201 | def post(url, **kw): |
203 | 202 | return request("POST", url, **kw) |
204 | 203 |
|
| 204 | + |
205 | 205 | def put(url, **kw): |
206 | 206 | return request("PUT", url, **kw) |
207 | 207 |
|
| 208 | + |
208 | 209 | def patch(url, **kw): |
209 | 210 | return request("PATCH", url, **kw) |
210 | 211 |
|
| 212 | + |
211 | 213 | def delete(url, **kw): |
212 | 214 | return request("DELETE", url, **kw) |
213 | 215 |
|
214 | | -headers ={ |
| 216 | + |
| 217 | +headers = { |
215 | 218 | "User-Agent": "MaixPy" |
216 | 219 | } |
217 | 220 |
|
218 | 221 | res = get("http://static.sipeed.com/example/MaixPy.jpg", headers=headers) |
219 | 222 | print("response:", res.status_code) |
220 | 223 | 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']))) |
222 | 226 |
|
223 | | -if len(content)!= int(res.headers['Content-Length']): |
| 227 | +if len(content) != int(res.headers['Content-Length']): |
224 | 228 | print("download img fail, not complete, try again") |
225 | 229 | else: |
226 | 230 | print("save to /flash/MaixPy.jpg") |
227 | | - f = open("/flash/MaixPy.jpg","wb") |
| 231 | + f = open("/flash/MaixPy.jpg", "wb") |
228 | 232 | f.write(content) |
229 | 233 | f.close() |
230 | 234 | del content |
231 | 235 | print("save ok") |
232 | 236 | print("display") |
| 237 | + import lcd |
| 238 | + import image |
233 | 239 | img = image.Image("/flash/MaixPy.jpg") |
234 | 240 | lcd.init() |
235 | 241 | 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 | +''' |
0 commit comments