M5StickV+M5StickC发送HTTP视频流
本帖最后由 vany5921 于 2020-3-5 09:42 编辑M5StickC通过串口发送图像数据,由StickC的ESP32建立webserver,使用浏览器访问本地地址即可实时获取到视频流,参考链接https://homemadegarbage.com/ai13
M5StickC端程序
#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;
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 = s1;
}
if (s2 != "") {
msg = s2;
}
if (s3 != "") {
msg = s3;
}
if (s4 != "") {
msg = s4;
}
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setTextFont(2);
M5.Lcd.setCursor(0, 0);
M5.Lcd.print(msg);
M5.Lcd.setTextFont(1);
M5.Lcd.setCursor(0, 20);
M5.Lcd.print(msg);
M5.Lcd.setCursor(0, 40);
M5.Lcd.print(msg);
M5.Lcd.setTextFont(2);
M5.Lcd.setCursor(0, 60);
M5.Lcd.print(msg);
}
bool transfer(WiFiClient client) {
Serial.println("transfer start");
Serial2.write("get");
delay(50);
uint8_t buf;
int len = Serial2.readBytes(buf, 3);
if (len != 3) {
Serial.println("transfer get length error");
return false;
}
int pLen = (uint32_t)(buf << 16) | (buf << 8) | buf;
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();
}
M5StickV端程序
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()
uart.write(data_packet)
uart.write(img_buf)
data_packet = bytearray()
uart.write(data_packet)
C作为一个无线中继,好思路,不知道卡顿不,回头有机会可以试试。谢谢分享 本帖最后由 laai 于 2020-3-4 01:52 编辑
M5库加进来了,开发板一直添加不进来。。:( 本帖最后由 laai 于 2020-3-4 01:44 编辑
很有用,感觉放在UNIT-V上更实用,因为V没有屏幕。再或者,能否直接在C上显示UNIT-V的图像?
setting.h的内容是? laai 发表于 2020-3-4 02:45
setting.h的内容是?
setting.h忘了注释掉 里面是ssid和password 可以直接写在程序里 laai 发表于 2020-3-4 01:42
很有用,感觉放在UNIT-V上更实用,因为V没有屏幕。再或者,能否直接在C上显示UNIT-V的图像?
...
可以显示的
页:
[1]