User avatar
joseplaselva
Posts: 87
Joined: Tue Oct 04, 2016 4:45 am
Location: Kvilla , Sweden

About Pi camera , intervals and email

Fri Mar 24, 2017 4:29 pm

Hi, I try to take pictures with the pi cam at a interval of 60 seconds, and send by email after each shoot. I can't found the way to establish a count of time for the interval that runs simultanoesly with the fact of send the pic by email. Hereby the code I have to just take a pic, send by email and move the file to a usb stick memory. Now I need the way to take exactly a picture every 60 seconds. Thanks in advance for your always useffull help.

Code: Select all

# -*- coding: utf-8 -*-

import picamera
import time
import shutil
import smtplib

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
from email.mime.image import MIMEImage
 
fromaddr = "[email protected]"   
toaddr = "xxxx[email protected]"

camera = picamera.PiCamera()
camera.awb_mode= 'auto'
camera.brightness=55

mail = MIMEMultipart()
mail['Subject'] = "Pictures from home"
mail['From'] = fromaddr
mail['To'] = toaddr

body = "Please find the attachment"
path = "/home/pi/"
moveto = "/media/pi/STORE N GO/"
data=""
texte=""

def capture_image():
    global texte
    data= time.strftime("%Y-%b-%d_(%H%M%S)")
    texte= "picture take:"+ data
    camera.start_preview()
    time.sleep(2)
    camera.capture('%s.jpg'%data, resize=(720, 540))
    camera.stop_preview()
    time.sleep(1)
    
    sendMail(data)
    
def sendMail(data):
    global texte
    mail.attach(MIMEText(tbody, 'plain'))

    dat='%s.jpg'%data
    attachment = open(dat, 'rb')
    image=MIMEImage(attachment.read())
    attachment.close()
    mail.attach(image)
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, "xxxxpasword")
    text = mail.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()

    movepic(data)

def movepic(data):
    pic= '%s.jpg'%data
    src = path+pic
    dst = moveto+pic
    shutil.move(src,dst)

capture_image()


        

knute
Posts: 445
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: About Pi camera , intervals and email

Fri Mar 24, 2017 6:52 pm

I can't help you with the python code but there are two generic ways to accomplish what you want and that is to take a picture, email somewhere and then take the next picture after a fixed time interval. Since you said every 60 seconds we'll assume that sending of the email takes less time than your interval. So some pseudo code:

time = now
takePicture
wait until now = time + 60 seconds
loop

Or you can have a timing loop and send the email job off to another thread:

every 60 seconds
start picture taking thread that calls takePicture
loop

gordon77
Posts: 4072
Joined: Sun Aug 05, 2012 3:12 pm

Re: About Pi camera , intervals and email

Sat Mar 25, 2017 2:55 pm

Here's a fairly simple solution by using datetime and reading the seconds on the clock, when they are 0 it takes the picture etc. I have added a step to make sure it only does it once, at 1 second past, just in case it manages to take a photo and send it in less than 1 sec( which I doubt)

Code: Select all

# -*- coding: utf-8 -*-

import picamera
import time
import datetime
import shutil
import smtplib

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
from email.mime.image import MIMEImage
 
fromaddr = "[email protected]"   
toaddr = "[email protected]"

camera = picamera.PiCamera()
camera.awb_mode= 'auto'
camera.brightness=55

mail = MIMEMultipart()
mail['Subject'] = "Pictures from home"
mail['From'] = fromaddr
mail['To'] = toaddr

body = "Please find the attachment"
path = "/home/pi/"
moveto = "/media/pi/STORE N GO/"
data=""
texte=""

def capture_image():
    global texte
    data= time.strftime("%Y-%b-%d_(%H%M%S)")
    texte= "picture take:"+ data
    camera.start_preview()
    time.sleep(2)
    camera.capture('%s.jpg'%data, resize=(720, 540))
    camera.stop_preview()
    time.sleep(1)
    
    sendMail(data)
    
def sendMail(data):
    global texte
    mail.attach(MIMEText(tbody, 'plain'))

    dat='%s.jpg'%data
    attachment = open(dat, 'rb')
    image=MIMEImage(attachment.read())
    attachment.close()
    mail.attach(image)
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, "xxxxpasword")
    text = mail.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()

    movepic(data)

def movepic(data):
    pic= '%s.jpg'%data
    src = path+pic
    dst = moveto+pic
    shutil.move(src,dst)

while True:
   if str(datetime.datetime.now().time())[6:8] == "00":
      while str(datetime.datetime.now().time())[6:8] == "00":
          time.sleep(.1)
      capture_image()
