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 794df9a commit 1586adf
Show file tree
Hide file tree
Showing 11 changed files with 718 additions and 143 deletions.
86 changes: 45 additions & 41 deletions examples/GSM/GSM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

// This example shows how to use TTGO T-A7670 (ESP32 with SIMCom SIMA7670) and TinyGSMClient to create OAuth2 access token.

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

#define TINY_GSM_MODEM_SIM7600 // SIMA7670 Compatible with SIM7600 AT instructions
Expand Down Expand Up @@ -42,6 +42,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 @@ -58,9 +61,7 @@ const char gprsPass[] = "";
#define SD_SCLK 14
#define SD_CS 13


#include <ESP_Signer.h>

#include <TinyGsmClient.h>

/** These credentials are taken from Service Account key file (JSON)
Expand All @@ -72,27 +73,56 @@ const char gprsPass[] = "";
#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.


// 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);

SignerConfig config;

void initModem()
void tokenStatusCallback(TokenInfo info)
{

if (modem.isGprsConnected())
if (info.status == esp_signer_token_status_error)
{
modem.gprsDisconnect();
SerialMon.println(F("GPRS disconnected"));
Signer.printf("Token info: type = %s, status = %s\n", Signer.getTokenType(info).c_str(), Signer.getTokenStatus(info).c_str());
Signer.printf("Token error: %s\n", Signer.getTokenError(info).c_str());
}
else
{
Signer.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)
Signer.printf("Token: %s\n", Signer.accessToken().c_str());
}
}

void setup()
{

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 @@ -113,40 +143,14 @@ void initModem()
if (modem.waitResponse(10000L) != 1)
{
DBG(" setNetworkMode faill");
return;
}
}

void tokenStatusCallback(TokenInfo info)
{
if (info.status == esp_signer_token_status_error)
{
Signer.printf("Token info: type = %s, status = %s\n", Signer.getTokenType(info).c_str(), Signer.getTokenStatus(info).c_str());
Signer.printf("Token error: %s\n", Signer.getTokenError(info).c_str());
}
else
{
Signer.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)
Signer.printf("Token: %s\n", Signer.accessToken().c_str());
}
}

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();

/* 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;
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 Signer",
"version": "1.4.1",
"version": "1.4.2",
"keywords": "communication, REST, esp32, esp8266, raspberrypi, arduino",
"description": "The Google OAuth2.0 access token generation for Arduino devices. This library also supports external networking devices wirh Arduino Clients e.g. WiFiClient, EthernetClient, and GSMClient.",
"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.4.1
version=1.4.2

author=Mobizt

Expand Down
26 changes: 12 additions & 14 deletions src/ESP_Signer_Error.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Created August 12, 2023
* Created August 21, 2023
*/

#ifndef ESP_SIGNER_ERROR_H
Expand Down Expand Up @@ -69,18 +69,16 @@
#define ESP_SIGNER_ERROR_EXTERNAL_CLIENT_DISABLED /* */ (ESP_SIGNER_ERROR_RANGE - 4)
#define ESP_SIGNER_ERROR_EXTERNAL_CLIENT_NOT_INITIALIZED /* */ (ESP_SIGNER_ERROR_RANGE - 5)
#define ESP_SIGNER_ERROR_TOKEN_SET_TIME /* */ (ESP_SIGNER_ERROR_RANGE - 6)
#define ESP_SIGNER_ERROR_TOKEN_CREATE_HASH /* */ (ESP_SIGNER_ERROR_RANGE - 7)
#define ESP_SIGNER_ERROR_TOKEN_PARSE_PK /* */ (ESP_SIGNER_ERROR_RANGE - 8)
#define ESP_SIGNER_ERROR_TOKEN_SIGN /* */ (ESP_SIGNER_ERROR_RANGE - 9)
#define ESP_SIGNER_ERROR_TOKEN_EXCHANGE /* */ (ESP_SIGNER_ERROR_RANGE - 10)
#define ESP_SIGNER_ERROR_TOKEN_NOT_READY /* */ (ESP_SIGNER_ERROR_RANGE - 11)
#define ESP_SIGNER_ERROR_SYS_TIME_IS_NOT_READY /* */ (ESP_SIGNER_ERROR_RANGE - 12)
#define ESP_SIGNER_ERROR_NTP_SYNC_TIMED_OUT /* */ (ESP_SIGNER_ERROR_RANGE - 13)
#define ESP_SIGNER_ERROR_TOKEN_COMPLETE_NOTIFY /* */ (ESP_SIGNER_ERROR_RANGE - 14)
#define ESP_SIGNER_ERROR_TOKEN_COMPLETE_UNNOTIFY /* */ (ESP_SIGNER_ERROR_RANGE - 15)
#define ESP_SIGNER_ERROR_TOKEN_ERROR_UNNOTIFY /* */ (ESP_SIGNER_ERROR_RANGE - 16)
#define ESP_SIGNER_ERROR_UDP_CLIENT_REQUIRED /* */ (ESP_SIGNER_ERROR_RANGE - 17)
#define ESP_SIGNER_ERROR_MISSING_SERVICE_ACCOUNT_CREDENTIALS /* */ (ESP_SIGNER_ERROR_RANGE - 18)
#define ESP_SIGNER_ERROR_SERVICE_ACCOUNT_JSON_FILE_PARSING_ERROR /* */ (ESP_SIGNER_ERROR_RANGE - 19)
#define ESP_SIGNER_ERROR_TOKEN_PARSE_PK /* */ (ESP_SIGNER_ERROR_RANGE - 7)
#define ESP_SIGNER_ERROR_TOKEN_SIGN /* */ (ESP_SIGNER_ERROR_RANGE - 8)
#define ESP_SIGNER_ERROR_TOKEN_EXCHANGE /* */ (ESP_SIGNER_ERROR_RANGE - 9)
#define ESP_SIGNER_ERROR_TOKEN_NOT_READY /* */ (ESP_SIGNER_ERROR_RANGE - 10)
#define ESP_SIGNER_ERROR_SYS_TIME_IS_NOT_READY /* */ (ESP_SIGNER_ERROR_RANGE - 11)
#define ESP_SIGNER_ERROR_NTP_SYNC_TIMED_OUT /* */ (ESP_SIGNER_ERROR_RANGE - 12)
#define ESP_SIGNER_ERROR_TOKEN_COMPLETE_NOTIFY /* */ (ESP_SIGNER_ERROR_RANGE - 13)
#define ESP_SIGNER_ERROR_TOKEN_COMPLETE_UNNOTIFY /* */ (ESP_SIGNER_ERROR_RANGE - 14)
#define ESP_SIGNER_ERROR_TOKEN_ERROR_UNNOTIFY /* */ (ESP_SIGNER_ERROR_RANGE - 15)
#define ESP_SIGNER_ERROR_MISSING_SERVICE_ACCOUNT_CREDENTIALS /* */ (ESP_SIGNER_ERROR_RANGE - 16)
#define ESP_SIGNER_ERROR_SERVICE_ACCOUNT_JSON_FILE_PARSING_ERROR /* */ (ESP_SIGNER_ERROR_RANGE - 17)

#endif
32 changes: 1 addition & 31 deletions src/ESP_Signer_Helper.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Created August 12, 2023
* Created August 21, 2023
*/

#ifndef ESP_SIGNER_HELPER_H
Expand Down Expand Up @@ -253,36 +253,6 @@ namespace StringHelper
return mbfs->getReservedLen(len);
}

inline void splitString(const MB_String &str, MB_VECTOR<MB_String> out, const char delim)
{
size_t current = 0, previous = 0;
current = str.find(delim, 0);
MB_String s;
while (current != MB_String::npos)
{
s.clear();
str.substr(s, previous, current - previous);
s.trim();
if (s.length() > 0)
out.push_back(s);

previous = current + 1;
current = str.find(delim, previous);
Utils::idle();
}

s.clear();

if (previous > 0 && current == MB_String::npos)
str.substr(s, previous, str.length() - previous);
else
s = str;
s.trim();
if (s.length() > 0)
out.push_back(s);
s.clear();
}

inline void pushTk(const MB_String &str, MB_VECTOR<MB_String> &tk)
{
MB_String s = str;
Expand Down
5 changes: 4 additions & 1 deletion src/ESP_Signer_Network.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/**
* Created August 12, 2023
* Created August 21, 2023
*/
#ifndef ESP_SIGNER_NETWORK_H
#define ESP_SIGNER_NETWORK_H

#include "FS_Config.h"
#include "ESP_Signer_ESP8266_Supports.h"

#if __has_include(<esp_idf_version.h>)
#include <esp_idf_version.h>
#endif

#if defined(ESP32) || defined(ESP8266) || defined(ARDUINO_RASPBERRY_PI_PICO_W) || \
defined(ARDUINO_UNOWIFIR4) || defined(ARDUINO_PORTENTA_C33) || \
Expand Down
6 changes: 3 additions & 3 deletions src/FS_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ static SdFat sd_fat_fs; // should declare as static here
// #define ESP_SIGNER_DISABLE_NATIVE_ETHERNET

// For ESP8266 ENC28J60 Ethernet module
// #define ESP_SIGNER_ENABLE_ESP8266_ENC28J60_ETH
// #define ENABLE_ESP8266_ENC28J60_ETH

// For ESP8266 W5100 Ethernet module
// #define ESP_SIGNER_ENABLE_ESP8266_W5100_ETH
// #define ENABLE_ESP8266_W5100_ETH

// For ESP8266 W5500 Ethernet module
// #define ESP_SIGNER_ENABLE_ESP8266_W5500_ETH
// #define ENABLE_ESP8266_W5500_ETH

// To use your custom config, create Custom_FS_Config.h in the same folder of this FS_Config.h file
#if __has_include("Custom_FS_Config.h")
Expand Down
Loading

0 comments on commit 1586adf

Please sign in to comment.