M5StickV+M5StickC发送HTTP视频流-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2266|回复: 6

M5StickV+M5StickC发送HTTP视频流

[复制链接]
发表于 2020-3-2 16:20 | 显示全部楼层 |阅读模式
本帖最后由 vany5921 于 2020-3-5 09:42 编辑

      M5StickC通过串口发送图像数据,由StickC的ESP32建立webserver,使用浏览器访问本地地址即可实时获取到视频流,参考链接https://homemadegarbage.com/ai13 IMG_0722.JPG 截屏2020-03-02 16.06.45.png


M5StickC端程序
[mw_shl_code=arduino,true]#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <M5StickC.h>
//#include "setting.h"

const char* ssid = "SSID";
const char* password = "PASSWORD";
String msg[4];

WebServer server(80);
uint8_t *buff;
static const int RX_BUF_SIZE = 100000;

void disp(String s1, String s2, String s3, String s4) {
  if (s1 != "") {
    msg[0] = s1;
  }
  if (s2 != "") {
    msg[1] = s2;
  }
  if (s3 != "") {
    msg[2] = s3;
  }
  if (s4 != "") {
    msg[3] = s4;
  }
  M5.Lcd.fillScreen(TFT_BLACK);
  M5.Lcd.setTextFont(2);
  M5.Lcd.setCursor(0, 0);
  M5.Lcd.print(msg[0]);

  M5.Lcd.setTextFont(1);
  M5.Lcd.setCursor(0, 20);
  M5.Lcd.print(msg[1]);
  
  M5.Lcd.setCursor(0, 40);
  M5.Lcd.print(msg[2]);
  
  M5.Lcd.setTextFont(2);
  M5.Lcd.setCursor(0, 60);
  M5.Lcd.print(msg[3]);
}

bool transfer(WiFiClient client) {
  Serial.println("transfer start");

  Serial2.write("get");
  delay(50);

  uint8_t buf[3];
  int len = Serial2.readBytes(buf, 3);
  if (len != 3) {
    Serial.println("transfer get length error");
    return false;
  }
  int pLen = (uint32_t)(buf[0] << 16) | (buf[1] << 8) | buf[2];
  Serial.print("transfer picture length : ");
  Serial.println(pLen);
  int tLen = pLen;
  while (1) {
    len = Serial2.readBytes(buff, tLen);
    if (len == 0) {
      Serial.print("transfer get picture error ");
      Serial.print(len);
      Serial.print("-");
      Serial.println(tLen);
      return false;
    } else if (len != tLen) {
      Serial.print("transfer get picture retry ");
      Serial.print(len);
      Serial.print("-");
      Serial.println(tLen);
      tLen -= len;
    } else {
      break;
    }
    if (!client.connected()) {
      break;
    }
    delay(10);
  }

  len = Serial2.readBytes(buf, 3);
  if (len != 3) {
    Serial.println("transfer get footer error");
    return false;
  }
  client.write(buff, pLen);
  //
  Serial.println("transfer end");
  return true;
}

void sendPic() {
  disp("", "", "", "/pic");
  WiFiClient client = server.client();
  Serial.println("sendPic start");

  transfer(client);

  Serial.println("sendPic end");
  disp("", "", "", "/pic end");
}

void streamPic() {
  disp("", "", "", "/stream");
  Serial.println("streamPic start");
  WiFiClient client = server.client();
  String response = "HTTP/1.1 200 OK\r\n";
  response += "Content-Type: multipart/x-mixed-replace; boundary=frame\r\n\r\n";
  server.sendContent(response);

  while (1) {
    if (!client.connected()) {
      break;
    }
    response = "--frame\r\n";
    response += "Content-Type: image/jpeg\r\n\r\n";
    server.sendContent(response);

    if (!transfer(client)) {
      break;
    }
   
    server.sendContent("\r\n");
    if (!client.connected()) {
      break;
    }
  }
  Serial.println("streamPic end");
  disp("", "", "", "/stream end");
}

void setup() {
  M5.begin();
  M5.begin();
  M5.Lcd.setRotation(1);
   
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, 32, 33);

  buff = (uint8_t *) malloc(sizeof(uint8_t) * RX_BUF_SIZE);

  disp(" AP : " +  String(ssid), "", "", "STATUS : Connecting...");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.print("  http://");
  Serial.print(WiFi.localIP());
  Serial.println("/pic");
  Serial.print("  http://");
  Serial.print(WiFi.localIP());
  Serial.println("/stream");
  disp("", " http://" + WiFi.localIP().toString() + "/pic", " http://" + WiFi.localIP().toString() + "/stream", "STATUS : Connected");

  if (MDNS.begin("esp32")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", []() {
    server.send(200, "text/plain", "this works as well");
  });

  server.on("/pic", HTTP_GET, sendPic);
  server.on("/stream", HTTP_GET, streamPic);

  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
}[/mw_shl_code]

M5StickV端程序
[mw_shl_code=python,true]from machine import UART
from board import board_info
from fpioa_manager import fm
from Maix import GPIO
import sensor, lcd

lcd.init()
lcd.rotation(2)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
#sensor.set_vflip(1)
#sensor.set_hmirror(1)
sensor.run(1)
sensor.skip_frames(time = 2000)


fm.register(35, fm.fpioa.UART1_TX, force=True)
fm.register(34, fm.fpioa.UART1_RX, force=True)

uart = UART(UART.UART1, 115200,8,0,0, timeout=1000, read_buf_len=4096)



while(True):
    img = sensor.snapshot()
    lcd.display(img)
    buf = uart.readline()
    if buf and len(buf) == 3:
        print(buf)
        img_buf = img.compress(quality=50)
        sz = img_buf.size()
        img_size1 = (sz & 0xFF0000)>>16
        img_size2 = (sz & 0x00FF00)>>8
        img_size3 = (sz & 0x0000FF)>>0
        data_packet = bytearray([img_size1,img_size2,img_size3])
        uart.write(data_packet)
        uart.write(img_buf)
        data_packet = bytearray([0xAA,0x55,0xFF])
        uart.write(data_packet)[/mw_shl_code]

发表于 2020-3-3 10:04 | 显示全部楼层
C作为一个无线中继,好思路,不知道卡顿不,回头有机会可以试试。谢谢分享
发表于 2020-3-4 01:39 | 显示全部楼层
本帖最后由 laai 于 2020-3-4 01:52 编辑

M5库加进来了,开发板一直添加不进来。。
发表于 2020-3-4 01:42 | 显示全部楼层
本帖最后由 laai 于 2020-3-4 01:44 编辑

很有用,感觉放在UNIT-V上更实用,因为V没有屏幕。再或者,能否直接在C上显示UNIT-V的图像?
发表于 2020-3-4 02:45 | 显示全部楼层
setting.h的内容是?
 楼主| 发表于 2020-3-5 09:41 | 显示全部楼层
laai 发表于 2020-3-4 02:45
setting.h的内容是?

setting.h忘了注释掉 里面是ssid和password 可以直接写在程序里
 楼主| 发表于 2020-3-5 18:04 | 显示全部楼层
laai 发表于 2020-3-4 01:42
很有用,感觉放在UNIT-V上更实用,因为V没有屏幕。再或者,能否直接在C上显示UNIT-V的图像?
...

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

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-30 23:55 , Processed in 0.116778 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表