diff --git a/hardware/esp32-cam-udp/.gitignore b/hardware/esp32-cam-udp/.gitignore index 03f4a3c1..c1a4321f 100644 --- a/hardware/esp32-cam-udp/.gitignore +++ b/hardware/esp32-cam-udp/.gitignore @@ -1 +1,2 @@ .pio +node_modules \ No newline at end of file diff --git a/hardware/esp32-cam-udp/package-lock.json b/hardware/esp32-cam-udp/package-lock.json index 414b85d6..467c694a 100644 --- a/hardware/esp32-cam-udp/package-lock.json +++ b/hardware/esp32-cam-udp/package-lock.json @@ -1,5 +1,5 @@ { - "name": "eye", + "name": "esp32-cam-udp", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/hardware/esp32-cam-udp/src/main.cpp b/hardware/esp32-cam-udp/src/main.cpp index 23b4c4be..dad53127 100644 --- a/hardware/esp32-cam-udp/src/main.cpp +++ b/hardware/esp32-cam-udp/src/main.cpp @@ -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, @@ -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. @@ -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;ibuf+SIZE*i,i+1==n?fb->len-SIZE*i:SIZE);udp.endPacket(); + for(uint8_t n=(fb->len+SIZE-1)/SIZE,i=0;ibuf+SIZE*i,i+1==n?fb->len-SIZE*i:SIZE);udp.endPacket(); } + t++; esp_camera_fb_return(fb); } diff --git a/hardware/esp32-cam-udp/svr.mjs b/hardware/esp32-cam-udp/svr.mjs index ba8f8fae..d45a70c0 100644 --- a/hardware/esp32-cam-udp/svr.mjs +++ b/hardware/esp32-cam-udp/svr.mjs @@ -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'}), @@ -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} ...`));