7805浏览
查看: 7805|回复: 7

[项目分享] 零基础k210 dock实现esp8285wifi图传

[复制链接]
本帖最后由 1784450870 于 2021-3-9 16:02 编辑

首先你需要装maixpy ide开发环境、ch340驱动kflash gui
Maixpy ide开发环境如图:
Ide的下载位置:
零基础k210 dock实现esp8285wifi图传图4
Kflash gui如图:


你需要下载一个固件:我是下载的0.5.1固件maixpy_v0.5.1_124_ga3f708c.bin
如果你要找可以在sipeed的下载站里找


有余力可以装一个uPyLoader-win(由于我插了sd,不像openmv那样可以直接打开查看sd卡内容,这个软件可以帮助你查看sd卡内容)
如图:
零基础k210 dock实现esp8285wifi图传图7
如何看自己装没装ch340驱动:
计算机右键属性到设备管理器->端口
零基础k210 dock实现esp8285wifi图传图3
这些软件在哪下载
网址:https://dl.sipeed.com/shareURL/MAIX/tools(这个就是sipeed的下载站)


首先k210 dock开发版里自带的wifi模块是esp8285,出厂时候厂家已经帮你把esp8285at固件已经烧录进去了,如果你要更新固件可以参考文档的这个网址:https://maixpy.sipeed.com/zh/get_started/upgrade_esp8285_firmware.html
第一步开始验证 ESP8285 是否能够正常工作
最好使用xcom串口助手,我之前使用友善串口助手没测试成功,这个原因和串口助手内部的协议有关吧,推荐最好使用xcom
零基础k210 dock实现esp8285wifi图传图1
下面是我的运行结果:esp8285没有问题
零基础k210 dock实现esp8285wifi图传图5
现在就是注意2.4g和5g的区别:
这里两个信号指的都是频段,5g不是移动网里的5g,现在家有的WiFi路由器可以看到会产生两个频段,2.4g和5g,零基础k210 dock实现esp8285wifi图传图6
可以看到我这就是5g
该程序是基于的2.4g,可以手机开热点
零基础k210 dock实现esp8285wifi图传图2
Dock开发板代码(client端代码)
  1. import time, network
  2. from Maix import GPIO
  3. from machine import UART
  4. from fpioa_manager import fm
  5. from board import board_info
  6. WIFI_SSID   = "wxw"
  7. WIFI_PASSWD = "12345678"
  8. class wifi():
  9.     __is_m1w__ = True
  10.     uart = None
  11.     eb = None
  12.     nic = None
  13.     def init():
  14.         if __class__.__is_m1w__:
  15.             fm.register(0, fm.fpioa.GPIOHS1, force=True)
  16.             M1wPower=GPIO(GPIO.GPIOHS1, GPIO.OUT)
  17.             M1wPower.value(0) # b'\r\n ets Jan  8 2013,rst cause:1, boot mode:(7,6)\r\n\r\nwaiting for host\r\n'
  18.         fm.register(board_info.WIFI_EN, fm.fpioa.GPIOHS0) # board_info.WIFI_EN == IO 8
  19.         __class__.en = GPIO(GPIO.GPIOHS0,GPIO.OUT)
  20.         fm.register(board_info.WIFI_RX,fm.fpioa.UART2_TX) # board_info.WIFI_RX == IO 7
  21.         fm.register(board_info.WIFI_TX,fm.fpioa.UART2_RX) # board_info.WIFI_TX == IO 6
  22.         __class__.uart = UART(UART.UART2, 115200, timeout=1000, read_buf_len=8192)
  23.     def enable(en):
  24.         __class__.en.value(en)
  25.     def _at_cmd(cmd="AT\r\n", resp="OK\r\n", timeout=20):
  26.         __class__.uart.write(cmd) # "AT+GMR\r\n"
  27.         time.sleep_ms(timeout)
  28.         tmp = __class__.uart.read()
  29.         # print(tmp)
  30.         if tmp and tmp.endswith(resp):
  31.             return True
  32.         return False
  33.     def at_cmd(cmd="AT\r\n", timeout=20):
  34.         __class__.uart.write(cmd) # "AT+GMR\r\n"
  35.         time.sleep_ms(timeout)
  36.         tmp = __class__.uart.read()
  37.         return tmp
  38.     def reset(force=False, reply=5):
  39.         if force == False and __class__.isconnected():
  40.             return True
  41.         __class__.init()
  42.         for i in range(reply):
  43.             print('reset...')
  44.             __class__.enable(False)
  45.             time.sleep_ms(50)
  46.             __class__.enable(True)
  47.             time.sleep_ms(500) # at start > 500ms
  48.             if __class__._at_cmd(timeout=500):
  49.                 break
  50.         __class__._at_cmd()
  51.         __class__._at_cmd('AT+UART_CUR=921600,8,1,0,0\r\n', "OK\r\n")
  52.         __class__.uart = UART(UART.UART2, 921600, timeout=1000, read_buf_len=10240)
  53.         # important! baudrate too low or read_buf_len too small will loose data
  54.         #print(__class__._at_cmd())
  55.         try:
  56.             __class__.nic = network.ESP8285(__class__.uart)
  57.             time.sleep_ms(500) # wait at ready to connect
  58.         except Exception as e:
  59.             print(e)
  60.             return False
  61.         return True
  62.     def connect(ssid="wifi_name", pasw="pass_word"):
  63.         if __class__.nic != None:
  64.             return __class__.nic.connect(ssid, pasw)
  65.     def ifconfig(): # should check ip != 0.0.0.0
  66.         if __class__.nic != None:
  67.             return __class__.nic.ifconfig()
  68.     def isconnected():
  69.         if __class__.nic != None:
  70.             return __class__.nic.isconnected()
  71.         return False
  72.     def check_wifi_net(reply=5):
  73.         if wifi.isconnected() != True:
  74.             for i in range(reply):
  75.                 try:
  76.                     wifi.reset()
  77.                     print('try AT connect wifi...', wifi._at_cmd())
  78.                     wifi.connect(WIFI_SSID, WIFI_PASSWD)
  79.                     if wifi.isconnected():
  80.                         break
  81.                 except Exception as e:
  82.                     print(e)
  83.         return wifi.isconnected()
  84. wifi.check_wifi_net()
  85. addr       = ("192.168.43.6", 8000)
  86. ##################################
  87. import socket, time, sensor, image
  88. import lcd
  89. clock = time.clock()
  90. lcd.init()
  91. sensor.reset()
  92. sensor.set_pixformat(sensor.RGB565)
  93. sensor.set_framesize(sensor.QVGA)
  94. sensor.skip_frames(time = 2000)
  95. while True:
  96.     # send pic
  97.     while True:
  98.         try:
  99.             sock = socket.socket()
  100.             print(sock)
  101.             sock.connect(addr)
  102.             break
  103.         except Exception as e:
  104.             print("connect error:", e)
  105.             sock.close()
  106.             continue
  107.     sock.settimeout(5)
  108.     send_len, count, err = 0, 0, 0
  109.     while True:
  110.         clock.tick()
  111.         if err >=10:
  112.             print("socket broken")
  113.             break
  114.         img = sensor.snapshot()
  115.         lcd.display(img)
  116.         img = img.compress(quality=60)
  117.         img_bytes = img.to_bytes()
  118.         print("send len: ", len(img_bytes))
  119.         try:
  120.             block = int(len(img_bytes)/2048)
  121.             for i in range(block):
  122.                 send_len = sock.send(img_bytes[i*2048:(i+1)*2048])
  123.                 #time.sleep_ms(500)
  124.             send_len2 = sock.send(img_bytes[block*2048:])
  125.             #send_len = sock.send(img_bytes[0:2048])
  126.             #send_len = sock.send(img_bytes[2048:])
  127.             #time.sleep_ms(500)
  128.             if send_len == 0:
  129.                 raise Exception("send fail")
  130.         except OSError as e:
  131.             if e.args[0] == 128:
  132.                 print("connection closed")
  133.                 break
  134.         except Exception as e:
  135.             print("send fail:", e)
  136.             time.sleep(1)
  137.             err += 1
  138.             continue
  139.         count += 1
  140.         print("send:", count)
  141.         print("fps:", clock.fps())
  142.         #time.sleep_ms(500)
  143.     print("close now")
  144.     sock.close()
