|
1 | 1 | # arduino-gmail-client
|
2 | 2 |
|
3 |
| -A simple gmail client that makes cURL requests using gmail API to check for unread emails, the response is then used to control arduino pins. So if you connect an LED to the right pins, it will light up everytime you recieve an email! |
| 3 | +A simple gmail API client that makes cURL requests to check for unread emails, and controls arduino GPIO pins. So.. when you get an email, an LED lights up! |
4 | 4 |
|
5 |
| -This was something I wanted to try when I first stumbled upon gmail API and OAuth 2.0 protocol, finally got an arduino to implement it!! |
| 5 | +This is a proof of concept, something I wanted to try back when I came across gmail API and OAuth 2.0 for the first time, finally got an arduino to implement it!! |
6 | 6 |
|
7 | 7 | ### How Does It Work?
|
8 | 8 |
|
9 |
| -The code is extremely simple if you understand OAuth 2.0, if you don't, read [this article from google](https://developers.google.com/identity/protocols/oauth2). In a nutshell, to access information from a user (a.k.a the resource owner), a client must do the following: |
10 |
| -1. Get the authorization grant from the resource owner (for example, via a [consent screen](https://developers.google.com/identity/protocols/oauth2/web-server#userconsentprompt)). |
11 |
| -2. In exchange to the grant, you recieve an authorization code. |
12 |
| -3. Exchange the authorization code for access and refresh tokens from the authorization server. |
13 |
| -4. Use the access tokens to make API calls on behalf of the user. |
14 |
| -5. If the access token expires, use the refresh token to request another access token. |
| 9 | +Getting the unread email count using gmail API is not a simple task, since email is a private service a client cannot get access to information without permission. |
15 | 10 |
|
16 |
| -Note: Refresh tokens are only provided on the first request. The IETF documentation on the OAuth 2.0 framework can be found [here](https://tools.ietf.org/html/rfc6749). |
| 11 | +OAuth 2.0 is a framework that lays out guidelines for this interaction, before requesting senstive information, getting consent from the user (a.k.a resource owner) is mandatory. Once obtained, the client can exchange it (consent) for temporary access tokens, which must be sent with every future API call that is requesting user data ([more info](https://developers.google.com/identity/protocols/oauth2)). |
17 | 12 |
|
18 |
| -### What You'll Need To Run This Locally |
| 13 | +Signals are sent to the arduino board based on the response obtained from the API requests, GPIO pins are controlled using standard firmata protocol, python APIs for the same are provided by the pyFirmata library. |
19 | 14 |
|
20 |
| -#### Setting Up the API Client |
| 15 | +### How Do I Run This? |
21 | 16 |
|
22 |
| -You'll need to just follow [this tutorial](https://developers.google.com/identity/protocols/oauth2/web-server#top_of_page) and get the following fields: |
| 17 | +#### Set Up an API Client |
| 18 | + |
| 19 | +To obtain your access token and refresh tokens, [follow this guide](https://developers.google.com/identity/protocols/oauth2/web-server#top_of_page). |
| 20 | + |
| 21 | +For future reference, I obtained the authorization grant (code), by browsing: |
| 22 | + |
| 23 | +```sh |
| 24 | +https://accounts.google.com/o/oauth2/v2/auth?client_id=CLIENT_ID&scope=https://www.googleapis.com/auth/gmail.readonly&response_type=code&access_type=offline&redirect_uri=http://localhost |
23 | 25 | ```
|
| 26 | + |
| 27 | +and exchanged the authorisation code for access tokens via a POST request to [oauth2.googleapis.com/token](https://oauth2.googleapis.com): |
| 28 | + |
| 29 | +```sh |
| 30 | +curl -X POST "https://oauth2.googleapis.com/token" \ |
| 31 | + -d client_id=CLIENT_ID \ |
| 32 | + -d client_secret=CLIENT_SECRET \ |
| 33 | + -d code=CODE \ |
| 34 | + -d grant_type=authorization_code \ |
| 35 | + -d redirect_uri=http://localhost |
| 36 | +``` |
| 37 | + |
| 38 | +Once you're done, you should have all of the following fields: |
| 39 | + |
| 40 | +```sh |
24 | 41 | API_KEY [created on the google developer console]
|
25 | 42 | CLIENT_ID [registered on google developer console]
|
26 | 43 | CLIENT_SECRET
|
27 | 44 | EMAIL_ID [which authorized your client, and whose access + refresh tokens you own]
|
28 | 45 | ACCESS_TOKEN
|
29 | 46 | REFRESH_TOKEN
|
30 |
| - ``` |
31 |
| -You'll only need to do this once because the once you have one access token and the refresh token, you are set. Follow the remaining setps: |
| 47 | +``` |
32 | 48 |
|
33 |
| -1. Rename the `config/template` to `config/.env` and replace the first 5 fields with their respective values. |
34 |
| -2. Move `data/access_token.json` to `/bin/data/access_token.json` and replace the `<YOUR_ACCESS_TOKEN>` field with the respective value. |
| 49 | +Rename the `config/template` to `config/env` and replace the first 5 fields with their respective values. |
35 | 50 |
|
36 | 51 | #### Required Libraries
|
37 | 52 |
|
38 |
| -You'll need the following libraries the `pyfirmata` python library to communicate with the arduino board, and you'll need to flash your arduino with the [standrard firmata](https://github.com/firmata/arduino/blob/master/examples/StandardFirmata/StandardFirmata.ino), firmata is a generic protocol for communicating with microcontrollers from software on a host computer. And you're all set! |
| 53 | +- `pyfirmata` |
| 54 | +- flash your arduino with the [standrard firmata](https://github.com/firmata/arduino/blob/master/examples/StandardFirmata/StandardFirmata.ino), firmata is a generic protocol for communicating with microcontrollers from software on a host computer. |
39 | 55 |
|
40 |
| -Connect an LED in the pins mentioned in `gmailLEDControl.py` and run `python gmailLEDControl.py` to start making cURL requests every 2 seconds. |
| 56 | +Connect an LED to the pin specified in `gpio_pin_control.py`, and run the python script to make cURL requests every 10 seconds <3 |
0 commit comments