【点灯鼠教程】[Maixpy]+[vofa+]实现K210串口图传【tcp图传】
作者:互联网
本节我们将进行VOFA+的tcp协议使用。
通过tcp,我们将可以通过网络,无线图传到上位机,很有实际意义。
由于博主使用的是bit板,自带的没有网卡固件,本节我们将使用外挂网卡(esp01s,小巧玲珑)
关于K10如何使用外挂网卡,我将于另外一个帖子详细讲解,本贴不另作赘述。
将esp01s和K210链接
第一步,配置wifi及socket
from fpioa_manager import fm
fm.register(10, fm.fpioa.UART1_TX, force=True)
fm.register(9, fm.fpioa.UART1_RX, force=True)
from machine import UART
import sensor, image, time
import struct
import network,socket
from setuart import setUART as SU
#wifi config
ssid="1234"
key="11111111"
baudrate=2048000
SU.setuart(baudrate)
uart = UART(UART.UART1,baudrate, timeout=1000, read_buf_len=10240)#创建外部可访问的uart
net=network.ESP8285(uart)
net.connect(ssid, key)
A=net.ifconfig()#cheak ip address. Your sock addr should be same to this
print(A)
#wifi config end
#socket config
ADDR = ("192.168.137.1", 1347)
sock = socket.socket()
sock.connect(ADDR)
sock.settimeout(10)
#socket config end
这里需要注意的是,esp01s默认的串口波特率是115200,传输数据时会很慢。于是我通过setuart,在esp01s链接后,修改波特率为204800(这里使用的是软配置,重启后将恢复到115200)
sock的地址,要与ifconfig打印出来的第三个数保持一致。
如
('192.168.137.196', '255.255.255.0', '192.168.137.1', '0', '0', 'd2:57:7b:35:a7:e1', '1234')
连接上sock以后,我们就可以发送数据了。这里继续使用justfloat协议。
稍作修改,将uart.write改为sock.send即可使用。
img = sensor.snapshot()
img = img.compress(quality=10)#压缩图片为jpg,质量为10%
perFrame[0] = 0
perFrame[1] = img.size()
perFrame[2] =img.width() # 图片宽度
perFrame[3] = img.height() # 图片高度
perFrame[4] =27
img_bytes = img.to_bytes() #转换图片为byte数组
#####此处为vota+上位机的前导帧,使用其他接收时自行修改#####
for i in perFrame:
buff=i.to_bytes(4,'little')
sock.send(buff)
sock.send(bytes(prendFrame))
######################################################
block = int(len(img_bytes)/2048)
for i in range(block):
sock.send(img_bytes[i*2048:(i+1)*2048])
sock.send(img_bytes[block*2048:])#按块发送
这里需要注意的是,tcp发包时有数据上限,所以要将数据切割成小于上限的n个小块发送。
vofa+打开后,设置为下图所示:
效果如图所示:
视频就不上了,惨不忍睹。。效果只能达到1fps,调了一下午没定位到问题,目测esp01s体质太差?或者是杜邦线太长?有能解决的希望回复下。
文章来源于Sipeed点灯鼠,原文链接:https://bbs.sipeed.com/thread/530
搬运请注明原文地址
最后,贴上setuart源码:
import time, network
from machine import UART
from fpioa_manager import fm
class setUART():
def init():
fm.register(10, fm.fpioa.UART1_TX, force=True)
fm.register(9, fm.fpioa.UART1_RX, force=True)
__class__.uart = UART(UART.UART1, 115200, timeout=1000, read_buf_len=8192)
def _at_cmd(cmd="AT\r\n", resp="OK\r\n", timeout=20):
__class__.uart.write(cmd) # "AT+GMR\r\n"
time.sleep_ms(timeout)
tmp = __class__.uart.read()
# print(tmp)
if tmp and tmp.endswith(resp):
return True
return False
def setuart(baudrate=1152000, reply=5):
__class__.init()
for i in range(reply):
print('set baudrate=%d...'%baudrate)
time.sleep_ms(500) # at start > 500ms
if __class__._at_cmd(timeout=500):
break
__class__._at_cmd()
__class__._at_cmd('AT+UART_CUR=%d,8,1,0,0\r\n'%baudrate, "OK\r\n")#设置当前波特率,重启模块恢复到115200
if __name__ == "__main__":
setUART.setuart()
uart2 = UART(UART.UART1,1152000, timeout=1000, read_buf_len=10240)
while(True):
uart2.write("AT\r\n")
B=uart2.read()
print(B)
补一下代码:
# Untitled - By: Lithromantic - 周二 3月 9 2021
from fpioa_manager import fm
fm.register(10, fm.fpioa.UART1_TX, force=True)
fm.register(9, fm.fpioa.UART1_RX, force=True)
from machine import UART
import sensor, image, time
import struct
import network,socket
from setuart import setUART as SU
#wifi config
ssid="1234"
key="11111111"
baudrate=1152000
SU.setuart(baudrate,1)
uart = UART(UART.UART1,baudrate, timeout=1000, read_buf_len=10240)#创建外部可访问的uart
net=network.ESP8285(uart)
net.connect(ssid, key)
#A=net.ifconfig()#cheak ip address. Your sock addr should be same to this
ADDR={}
ADDR[0]=net.ifconfig()[2]
print(ADDR[0])
#wifi config end
#socket config
ADDR = ("{}".format(ADDR[0]), 1347)
sock = socket.socket()
sock.connect(ADDR)
sock.settimeout(10)
#socket config end
#sensor.config
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing((200,200))
sensor.skip_frames(time = 2000)
#sensor.config end
perFrame=[0,0,0,0,0]
prendFrame =[0x00, 0x00, 0x80, 0x7f,0x00, 0x00, 0x80, 0x7f]
clock = time.clock()
while(True):
clock.tick()
img = sensor.snapshot()
img = img.compress(quality=10)#压缩图片为jpg,质量为10%
perFrame[0] = 0
perFrame[1] = img.size()
perFrame[2] =img.width() # 图片宽度
perFrame[3] = img.height() # 图片高度
perFrame[4] =27
img_bytes = img.to_bytes() #转换图片为byte数组
#####此处为vota+上位机的前导帧,使用其他接收时自行修改#####
for i in perFrame:
buff=i.to_bytes(4,'little')
sock.send(buff)
sock.send(bytes(prendFrame))
######################################################
block = int(len(img_bytes)/2048)
# for i in range(block):
# sock.send(img_bytes[i*2048:(i+1)*2048])
# sock.send(img_bytes[block*2048:])#按块发送
sock.send(img_bytes)#以数组形式发送图片
# time.sleep_ms(20)
3.12更新
更新日志:
1.大幅修改了代码排版,美观度提升了
2.完善了程序逻辑,稳定性提升了
# Untitled - By: Lithromantic - 周二 3月 9 2021
from fpioa_manager import fm
fm.register(10, fm.fpioa.UART1_TX, force=True)
fm.register(9, fm.fpioa.UART1_RX, force=True)
from machine import UART
import sensor, image, time
import network,socket,gc
from setuart import setUART as SU
perFrame=[0,0,320,240,27,0x7F800000,0x7F800000]
ssid="1234"
key="11111111"
def set_net(baudrate=1152000):
#wifi config
SU.setuart(baudrate,1)
uart = UART(UART.UART1,baudrate, timeout=1000, read_buf_len=10240)#创建外部可访问的uart
net=network.ESP8285(uart)
net.disconnect()
net.connect(ssid, key)
#A=net.ifconfig()#cheak ip address. Your sock addr should be same to this
ADDR={}
ADDR[0]=net.ifconfig()[2]
print(ADDR[0])
#wifi config end
#socket config
ADDR = ("{}".format(ADDR[0]), 1347)
return ADDR
#socket config end
def set_img():
#sensor.config
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
#sensor.config end
def get_img(quality=70):
img = sensor.snapshot()
img = img.compress(quality)#压缩图片为jpg,质量为10%
perFrame[1] = img.size()
img_bytes = img.to_bytes() #转换图片为byte数组
return img_bytes
def send_img(img_bytes):
sendbuff=b''
for i in perFrame:
buff=i.to_bytes(4,'little')
sendbuff=sendbuff+buff
sock.send(sendbuff)#以数组形式发送图片
block = int(len(img_bytes)/2048)
for i in range(block):
sock.send(img_bytes[i*2048:(i+1)*2048])
sock.send(img_bytes[block*2048:])
clock = time.clock()
sock = socket.socket()
sock.connect(set_net(2048000))
sock.settimeout(10)
set_img()
while(True):
send_img(get_img(50))
标签:vofa,img,bytes,sock,串口,图传,import,sensor,fm 来源: https://blog.csdn.net/xuguoliang757/article/details/117361261