# USB webcam issue

**URL:** <https://discourse.nodered.org/t/usb-webcam-issue/48200>\
**Category:** General\
**Created:** [9 July 2021 03:03 UTC](https://discourse.nodered.org/t/usb-webcam-issue/48200 "2021-07-09T03:03:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MK1992](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@MK1992](https://discourse.nodered.org/u/MK1992)\
**Post date:** [9 July 2021 03:03 UTC](https://discourse.nodered.org/t/usb-webcam-issue/48200/1 "2021-07-09T03:03:53Z")

</div>

Hi I want to make some adjustment to usb camera. So once the push button is pressed, webcam will capture the image but the problem is after it is captured, it will saved as 'image01.png' and next time when I press the button, the webcam will saved as 'image01.png' again. So there is only one photo although I have pressed push button several times. So I need some suggestion to capture and saved one photo at a time when I press the button.

 ![webcam1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dc4ebfbe05f31b47af714160414f26f441630c51.jpeg)  
 ![webcam3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/984ac94b76aadab9787a22938b202c746a1285a3.jpeg)

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [9 July 2021 03:07 UTC](https://discourse.nodered.org/t/usb-webcam-issue/48200/2 "2021-07-09T03:07:16Z")

</div>

You need to increment the number after the file name, or inject a timestamp... I haven't done it myself yet (no USB cam on my RPi) but this is probably done with a function node that replaces the "File Name" field in the `usbcamera` node You can probably search for something similar in this forum.

PS blurry screenshots don't help. The proper way is to Export your flow and paste it (formatted with the \</\> key) here.

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [9 July 2021 04:14 UTC](https://discourse.nodered.org/t/usb-webcam-issue/48200/3 "2021-07-09T04:14:17Z")

</div>

This is what I came up with to place in a `function` node prior to the `usbcamera` node

```auto
var now = new Date().toLocaleString("en-US");
now = now.replace(",",""); // clear out comma
now = now.replace(/\//g,"-"); // replace all slashes
now = now.replace(/ /g,"_"); // replace all spaces
msg.filename = "image_" + now;
return msg;

```

Or at least it seems to do the job in the error msg I get, what with not having a USB camera connected 😛

```auto
USB Camera error: Error: Command failed: fswebcam -q -r 320x240 --jpeg 100 -D 2 --no-banner /home/pi/Pictures/image_7-8-2021_9:11:40_PM.jpg
/bin/sh: 1: fswebcam: not found

```

> [@Gunner](#):
>
> PS blurry screenshots don't help. The proper way is to Export your flow and paste it (formatted with the \</\> key) here.

As shown here with a proper screen snipping

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0f1cb2e003a69ba594dd7fb0e6e4f4e155b95dcc.png)

```auto
[{"id":"43641b25.40b524","type":"tab","label":"Flow 8","disabled":false,"info":""},{"id":"fa215a7c.a1dd78","type":"usbcamera","z":"43641b25.40b524","filemode":"1","filename":"{{filename}}","filedefpath":"1","filepath":"","fileformat":"jpeg","resolution":"1","name":"","x":610,"y":320,"wires":[["754da95e.5324e8"]]},{"id":"a8302fbf.07fcb","type":"inject","z":"43641b25.40b524","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":320,"wires":[["7db34e5a.61d7a"]]},{"id":"754da95e.5324e8","type":"debug","z":"43641b25.40b524","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":320,"wires":[]},{"id":"7db34e5a.61d7a","type":"function","z":"43641b25.40b524","name":"To Date & Time","func":"var now = new Date().toLocaleString(\"en-US\");\nnow = now.replace(\",\",\"\"); // clear out comma\nnow = now.replace(/\\//g,\"-\"); // replace all slashes\nnow = now.replace(/ /g,\"_\"); // replace all spaces\nmsg.filename = \"image_\" + now;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":320,"wires":[["fa215a7c.a1dd78"]]}]

```

---

<div class="post-metadata">

**Author:** ![MK1992](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@MK1992](https://discourse.nodered.org/u/MK1992)\
**Post date:** [9 July 2021 04:58 UTC](https://discourse.nodered.org/t/usb-webcam-issue/48200/4 "2021-07-09T04:58:00Z")

</div>

thank you Gunner I will try to do that now.

---

<div class="post-metadata">

**Author:** ![MK1992](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@MK1992](https://discourse.nodered.org/u/MK1992)\
**Post date:** [9 July 2021 05:11 UTC](https://discourse.nodered.org/t/usb-webcam-issue/48200/5 "2021-07-09T05:11:42Z")

</div>

it works with timestamp but for me I want to capture image once the pushbutton is pressed. So once the push button is pressed, the logic is 0 or 1. So this 0 or 1 will trigger the camera to capture image so any chance on how to proceed with that? I mean how to write increment function?  
[flows.json](https://discourse.nodered.org/uploads/short-url/tR2ijq4bLIclZ9kgZKwPTTpvGqA.json) (1.4 KB)

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [9 July 2021 06:19 UTC](https://discourse.nodered.org/t/usb-webcam-issue/48200/6 "2021-07-09T06:19:51Z")

</div>

When it is this basic I shouldn't do it all for you 😛

Just use your button node instead of the inject node, the timestamp comes from the `new Date()` constructor in the function, not the inject node.

Nor do you need all those other change nodes, a 1 will trigger the camera and the date/time stamp will make each image separate from the rest (by the seconds).

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/5/e585d72bddb7abf711d7484df4c28fae02243ba6.png)

Well, OK... the camera _will_ trigger on a 0 as well, but then just wrap the function commands in a `if()` condition

```auto
if (msg.payload == 1){
var now = new Date().toLocaleString("en-US");
now = now.replace(",",""); // clear out comma
now = now.replace(/\//g,"-"); // replace all slashes
now = now.replace(/ /g,"_"); // replace all spaces
msg.filename = "image_" + now;
return msg;
}

```

---

<div class="post-metadata">

**Author:** ![MK1992](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@MK1992](https://discourse.nodered.org/u/MK1992)\
**Post date:** [9 July 2021 07:02 UTC](https://discourse.nodered.org/t/usb-webcam-issue/48200/7 "2021-07-09T07:02:45Z")

</div>

Thanks Gunner, it works and sorry to trouble you with basic stuff, as I am very new to nodered.

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [9 July 2021 07:57 UTC](https://discourse.nodered.org/t/usb-webcam-issue/48200/8 "2021-07-09T07:57:09Z")

</div>

No trouble... what I meant by not doing it all was not giving you the entire flow again, just the info so you could put the pieces together, and you did! I don't mind helping those that show an interest and attempts in learning.

And besides, I am not that far ahead in learning NR myself 😃

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 July 2021 07:58 UTC](https://discourse.nodered.org/t/usb-webcam-issue/48200/9 "2021-07-23T07:58:10Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
