ftp causing invalid syntax

I have built a motion sensor detector using this code: from the dronebotworkshop on youtube. While it works very well for me, I now want to upload the pictures to my NAS server. I googled this and found this line of code:

curl -T /home/pi/filename.jpg ftp://ftp-host-machine//yourwebsite/folder/filename.jpg --user

Now when I run the python script:

from picamera import PiCamera import time import datetime

# Create object for PIR Sensor # PIR Sensor is on GPIO-4 (Pin 7) pir = MotionSensor(4)

# Create Object for Camera camera = PiCamera()

# Function to create new Filename from date and time def getFileName(): return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")

while True: # Get a Filename filename = getFileName() # Wait for a motion to be detected pir.wait_for_motion # Print text to Shell print("Jellybean thief detected!") # Preview camera on screen during video camera.start_preview() # Start recording video camera.start_recording(filename) # Record for 10 seconds camera.wait_recording(10) # Stop preview and recording camera.stop_preview() camera.stop_recording() curl -T /home/pi/filename.jpg ftp://ftp-ipaddressof NAS server/mnt/folder/filename.jpg --user # Wait 25 seconds and repeat time.sleep(25)

It returns Invalid syntax at the ftp://ftp- etc

I can get as far as a login box from my Ubuntu desktop with this: ftp://server ip address/mnt/Folder/subfolder, but it fails completely on the PiZero with invalid synatx at ftp

Reply to
RobH
Loading thread data ...

[snip]

curl is not a Python command but an external program. You either need to use pycurl (a la import pycurl) or you want to get your Python script to execute the curl program using os.system(....) or similar.

Much more simple would be to use the ftp library included in the standard Python library and do the ftp yourself. YMMV.

Reply to
mm0fmf

Thanks, but when the python script runs without the inserted curl line, it takes pictures of whatever is sensed and writes the actual file to the sdcard.

I read somewhere that writing to the Pizero sdcard so many times, as it would, is not a could idea as it could be corrupted.

So I looked for a way to upload the jpg file to my server.

Reply to
RobH

import os, subprocess

def GetUrl_curl(url): with open(os.devnull, 'w') as FNULL: return subprocess.check_output(["curl", "-L", url], stderr=FNULL)

---druck

Reply to
druck

Well curl is a program not a Python method. So you have to tell Python to execute curl with os.system() or similar or you can use the curl library pycurl or use ftplib. An example for Python2 on Windows, edit to fit.

import ftplib from ftplib import FTP

File2Send = "V://GIS//Maps//County//11x17shd//2010BEAVER11x17shd.pdf" Output_Directory = "//data2//ftp//pub//download//maps//"

try: ftp = FTP("XXX.XXX.XXX.XXX") ftp.login(username, password) file = open(File2Send, "rb") ftp.cwd(Output_Directory) ftp.storbinary('STOR ' + File2Send, file) print "STORing File now..." ftp.quit() file.close() print "File transfered" else: print "An error occured"

See

formatting link

Reply to
mm0fmf

On Mon, 14 May 2018 20:49:57 +0100, RobH declaimed the following:

Unless your JPG fits entirely within the system RAM, you still have to write the file to the SD card. "curl" and pretty much every other FTP option requires files stored on the file system in order to read/transfer them -- so no saving of the SD card, you still are writing to it.

If the JPG does fit in the RAM and is not written to SD card by the capture process, you could (with effort) feed the RAM copy to a programmed FTP transfer. Python's ftplib .storbinary() expects "an open file object"... I suspect anything that provides file-like I/O operations might be usable -- like using the StringIO library for the JPG data (but again, this assumes the camera capture can be to a string buffer and not to a file on the SD card). {"pycurl" is not part of the standard library, you'd have to install it}

You are using JPG for a video? Not MJPEG or JPEG XS? To my knowledge, JPeG (joint photographic experts group) is a still image format. Note that your filename format has an .h264 extension, which is likely compatible with AVCHD (advanced video codec for high definition) MPEG (motion picture experts group).

However -- THAT is the statement that specifies where the capture is being stored. And therefore, that is the statement that needs to change to avoid writing to the SD card.

There are so many things wrong with that line... Even ignoring that "curl" is an external program and not Python statement.

a) You need to plug in the actual file name used in the capture command; there is no "/home/pi/filename.jpg" being created

b) What is that "ftp-ipaddressof NAS ..." supposed to represent? Those added spaces are going to play hob with parsing of the command.

c) If you are trying to save that file to a local network (NAS -- network attached storage) server, you don't need FTP... The NAS storage should have been mounted somewhere in the Linux filesystem associated with NFS protocol (or would show up with a "drive letter" in Windows). Just create your filename with the full path to the NAS mount point/directory you want to use for saving videos.

formatting link

formatting link
"""

5 The client machine requests access to exported data, typically by issuing a mount command. (The client asks the server (rpcbind) which port the NFS server is using, the client connects to the NFS server (nfsd), nfsd passes the request to mountd) 6 If all goes well, users on the client machine can then view and interact with mounted filesystems on the server within the parameters permitted. """

Using direct reference to a NAS mounted directory avoids ever writing to the SD card... And if you were going to use FTP you have a network connection (wired or WiFi) so you could have a mounted NAS directory.

