If you would like to see how the things you make this time will work, please see here (youtube video).
First, go to the site of google cloud platform and press the API and service library. Then scroll down to find the Gmail API and press it. Then press Enable. When the screen changes, press the summary of the one in the list on the left menu. When the screen changes, press Create Credentials on the far right. Then, enter as shown in the image and press the required authentication information below. When you have finished entering this as well, press Create OAuth Client ID. It doesn't matter what the name is. Still, without pressing Done, Press the download area. This will create a file called client_id.json in your current directory.
pip install --upgrade google-api-python-client
pip install requests
pip install httplib2
Since I am using python3.6 this time, if you are using python3.7 etc. and it does not work, please install using pip3.
Go to the Line Developers site and press Documents in the menu above. Scroll down and press Line Notify. When the page changes and you log in, press My page. Then press Create token. Then you will be asked for a name, but it will only be at the beginning of the talk message, so anything is fine. Then the token is displayed, so copy it. However, once you close it, you will not be able to see it again, so be careful.
Create the following file in the directory where client_id.json is located and execute it.
g_oauth.py
import httplib2, os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
CLIENT_SECRET_FILE = '/home/igor-bond/Desktop/client_id.json'
USER_SECRET_FILE = '/home/igor-bond/Desktop/credentials_gmail.json'
def gmail_user_auth():
store = Storage(USER_SECRET_FILE)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = 'Python Gmail API'
credentials = tools.run_flow(flow, store, None)
print('I saved the authentication result:' + USER_SECRET_FILE)
return credentials
Here, a file called credentials_gmail.json that also stores the user's secrets is also created in the same directory.
Create this file in the same directory that you have created so far. For Your token, paste the Line Notify token you copied earlier.
gpio.py
import os,httplib2
from apiclient import discovery
import g_oauth
import time
from datetime import datetime
import picamera
import requests
token = 'Your Token'
def gmail_get_service():
credentials = g_oauth.gmail_user_auth()
http = credentials.authorize(httplib2.Http())
service = discovery.build('gmail', 'v1', http=http)
return service
mail_list = []
def gmail_get_messages():
service = gmail_get_service()
messages = service.users().messages()
msg_list = messages.list(userId='me', maxResults=1).execute()
for msg in msg_list['messages']:
topid = msg['id']
msg = messages.get(userId='me', id=topid).execute()
if msg['snippet'] == 'Security Check2':
if not msg['id'] in mail_list:
mail_list.append(msg['id'])
send_msg()
def send_msg():
filename = datetime.now()
with picamera.PiCamera() as camera:
camera.resolution = (1024,768)
camera.capture(str(filename)+'.jpg')
url = 'https://notify-api.line.me/api/notify'
headers = {'Authorization':'Bearer '+token}
data = {"message":"Here is your room."}
img = f'/home/pi/Desktop/RaspberryPi_for_convenient_life/Projeect 1/{filename}.jpg'
file = {'imageFile': open(img, 'rb')}
r = requests.post(url, headers=headers, params=data, files=file,)
run = True
while run:
try:
time.sleep(30)
gmail_get_messages()
except KeyboardInterrupt:
run = False
Here, every 30 seconds, the email at the top of the logged-in user's Gmail is retrieved, and if the content is "Security Check2" and the email is not processed with the same content, take a picture with Raspberry Pi and Line Notify. Come to me. That is. I haven't implemented it yet, but I think it is necessary to delete the photo after sending it. The photos will accumulate and the operation will be heavy, so ...
How to make this simple surveillance camera is also explained in Youtube, so please have a look if you like it. If you have any questions, please use the comment section of the video or the comment section of this article. Also, if you like it, please subscribe to the channel.