or if you want to set other times you can use time eg 59 seconds...

Code: Select all

# -*- coding: utf-8 -*-

import picamera
import time
import shutil
import smtplib

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
from email.mime.image import MIMEImage
 
fromaddr = "[email protected]"   
toaddr = "[email protected]"

camera = picamera.PiCamera()
camera.awb_mode= 'auto'
camera.brightness=55

mail = MIMEMultipart()
mail['Subject'] = "Pictures from home"
mail['From'] = fromaddr
mail['To'] = toaddr

body = "Please find the attachment"
path = "/home/pi/"
moveto = "/media/pi/STORE N GO/"
data=""
texte=""

def capture_image():
    global texte
    data= time.strftime("%Y-%b-%d_(%H%M%S)")
    texte= "picture take:"+ data
    camera.start_preview()
    time.sleep(2)
    camera.capture('%s.jpg'%data, resize=(720, 540))
    camera.stop_preview()
    time.sleep(1)
    
    sendMail(data)
    
def sendMail(data):
    global texte
    mail.attach(MIMEText(tbody, 'plain'))

    dat='%s.jpg'%data
    attachment = open(dat, 'rb')
    image=MIMEImage(attachment.read())
    attachment.close()
    mail.attach(image)
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, "xxxxpasword")
    text = mail.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()

    movepic(data)

def movepic(data):
    pic= '%s.jpg'%data
    src = path+pic
    dst = moveto+pic
    shutil.move(src,dst)

settime = 59

while True:
   start = time.time()
   while time.time() - start < settime:
      time.sleep(.1)
   capture_image()

User avatar
joseplaselva
Posts: 87
Joined: Tue Oct 04, 2016 4:45 am
Location: Kvilla , Sweden

Re: About Pi camera , intervals and email

Sat Mar 25, 2017 4:41 pm

Thanks a lot, the last code works perfectly , I've been checking taking pictures of a clock and take exactly 60 sec interval.
Thanks AGAIN

gordon77
Posts: 4072
Joined: Sun Aug 05, 2012 3:12 pm

Re: About Pi camera , intervals and email

Sat Mar 25, 2017 6:48 pm

This should be a bit better it will keep the 60 second interval even if the photo/mail etc takes longer than expected..

Code: Select all

# -*- coding: utf-8 -*-

import picamera
import time
import shutil
import smtplib

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
from email.mime.image import MIMEImage
 
fromaddr = "[email protected]"   
toaddr = "[email protected]"

camera = picamera.PiCamera()
camera.awb_mode= 'auto'
camera.brightness=55

mail = MIMEMultipart()
mail['Subject'] = "Pictures from home"
mail['From'] = fromaddr
mail['To'] = toaddr

body = "Please find the attachment"
path = "/home/pi/"
moveto = "/media/pi/STORE N GO/"
data=""
texte=""

def capture_image():
    global texte
    data= time.strftime("%Y-%b-%d_(%H%M%S)")
    texte= "picture take:"+ data
    camera.start_preview()
    time.sleep(2)
    camera.capture('%s.jpg'%data, resize=(720, 540))
    camera.stop_preview()
    time.sleep(1)
    
    sendMail(data)
    
def sendMail(data):
    global texte
    mail.attach(MIMEText(tbody, 'plain'))

    dat='%s.jpg'%data
    attachment = open(dat, 'rb')
    image=MIMEImage(attachment.read())
    attachment.close()
    mail.attach(image)
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, "xxxxpasword")
    text = mail.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()

    movepic(data)

def movepic(data):
    pic= '%s.jpg'%data
    src = path+pic
    dst = moveto+pic
    shutil.move(src,dst)

settime = 60
start = time.time()

while True:
   while time.time() - start < settime:
      time.sleep(.1)
   start = time.time()
   capture_image()

Krishan Kumar
Posts: 2
Joined: Sun Mar 26, 2017 2:25 pm

How to upload image from Raspberry pi to Firebase server

Sun Mar 26, 2017 2:44 pm

I have an USB webcam which i have used to capture image.I have connected webcam with raspberry pi and stored the image in folder.Now ,problem is that how i can upload that image from raspberry pi to firebase server.

User avatar
joseplaselva
Posts: 87
Joined: Tue Oct 04, 2016 4:45 am
Location: Kvilla , Sweden

Re: About Pi camera , intervals and email

Sun Mar 26, 2017 7:21 pm

