Skip to content

Commit

Permalink
Fix TinyGSMClient integration and examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Aug 21, 2023
1 parent a41763c commit 54964aa
Show file tree
Hide file tree
Showing 9 changed files with 711 additions and 98 deletions.
57 changes: 32 additions & 25 deletions examples/GSM/GSM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

// This example shows how to use TTGO T-A7670 (ESP32 with SIMCom SIMA7670) and TinyGSMClient This example to connect to Google API.

// To allow TinyGSM library integration, the following macro should be defined in src/ESP_Google_Sheet_Client_FS_Config.h.
// To allow TinyGSM library integration, the following macro should be defined in src/ESP_Google_Sheet_Client_FS_Config.h or
// your custom config file src/Custom_ESP_GOOGLE_SHEET_CLIENT_FS_Config
// #define TINY_GSM_MODEM_SIM7600

#define TINY_GSM_MODEM_SIM7600 // SIMA7670 Compatible with SIM7600 AT instructions
Expand Down Expand Up @@ -42,6 +43,9 @@ const char apn[] = "YourAPN";
const char gprsUser[] = "";
const char gprsPass[] = "";

#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 600 // Time ESP32 will go to sleep (in seconds)

#define UART_BAUD 115200
#define PIN_DTR 25
#define PIN_TX 26
Expand All @@ -59,7 +63,6 @@ const char gprsPass[] = "";
#define SD_CS 13

#include <ESP_Google_Sheet_Client.h>

#include <TinyGsmClient.h>

// For how to create Service Account and how to use the library, go to https://github.com/mobizt/ESP-Google-Sheet-Client
Expand All @@ -72,12 +75,6 @@ const char gprsPass[] = "";
// Service Account's private key
const char PRIVATE_KEY[] PROGMEM = "-----BEGIN PRIVATE KEY-----XXXXXXXXXXXX-----END PRIVATE KEY-----\n";

// Set serial for debug console
#define SerialMon Serial

// Set serial for AT commands (to the module)
#define SerialAT Serial1

TinyGsm modem(SerialAT);

TinyGsmClient gsm_client(modem);
Expand All @@ -90,14 +87,35 @@ void setupGsheet();

void tokenStatusCallback(TokenInfo info);

void initModem()
void setup()
{

if (modem.isGprsConnected())
{
modem.gprsDisconnect();
SerialMon.println(F("GPRS disconnected"));
}
SerialMon.begin(115200);

delay(10);
pinMode(BAT_EN, OUTPUT);
digitalWrite(BAT_EN, HIGH);

// A7670 Reset
pinMode(RESET, OUTPUT);
digitalWrite(RESET, LOW);
delay(100);
digitalWrite(RESET, HIGH);
delay(3000);
digitalWrite(RESET, LOW);

pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, LOW);
delay(100);
digitalWrite(PWR_PIN, HIGH);
delay(1000);
digitalWrite(PWR_PIN, LOW);

DBG("Wait...");

delay(3000);

SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);

// Restart takes quite some time
// To skip it, call init() instead of restart()
Expand All @@ -118,25 +136,14 @@ void initModem()
if (modem.waitResponse(10000L) != 1)
{
DBG(" setNetworkMode faill");
return;
}
}

void setup()
{

SerialMon.begin(115200);

SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);

String name = modem.getModemName();
DBG("Modem Name:", name);

String modemInfo = modem.getModemInfo();
DBG("Modem Info:", modemInfo);

initModem();

GSheet.printf("ESP Google Sheet Client v%s\n\n", ESP_GOOGLE_SHEET_CLIENT_VERSION);

GSheet.setGSMClient(&gsm_client, &modem, GSM_PIN, apn, gprsUser, gprsPass);
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.4.0",
"version": "1.4.1",
"keywords": "communication, REST, esp32, esp8266, raspberrypi, arduino",
"description": "Arduino Google Sheet REST client library for Arduino devices. 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.4.0
version=1.4.1

