logo

如何通过向OpenMV发送不同的命令获取距离或者是图片?

2023-08-11 11:06
U
Kevin

如何通过向OpenMV发送不同的命令获取距离或者是图片?

import sensor, image, time, network, usocket, sys, json

from machine import I2C
from vl53l1x import VL53L1X
import time
import gc
gc.enable()

i2c = I2C(2)
distance = VL53L1X(i2c)

SSID ='HSAuto'     # Network SSID
KEY  ='Aa123456'     # Network key
HOST =' 10.119.96.163'     # Use first available interface
PORT = 8082  # Arbitrary non-privileged port
dic = {'Fn':0,'Q':1,'T':2,'L':3,'Space':4,'Up':5,'Down':6,'N':7}

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.VGA)    # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)
sensor.set_auto_whitebal(True)
sensor.set_auto_gain(True)
#srnsor.set_
#sensor.set_hmirror(True) #水平方向翻转
#sensor.set_vflip(True) #垂直方向翻转
# 二维码的识别img = sensor.snapshot()
img_w = 640
img_h = 480

#OV5640 VGA 的像素大小
f_x = (2.8 / 3.6736) * img_w # 默认值
f_y = (2.8 / 2.7384) * img_h # 默认值
#图像的中心位置 分辨率的一半
c_x = img_w * 0.5 # 默认值(image.w * 0.5)
c_y = img_h * 0.5 # 默认值(image.h * 0.5)

# Init wlan module and connect to network
print("Trying to connect... (may take a while)...")
wlan = network.WINC()
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)
# We should have a valid IP now via DHCP
print(wlan.ifconfig())
# Create server socket
s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
# Bind and listen
print(HOST)
s.bind([HOST, PORT])
s.listen(5)
print(s)

s.setblocking(True)
def start_streaming(s):
    try:
        print ('Waiting for connections..')
        client, addr = s.accept()
        img_w = 640 * 0.5
        img_h = 480 * 0.5
        client.settimeout(2.0)
        print ('Connected to ' + addr[0] + ':' + str(addr[1]))
        data = client.recv(32)
        recv_data = str(data,'utf-8')
        print(recv_data)
        clock = time.clock()
        img = sensor.snapshot().lens_corr(0.9)
        if recv_data == "GetDistance":
             Camera_Distance = int(distance.read()) - 11
             print(Camera_Distance)
             client.send(str(Camera_Distance)) # +"mm \r\n"
        elif "GetImage" in recv_data:

            clock = time.clock()
            frame = sensor.snapshot()
            cframe = frame.compressed(quality=35)
            client.send("HTTP/1.1 200 OK\r\n" \
                        "Server: OpenMV\r\n" \
                        "Content-Type: image/jpeg\r\n\r\n" )

            print(len(cframe))
            client.send(cframe)
            client.send("END")
#            client.close()
#            cframe = img.compressed(quality=40)
#            client.write(cframe)
            client.close()
        client.close()
        gc.collect()
        print(gc.mem_free())
    except OSError as e:
        print(e)

while (True):
    try:
        start_streaming(s)
    except OSError as e:
        print("socket error: ", e)
        sys.print_exception(e)
2023-08-11 11:20
I
网红小智智 星瞳科技

recv_data是http的请求头,里面可以看到url。如果你访问的是192.168.1.1:8080/distance或者192.168.1.1:8080/image,recv_data是可以看到的。

2023-08-14 09:31
U
Kevin

@kidswong999 不是用Http请求的,是用的Socket通信 TCP 请求的,发完参数,如何返回图片呢,直接 client.send()吗?这个可以实现吗

2023-08-14 23:44
I
网红小智智 星瞳科技

代码从这里改:https://book.openmv.cc/MCU/wifi.html#只传输一张图片