# Usbcamera not saving properly files

**URL:** <https://discourse.nodered.org/t/usbcamera-not-saving-properly-files/14279>\
**Category:** General\
**Created:** [13 August 2019 13:18 UTC](https://discourse.nodered.org/t/usbcamera-not-saving-properly-files/14279 "2019-08-13T13:18:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charlotte](https://avatars.discourse-cdn.com/v4/letter/c/65b543/32.png) [@Charlotte](https://discourse.nodered.org/u/Charlotte)\
**Post date:** [13 August 2019 13:18 UTC](https://discourse.nodered.org/t/usbcamera-not-saving-properly-files/14279/1 "2019-08-13T13:18:43Z")

</div>

Hello,

I'm using node-red-contrib-usbcamera both in Buffer mode for streaming and in File mode to save pictures.

The issue I have is that in File mode, the files are saved only some times, while the output from the node is fine (payload, filename, filepath all OK), the files just don't exist.

Did you already face this issue and how did you solve it ?

Thanks !

Here is my flow:

```auto
[{"id":"b8a0d690.062448","type":"tab","label":"Camera","disabled":false,"info":"This flow include the driver for :\nOne USB webcam"},{"id":"6b7b72ce.8664fc","type":"usbcamera","z":"b8a0d690.062448","filemode":"0","filename":"image01.jpg","filedefpath":"1","filepath":"","fileformat":"jpeg","resolution":"5","name":"","x":550,"y":100,"wires":[["7265faa3.75b2a4"]]},{"id":"f37f9486.30bc68","type":"inject","z":"b8a0d690.062448","name":"","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":true,"onceDelay":0.1,"x":130,"y":100,"wires":[["8e58a7fb.145c28"]]},{"id":"5e5eab8d.55a544","type":"base64","z":"b8a0d690.062448","name":"","action":"str","property":"payload","x":880,"y":100,"wires":[["99b24069.8ca18"]]},{"id":"99b24069.8ca18","type":"template","z":"b8a0d690.062448","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<img height=\"600px\" src=\"data:image/jpg;base64,{{{payload}}}\">","output":"str","x":1020,"y":100,"wires":[["6247e54b.f4930c"]]},{"id":"6247e54b.f4930c","type":"ui_template","z":"b8a0d690.062448","group":"6013b77f.8f01d8","name":"","order":4,"width":"0","height":"0","format":"<div ng-bind-html=\"msg.payload\"></div>","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":1160,"y":100,"wires":[[]]},{"id":"8a2c52df.ea0cc","type":"comment","z":"b8a0d690.062448","name":"Only one camera can be attached","info":"","x":200,"y":60,"wires":[]},{"id":"23c29700.5d3d9a","type":"ui_button","z":"b8a0d690.062448","name":"","group":"6013b77f.8f01d8","order":1,"width":0,"height":0,"passthru":false,"label":"Take picture","tooltip":"","color":"","bgcolor":"","icon":"fa-camera","payload":"","payloadType":"str","topic":"","x":130,"y":200,"wires":[["c42a6fb3.ae6b5"]]},{"id":"c42a6fb3.ae6b5","type":"function","z":"b8a0d690.062448","name":"set filename and mode","func":"msg.filename = \"image_\" + Date.now();\nmsg.filemode = 1;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":200,"wires":[["6b7b72ce.8664fc"]]},{"id":"8e58a7fb.145c28","type":"function","z":"b8a0d690.062448","name":"set filemode","func":"msg.filemode = 0;\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":100,"wires":[["6b7b72ce.8664fc"]]},{"id":"7265faa3.75b2a4","type":"switch","z":"b8a0d690.062448","name":"","property":"filemode","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"},{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":710,"y":100,"wires":[["5e5eab8d.55a544","cf8887a8.d676e8"],["522c5aa1.26d854"]]},{"id":"cf8887a8.d676e8","type":"debug","z":"b8a0d690.062448","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":870,"y":60,"wires":[]},{"id":"522c5aa1.26d854","type":"debug","z":"b8a0d690.062448","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":870,"y":180,"wires":[]},{"id":"6013b77f.8f01d8","type":"ui_group","z":"","name":"Camera","tab":"f50ca35c.d425b","order":1,"disp":true,"width":"20","collapse":false},{"id":"f50ca35c.d425b","type":"ui_tab","z":"","name":"Camera","icon":"fa-camera","order":2,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 August 2019 13:57 UTC](https://discourse.nodered.org/t/usbcamera-not-saving-properly-files/14279/2 "2019-08-13T13:57:05Z")

</div>

What platform (Pi, macOS, Windows) are you running?  
What version?

Where exactly are you trying to write the file to disk in your flow?

---

<div class="post-metadata">

**Author:** ![Charlotte](https://avatars.discourse-cdn.com/v4/letter/c/65b543/32.png) [@Charlotte](https://discourse.nodered.org/u/Charlotte)\
**Post date:** [13 August 2019 14:20 UTC](https://discourse.nodered.org/t/usbcamera-not-saving-properly-files/14279/3 "2019-08-13T14:20:07Z")

</div>

Hi,  
I am working on Raspberry Pi with Rasbian Stretch.  
I am writing the file on the default path /home/pi/Pictures.

It seems that if I send a command to take a picture while the camera is busy with the streaming part, the command is ignored. I'll try to manage this with a status node. If you have another idea I'll please to hear it.

Thanks

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 August 2019 14:47 UTC](https://discourse.nodered.org/t/usbcamera-not-saving-properly-files/14279/4 "2019-08-13T14:47:26Z")

</div>

Where in the flow do you try to save the file?

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [13 August 2019 14:53 UTC](https://discourse.nodered.org/t/usbcamera-not-saving-properly-files/14279/5 "2019-08-13T14:53:03Z")

</div>

> [@Charlotte](#):
>
> It seems that if I send a command to take a picture while the camera is busy with the streaming part

Hi Charlotte,  
So I assume you have (some kind of) a video stream already? Can't you just grab periodically individual pictures from that stream, instead of getting extra snapshot pictures from the camera? Or is the video stream perhaps not running through Node-RED?  
Bart

---

<div class="post-metadata">

**Author:** ![Charlotte](https://avatars.discourse-cdn.com/v4/letter/c/65b543/32.png) [@Charlotte](https://discourse.nodered.org/u/Charlotte)\
**Post date:** [13 August 2019 15:08 UTC](https://discourse.nodered.org/t/usbcamera-not-saving-properly-files/14279/6 "2019-08-13T15:08:35Z")

</div>

You are right, it's much better ! A lot more faster !!  
Thanks

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [13 August 2019 17:46 UTC](https://discourse.nodered.org/t/usbcamera-not-saving-properly-files/14279/7 "2019-08-13T17:46:43Z")

</div>

Hi Charlotte,  
Nice to hear you managed to get it done!  
If you have some time left, it would be nice if you could share a screenshot of (this particular part of) your flow, the flow json itself and some explanation. I'm pretty sure other users could benefit from your use case ...  
Thanks in advance!!  
Bart

---

<div class="post-metadata">

**Author:** ![Charlotte](https://avatars.discourse-cdn.com/v4/letter/c/65b543/32.png) [@Charlotte](https://discourse.nodered.org/u/Charlotte)\
**Post date:** [14 August 2019 08:20 UTC](https://discourse.nodered.org/t/usbcamera-not-saving-properly-files/14279/8 "2019-08-14T08:20:48Z")

</div>

Yes sure.

I have a first line streaming the webcam and updating the image each 5 seconds, these images are automatically saved under /home/pi/Pictures/image01.jpg.  
And a second line taking this picture and saving it under another name when the user is pressing the button on the dashboard.

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/a69700482caaf20bdf8d9db577e6bebbb13d557c.png)

```auto
[{"id":"172a41fc.9701fe","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"529141ec.8ee6c","type":"usbcamera","z":"172a41fc.9701fe","filemode":"0","filename":"image01.jpg","filedefpath":"1","filepath":"","fileformat":"jpeg","resolution":"5","name":"","x":330,"y":120,"wires":[["66e97202.20593c"]]},{"id":"34bba1ad.8e41be","type":"inject","z":"172a41fc.9701fe","name":"","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":true,"onceDelay":0.1,"x":130,"y":120,"wires":[["529141ec.8ee6c"]]},{"id":"66e97202.20593c","type":"base64","z":"172a41fc.9701fe","name":"","action":"str","property":"payload","x":520,"y":120,"wires":[["2509ebee.5750a4"]]},{"id":"2509ebee.5750a4","type":"template","z":"172a41fc.9701fe","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<img height=\"600px\" src=\"data:image/jpg;base64,{{{payload}}}\">","output":"str","x":700,"y":120,"wires":[["ef26da2d.0024b8"]]},{"id":"ef26da2d.0024b8","type":"ui_template","z":"172a41fc.9701fe","group":"6013b77f.8f01d8","name":"","order":4,"width":"0","height":"0","format":"<div ng-bind-html=\"msg.payload\"></div>","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":880,"y":120,"wires":[[]]},{"id":"dd84c1a6.c4bc5","type":"ui_button","z":"172a41fc.9701fe","name":"","group":"6013b77f.8f01d8","order":1,"width":0,"height":0,"passthru":false,"label":"Take picture","tooltip":"","color":"","bgcolor":"","icon":"fa-camera","payload":"","payloadType":"str","topic":"","x":130,"y":180,"wires":[["c64474d1.d018e8"]]},{"id":"c64474d1.d018e8","type":"file in","z":"172a41fc.9701fe","name":"","filename":"/home/pi/Pictures/image01.jpg","format":"","chunk":false,"sendError":false,"encoding":"none","x":390,"y":180,"wires":[["5dae658c.dce01c"]]},{"id":"5df71bd2.13faf4","type":"file","z":"172a41fc.9701fe","name":"","filename":"","appendNewline":true,"createDir":false,"overwriteFile":"true","encoding":"none","x":830,"y":180,"wires":[[]]},{"id":"5dae658c.dce01c","type":"function","z":"172a41fc.9701fe","name":"set filename","func":"date = Date.now();\nday = date.prototype.getDate();\nmonth = date.prototype.getMonth();\nyear = date.prototype.getFullYear();\nhours = date.prototype.getHours();\nmin = date.prototype.getMinutes();\nsec = date.prototype.getSeconds();\n\nformated_date = year + month + day + \"_\" + hours + min + sec;\n\nmsg.filename = \"/home/pi/Pictures/image_\" + formated_date;\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":180,"wires":[["5df71bd2.13faf4"]]},{"id":"6013b77f.8f01d8","type":"ui_group","z":"","name":"Camera","tab":"f50ca35c.d425b","order":1,"disp":true,"width":"20","collapse":false},{"id":"f50ca35c.d425b","type":"ui_tab","z":"","name":"Camera","icon":"fa-camera","order":2,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [14 August 2019 09:03 UTC](https://discourse.nodered.org/t/usbcamera-not-saving-properly-files/14279/9 "2019-08-14T09:03:11Z")

</div>

Just be careful, saving that frequently to a sd card will not be good in the long run, better use a usb memory stick and mount that to a new folder. Or even better, use a local ssd disk or a shared one (via samba)

---

<div class="post-metadata">

**Author:** ![Charlotte](https://avatars.discourse-cdn.com/v4/letter/c/65b543/32.png) [@Charlotte](https://discourse.nodered.org/u/Charlotte)\
**Post date:** [14 August 2019 10:34 UTC](https://discourse.nodered.org/t/usbcamera-not-saving-properly-files/14279/10 "2019-08-14T10:34:08Z")

</div>

Ok thanks.  
It won't be good because of the volume of the pictures ?

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [14 August 2019 12:10 UTC](https://discourse.nodered.org/t/usbcamera-not-saving-properly-files/14279/11 "2019-08-14T12:10:39Z")

</div>

Once thing is the total volume, nbr of pictures, depending on the size of your sd card, the other critical is number of times you write data to the card, it can wear out

Myself, I also have a house keeping python script, erasing pictures older than a number of days to free up space. The script is executed daily by Node-RED and it erases pictures older than 14 days

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bb3e711a7f467b64eb6156b5b59a6e3063e8e145.png)  
The content of the python script:

```auto
import os
import time

workdir = "/media/pi/USB P/MotionPics"
 
now = time.time()
old = now - 14 * 24 * 60 * 60

for f in os.listdir(workdir):
    path = os.path.join(workdir, f)
    if os.path.isfile(path):
        stat = os.stat(path)
        #print stat
        if stat.st_ctime < old:
            #print "removing: ", path
            os.remove(path) # uncomment when you will sure :)

```
