Reputation: 677
I would like to implement the following concept on Python but, I'm trying to figure out the programming idea.
An image will be taken using a camera, if Internet connection is available then upload it on an FTP Server, if not keep the image stored on the drive. In the meanwhile keep doing other stuff but, when the internet connection is up again then upload it.
This could be number of images waiting to be uploaded. What do you think?
I already wrote the code to take the picture and upload it, but if there is no internet connection to upload the image on the ftp server, the script returns errors and quit. The code bellow is just some parts from my project. I have shared what I think is useful to get an idea of what Im trying to do.
def takePicture(self, image_name):
image_path = '/home/pi/pictures/' + image_name
interact().ftpSession(image_path, image_name)
rLink = 'http://www.webpage.com/images/' + image_name
print rLink
interact().sendSms("Demo Picture " + rLink)
def grabPicture(self):
grab_cam = subprocess.Popen("sudo fswebcam --timestamp '%d-%m-%Y %H:%M:%S (%Z)' -r 640x480 -d /dev/v4l/by-id/usb-OmniVision_Technologies__Inc._USB_Camera-B4.09.24.1-video-index0 -q /home/pi/pictures/%m-%d-%y-%H%M.jpg", shell=True)
grab_cam.wait()
todays_date = datetime.datetime.today()
image_name = todays_date.strftime('%m-%d-%y-%H%M') + '.jpg'
return image_name
def ftpSession(self, image_path, image_name):
session = ftplib.FTP('ftp.webpage.com','user','fg78fy87fyg')
session.cwd('images') #Give the rigth folder where to store the image
print "FTP Connection established"
file = open(image_path,'rb') # file to send
session.storbinary('STOR ' + image_name, file) # send the file
file.close() # close file and FTP
session.quit()
link = 'http://www.webpage.com/images/' + image_name
print "File has been uploaded!"
return link
def pir():
prevState = 0
while True:
time.sleep(0.1)
currState=mcp2.input(sensorPin)
if prevState==0 and currState==128:
image_name = interact().grabPicture()
#....#
status = database().getState()
#.....#
if (status == 'True'):
#Do something
else:
if os.path.exists('/home/pi/pictures/'+image_name):
os.remove('/home/pi/pictures/'+image_name) #Deletes the taken picture in case of False-Alarm
print 'File', image_name, 'has beeen deleted'
prevState = currState
time.sleep(1)
Process(target=pir).start()
Upvotes: 0
Views: 373
Reputation: 16711
I'd use urllib
for a simple check to see if the internet is connected:
while True:
try:
urllib.urlopen('http://google.com')
break # exit loop if connected
except:
print 'Establish a connection.'
time.sleep(5) # wait five seconds
print 'Now continue' # outside of loop
Upvotes: 2
Reputation: 4507
Have a thread that tries to upload the image every once in a while (e.g. every 5 minutes or whatever amount seems to fit your needs), and keeps iterating if it fails due to lack of internet connection (use try/except).
Pseudocode:
import time
while True:
try:
upload_file()
except NoConnectionException:
time.sleep(300)
Upvotes: 0