--
	Wulfraed                 Dennis Lee Bieber         AF6VN 
	wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/
Reply to
Dennis Lee Bieber

A PiZero has 512MB does it not... even with Linux and some stuff running there's going to be c. 400MB of RAM available which would allow for a fairly big file to be stored in RAM.

Whilst that script was storing the file on the SD card and thus wearing it out, it's a place to start. Get it going then use some kind of tmpfs in RAM so the SD card isn't touched once you have the bugs more or less ironed out.

Reply to
mm0fmf

One jpeg will *easily* fit in system RAM. Also it?s in the filesystem. Write to RAM at /run/shm/ and avoid all sd card writes. (Unless there?s heavy swapping.)

Reply to
A. Dumas

On 14 May 2018 23:12:41 GMT, A. Dumas declaimed the following:

A true JPEG would... But closer look at the provided code indicates it is not a JPEG, but a variant of h.264 VIDEO... That's likely to get a bit larger ... Depends upon resolution and frame rate (which aren't specified in the code -- the V2 Pi Camera module is 8MP as stills, and supports 1080p30 video. I've not found a good source for estimating file size -- Wikipedia seems to show around 20-50kbps for those resolutions... Which should still hold 10 seconds worth of video (but note that the original code does not seem to ever delete anything, it just creates new time-stamped 10-second files with 25-second skips as long as motion continues to be detected).

However, given the OP's statement of trying to put the files on a NAS device -- it should be possible to directly address the NAS storage and touch neither the SD card nor RAM; and that avoids the complication of having to delete the file(s) after transfer. I don't know how much overhead the NAS access would inflict upon the camera capture... 50kbps shouldn't have much of a hit on WiFi or 100Mbps Ethernet. If it does, using RAM for the capture, and using the 25 second gap to do a transfer/delete may be viable.

I'd probably also change the OP's delay logic by capturing the start time after motion is detected and then, after capture (~10 seconds) and post-processing (assuming RAM to NAS/delete RAM), using initial capture time + (say 30 seconds) - current time to determine the sleep time before checking for motion again. This would result in evenly spaced clips as long as there is motion... OP's code, if there were not post-capture processing, is resulting in ~35 second spacing...

--
	Wulfraed                 Dennis Lee Bieber         AF6VN 
	wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/
Reply to
Dennis Lee Bieber

Il 14/05/2018 21:23, RobH ha scritto:

Try to add a third "/" after "ftp:".

Reply to
Cordy

Thanks but still getting the 'invalid syntax' after adding another / So it looks like this ftp:///

Reply to
RobH

Try deleting the second / after 'ftp-host-machine' :-)

--
Any fool can believe in principles -  and most of them do!
Reply to
The Natural Philosopher

This is what I have now:

curl -T /home/pi/filename.jpg ftp://NAS server ip address/mnt/folder/subfolder

And it still fails at the ftp:// part

Reply to
RobH

A NAS usually exports fileshare, and often via smb/cifs mount a fileshare to the NAS on the pi, and save the picture directly on the NAS - without ftp.

eg #mount -t cifs -o username=USERNAME,password=PASSWD //192.168.1.88/shares /mnt/share

(as root)

I from

formatting link

--
--
Reply to
Björn Lundin

On the shell (command line/terminal) or in your Python script?

Reply to
A. Dumas

Ignore the people who are telling you to tinker with the URL. You cannot insert shell commands into Python scripts like that; it will never work.

Instead pay attention to the people telling you to use os.system or the subprocess module.

--
https://www.greenend.org.uk/rjk/
Reply to
Richard Kettlewell

Just use a USB thumb drive for photo storage - that can fill up and wear out without doing any harm to your SD card or system files. Or mount a remote network file system as a local drive and store the photos there.

Reply to
Rob Morley

I've tried telling him but he's not listening.

Reply to
mm0fmf

On 2018 May 15 20:40:52, you wrote to druck:

Ro> import os, subprocess returns bash: import: command not found.

fix your script, then! the very first line should point to the interpreter that should be used to execute the script... so your very first line should be something like

#!/usr/bin/env python3

assuming that you are using python3, that is...

have you posted your script so we can see where your mistakes are and correct them for you? remember to block out any usernames and passwords... you can edit them back in in the fixed script...

)\/(ark

Always Mount a Scratch Monkey Do you manage your own servers? If you are not running an IDS/IPS yer doin' it wrong... ... "A lot of people my age are dead at the present time." Casey Stengel

Reply to
mark lewis

On 2018 May 15 17:38:48, you wrote to All:

DB> {Obviously untested -- I don't have a picamera, and I don't have a NAS} DB> {Save as "CameraExample.py", run by enterings "python CameraExample.py" at DB> the command line} DB> {Watch out for line wrap on long comments; and don't mess up the DB> indentation} DB> -=-=-=-=- #!/usr/bin/env python3 DB> from picamera import PiCamera DB> import time DB> import os DB> import os.path DB> import shutil

FTFY

)\/(ark

Always Mount a Scratch Monkey Do you manage your own servers? If you are not running an IDS/IPS yer doin' it wrong... ... Boiled chicken ovulations with crisped pig lard? Delicious!

Reply to
mark lewis

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.