复制代码
服务端代码:
  1. import socket
  2. import time
  3. import threading
  4. import datetime
  5. import pygame
  6. from pygame.locals import QUIT, KEYDOWN, K_f, K_F11, FULLSCREEN
  7. local_ip = "192.168.43.6"
  8. local_port = 8000
  9. width = 320
  10. height = 240
  11. # jpeg 20 fps
  12. # esp32 spi dma temp buffer MAX Len: 4k
  13. def receiveThread(conn):
  14.     conn.settimeout(10)
  15.     conn_end = False
  16.     pack_size = 1024*5
  17.     while True:
  18.         if conn_end:
  19.             break
  20.         img = b""
  21.         tmp = b''
  22.         while True:
  23.             try:
  24.                 client_data = conn.recv(1)
  25.             except socket.timeout:
  26.                 conn_end = True
  27.                 break
  28.             if tmp == b'\xFF' and client_data == b'\xD8':
  29.                 img = b'\xFF\xD8'
  30.                 break
  31.             tmp = client_data
  32.         while True:
  33.             try:
  34.                 client_data = conn.recv(4096)
  35.             except socket.timeout:
  36.                 client_data = None
  37.                 conn_end = True
  38.             if not client_data:
  39.                 break
  40.             # print("received data,len:",len(client_data) )
  41.             img += client_data
  42.             if img[-2:] == b'\xFF\xD9':
  43.                 break
  44.             if len(client_data) > pack_size:
  45.                 break
  46.         print("recive end, pic len:", len(img))
  47.         if not img.startswith(b'\xFF\xD8') or not img.endswith(b'\xFF\xD9'):
  48.             print("image error")
  49.             continue
  50.         f = open("tmp.jpg", "wb")
  51.         f.write(img)
  52.         f.close()
  53.         try:
  54.             surface = pygame.image.load("tmp.jpg").convert()
  55.             screen.blit(surface, (0, 0))
  56.             pygame.display.update()
  57.             print("recieve ok")
  58.         except Exception as e:
  59.             print(e)
  60.     conn.close()
  61.     print("receive thread end")
  62. pygame.init()
  63. screen = pygame.display.set_mode((width, height), 0, 32)
  64. pygame.display.set_caption("pic from client")
  65. ip_port = (local_ip, local_port)
  66. sk = socket.socket()
  67. sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  68. sk.bind(ip_port)
  69. sk.listen(50)
  70. print("accept now,wait for client")
  71. def server():
  72.     while True:
  73.         conn, addr = sk.accept()
  74.         print("hello client,ip:")
  75.         print(addr)
  76.         t = threading.Thread(target=receiveThread, args=(conn,))
  77.         t.setDaemon(True)
  78.         t.start()
  79. tmp = threading.Thread(target=server, args=())
  80. tmp.setDaemon(True)
  81. tmp.start()
  82. while True:
  83.     for event in pygame.event.get():
  84.         if event.type == pygame.QUIT:
  85.             exit()