author=Mobizt

Expand Down
9 changes: 2 additions & 7 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_Google_Sheet_Client.cpp v1.4.0
* Google Sheet Client, ESP_GOOGLE_SHEET_CLIENT_Google_Sheet_Client.cpp v1.4.1
*
* This library supports Espressif ESP8266 and ESP32 MCUs
*
* Created August 13, 2023
* Created August 21, 2023
*
* The MIT License (MIT)
* Copyright (c) 2022 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -192,9 +192,7 @@ void GSheetClass::setCert(const char *ca)
{
cert_updated = true;
cert_addr = addr;
#if defined(ESP8266) || defined(MB_ARDUINO_PICO)
waitClockReady();
#endif
}
}

Expand All @@ -206,10 +204,7 @@ void GSheetClass::setCertFile(const char *filename, esp_google_sheet_file_storag
if (config.cert.file.length() > 0)
{
cert_updated = true;

#if defined(ESP8266) || defined(MB_ARDUINO_PICO)
waitClockReady();
#endif
}
}

Expand Down
7 changes: 4 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.4.0"
#define ESP_GOOGLE_SHEET_CLIENT_VERSION "1.4.1"
#endif

/**
* Google Sheet Client, ESP_Google_Sheet_Client.h v1.3.6
* Google Sheet Client, ESP_Google_Sheet_Client.h v1.4.1
*
* This library supports Espressif ESP8266 and ESP32 MCUs
*
* Created August 13, 2023
* Created August 21, 2023
*
* The MIT License (MIT)
* Copyright (c) 2023 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -113,6 +113,7 @@ class GSheetClass
MB_String mGetValue(MB_String &response, const char *key);
bool createPermission(MB_String &response, const char *fileId, const char *role, const char *type, const char *email);
bool setClock(float gmtOffset);
bool getTime(float gmtOffset);
void setClient(Client *client, ESP_GOOGLE_SHEET_CLIENT_NetworkConnectionRequestCallback networkConnectionCB,
ESP_GOOGLE_SHEET_CLIENT_NetworkStatusRequestCallback networkStatusCB);
void setGSMClient(Client *client, void *modem = nullptr, const char *pin = nullptr, const char *apn = nullptr, const char *user = nullptr, const char *password = nullptr);
Expand Down
65 changes: 47 additions & 18 deletions src/auth/GAuthManager.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Google Sheet Client, GAuthManager v1.0.3
* Google Sheet Client, GAuthManager v1.0.4
*
* This library supports Espressif ESP8266, ESP32 and Raspberry Pi Pico MCUs.
*
* Created August 13, 2023
* Created August 21, 2023
*
* The MIT License (MIT)
* Copyright (c) 2022 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -70,9 +70,15 @@ void GAuthManager::newClient(GS_TCP_Client **client)
if (!*client)
{
*client = new GS_TCP_Client();
// restore only external client (gsm client integration cannot restore)

if (_cli_type == esp_google_sheet_client_type_external_basic_client)
(*client)->setClient(_cli, _net_con_cb, _net_stat_cb);
else if (_cli_type == esp_google_sheet_client_type_external_gsm_client)
{
#if defined(ESP_GOOGLE_SHEET_CLIENT_GSM_MODEM_IS_AVAILABLE)
(*client)->setGSMClient(_cli, _modem, _pin.c_str(), _apn.c_str(), _user.c_str(), _password.c_str());
#endif
}
else
(*client)->_client_type = _cli_type;
}
Expand All @@ -82,11 +88,23 @@ void GAuthManager::freeClient(GS_TCP_Client **client)
{
if (*client)
{
// Keep external client pointers
_cli_type = (*client)->type();
_net_con_cb = (*client)->_network_connection_cb;
_net_stat_cb = (*client)->_network_status_cb;
_cli = (*client)->_basic_client;
if (_cli_type == esp_google_sheet_client_type_external_basic_client)
{
_net_con_cb = (*client)->_network_connection_cb;
_net_stat_cb = (*client)->_network_status_cb;
}
else if (_cli_type == esp_google_sheet_client_type_external_gsm_client)
{
#if defined(ESP_GOOGLE_SHEET_CLIENT_GSM_MODEM_IS_AVAILABLE)
_pin = (*client)->_pin;
_apn = (*client)->_apn;
_user = (*client)->_user;
_password = (*client)->_password;
_modem = (*client)->_modem;
#endif
}
delete *client;
}
*client = nullptr;
Expand Down Expand Up @@ -403,6 +421,27 @@ void GAuthManager::freeJson()
resultPtr = nullptr;
}

void GAuthManager::tryGetTime()
{
if (!tcpClient || config->internal.clock_rdy)
return;

_cli_type = tcpClient->type();

if (tcpClient->type() == esp_google_sheet_client_type_external_gsm_client)
{
uint32_t _time = tcpClient->gprsGetTime();
if (_time > 0)
{
*mb_ts = _time;
TimeHelper::setTimestamp(_time, mb_ts_offset);
config->internal.clock_rdy = TimeHelper::clockReady(mb_ts, mb_ts_offset);
}
}
else
TimeHelper::syncClock(mb_ts, mb_ts_offset, config->time_zone, config);
}

void GAuthManager::tokenProcessingTask()
{
// We don't have to use memory reserved tasks e.g., RTOS task in ESP32 for this JWT
Expand Down Expand Up @@ -445,17 +484,7 @@ void GAuthManager::tokenProcessingTask()
}

// check or set time again
if (_cli_type == esp_google_sheet_client_type_external_gsm_client)
{
uint32_t _time = tcpClient->gprsGetTime();
if (_time > 0)
{
*mb_ts = _time;
TimeHelper::setTimestamp(_time, mb_ts_offset);
}
}
else
TimeHelper::syncClock(mb_ts, mb_ts_offset, config->time_zone, config);
tryGetTime();

// exit task immediately if time is not ready synched
// which handleToken function should run repeatedly to enter this function again.
Expand All @@ -473,7 +502,7 @@ void GAuthManager::tokenProcessingTask()
{

// time must be set first
TimeHelper::syncClock(mb_ts, mb_ts_offset, config->time_zone, config);
tryGetTime();
config->internal.last_jwt_begin_step_millis = millis();

if (config->internal.clock_rdy)
Expand Down
11 changes: 9 additions & 2 deletions src/auth/GAuthManager.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Google Sheet Client, GAuthManager v1.0.3
* Google Sheet Client, GAuthManager v1.0.4
*
* This library supports Espressif ESP8266, ESP32 and Raspberry Pi Pico MCUs.
*
* Created August 13, 2023
* Created August 21, 2023
*
* The MIT License (MIT)
* Copyright (c) 2022 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -78,6 +78,11 @@ class GAuthManager
ESP_GOOGLE_SHEET_CLIENT_NetworkStatusRequestCallback _net_stat_cb = NULL;
Client *_cli = nullptr;

#if defined(ESP_GOOGLE_SHEET_CLIENT_GSM_MODEM_IS_AVAILABLE)
MB_String _pin, _apn, _user, _password;
void *_modem = nullptr;
#endif

/* intitialize the class */
void begin(esp_google_sheet_auth_cfg_t *cfg, MB_FS *mbfs, uint32_t *mb_ts, uint32_t *mb_ts_offset);
void end();
Expand Down Expand Up @@ -117,6 +122,8 @@ class GAuthManager
bool handleTaskError(int code, int httpCode = 0);
// parse the auth token response
bool handleResponse(GS_TCP_Client *client, int &httpCode, MB_String &payload, bool stopSession = true);
/* Get time */
void tryGetTime();
/* process the tokens (generation, signing, request and refresh) */
void tokenProcessingTask();
/* encode and sign the JWT token */
Expand Down
Loading

0 comments on commit 54964aa

Please sign in to comment.