Skip to content

Commit a737be3

Browse files
author
Eric
committed
Unity3D | ESP32CAM - FROM REALITY TO VIRTUAL (Ft. WebSocket)
1 parent 9a285ff commit a737be3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+16373
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include "esp_camera.h"
2+
#include <WiFi.h>
3+
#include <ArduinoWebsockets.h>
4+
#define CAMERA_MODEL_AI_THINKER
5+
#include "camera_pins.h"
6+
7+
const char* ssid = "<YOUR_WIFI_SSID>";
8+
const char* password = "<YOUR_WIFI_PASSWORD>";
9+
const char* websocket_server_host = "<YOUR_LOCAL_IP_ADDRESS>";
10+
const uint16_t websocket_server_port = 8888;
11+
12+
using namespace websockets;
13+
WebsocketsClient client;
14+
15+
void setup() {
16+
Serial.begin(115200);
17+
Serial.setDebugOutput(true);
18+
Serial.println();
19+
20+
camera_config_t config;
21+
config.ledc_channel = LEDC_CHANNEL_0;
22+
config.ledc_timer = LEDC_TIMER_0;
23+
config.pin_d0 = Y2_GPIO_NUM;
24+
config.pin_d1 = Y3_GPIO_NUM;
25+
config.pin_d2 = Y4_GPIO_NUM;
26+
config.pin_d3 = Y5_GPIO_NUM;
27+
config.pin_d4 = Y6_GPIO_NUM;
28+
config.pin_d5 = Y7_GPIO_NUM;
29+
config.pin_d6 = Y8_GPIO_NUM;
30+
config.pin_d7 = Y9_GPIO_NUM;
31+
config.pin_xclk = XCLK_GPIO_NUM;
32+
config.pin_pclk = PCLK_GPIO_NUM;
33+
config.pin_vsync = VSYNC_GPIO_NUM;
34+
config.pin_href = HREF_GPIO_NUM;
35+
config.pin_sscb_sda = SIOD_GPIO_NUM;
36+
config.pin_sscb_scl = SIOC_GPIO_NUM;
37+
config.pin_pwdn = PWDN_GPIO_NUM;
38+
config.pin_reset = RESET_GPIO_NUM;
39+
config.xclk_freq_hz = 10000000;
40+
config.pixel_format = PIXFORMAT_JPEG;
41+
//init with high specs to pre-allocate larger buffers
42+
if(psramFound()){
43+
config.frame_size = FRAMESIZE_VGA;
44+
config.jpeg_quality = 40;
45+
config.fb_count = 2;
46+
} else {
47+
config.frame_size = FRAMESIZE_SVGA;
48+
config.jpeg_quality = 12;
49+
config.fb_count = 1;
50+
}
51+
52+
53+
// camera init
54+
esp_err_t err = esp_camera_init(&config);
55+
if (err != ESP_OK) {
56+
Serial.printf("Camera init failed with error 0x%x", err);
57+
return;
58+
}
59+
60+
WiFi.begin(ssid, password);
61+
62+
while (WiFi.status() != WL_CONNECTED) {
63+
delay(500);
64+
Serial.print(".");
65+
}
66+
Serial.println("");
67+
Serial.println("WiFi connected");
68+
69+
while(!client.connect(websocket_server_host, websocket_server_port, "/")){
70+
delay(500);
71+
Serial.print(".");
72+
}
73+
Serial.println("Websocket Connected!");
74+
}
75+
76+
void loop() {
77+
camera_fb_t *fb = esp_camera_fb_get();
78+
if(!fb){
79+
Serial.println("Camera capture failed");
80+
esp_camera_fb_return(fb);
81+
return;
82+
}
83+
84+
if(fb->format != PIXFORMAT_JPEG){
85+
Serial.println("Non-JPEG data not implemented");
86+
return;
87+
}
88+
89+
client.sendBinary((const char*) fb->buf, fb->len);
90+
esp_camera_fb_return(fb);
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
2+
#if defined(CAMERA_MODEL_WROVER_KIT)
3+
#define PWDN_GPIO_NUM -1
4+
#define RESET_GPIO_NUM -1
5+
#define XCLK_GPIO_NUM 21
6+
#define SIOD_GPIO_NUM 26
7+
#define SIOC_GPIO_NUM 27
8+
9+
#define Y9_GPIO_NUM 35
10+
#define Y8_GPIO_NUM 34
11+
#define Y7_GPIO_NUM 39
12+
#define Y6_GPIO_NUM 36
13+
#define Y5_GPIO_NUM 19
14+
#define Y4_GPIO_NUM 18
15+
#define Y3_GPIO_NUM 5
16+
#define Y2_GPIO_NUM 4
17+
#define VSYNC_GPIO_NUM 25
18+
#define HREF_GPIO_NUM 23
19+
#define PCLK_GPIO_NUM 22
20+
21+
#elif defined(CAMERA_MODEL_ESP_EYE)
22+
#define PWDN_GPIO_NUM -1
23+
#define RESET_GPIO_NUM -1
24+
#define XCLK_GPIO_NUM 4
25+
#define SIOD_GPIO_NUM 18
26+
#define SIOC_GPIO_NUM 23
27+
28+
#define Y9_GPIO_NUM 36
29+
#define Y8_GPIO_NUM 37
30+
#define Y7_GPIO_NUM 38
31+
#define Y6_GPIO_NUM 39
32+
#define Y5_GPIO_NUM 35
33+
#define Y4_GPIO_NUM 14
34+
#define Y3_GPIO_NUM 13
35+
#define Y2_GPIO_NUM 34
36+
#define VSYNC_GPIO_NUM 5
37+
#define HREF_GPIO_NUM 27
38+
#define PCLK_GPIO_NUM 25
39+
40+
#elif defined(CAMERA_MODEL_M5STACK_PSRAM)
41+
#define PWDN_GPIO_NUM -1
42+
#define RESET_GPIO_NUM 15
43+
#define XCLK_GPIO_NUM 27
44+
#define SIOD_GPIO_NUM 25
45+
#define SIOC_GPIO_NUM 23
46+
47+
#define Y9_GPIO_NUM 19
48+
#define Y8_GPIO_NUM 36
49+
#define Y7_GPIO_NUM 18
50+
#define Y6_GPIO_NUM 39
51+
#define Y5_GPIO_NUM 5
52+
#define Y4_GPIO_NUM 34
53+
#define Y3_GPIO_NUM 35
54+
#define Y2_GPIO_NUM 32
55+
#define VSYNC_GPIO_NUM 22
56+
#define HREF_GPIO_NUM 26
57+
#define PCLK_GPIO_NUM 21
58+
59+
#elif defined(CAMERA_MODEL_M5STACK_WIDE)
60+
#define PWDN_GPIO_NUM -1
61+
#define RESET_GPIO_NUM 15
62+
#define XCLK_GPIO_NUM 27
63+
#define SIOD_GPIO_NUM 22
64+
#define SIOC_GPIO_NUM 23
65+
66+
#define Y9_GPIO_NUM 19
67+
#define Y8_GPIO_NUM 36
68+
#define Y7_GPIO_NUM 18
69+
#define Y6_GPIO_NUM 39
70+
#define Y5_GPIO_NUM 5
71+
#define Y4_GPIO_NUM 34
72+
#define Y3_GPIO_NUM 35
73+
#define Y2_GPIO_NUM 32
74+
#define VSYNC_GPIO_NUM 25
75+
#define HREF_GPIO_NUM 26
76+
#define PCLK_GPIO_NUM 21
77+
78+
#elif defined(CAMERA_MODEL_AI_THINKER)
79+
#define PWDN_GPIO_NUM 32
80+
#define RESET_GPIO_NUM -1
81+
#define XCLK_GPIO_NUM 0
82+
#define SIOD_GPIO_NUM 26
83+
#define SIOC_GPIO_NUM 27
84+
85+
#define Y9_GPIO_NUM 35
86+
#define Y8_GPIO_NUM 34
87+
#define Y7_GPIO_NUM 39
88+
#define Y6_GPIO_NUM 36
89+
#define Y5_GPIO_NUM 21
90+
#define Y4_GPIO_NUM 19
91+
#define Y3_GPIO_NUM 18
92+
#define Y2_GPIO_NUM 5
93+
#define VSYNC_GPIO_NUM 25
94+
#define HREF_GPIO_NUM 23
95+
#define PCLK_GPIO_NUM 22
96+
97+
#else
98+
#error "Camera model not selected"
99+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<html>
2+
3+
<head>
4+
<title>Client</title>
5+
</head>
6+
7+
<body>
8+
<img src="">
9+
<script>
10+
const img = document.querySelector('img');
11+
const WS_URL = 'ws:///<YOUR_LOCAL_IP_ADDRESS>:8888';
12+
const ws = new WebSocket(WS_URL);
13+
let urlObject;
14+
ws.onopen = () => console.log(`Connected to ${WS_URL}`);
15+
ws.onmessage = message => {
16+
const arrayBuffer = message.data;
17+
if (urlObject) {
18+
URL.revokeObjectURL(urlObject);
19+
}
20+
urlObject = URL.createObjectURL(new Blob([arrayBuffer]));
21+
img.src = urlObject;
22+
}
23+
</script>
24+
</body>
25+
26+
</html>

0 commit comments

Comments
 (0)