update socket demo
This commit is contained in:
parent
63db27202e
commit
416dc2df93
|
@ -1,14 +1,8 @@
|
|||
import usocket
|
||||
import network
|
||||
import gc
|
||||
import os
|
||||
import usocket, network, time
|
||||
import lcd, image
|
||||
import machine
|
||||
from Maix import GPIO
|
||||
from machine import UART
|
||||
from board import board_info
|
||||
from fpioa_manager import fm
|
||||
import time
|
||||
from fpioa_manager import fm, board_info
|
||||
|
||||
|
||||
|
||||
|
@ -48,12 +42,9 @@ def wifi_reset():
|
|||
return None
|
||||
return nic
|
||||
|
||||
wifi_enable(0)
|
||||
time.sleep(1)
|
||||
wifi_enable(1)
|
||||
time.sleep(2)
|
||||
|
||||
nic = wifi_reset()
|
||||
if not nic:
|
||||
raise Exception("WiFi init fail")
|
||||
|
||||
nic.connect("Sipeed_2.4G","passwd")
|
||||
nic.ifconfig()
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
Refer to https://github.com/micropython/micropython-lib/tree/master/umqtt.simple
|
||||
|
|
@ -1,35 +1,63 @@
|
|||
import socket
|
||||
import network
|
||||
import gc
|
||||
import os
|
||||
import socket, network, time
|
||||
import lcd, image
|
||||
import machine
|
||||
from Maix import GPIO
|
||||
from board import board_info
|
||||
from fpioa_manager import fm
|
||||
from machine import UART
|
||||
from fpioa_manager import fm, board_info
|
||||
|
||||
#reset ESP8285 begin
|
||||
fm.register(8, fm.fpioa.GPIOHS0, force=True)
|
||||
wifi_en=GPIO(GPIO.GPIOHS0, GPIO.OUT)
|
||||
|
||||
fm.register(0, fm.fpioa.GPIOHS1, force=True)
|
||||
wifi_io0_en=GPIO(GPIO.GPIOHS1, GPIO.OUT)
|
||||
wifi_io0_en.value(0)
|
||||
|
||||
wifi_en.value(0) ##if ESP8285 init error ,try add ## or remove ## this line.
|
||||
time.sleep(2)
|
||||
wifi_en.value(1)
|
||||
time.sleep(5)
|
||||
#reset ESP8285 end
|
||||
fm.register(8, fm.fpioa.GPIOHS0, force=True)
|
||||
wifi_en=GPIO(GPIO.GPIOHS0,GPIO.OUT)
|
||||
fm.register(board_info.WIFI_RX,fm.fpioa.UART2_TX, force=True)
|
||||
fm.register(board_info.WIFI_TX,fm.fpioa.UART2_RX, force=True)
|
||||
|
||||
def wifi_enable(en):
|
||||
global wifi_en
|
||||
wifi_en.value(en)
|
||||
|
||||
def wifi_reset():
|
||||
global uart
|
||||
wifi_enable(0)
|
||||
time.sleep_ms(200)
|
||||
wifi_enable(1)
|
||||
time.sleep(2)
|
||||
uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096)
|
||||
tmp = uart.read()
|
||||
uart.write("AT+UART_CUR=921600,8,1,0,0\r\n")
|
||||
print(uart.read())
|
||||
uart = UART(UART.UART2,921600,timeout=1000, read_buf_len=10240) # important! baudrate too low or read_buf_len too small will loose data
|
||||
uart.write("AT\r\n")
|
||||
tmp = uart.read()
|
||||
print(tmp)
|
||||
if not tmp.endswith("OK\r\n"):
|
||||
print("reset fail")
|
||||
return None
|
||||
try:
|
||||
nic = network.ESP8285(uart)
|
||||
except Exception:
|
||||
return None
|
||||
return nic
|
||||
|
||||
nic = wifi_reset()
|
||||
if not nic:
|
||||
raise Exception("WiFi init fail")
|
||||
|
||||
fm.register(board_info.WIFI_RX, fm.fpioa.UART2_TX, force=True)
|
||||
fm.register(board_info.WIFI_TX, fm.fpioa.UART2_RX, force=True)
|
||||
uart = machine.UART(machine.UART.UART2, 115200,timeout=1000, read_buf_len=4096)
|
||||
nic=network.ESP8285(uart)
|
||||
nic.connect("Sipeed_2.4G","passwd")
|
||||
nic.ifconfig()
|
||||
|
||||
err = 0
|
||||
sock = socket.socket()
|
||||
addr = socket.getaddrinfo("dl.sipeed.com", 80)[0][-1]
|
||||
while 1:
|
||||
try:
|
||||
addr = socket.getaddrinfo("dl.sipeed.com", 80)[0][-1]
|
||||
break
|
||||
except Exception:
|
||||
err += 1
|
||||
if err > 5:
|
||||
raise Exception("get ip failed!")
|
||||
sock.connect(addr)
|
||||
sock.send('''GET /MAIX/MaixPy/assets/Alice.jpg HTTP/1.1
|
||||
Host: dl.sipeed.com
|
||||
|
@ -48,17 +76,24 @@ while True:
|
|||
print("rcv:", len(data))
|
||||
img = img + data
|
||||
|
||||
print(len(img))
|
||||
sock.close()
|
||||
|
||||
print("rcv len:", len(img))
|
||||
begin=img.find(b"\r\n\r\n")+4
|
||||
img = img[begin:begin+43756] ## jpg file size is 43756 byte
|
||||
print(len(img))
|
||||
if len(img) != 43756:
|
||||
raise Exception("recv jpg not complete, try again")
|
||||
print("image len:", len(img))
|
||||
|
||||
print("save to /flash/Alice.jpg")
|
||||
f = open("/flash/Alice.jpg","wb")
|
||||
f.write(img)
|
||||
f.close()
|
||||
del img
|
||||
print("save ok")
|
||||
print("display")
|
||||
img = image.Image("/flash/Alice.jpg")
|
||||
lcd.init()
|
||||
lcd.display(img)
|
||||
del img
|
||||
|
||||
|
|
Loading…
Reference in New Issue