复制代码


如果以上不够仔细的话:大家可以参考spieed文档里的内容。
测试视频



7UX7R1WK}]89_92QT6AHC]1.png
DKL2)%[HZ@FJZV)}_ML]L6S.png
W`K1`~[B4}`ACICBHA31P88.png
ZFLJZXDQ8Y$E8F~7`Z9I{~W.png

xiaee  学徒

发表于 2021-3-1 11:06:32

前来回复,祝maixpy越来越牛逼
回复

使用道具 举报

empty  版主

发表于 2021-3-1 12:33:56

厉害厉害
回复

使用道具 举报

1784450870  见习技师
 楼主|

发表于 2021-3-2 21:11:22


好耶好耶
回复

使用道具 举报

我朝女王扔石头  见习技师

发表于 2021-3-7 15:27:11

那个,client端的代码乱码了,可以重新发一遍吗
回复

使用道具 举报

1784450870  见习技师
 楼主|

发表于 2021-3-9 15:27:04

我朝女王扔石头 发表于 2021-3-7 15:27
那个,client端的代码乱码了,可以重新发一遍吗
  1. import socket
  2. import time
  3. import threading
  4. import datetime
  5. import pygame
  6. from pygame.locals import QUIT, KEYDOWN, K_f, K_F11, FULLSCREEN
  7. local_ip = "192.168.43.6"
  8. local_port = 8000
  9. width = 320
  10. height = 240
  11. # jpeg 20 fps
  12. # esp32 spi dma temp buffer MAX Len: 4k
  13. def receiveThread(conn):
  14.     conn.settimeout(10)
  15.     conn_end = False
  16.     pack_size = 1024*5
  17.     while True:
  18.         if conn_end:
  19.             break
  20.         img = b""
  21.         tmp = b''
  22.         while True:
  23.             try:
  24.                 client_data = conn.recv(1)
  25.             except socket.timeout:
  26.                 conn_end = True
  27.                 break
  28.             if tmp == b'\xFF' and client_data == b'\xD8':
  29.                 img = b'\xFF\xD8'
  30.                 break
  31.             tmp = client_data
  32.         while True:
  33.             try:
  34.                 client_data = conn.recv(4096)
  35.             except socket.timeout:
  36.                 client_data = None
  37.                 conn_end = True
  38.             if not client_data:
  39.                 break
  40.             # print("received data,len:",len(client_data) )
  41.             img += client_data
  42.             if img[-2:] == b'\xFF\xD9':
  43.                 break
  44.             if len(client_data) > pack_size:
  45.                 break
  46.         print("recive end, pic len:", len(img))
  47.         if not img.startswith(b'\xFF\xD8') or not img.endswith(b'\xFF\xD9'):
  48.             print("image error")
  49.             continue
  50.         f = open("tmp.jpg", "wb")
  51.         f.write(img)
  52.         f.close()
  53.         try:
  54.             surface = pygame.image.load("tmp.jpg").convert()
  55.             screen.blit(surface, (0, 0))
  56.             pygame.display.update()
  57.             print("recieve ok")
  58.         except Exception as e:
  59.             print(e)
  60.     conn.close()
  61.     print("receive thread end")
  62. pygame.init()
  63. screen = pygame.display.set_mode((width, height), 0, 32)
  64. pygame.display.set_caption("pic from client")
  65. ip_port = (local_ip, local_port)
  66. sk = socket.socket()
  67. sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  68. sk.bind(ip_port)
  69. sk.listen(50)
  70. print("accept now,wait for client")
  71. def server():
  72.     while True:
  73.         conn, addr = sk.accept()
  74.         print("hello client,ip:")
  75.         print(addr)
  76.         t = threading.Thread(target=receiveThread, args=(conn,))
  77.         t.setDaemon(True)
  78.         t.start()
  79. tmp = threading.Thread(target=server, args=())
  80. tmp.setDaemon(True)
  81. tmp.start()
  82. while True:
  83.     for event in pygame.event.get():
  84.         if event.type == pygame.QUIT:
  85.             exit()
复制代码
回复

使用道具 举报

hwhsiww  学徒

发表于 2021-11-18 22:45:13

博主,这个服务端和客服端都是用K210做的吗?
回复

使用道具 举报

hwhsiww  学徒

发表于 2022-1-1 17:30:30

太牛了。。。。老哥你这个wifi的速率可以用来传输视频流吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail