Skip to content

Commit

Permalink
Update FirbaseJson.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Dec 20, 2021
1 parent 29d19ff commit 432d21c
Show file tree
Hide file tree
Showing 17 changed files with 641 additions and 325 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Google OAuth2.0 Access Token generation Arduino Library v1.1.0
# Google OAuth2.0 Access Token generation Arduino Library v1.1.2


This is the library will create the OAuth2.0 access token used in the Google API's http request (REST).
Expand All @@ -9,6 +9,8 @@ See [How to Create Service Account Private Key](#how-to-create-service-account-p

This library supports ESP8266 and ESP32 MCU from Espressif.

Supports ethernet in ESP32 using LAN8720, TLK110 and IP101 Ethernet modules and ESP8266 using ENC28J60, W5100 and W5500 Ethernet modules.


## Tested Devices

Expand Down
201 changes: 201 additions & 0 deletions examples/Ethernet/ESP32/ESP32.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@


/**
* This example show how to get access token using ESP32 and LAN8720 Ethernet module.
*
* Created by K. Suwatchai (Mobizt)
*
* Email: [email protected]
*
* Github: https://github.com/mobizt/ESP-Mail-Client
*
* Copyright (c) 2021 mobizt
*
*/

/**
* There are many sources for LAN8720 and ESP32 interconnection on the internet which may
* work for your LAN8720 board.
*
* Some methods worked unless no IP is available.
*
* This modification and interconnection provided in this example are mostly worked as
* the 50 MHz clock was created internally in ESP32 which GPIO 17 is set to be output of this clock
* and feeds to the LAN8720 chip XTAL input.
*
* The on-board LAN8720 50 MHz XTAL chip will be disabled by connect its enable pin or pin 1 to GND.
*
* Please see the images in the folder "modified_LAN8720_board_images" for how to modify the LAN8720 board.
*
* The LAN8720 Ethernet modified board and ESP32 board wiring connection.
*
* ESP32 LAN8720
*
* GPIO17 - EMAC_CLK_OUT_180 nINT/REFCLK - LAN8720 XTAL1/CLKIN 4k7 Pulldown
* GPIO22 - EMAC_TXD1 TX1
* GPIO19 - EMAC_TXD0 TX0
* GPIO21 - EMAC_TX_EN TX_EN
* GPIO26 - EMAC_RXD1 RX1
* GPIO25 - EMAC_RXD0 RX0
* GPIO27 - EMAC_RX_DV CRS
* GPIO23 - MDC MDC
* GPIO18 - MDIO MDIO
* GND GND
* 3V3 VCC
*
*/

//In case of Gmail, to send the Email via port 465 (SSL), less secure app option should be enabled in the account settings. https://myaccount.google.com/lesssecureapps?pli=1

#include <Arduino.h>

#include <ESPSigner.h>

/** These credentials are taken from Service Account key file (JSON)
* https://cloud.google.com/iam/docs/service-accounts
*/
// See https://github.com/mobizt/ESP-Signer#how-to-create-service-account-private-key

#define PROJECT_ID "The project ID" //Taken from "project_id" key in JSON file.
#define CLIENT_EMAIL "Client Email" //Taken from "client_email" key in JSON file.
const char PRIVATE_KEY[] PROGMEM = "-----BEGIN PRIVATE KEY-----\\n-----END PRIVATE KEY-----\n"; //Taken from "private_key" key in JSON file.

#ifdef ETH_CLK_MODE
#undef ETH_CLK_MODE
#endif
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT //RMII clock output from GPIO17

// Pin# of the enable signal for the external crystal oscillator (-1 to disable)
#define ETH_POWER_PIN -1

// Type of the Ethernet PHY (LAN8720 or TLK110)
#define ETH_TYPE ETH_PHY_LAN8720

// I²C-address of Ethernet PHY (0 or 1 for LAN8720, 31 for TLK110)
#define ETH_ADDR 1

// Pin# of the I²C clock signal for the Ethernet PHY
#define ETH_MDC_PIN 23

// Pin# of the I²C IO signal for the Ethernet PHY
#define ETH_MDIO_PIN 18

static bool eth_connected = false;

SignerConfig config;

bool beginReady = false;

void tokenStatusCallback(TokenInfo info);

void WiFiEvent(WiFiEvent_t event)
{
#if defined(ESP32)
//Do not run any function here to prevent stack overflow or nested interrupt
switch (event)
{
case SYSTEM_EVENT_ETH_START:
Serial.println("ETH Started");
//set eth hostname here
ETH.setHostname("esp32-ethernet");

break;
case SYSTEM_EVENT_ETH_CONNECTED:
Serial.println("ETH Connected");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
Serial.print("ETH MAC: ");
Serial.print(ETH.macAddress());
Serial.print(", IPv4: ");
Serial.print(ETH.localIP());
if (ETH.fullDuplex())
{
Serial.print(", FULL_DUPLEX");
}
Serial.print(", ");
Serial.print(ETH.linkSpeed());
Serial.println("Mbps");
eth_connected = true;

break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
Serial.println("ETH Disconnected");
eth_connected = false;
break;
case SYSTEM_EVENT_ETH_STOP:
Serial.println("ETH Stopped");
eth_connected = false;
break;
default:
break;
}
#endif
}

void begin()
{
beginReady = true;

/* Assign the sevice account credentials and private key (required) */
config.service_account.data.client_email = CLIENT_EMAIL;
config.service_account.data.project_id = PROJECT_ID;
config.service_account.data.private_key = PRIVATE_KEY;

/* Expired period in seconds (optional). Default is 3600 sec.*/
config.signer.expiredSeconds = 3600;

/* Seconds to refresh the token before expired (optional). Default is 60 sec.*/
config.signer.preRefreshSeconds = 60;

config.token_status_callback = tokenStatusCallback;

config.signer.tokens.scope = "https://www.googleapis.com/auth/spreadsheets,https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/drive.file";

Signer.begin(&config);
}

void setup()
{
Serial.begin(115200);
Serial.println();

WiFi.onEvent(WiFiEvent);

#if defined(ESP32)
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE);
#endif
}