The interval works fine , thanks.
But a strange behaviour occure whith the email action: even if I move the file to a USB driver , all the pictures are attached to the mails , I mean that I got every email with all the previous pictures atached (???) I miss something in my code? how can be ? Any idea?. Many thanks again for all the help.
Cheers

gordon77
Posts: 4072
Joined: Sun Aug 05, 2012 3:12 pm

Re: About Pi camera , intervals and email

Mon Mar 27, 2017 8:09 am

The reason is its not moving the images, you use path and moveto but don't pass them to the function.

add

Code: Select all

global path
global moveto

to the movepic def.

Code: Select all

def movepic(data):
    global path
    global moveto
    pic= '%s.jpg'%data
    src = path+pic
    dst = moveto+pic
    print src,dst
    shutil.move(src,dst)

User avatar
joseplaselva
Posts: 87
Joined: Tue Oct 04, 2016 4:45 am
Location: Kvilla , Sweden

Re: About Pi camera , intervals and email

Mon Mar 27, 2017 3:27 pm

Thanks again for your fast reply , but still having the problem . Moving the file it worked even with no global declaration of the path and move but I writed as you say for much safety , the result it's the same ,even when I delete directly from the pi file manager , the file appears again and again in all the folowing mails. Can be a problem with gmail server ? . Is it a way to clear the attachement content ( exist a buffer in this element that save the pics ) . Only when I close the script and start again works well, but just for the firs image sended, after this appears all the images attached . First email - just one / second email - two / third email -three , and so and so....

ghp
Posts: 1392
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: About Pi camera , intervals and email

Mon Mar 27, 2017 3:42 pm

Hello,
your mail-object is created on 'globel scope'

Code: Select all

mail = MIMEMultipart()
mail['Subject'] = "Pictures from home"
mail['From'] = fromaddr
mail['To'] = toaddr
and so exists all the time.
Perhaps it helps to move this code fragment into the sendMail-Function ?

Regards,
Gerhard

User avatar
joseplaselva
Posts: 87
Joined: Tue Oct 04, 2016 4:45 am
Location: Kvilla , Sweden

Re: About Pi camera , intervals and email

Mon Mar 27, 2017 3:50 pm

Simply GREAT!!!!!! Thanks Gerhard , now runs fine, just one at each mail, it was as you well said that object was declared at the begining as global....
One learn a lot with so inteligent people like you. Many Many thanks .
Have a nice day

gordon77
Posts: 4072
Joined: Sun Aug 05, 2012 3:12 pm

Re: About Pi camera , intervals and email

Mon Mar 27, 2017 6:48 pm

joseplaselva wrote:Thanks again for your fast reply , but still having the problem . Moving the file it worked even with no global declaration of the path and move but I writed as you say for much safety , the result it's the same ,even when I delete directly from the pi file manager , the file appears again and again in all the folowing mails. Can be a problem with gmail server ? . Is it a way to clear the attachement content ( exist a buffer in this element that save the pics ) . Only when I close the script and start again works well, but just for the firs image sended, after this appears all the images attached . First email - just one / second email - two / third email -three , and so and so....
Interesting the move didn't work on my pi with python 2, without the globals added, are you using python 3?

User avatar
joseplaselva
Posts: 87
Joined: Tue Oct 04, 2016 4:45 am
Location: Kvilla , Sweden

Re: About Pi camera , intervals and email

Tue Mar 28, 2017 7:35 am

I use pyhton v.2.7.9.
But I must admit that sometimes works ans sometimes I need to especify the variable as global inside a def function, but in this script it runs withouth...

rattidotcom
Posts: 13
Joined: Tue Jan 29, 2019 12:30 am

Re: About Pi camera , intervals and email

Tue Jan 29, 2019 9:39 pm

Hello,
I know this is old, but I hope someone will read it :)
I copied the code and get it to run but the "images" I get to my email adress don´t have a file extension.
When I download the files and simle add .jpg to the end of filename I can open and view them.
Is there any solution that the files keep the .jpg ending after sending them via email?
Hope somebody can help me..
Greets from germany

gordon77
Posts: 4072
Joined: Sun Aug 05, 2012 3:12 pm

Re: About Pi camera , intervals and email

Thu Jan 31, 2019 10:36 am

I have tried the original code and it does send a jpg with the extension, so not sure why yours isn't working.

i had to correct the line mail.attach(MIMEText(body, 'plain')) as it said tbody,

updated to python3

Code: Select all

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import picamera
import time
import shutil
import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from email.mime.image import MIMEImage
 
fromaddr = "[email protected]"   
toaddr = "[email protected]"

