Skip to content

Commit

Permalink
Fixed NTP time synching issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Apr 23, 2022
1 parent f495568 commit 89d8166
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 240 deletions.
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Arduino Google Sheet Client Library for ESP8266 and ESP32


Arduino Google Sheet Client Library for ESP8266 and ESP32 v1.0.3.
Arduino Google Sheet Client Library for ESP8266 and ESP32 v1.0.4.

This library allows devices to authenticate and communicate with Google Sheet APIs using the Service Account.

Expand All @@ -17,13 +17,6 @@ You can create, edit and deploy the Apps Script code via extension of spreadshee
Spreadsheet created or owned by you, needed to share the access with Service Account's client email then library can read, and edit except for delete the user's spreadsheet due to permission denied.


## Important Note

The connection to Google Sheet API endpoint takes time and the time from sending request to the complete response received can be as much as 3 seconds.

The speed is not the library or device issue unless the server issue.


## Dependencies


Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ESP-Google-Sheet-Client",
"version": "1.0.3",
"version": "1.0.4",
"keywords": "communication, REST, esp32, esp8266, arduino",
"description": "Arduino Google Sheet REST client library for ESP8266 and ESP32. This library allows devices to communicate with Google Sheet API to read, edit and delete the spreadsheets",
"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-Google-Sheet-Client

version=1.0.3
version=1.0.4

author=Mobizt

Expand Down
42 changes: 16 additions & 26 deletions src/ESP_Google_Sheet_Client.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Google Sheet Client, ESP_Google_Sheet_Client.cpp v1.0.1
* Google Sheet Client, ESP_Google_Sheet_Client.cpp v1.0.4
*
* This library supports Espressif ESP8266 and ESP32 MCUs
*
* Created April 18, 2022
* Created April 23, 2022
*
* The MIT License (MIT)
* Copyright (c) 2022 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -57,7 +57,7 @@ void GSheetClass::auth(const char *client_email, const char *project_id, const c
config.service_account.data.private_key = private_key;
config.signer.expiredSeconds = 3600;
config.signer.preRefreshSeconds = 60;
config._int.esp_signer_reconnect_wifi = WiFi.getAutoReconnect();
config.internal.esp_signer_reconnect_wifi = WiFi.getAutoReconnect();
config.signer.tokens.scope = (const char *)FPSTR("https://www.googleapis.com/auth/drive.metadata,https://www.googleapis.com/auth/drive.appdata,https://www.googleapis.com/auth/spreadsheets,https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/drive.file");
this->begin(&config);
}
Expand All @@ -69,25 +69,20 @@ void GSheetClass::setTokenCallback(TokenStatusCallback callback)

bool GSheetClass::checkToken()
{
if (setClock(0))
{
return this->tokenReady();
}

return false;
return this->tokenReady();
}

bool GSheetClass::setClock(float gmtOffset)
{

if (time(nullptr) > ESP_DEFAULT_TS && gmtOffset == config._int.esp_signer_gmt_offset)
if (time(nullptr) > ESP_DEFAULT_TS && gmtOffset == config.internal.esp_signer_gmt_offset)
return true;

time_t now = time(nullptr);

config._int.esp_signer_clock_rdy = now > ESP_DEFAULT_TS;
config.internal.esp_signer_clock_rdy = now > ESP_DEFAULT_TS;

if (!config._int.esp_signer_clock_rdy || gmtOffset != config._int.esp_signer_gmt_offset)
if (!config.internal.esp_signer_clock_rdy || gmtOffset != config.internal.esp_signer_gmt_offset)
{
configTime(gmtOffset * 3600, 0, "pool.ntp.org", "time.nist.gov");

Expand All @@ -102,11 +97,11 @@ bool GSheetClass::setClock(float gmtOffset)
}
}

config._int.esp_signer_clock_rdy = now > ESP_DEFAULT_TS;
if (config._int.esp_signer_clock_rdy)
config._int.esp_signer_gmt_offset = gmtOffset;
config.internal.esp_signer_clock_rdy = now > ESP_DEFAULT_TS;
if (config.internal.esp_signer_clock_rdy)
config.internal.esp_signer_gmt_offset = gmtOffset;

return config._int.esp_signer_clock_rdy;
return config.internal.esp_signer_clock_rdy;
}

void GSheetClass::beginRequest(FirebaseJson *response, MB_String &req, host_type_t host_type)
Expand Down Expand Up @@ -189,30 +184,25 @@ void GSheetClass::setSecure()
if (config.signer.wcs->_certType == -1 || cert_updated)
{

if (!config._int.esp_signer_clock_rdy && (cert_addr > 0))
if (!config.internal.esp_signer_clock_rdy && (cert_addr > 0))
{

#if defined(ESP8266)
int retry = 0;
while (!config._int.esp_signer_clock_rdy && retry < 5)
{
setClock(0);
retry++;
}
#endif
}

config.signer.wcs->_clockReady = config._int.esp_signer_clock_rdy;
config.signer.wcs->_clockReady = config.internal.esp_signer_clock_rdy;

if (certFile.length() > 0)
{

#if defined(ESP8266)
if (config._int.sd_config.ss == -1)
config._int.sd_config.ss = SD_CS_PIN;
if (config.internal.sd_config.ss == -1)
config.internal.sd_config.ss = SD_CS_PIN;
#endif
int type = certFileStorageType == esP_google_sheet_file_storage_type_flash ? 1 : 2;
config.signer.wcs->setCACertFile(certFile.c_str(), type, config._int.sd_config);
config.signer.wcs->setCACertFile(certFile.c_str(), type, config.internal.sd_config);
}else
{
if (cert_addr > 0)
Expand Down
6 changes: 3 additions & 3 deletions src/ESP_Google_Sheet_Client.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#ifndef ESP_GOOGLE_SHEET_CLIENT_VERSION
#define ESP_GOOGLE_SHEET_CLIENT_VERSION "1.0.3"
#define ESP_GOOGLE_SHEET_CLIENT_VERSION "1.0.4"
#endif

/**
* Google Sheet Client, ESP_Google_Sheet_Client.h v1.0.3
* Google Sheet Client, ESP_Google_Sheet_Client.h v1.0.4
*
* This library supports Espressif ESP8266 and ESP32 MCUs
*
* Created April 20, 2022
* Created April 23, 2022
*
* The MIT License (MIT)
* Copyright (c) 2022 K. Suwatchai (Mobizt)
Expand Down
Loading

0 comments on commit 89d8166

Please sign in to comment.