ESP32CAM通过UDP与服务器视频传输

/*
网络调试助手
https://soft.3dmgame.com/down/213757.html
*/

#include "esp_camera.h"
#include <WiFi.h>
#include "AsyncUDP.h"
#include <vector>
const char *ssid = "888";   
const char *password = "888888888";

#define maxcache 1430

//设置每次发送最大的数据量,如果选择一次发送会出现丢失数据,经测试,我这边每
//次最大发送1436,选择一个稍微小点的数
AsyncUDP udp;                      //异步udp既可以发送也可以接收

//CAMERA_MODEL_AI_THINKER类型摄像头的引脚定义
#define PWDN_GPIO_NUM     32
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM      0
#define SIOD_GPIO_NUM     26
#define SIOC_GPIO_NUM     27

#define Y9_GPIO_NUM       35
#define Y8_GPIO_NUM       34
#define Y7_GPIO_NUM       39
#define Y6_GPIO_NUM       36
#define Y5_GPIO_NUM       21
#define Y4_GPIO_NUM       19
#define Y3_GPIO_NUM       18
#define Y2_GPIO_NUM        5
#define VSYNC_GPIO_NUM    25
#define HREF_GPIO_NUM     23
#define PCLK_GPIO_NUM     22

static camera_config_t camera_config = {
    .pin_pwdn = PWDN_GPIO_NUM,
    .pin_reset = RESET_GPIO_NUM,
    .pin_xclk = XCLK_GPIO_NUM,
    .pin_sscb_sda = SIOD_GPIO_NUM,
    .pin_sscb_scl = SIOC_GPIO_NUM,
    
    .pin_d7 = Y9_GPIO_NUM,
    .pin_d6 = Y8_GPIO_NUM,
    .pin_d5 = Y7_GPIO_NUM,
    .pin_d4 = Y6_GPIO_NUM,
    .pin_d3 = Y5_GPIO_NUM,
    .pin_d2 = Y4_GPIO_NUM,
    .pin_d1 = Y3_GPIO_NUM,
    .pin_d0 = Y2_GPIO_NUM,
    .pin_vsync = VSYNC_GPIO_NUM,
    .pin_href = HREF_GPIO_NUM,
    .pin_pclk = PCLK_GPIO_NUM,
    
    .xclk_freq_hz = 20000000,
    .ledc_timer = LEDC_TIMER_0,
    .ledc_channel = LEDC_CHANNEL_0,
    
    .pixel_format = PIXFORMAT_JPEG,
    .frame_size = FRAMESIZE_VGA,
    .jpeg_quality = 12,
    .fb_count = 1,
};

esp_err_t camera_init() {
    //initialize the camera
    esp_err_t err = esp_camera_init(&camera_config);
    if (err != ESP_OK) {
        Serial.print("Camera Init Failed");
        return err;
    }
    sensor_t * s = esp_camera_sensor_get();
    //initial sensors are flipped vertically and colors are a bit saturated
    if (s->id.PID == OV2640_PID) {
    //        s->set_vflip(s, 1);//flip it back
    //        s->set_brightness(s, 1);//up the blightness just a bit
    //        s->set_contrast(s, 1);
    }
    Serial.print("Camera Init OK");
    return ESP_OK;
}

void wifi_init(void)
{
    delay(10);
    WiFi.mode(WIFI_STA);
//    WiFi.setSleep(false); //关闭STA模式下wifi休眠,提高响应速度
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }
    Serial.println("Connected");
    Serial.print("IP Address:");
    Serial.println(WiFi.localIP());
    if(udp.connect(IPAddress(192,168,199,171), 8080))  //连接远端的udp
    {
        Serial.println("UDP connected");
        udp.print("Hello Server!"); 
    }
}

void setup() {
    Serial.begin(115200);
    camera_init(); //摄像头初始化
    wifi_init(); //wifi初始化
    Serial.println("sys is running!");
    //连结上服务器 服务器会发送一条欢迎数据到客户端  此处用于显示客户端接受到的欢迎数据  此部分可忽略
    if(udp.connect(IPAddress(192,168,199,171), 8080)) {
        Serial.println("UDP connected");
        udp.onPacket([](AsyncUDPPacket packet) {
            Serial.print("UDP Packet Type: ");
            Serial.print(packet.isBroadcast()?"Broadcast":packet.isMulticast()?"Multicast":"Unicast");
            Serial.print(", From: ");
            Serial.print(packet.remoteIP());
            Serial.print(":");
            Serial.print(packet.remotePort());
            Serial.print(", To: ");
            Serial.print(packet.localIP());
            Serial.print(":");
            Serial.print(packet.localPort());
            Serial.print(", Length: ");
            Serial.print(packet.length());
            Serial.print(", Data: ");
            Serial.write(packet.data(), packet.length());
            Serial.println();
            //reply to the client
            packet.printf("Got %u bytes of data", packet.length());
        });
        //Send unicast
        //udp.print("Hello Server!");
    }
    //忽略到此部分
}

void loop() 
{
        delay(100000);
            //acquire a frame
            camera_fb_t * fb = esp_camera_fb_get();
            uint8_t * temp = fb->buf; //这个是为了保存一个地址,在摄像头数据发送完毕后需要返回,否则会出现板子发送一段时间后自动重启,不断重复
            if (!fb)
            {
                Serial.print( "Camera capture failed");
            }
            else
            { 
                // 将图片数据分段发送
                int leng = fb->len;
                int timess = leng/maxcache;
                int extra = leng%maxcache;
                for(int j = 0;j< timess;j++)
                {
                  udp.write(fb->buf, maxcache); 
                  for(int i =0;i< maxcache;i++)
                  {
                    fb->buf++;
                  }
                }
                udp.write(fb->buf, extra);
                Serial.println(fb->len);
                Serial.println("succes to send image for UDP");
                //return the frame buffer back to the driver for reuse
                fb->buf = temp; //将当时保存的指针重新返还
                esp_camera_fb_return(fb);  //这一步在发送完毕后要执行,具体作用还未可知。
            }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值