[PYTHON] Gently explain the process of making a simple serverless surveillance camera using Raspberry Pi, Gmail API and Line API

0. First

If you would like to see how the things you make this time will work, please see here (youtube video).

1. Gmail settings

First, go to the site of google cloud platform and press the API and service library. 1.png Then scroll down to find the Gmail API and press it. 2.png Then press Enable. 3.png When the screen changes, press the summary of the one in the list on the left menu. 4.png When the screen changes, press Create Credentials on the far right. 5.png Then, enter as shown in the image and press the required authentication information below. 6.png When you have finished entering this as well, press Create OAuth Client ID. It doesn't matter what the name is. 7.png Still, without pressing Done, 8.png Press the download area. This will create a file called client_id.json in your current directory. 9.png

2. Download the required library

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.

3. Line API settings

Go to the Line Developers site and press Documents in the menu above. l_2.png Scroll down and press Line Notify. l3.png When the page changes and you log in, press My page. l4.png 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. l5.png

4. Gmail authentication

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.

5. Create main process

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 ...

Finally

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.

Recommended Posts

Gently explain the process of making a simple serverless surveillance camera using Raspberry Pi, Gmail API and Line API
Create a color sensor using a Raspberry Pi and a camera
A memorandum when making a surveillance camera with Raspberry Pi
Create a web surveillance camera with Raspberry Pi and OpenCV
Get the weather using the API and let the Raspberry Pi speak!
Make a simple CO2 incubator using Raspberry PI and CO2 sensor (MH-Z14A)
The story of making a sound camera with Touch Designer and ReSpeaker
Create a simple app that incorporates the Fetch API of Ajax requests in Flask and explain it quickly
When a file is placed in the shared folder of Raspberry Pi, the process is executed.
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
A story that I wanted to realize the identification of parking lot fullness information using images obtained with a Web camera and Raspberry Pi and deep learning.
Send and receive Gmail via the Gmail API using Python
I tried to make a motion detection surveillance camera with OpenCV using a WEB camera with Raspberry Pi
Create a partition and then install the Raspberry Pi OS
The process of making Python code object-oriented and improving it
I made a surveillance camera with my first Raspberry PI.
The story of creating a database using the Google Analytics API
I made a Chatbot using LINE Messaging API and Python
A program that receives the servo command of the radio control, interrupts the Raspberry Pi and logs it
"2/2" I am making a simple web application for robot operation. "Raspberry Pi 3B + and Django Channels"
"1/2" I am making a simple web application for robot operation. "Raspberry Pi 3B + and Django Channels"
Control the motor with a motor driver using python on Raspberry Pi 3!
Evaluate the performance of a simple regression model using LeaveOneOut cross-validation
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
Find the intersection of a circle and a straight line (sympy matrix)