camera = picamera.PiCamera()
camera.awb_mode= 'auto'
camera.brightness=55

mail = MIMEMultipart()
mail['Subject'] = "Pictures from home"
mail['From'] = fromaddr
mail['To'] = toaddr

body = "Please find the attachment"
path = "/home/pi/"
moveto = "/media/pi/STORE N GO/"
data=""
texte=""

def capture_image():
    global texte
    data= time.strftime("%Y-%b-%d_(%H%M%S)")
    texte= "picture take:"+ data
    camera.start_preview()
    time.sleep(2)
    camera.capture('%s.jpg'%data, resize=(720, 540))
    camera.stop_preview()
    time.sleep(1)
    
    sendMail(data)
    
def sendMail(data):
    global texte
    mail = MIMEMultipart()
    mail.attach(MIMEText(body, 'plain'))

    dat='%s.jpg'%data
    attachment = open(dat, 'rb')
    image=MIMEImage(attachment.read())
    attachment.close()
    mail.attach(image)
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, "xxxxxxxxx")
    text = mail.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()

    movepic(data)

def movepic(data):
    pic= '%s.jpg'%data
    src = path+pic
    dst = moveto+pic
    shutil.move(src,dst)

settime = 1

while True:
   start = time.time()
   while time.time() - start < settime:
      time.sleep(.1)
   capture_image()
Last edited by gordon77 on Fri Apr 19, 2019 2:12 pm, edited 2 times in total.

rattidotcom
Posts: 13
Joined: Tue Jan 29, 2019 12:30 am

Re: About Pi camera , intervals and email

Thu Jan 31, 2019 10:49 am

I added
image.add_header('Content-Disposition', 'attachment; filename=filename.jpg')
under attachment.close()
Now it works for me too.

SeppPi
Posts: 13
Joined: Sat Sep 30, 2017 2:30 pm

Re: About Pi camera , intervals and email

Thu Mar 28, 2019 12:55 pm

Hello gordon77, may I ask you something to this thing here?

I use your code for my project and it is working fine. The only thing is that it sends me always all pictures in the mail. When it captures the first, it sends one, when it captures the second it sends the first and the second and so on. Do you know why?

Greetings

gordon77
Posts: 4072
Joined: Sun Aug 05, 2012 3:12 pm

Re: About Pi camera , intervals and email

Fri Mar 29, 2019 11:29 am

HI,

I have tried various things but I can't stop it from sending more and more attachments. I am confused why as it should only attach one jpg.

Maybe others on here can advise.

SeppPi
Posts: 13
Joined: Sat Sep 30, 2017 2:30 pm

Re: About Pi camera , intervals and email

Fri Mar 29, 2019 2:49 pm

Hello, I found out. It is because the function "mail = MIMEMultipart()" is call outside the def send_Mail(). When you call it inside this function, it sends only one picture.

gordon77
Posts: 4072
Joined: Sun Aug 05, 2012 3:12 pm

Re: About Pi camera , intervals and email

Fri Mar 29, 2019 4:09 pm

Thanks. :D

Gordon77

Code: Select all

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import picamera
import time
import shutil
import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from email.mime.image import MIMEImage
 
fromaddr = "[email protected]"   
toaddr = "[email protected]"

camera = picamera.PiCamera()
camera.awb_mode= 'auto'
camera.brightness=55

body = "Please find the attachment"
path = "/home/pi/"
moveto = "/home/pi/Pictures/"
data=""
texte=""

def capture_image():
    global texte
    data= time.strftime("%Y-%b-%d_(%H%M%S)")
    texte= "picture take:"+ data
    camera.start_preview()
    time.sleep(2)
    camera.capture('%s.jpg'%data, resize=(720, 540))
    camera.stop_preview()
    time.sleep(1)
    
    sendMail(data)
    
def sendMail(data):
    global texte, fromaddr, toaddr
    mail = MIMEMultipart()
    mail['Subject'] = "Pictures from home"
    mail['From'] = fromaddr
    mail['To'] = toaddr
    mail.attach(MIMEText(body, 'plain'))

    dat='%s.jpg'%data
    attachment = open(dat, 'rb')
    image=MIMEImage(attachment.read())
    attachment.close()
    mail.attach(image)
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, "password")
    text = mail.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()

    movepic(data)

def movepic(data):
    pic= '%s.jpg'%data
    src = path+pic
    dst = moveto+pic
    shutil.move(src,dst)

settime = 1

while True:
   start = time.time()
   while time.time() - start < settime:
      time.sleep(.1)
   capture_image()

Return to “Python”