esp32cam烧录代码
#include "WiFi.h" #include "esp_camera.h" #include//需要安装库:https://www.arduinolibraries.info/libraries/web-sockets #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 //替换成你的配置 const char* hostname = "ESP32CAM"; const char* ssid = "wifi名"; const char* password = "wifi密码"; const char* socket_url = "你的socket请求地址"; int socket_port = 你的端口; WebSocketsClient webSocket; void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { switch(type) { case WStype_DISCONNECTED: Serial.printf("[WSc] Disconnected!n"); break; case WStype_CONNECTED: { Serial.printf("[WSc] Connected to url: %sn", payload); webSocket.sendTXT("camlogin"); } break; case WStype_TEXT: Serial.printf("[WSc] get text: %sn", payload); break; case WStype_BIN: break; case WStype_PING: Serial.printf("[WSc] get pingn"); break; case WStype_PONG: Serial.printf("[WSc] get pongn"); break; } } void setupCamera() { camera_config_t config; config.ledc_channel = LEDC_CHANNEL_0; config.ledc_timer = LEDC_TIMER_0; config.pin_d0 = Y2_GPIO_NUM; config.pin_d1 = Y3_GPIO_NUM; config.pin_d2 = Y4_GPIO_NUM; config.pin_d3 = Y5_GPIO_NUM; config.pin_d4 = Y6_GPIO_NUM; config.pin_d5 = Y7_GPIO_NUM; config.pin_d6 = Y8_GPIO_NUM; config.pin_d7 = Y9_GPIO_NUM; config.pin_xclk = XCLK_GPIO_NUM; config.pin_pclk = PCLK_GPIO_NUM; config.pin_vsync = VSYNC_GPIO_NUM; config.pin_href = HREF_GPIO_NUM; config.pin_sscb_sda = SIOD_GPIO_NUM; config.pin_sscb_scl = SIOC_GPIO_NUM; config.pin_pwdn = PWDN_GPIO_NUM; config.pin_reset = RESET_GPIO_NUM; config.xclk_freq_hz = 20000000; config.pixel_format = PIXFORMAT_JPEG; config.frame_size = FRAMESIZE_VGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA config.jpeg_quality = 50;//图像质量(jpeg_quality) 可以是 0 到 63 之间的数字。数字越小意味着质量越高。然而,非常低的图像质量数字,特别是在更高的分辨率下,可能会导致 ESP32-CAM 崩溃或可能无法正常拍摄照片。 config.fb_count = 1; // Init Camera esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("Camera init failed with error 0x%x", err); return; } } void setup(){ Serial.begin(115200); // Connect to Wi-Fi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi.."); } // Print ESP32 Local IP Address Serial.println(WiFi.localIP()); setupCamera(); // server address, port and URL webSocket.begin(socket_url,socket_port); webSocket.onEvent(webSocketEvent); webSocket.setReconnectInterval(5000); webSocket.enableHeartbeat(15000, 3000, 2); } unsigned long messageTimestamp = 0; void loop() { webSocket.loop(); uint64_t now = millis(); if(now - messageTimestamp > 10) { messageTimestamp = now; camera_fb_t * fb = NULL; // Take Picture with Camera fb = esp_camera_fb_get(); if(!fb) { Serial.println("Camera capture failed"); return; } webSocket.sendBIn(fb->buf,fb->len); Serial.println("Image sent"); esp_camera_fb_return(fb); } }
微信小程序ts代码
Page({
onLoad() {
var that = this
var webSoket = wx.connectSocket({
url: 'ws://你的socket连接:端口',
})
webSoket.onMessage(onMessage => {
var base64 = wx.arrayBufferToBase64(onMessage.data);
that.setData({
motto:`data:image/jpg;base64,${base64}`
})
})
}
})
微信小程序wxml代码
服务器php代码:文件名server.php
connect('127.0.0.1', 6379);
$redis->set("fd", "[]"); //每次第一次执行都需要先清空reids里面的标识
$server = new swoole_websocket_server("0.0.0.0", 你的端口);#要在腾讯云和宝塔中放行
$server->set([
'daemonize' => true,//进程守护 可以后台运行
]);
$server->on('open', function (swoole_websocket_server $server, $request) use($redis) {
$str = json_decode($redis->get("fd"), true);
if($str == "") $str = [];
if(!in_array($request->fd, $str)){
array_push($str, $request->fd);
$str = json_encode($str);
$redis->set("fd", $str);
}
});
$server->on('message', function (swoole_websocket_server $server, $frame) use($redis) {
$str = json_decode($redis->get("fd"), true);
foreach ($str as $key => $value) {
if($frame->fd != $value){
$server->push($value,$frame->data,WEBSOCKET_OPCODE_BINARY);
}
}
});
$server->on('close', function ($ser, $fd) use($redis) {
$str = json_decode($redis->get("fd"), true);
$point = array_keys($str, $fd, true);
array_splice($str, $point['0'],1);
$str = json_encode($str);
$redis->set("fd", $str);
});
$server->start();
运行php脚本
php server.php



