Skip to content

Commit

Permalink
Merge pull request #221 from ueckoken/esp-cam-udp
Browse files Browse the repository at this point in the history
UDPカメラ 通信の改良
  • Loading branch information
McbeEringi authored Nov 13, 2023
2 parents c1bd6be + b65b933 commit 4517954
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
1 change: 1 addition & 0 deletions hardware/esp32-cam-udp/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.pio
node_modules
2 changes: 1 addition & 1 deletion hardware/esp32-cam-udp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions hardware/esp32-cam-udp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

WiFiUDP udp;
IPAddress host;
uint8_t t=0;

static camera_config_t cam_cfg={// https://github.com/espressif/esp-who/blob/master/docs/en/Camera_connections.md
.pin_pwdn=-1,.pin_reset=-1,
Expand All @@ -22,7 +23,7 @@ static camera_config_t cam_cfg={// https://github.com/espressif/esp-who/blob/mas
.ledc_timer=LEDC_TIMER_0,.ledc_channel=LEDC_CHANNEL_0,

.pixel_format=PIXFORMAT_JPEG,//YUV422,GRAYSCALE,RGB565,JPEG
.frame_size=FRAMESIZE_VGA,//QQVGA-UXGA, For ESP32, do not use sizes above QVGA when not JPEG. The performance of the ESP32-S series has improved a lot, but JPEG mode always gives better frame rates.
.frame_size=FRAMESIZE_SVGA,//QQVGA-UXGA, For ESP32, do not use sizes above QVGA when not JPEG. The performance of the ESP32-S series has improved a lot, but JPEG mode always gives better frame rates.

.jpeg_quality=12, //0-63, for OV series camera sensors, lower number means higher quality
.fb_count=2, //When jpeg mode is used, if fb_count more than one, the driver will work in continuous mode.
Expand All @@ -49,12 +50,9 @@ void setup(){

void loop(){
camera_fb_t *fb=esp_camera_fb_get();
const uint8_t *t=(uint8_t*)(&fb->timestamp.tv_usec);
uint8_t n=(fb->len+SIZE-1)/SIZE;

udp.beginPacket(host,PORT);udp.printf("STRT");udp.write(t,4);udp.write(n);udp.endPacket();
for(uint8_t i=0;i<n;i++){
udp.beginPacket(host,PORT);udp.printf("DATA");udp.write(t,4);udp.write(i);udp.write(fb->buf+SIZE*i,i+1==n?fb->len-SIZE*i:SIZE);udp.endPacket();
for(uint8_t n=(fb->len+SIZE-1)/SIZE,i=0;i<n;i++){
udp.beginPacket(host,PORT);udp.write(t);udp.write(n);udp.write(i);udp.write(fb->buf+SIZE*i,i+1==n?fb->len-SIZE*i:SIZE);udp.endPacket();
}
t++;
esp_camera_fb_return(fb);
}
32 changes: 14 additions & 18 deletions hardware/esp32-cam-udp/svr.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as DGRAM from 'dgram';
import * as HTTP from 'http';
import * as DGRAM from 'node:dgram';
import * as HTTP from 'node:http';
import * as WS from 'ws';

const
fb={},
udp=DGRAM.createSocket('udp4'),
svr=HTTP.createServer((req,res)=>(
res.writeHead(200,{'Content-Type':'text/html'}),
Expand Down Expand Up @@ -42,25 +43,20 @@ wss=new WS.WebSocketServer({server:svr,path:'/ws'});

wss.on('connection',_=>ws.add(_));
wss.on('close',_=>ws.delete(_));

let fb={},t;
udp.on('message',(x,i)=>(
x={
tag:x.subarray(0,4)+'',
t:x.readUInt32LE(4),
i:x[8],
x:x.subarray(9)
t:x[0],
n:x[1],
i:x[2],
x:x.subarray(3)
},
({
STRT:_=>(fb[x.t]=[...Array(x.i)],setTimeout(_=>delete fb[x.t],500)),
DATA:_=>fb[x.t]&&(
fb[x.t][x.i]=x.x,
fb[x.t].every(_=>_)&&(
_=Buffer.concat(fb[x.t]),delete fb[x.t],ws.forEach(x=>x.send(_)),
console.log({fps:Math.round(1000/(-t+(t=Date.now()))),size:_.length,t:x.t})
)
),
}[x.tag])()
fb[x.t]||(fb[x.t]=[...Array(x.n)],setTimeout(_=>delete fb[x.t],500)),
fb[x.t][x.i]=x.x,
fb[x.t].every(_=>_)&&((_=Buffer.concat(fb[x.t]))=>(
ws.forEach(x=>x.send(_)),
console.log({size:_.length,t:x.t,packets:fb[x.t].length}),
delete fb[x.t]
))()
// console.log(x+'',i.address,i.port)
));
udp.on('listening',_=>console.log(`port ${udp.address().port} ...`));
Expand Down

0 comments on commit 4517954

Please sign in to comment.