Periodically notify the processing status of Raspberry Pi with python → Google Spreadsheet → LINE

Introduction

About the reason why I decided to make this. I decided to run a little batch processing on the Raspberry Pi, but when I was running the server for a long time, not just the Raspberry Pi, the server stopped unknowingly. There can be an event. Also, although the server itself is working, it is possible that batch processing will start moss for some reason, but it is troublesome to log in and check every day. So, I was happy to be able to notify the smartphone of the result of whether the batch processing is normal or abnormal on the Raspberry Pi. If there is no notification, it can be determined that something went wrong with the server. One way to implement it is

  1. Make Google Spreadsheet run a python program that updates data periodically with cron
  2. IFTTT triggers LINE notification of Google Spreadsheet update It seemed to be possible with the above, so I tried it. It is an article called.

(1) Preparation for posting to Google Spread Sheet

-Access Python Quickstart and do the following: --Click the "Enable the Google Sheets API" button and enter an appropriate project name. Save "credentials.json" locally --Install the dedicated library with the following command.

pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

Regarding the preparation for posting, it is recommended because it is organized in an easy-to-understand manner in past articles. https://qiita.com/connvoi_tyou/items/7cd7ffd5a98f61855f5c

(2) Creating a program for posting to Google SpreadSheet

Create a Spreadsheet on Google Drive in advance. Make a note of part of the file URL as you will need to write it in your program.

https://docs.google.com/spreadsheets/d/<XXXXXXXXXXXXXXXXXXX>/edit#gid=zzzzzzzz
# "<XXXXXXXXXXXXXXXXXXX>"Make a note of

To Google Spreadsheet as below I made it with the name "server_check.py".

server_check.py


from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
creds = None
if os.path.exists('token.pickle'):
    with open('token.pickle', 'rb') as token:
        creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
        creds.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
        creds = flow.run_local_server(port=0)
    # Save the credentials for the next run
    with open('token.pickle', 'wb') as token:
        pickle.dump(creds, token)

service = build('sheets', 'v4', credentials=creds)

#So far, it is almost the same as the one described in the guide provided by google
#Below, fill in the information specific to Spreadsheet.
spreadsheet_id = '<XXXXXXXXXXXXXXXXXXX>'
sheetname='Sheet 1'
range_ = sheetname

#Get date information
import datetime
_str_dt=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")

#Substitute data to write to columns A, B, and C
#Column A is the date, columns B and C are arbitrary character strings(This time, substitute a test string)
values = [
  [_str_dt,"Status-1","Status-2"]
]
body = {
  'values' : values
}

#Write to Spreadsheet
result=service.spreadsheets().values().append(spreadsheetId=spreadsheet_id,valueInputOption='RAW',range=range_,body=body).execute()
print(result)

In the above program, the test message is something like "Status-1". When actually operating it, extract the output of the log file and I think it will be added to the values array.

(3) Program execution and browser permissions

After fixing the necessary parts, execute the above python program. ** At that time, the "credentials.json" file must exist in the same directory. ** ** Also, when the program is executed for the first time, browser authentication is required, so it must be executed in the GUI environment.

When executed, the browser will be launched and the confirmation page for the google account will be displayed. Grant access rights according to the screen. (On the way, a warning to unsafe pages will be displayed, but it will continue) When a character like "The authentication flow has completed. You may close this window." Is output to the browser, the setting is complete.

When you've granted access, you should have an entry in your Spreadsheet (with a value in the first row). Also, a "token.pickle" file will be created in the same directory as the program. With this file, the next time you SKIP your browser authentication, the line will be added.

(4) Notification settings in IFTTT

  1. IFTTT app is required, if not installed & initial setup
  2. Make an applet with "Make your own Applets from scratch"
  3. This (trigger) is "Google Sheets"-> "New row added to spreadsheet"
  4. Enter the sheet information created in (2)
  5. That (action) is "LINE"-> "Send message"
  6. Recipient "receives notifications from LINE Notify 1: 1"
  7. Customize the message body

Where I got stuck

If the browser cannot be opened in the program execution environment

The ultimate goal this time was to run the target program from the Raspberry Pi, but I did not have a GUI environment on the Raspberry Pi (usually working with an SSH connection). Even if I execute "server_check.py" on the Raspberry Pi, the browser cannot be opened, so I cannot set the permissions.

In such a case, if you run the same program in another GUI environment and place the generated "token.pickle" file on the target server, you should be able to run the program on the CUI as well. I can now run it from my Raspberry Pi by running the same program on my Mac and sending the generated token.pickle to my Raspberry Pi.

I get an error when I run the program

Occasionally, when I ran the program, I got an error.