void loop()
{
if (eth_connected && !beginReady)
{
begin();
}

//Check to status and also refresh the access token
bool ready = Signer.tokenReady();
if (ready)
{
int t = Signer.getExpiredTimestamp() - config.signer.preRefreshSeconds - time(nullptr);

Serial.print("Remaining seconds to refresh the token, ");
Serial.println(t);
delay(1000);
}
}

void tokenStatusCallback(TokenInfo info)
{
if (info.status == esp_signer_token_status_error)
{
Serial.printf("Token info: type = %s, status = %s\n", Signer.getTokenType(info).c_str(), Signer.getTokenStatus(info).c_str());
Serial.printf("Token error: %s\n", Signer.getTokenError(info).c_str());
}
else
{
Serial.printf("Token info: type = %s, status = %s\n", Signer.getTokenType(info).c_str(), Signer.getTokenStatus(info).c_str());
if (info.status == esp_signer_token_status_ready)
Serial.printf("Token: %s\n", Signer.accessToken().c_str());
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
148 changes: 148 additions & 0 deletions examples/Ethernet/ESP8266/ESP8266.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@

/**
* Created by K. Suwatchai (Mobizt)
*
* Email: [email protected]
*
* Github: https://github.com/mobizt
*
* Copyright (c) 2021 mobizt
*
*/

//This example show how to get access token using ESP8266 and ENC28J60 Ethernet module.

/**
*
* The ENC28J60 Ethernet module and ESP8266 board, SPI port wiring connection.
*
* ESP8266 (Wemos D1 Mini or NodeMCU) ENC28J60
*
* GPIO12 (D6) - MISO SO
* GPIO13 (D7) - MOSI SI
* GPIO14 (D5) - SCK SCK
* GPIO16 (D0) - CS CS
* GND GND
* 3V3 VCC
*
*/
#include <Arduino.h>

#include <ESPSigner.h>


#include <ENC28J60lwIP.h>
//#include <W5100lwIP.h>
//#include <W5500lwIP.h>

/** These credentials are taken from Service Account key file (JSON)
* https://cloud.google.com/iam/docs/service-accounts
*/
// See https://github.com/mobizt/ESP-Signer#how-to-create-service-account-private-key

#define PROJECT_ID "The project ID" //Taken from "project_id" key in JSON file.
#define CLIENT_EMAIL "Client Email" //Taken from "client_email" key in JSON file.
const char PRIVATE_KEY[] PROGMEM = "-----BEGIN PRIVATE KEY-----\\n-----END PRIVATE KEY-----\n"; //Taken from "private_key" key in JSON file.

#define ETH_CS_PIN 16 //D0

ENC28J60lwIP eth(ETH_CS_PIN);
//Wiznet5100lwIP eth(ETH_CS_PIN);
//Wiznet5500lwIP eth(ETH_CS_PIN);

SignerConfig config;

bool beginReady = false;

void tokenStatusCallback(TokenInfo info);

void begin()
{
beginReady = true;

/* Assign the sevice account credentials and private key (required) */
config.service_account.data.client_email = CLIENT_EMAIL;
config.service_account.data.project_id = PROJECT_ID;
config.service_account.data.private_key = PRIVATE_KEY;

/* Expired period in seconds (optional). Default is 3600 sec.*/
config.signer.expiredSeconds = 3600;

/* Seconds to refresh the token before expired (optional). Default is 60 sec.*/
config.signer.preRefreshSeconds = 60;

config.token_status_callback = tokenStatusCallback;

config.signer.tokens.scope = "https://www.googleapis.com/auth/spreadsheets,https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/drive.file";

Signer.begin(&config);
}

void setup()
{

Serial.begin(115200);
Serial.println();

#if defined(ESP8266)
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV4); // 4 MHz?
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
eth.setDefault(); // use ethernet for default route
if (!eth.begin())
{
Serial.println("ethernet hardware not found ... sleeping");
while (1)
{
delay(1000);
}
}
else
{
Serial.print("connecting ethernet");
while (!eth.connected())
{
Serial.print(".");
delay(1000);
}
}
Serial.println();
Serial.print("ethernet IP address: ");
Serial.println(eth.localIP());
#endif
}

void loop()
{
if (!beginReady)
{
begin();
}

//Check to status and also refresh the access token
bool ready = Signer.tokenReady();
if (ready)
{
int t = Signer.getExpiredTimestamp() - config.signer.preRefreshSeconds - time(nullptr);

Serial.print("Remaining seconds to refresh the token, ");
Serial.println(t);
delay(1000);
}
}

void tokenStatusCallback(TokenInfo info)
{
if (info.status == esp_signer_token_status_error)
{
Serial.printf("Token info: type = %s, status = %s\n", Signer.getTokenType(info).c_str(), Signer.getTokenStatus(info).c_str());
Serial.printf("Token error: %s\n", Signer.getTokenError(info).c_str());
}
else
{
Serial.printf("Token info: type = %s, status = %s\n", Signer.getTokenType(info).c_str(), Signer.getTokenStatus(info).c_str());
if (info.status == esp_signer_token_status_ready)
Serial.printf("Token: %s\n", Signer.accessToken().c_str());
}
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ESP Signer",
"version": "1.1.0",
"version": "1.1.2",
"keywords": "communication, REST, esp32, esp8266, arduino",
"description": "The Google OAuth2.0 access token generation for Google REST API's http request.",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=ESP Signer

version=1.1.0
version=1.1.2

author=Mobizt

Expand Down
Loading

0 comments on commit 432d21c

Please sign in to comment.