ESP32 wifi video/image transmission

ESP32 wifi video/image transmission


1. Install Arduino

1.1 Software download

https://arduino.me/download, download and install version 1.8.19 (mainly 2.xxx is unstable)

1.2 Follow the tutorial to install

https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-mac-and-linux-instructions/
pay attention to add ESP32 (by Espressif System) download version 2.0.2 (may have to go over the wall), And some Flash (80m), PartitionScheme (huge), baud rate (115200) of the board must be selected correspondingly

1.3 Precautions for programming

The board needs 5V power supply, IO0 and GND are short-circuited for download and programming mode, disconnected is for running mode, after conecting there is an underline, then reset it

2. Experiment

2.1 Code

https://gitcode.net/mirrors/yoursunny/esp32cam?utm_source=csdn_github_accelerator

2.2 Main program code

#include <esp32cam.h>
#include <WebServer.h>
#include <WiFi.h>

const char* WIFI_SSID = "***";  // 改成自己的wifi名称
const char* WIFI_PASS = "***";  // 改成自己的wifi密码

WebServer server(80);

static auto loRes = esp32cam::Resolution::find(320, 240);
static auto hiRes = esp32cam::Resolution::find(800, 600);

void handleBmp()
{
    
    
  if (!esp32cam::Camera.changeResolution(loRes)) {
    
    
    Serial.println("SET-LO-RES FAIL");
  }

  auto frame = esp32cam::capture();
  if (frame == nullptr) {
    
    
    Serial.println("CAPTURE FAIL");
    server.send(503, "", "");
    return;
  }
  Serial.printf("CAPTURE OK %dx%d %db\n", frame->getWidth(), frame->getHeight(),
                static_cast<int>(frame->size()));

  if (!frame->toBmp()) {
    
    
    Serial.println("CONVERT FAIL");
    server.send(503, "", "");
    return;
  }
  Serial.printf("CONVERT OK %dx%d %db\n", frame->getWidth(), frame->getHeight(),
                static_cast<int>(frame->size()));

  server.setContentLength(frame->size());
  server.send(200, "image/bmp");
  WiFiClient client = server.client();
  frame->writeTo(client);
}

void serveJpg()
{
    
    
  auto frame = esp32cam::capture();
  if (frame == nullptr) {
    
    
    Serial.println("CAPTURE FAIL");
    server.send(503, "", "");
    return;
  }
  Serial.printf("CAPTURE OK %dx%d %db\n", frame->getWidth(), frame->getHeight(),
                static_cast<int>(frame->size()));

  server.setContentLength(frame->size());
  server.send(200, "image/jpeg");
  WiFiClient client = server.client();
  frame->writeTo(client);
}

void handleJpgLo()
{
    
    
  if (!esp32cam::Camera.changeResolution(loRes)) {
    
    
    Serial.println("SET-LO-RES FAIL");
  }
  serveJpg();
}

void handleJpgHi()
{
    
    
  if (!esp32cam::Camera.changeResolution(hiRes)) {
    
    
    Serial.println("SET-HI-RES FAIL");
  }
  serveJpg();
}

void handleJpg()
{
    
    
  server.sendHeader("Location", "/cam-hi.jpg");
  server.send(302, "", "");
}

void handleMjpeg()
{
    
    
  if (!esp32cam::Camera.changeResolution(hiRes)) {
    
    
    Serial.println("SET-HI-RES FAIL");
  }

  Serial.println("STREAM BEGIN");
  WiFiClient client = server.client();
  auto startTime = millis();
  int res = esp32cam::Camera.streamMjpeg(client);
  if (res <= 0) {
    
    
    Serial.printf("STREAM ERROR %d\n", res);
    return;
  }
  auto duration = millis() - startTime;
  Serial.printf("STREAM END %dfrm %0.2ffps\n", res, 1000.0 * res / duration);
}

void setup()
{
    
    
  Serial.begin(115200);
  Serial.println();

  {
    
    
    using namespace esp32cam;
    Config cfg;
    cfg.setPins(pins::AiThinker);
    cfg.setResolution(hiRes);
    cfg.setBufferCount(2);
    cfg.setJpeg(80);

    bool ok = Camera.begin(cfg);
    Serial.println(ok ? "CAMERA OK" : "CAMERA FAIL");
  }

  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    
    
    delay(500);
  }

  Serial.print("http://");
  Serial.println(WiFi.localIP());
  Serial.println("  /cam.bmp");
  Serial.println("  /cam-lo.jpg");
  Serial.println("  /cam-hi.jpg");
  Serial.println("  /cam.mjpeg");

  server.on("/cam.bmp", handleBmp);
  server.on("/cam-lo.jpg", handleJpgLo);
  server.on("/cam-hi.jpg", handleJpgHi);
  server.on("/cam.jpg", handleJpg);
  server.on("/cam.mjpeg", handleMjpeg);

  server.begin();
}

void loop()
{
    
    
  server.handleClient();
}

3. IP access

3.1 Router Settings

Fix the router and camera IP port

3.2 Camera access address

The ip address of the camera will be returned on the serial port of Arduino, enter the address in the browser under the same LAN and add the suffix of the picture/video you want to see, such as http://192.168.1.240/cam.mjpeg

Guess you like

Origin blog.csdn.net/qq_37249793/article/details/131951968