Difference between revisions of "EBC Exercise 37 Logging to Sheets"

From eLinux.org
Jump to: navigation, search
(Initial Page)
 
m (JavaScript: Removed)
 
(19 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
{{YoderHead}}
 
{{YoderHead}}
  
Here are instructions on how to log data to a [https://docs.google.com/spreadsheets/u/0/ Google Sheet] from JavaScript.
+
Here are instructions on how to log data to a [https://docs.google.com/spreadsheets/u/0/ Google Sheet] from JavaScript or Python.
  
 
== Create a new Sheet ==
 
== Create a new Sheet ==
Line 9: Line 9:
 
* Give the sheet a meaningful name
 
* Give the sheet a meaningful name
 
* Enter some column labels
 
* Enter some column labels
[[File:BlankSheet.png|frame|Blank Google Sheet]]
+
[[File:BlankSheet.png|480px|Blank Google Sheet]]
 
* Note the sheetID in the path
 
* Note the sheetID in the path
[[File:SheetID.png|frame|SheetID]]
+
[[File:SheetID.png|SheetID]]
  
 +
== Get Credentials ==
 +
Go to the class repo and change to the sheets directory
 +
bone$ '''cd exercises/iot/google/sheets'''
  
 +
If you are using Python, got to
 +
[https://developers.google.com/sheets/api/quickstart/python https://developers.google.com/sheets/api/quickstart/python] and do
 +
everything up through '''Install the Google client library'''.
 +
 +
Put the '''credentials.json''' file in the sheets directory.
 +
 +
== Run the example ==
 +
=== Python ===
 +
* Edit '''demo.py''' and change the '''sheetID''' string to the string you noted above.
 +
* Run the example on your Bone.  The arguments are the values to be logged on the sheet.
 +
bone$ '''./demo.py 3.14 10'''
 +
Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=834314057708-q7dsgi6i0eeef4ap6gh3d6k2qu64g5k.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&state=mNbw6E6ZIbpHqU2j6mrxPamewzcbUE&prompt=consent&access_type=offline&code_challenge=AV-yuItiICbH9KjdudKbLCwg1KDJ-vvCzUUzotDNijE&code_challenge_method=S256
 +
 +
The first time you will have to paste a URL into your browser.  When you see
 +
 +
[[File:Appnotverified.png]]
 +
 +
go to Advanced and click the '''Go to Logging Data (unsafe)'''.  It's unsafe since Google hasn't verified our app.
 +
 +
Finally paste the string back to the Bone.
 +
 +
== Plot the Data ==
 +
Go to your Google sheet and the new data, and a time stamp should be there.
 +
 +
* Select the '''A''' column and select '''Format:Number:Date time'''
 +
[[File:Date.png|Formatting the Date]]
 +
* Select all the columns and click '''Insert chart'''
 +
[[File:InsertChart.png|Insert chart]]
 +
* Select '''Line chart'''
 +
* Select '''Use row 1 as headers'''
 +
* Select '''Use column A as labels'''
 +
[[File:FormatChart.png|Chart formatting options]]
 +
 +
Your data should be plotted.  Try running your script again and the chart will update.
  
 
{{YoderFoot}}
 
{{YoderFoot}}

Latest revision as of 11:20, 7 February 2023

thumb‎ Embedded Linux Class by Mark A. Yoder


Here are instructions on how to log data to a Google Sheet from JavaScript or Python.

Create a new Sheet

Create a new sheet by:

  • Going to https://sheets.google.com and clicking to Start a new spreadsheet, Blank
  • Give the sheet a meaningful name
  • Enter some column labels

Blank Google Sheet

  • Note the sheetID in the path

SheetID

Get Credentials

Go to the class repo and change to the sheets directory

bone$ cd exercises/iot/google/sheets

If you are using Python, got to https://developers.google.com/sheets/api/quickstart/python and do everything up through Install the Google client library.

Put the credentials.json file in the sheets directory.

Run the example

Python

  • Edit demo.py and change the sheetID string to the string you noted above.
  • Run the example on your Bone. The arguments are the values to be logged on the sheet.
bone$ ./demo.py 3.14 10
Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=834314057708-q7dsgi6i0eeef4ap6gh3d6k2qu64g5k.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&state=mNbw6E6ZIbpHqU2j6mrxPamewzcbUE&prompt=consent&access_type=offline&code_challenge=AV-yuItiICbH9KjdudKbLCwg1KDJ-vvCzUUzotDNijE&code_challenge_method=S256

The first time you will have to paste a URL into your browser. When you see

Appnotverified.png

go to Advanced and click the Go to Logging Data (unsafe). It's unsafe since Google hasn't verified our app.

Finally paste the string back to the Bone.

Plot the Data

Go to your Google sheet and the new data, and a time stamp should be there.

  • Select the A column and select Format:Number:Date time

Formatting the Date

  • Select all the columns and click Insert chart

Insert chart

  • Select Line chart
  • Select Use row 1 as headers
  • Select Use column A as labels

Chart formatting options

Your data should be plotted. Try running your script again and the chart will update.




thumb‎ Embedded Linux Class by Mark A. Yoder