I need some help! Does any one know how i can get my raspberry pi, with pi cam; to take a picture automatically every minute and then email it to an email address automatically?? Any help with this would be great!
Code: Select all
import time
from datetime import datetime
import picamera
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
with picamera.PiCamera() as camera:
camera.resolution = (1024, 768)
camera.start_preview()
# Camera warm-up time
time.sleep(2)
camera.capture('photo.jpg')
f_time = datetime.now().strftime('%a %d %b @ %H:%M')
toaddr = 'xxxxxx@gmail.com' # redacted
me = 'picamera@nnn.nnn.nnn.nnn.ifb.co.uk' # redacted
subject = 'Photo ' + f_time
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = me
msg['To'] = toaddr
msg.preamble = "Photo @ " + f_time
fp = open('photo.jpg', 'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)
try:
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
except:
print ("Error: unable to send email")I found I had to use pi@internet-resolvable-address.my-isp.co.uk because otherwise GMail rejected it as potential spam.archieoboyle wrote:Ok yes I see, the email address from the pi would just be the raspberrypi@IPADDRESS ?
Code: Select all
#!/usr/bin/python3
from urllib.request import urlopen
ip = urlopen('http://ipaddr.me/').read()
ip = ip.decode('utf-8')
print (ip)
import socket
name, alias, addresslist = socket.gethostbyaddr(ip)
print (name)