[root@localhost python]# python server_check.py 
Traceback (most recent call last):
  File "server_check.py", line 17, in <module>
    creds.refresh(Request())
  File "/usr/local/lib/python3.7/site-packages/google/oauth2/credentials.py", line 182, in refresh
    self._scopes,
  File "/usr/local/lib/python3.7/site-packages/google/oauth2/_client.py", line 248, in refresh_grant
    response_data = _token_endpoint_request(request, token_uri, body)
  File "/usr/local/lib/python3.7/site-packages/google/oauth2/_client.py", line 124, in _token_endpoint_request
    _handle_error_response(response_body)
  File "/usr/local/lib/python3.7/site-packages/google/oauth2/_client.py", line 60, in _handle_error_response
    raise exceptions.RefreshError(error_details, response_body)
google.auth.exceptions.RefreshError: ('invalid_scope: Some requested scopes were invalid. {invalid=[a, c, d, e, g, h, i, l, m, ., /, o, p, r, s, t, u, w, :]}', '{\n  "error": "invalid_scope",\n  "error_description": "Some requested scopes were invalid. {invalid\\u003d[a, c, d, e, g, h, i, l, m, ., /, o, p, r, s, t, u, w, :]}",\n  "error_uri": "http://code.google.com/apis/accounts/docs/OAuth2.html"\n}')
[root@localhost python]# 

I'm not sure what the error was, but I solved it by recreating "token.pickle". To recreate it, just delete "token.pickle" and run the program. However, please be careful about the CUI environment where you cannot open the browser because it will be set from the browser again.

By the way, I don't know why it happens, but from a personal point of view, I feel that an error occurred at the following timing.

--When the program is significantly renovated --When you execute another program and authenticate

in conclusion

With this, I started to notice early when batch processing was abnormal or when the server stopped, but I'm a little tired of LINE coming every day. Actually, I wanted to notify LINE when an abnormality was detected (the server detected an abnormality or there was no regular contact) in the style of "no news is a healthy proof", but that is the next issue. So that's it.

Recommended Posts

Periodically notify the processing status of Raspberry Pi with python → Google Spreadsheet → LINE
Notify LINE of body temperature from BLE thermometer with Raspberry Pi # 1
Periodically log the value of Omron environment sensor with Raspberry Pi
Get CPU information of Raspberry Pi with Python
Measure CPU temperature of Raspberry Pi with Python
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Get the operation status of JR West with Python
Let's operate GPIO of Raspberry Pi with Python CGI
I tried running Movidius NCS with python of Raspberry Pi3
Access google spreadsheet using python on raspberry pi (for myself)
Google search for the last line of the file in Python
Use vl53l0x with Raspberry Pi (python)
Control the motor with a motor driver using python on Raspberry Pi 3!
Basics of binarized image processing with Python
The story of making a university 100 yen breakfast LINE bot with Python
CSV output of pulse data with Raspberry Pi (confirm analog input with python)
Check the existence of the file with python
Periodically perform arbitrary processing with Python Twisted
Notify LINE of location information (Google Maps) with GPS multi-unit SORACOM Edition
Receive a list of the results of parallel processing in Python with starmap
Drawing with Matrix-Reinventor of Python Image Processing-
Logging the value of Omron environment sensor with Raspberry Pi (USB type)
The story of blackjack A processing (python)
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
Status of each Python processing system in 2020
python> print> Redirected only at the end of processing?> Run with -u
Working with GPS on Raspberry Pi 3 Python
Around the authentication of PyDrive2, a package that operates Google Drive with Python
Try to use up the Raspberry Pi 2's 4-core CPU with Parallel Python
I tried to automatically send the literature of the new coronavirus to LINE with Python
Discord bot with python raspberry pi zero with [Notes]
Send a message to LINE with Python (LINE Notify)
Prepare the execution environment of Python3 with Docker
2016 The University of Tokyo Mathematics Solved with Python
[Note] Export the html of the site with python.
View the result of geometry processing in Python
Calculate the total number of combinations with python
Check the date of the flag duty with Python
Image processing? The story of starting Python for
CSV output of pulse data with Raspberry Pi (CSV output)
Observe the Geminids meteor shower with Raspberry Pi 4
Sound the buzzer using python on Raspberry Pi 3!
[Python] Troubleshooting before accessing Google Spreadsheet with gspread
Convert the character code of the file with Python3
One-liner that outputs 10000 digits of pi with Python
[Python] Determine the type of iris with SVM
Connect to MySQL with Python on Raspberry Pi
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
Various memorandums when using sdk of LINE Messaging API with Python (2.7.9) + Google App Engine
I tried to compare the processing speed with dplyr of R and pandas of Python
Extract the table of image files with OneDrive & Python
Using the digital illuminance sensor TSL2561 with Raspberry Pi
Learn Nim with Python (from the beginning of the year).
Display images taken with the Raspberry Pi camera module
Try to visualize the room with Raspberry Pi, part 1
How to use the Raspberry Pi relay module Python
Destroy the intermediate expression of the sweep method with Python
Try debugging Python on Raspberry Pi with Visual Studio.
Detect mask wearing status with OpenCV and Raspberry Pi
Visualize the range of interpolation and extrapolation with python
Calculate the regression coefficient of simple regression analysis with python