Skip to content

Commit

Permalink
Fix time issue for ESP devices when using external client with OAuth2…
Browse files Browse the repository at this point in the history
… authentication.
  • Loading branch information
mobizt committed Jul 20, 2023
1 parent f7eb5c8 commit ff8b3a4
Show file tree
Hide file tree
Showing 48 changed files with 285 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/**
* Created by K. Suwatchai (Mobizt)
*
* Email: [email protected]
*
* Github: https://github.com/mobizt/Firebase-ESP-Client
*
* Copyright (c) 2023 mobizt
*
*/

/** This example shows the basic FCM HTTPv1 send message with external Client.
* This example used ESP32 and WIZnet W5500 (Etherner) devices which ESP_SSLClient will be used as the external Client.
*
* Don't gorget to define this in FirebaseFS.h
* #define FB_ENABLE_EXTERNAL_CLIENT
*/

#include <Firebase_ESP_Client.h>

// Provide the token generation process info.
#include <addons/TokenHelper.h>

// Provide the RTDB payload printing info and other helper functions.
#include <addons/RTDBHelper.h>

// https://github.com/arduino-libraries/Ethernet
#include <Ethernet.h>

// https://github.com/mobizt/ESP_SSLClient
#include <ESP_SSLClient.h>

// For NTP time client
#include "MB_NTP.h"

/* 1. Define the API Key */
#define API_KEY "API_KEY"

/** 2. Define the Service Account credentials (required for token generation)
*
* This information can be taken from the service account JSON file.
*
* To download service account file, from the Firebase console, goto project settings,
* select "Service accounts" tab and click at "Generate new private key" button
*/
#define FIREBASE_PROJECT_ID "PROJECT_ID"
#define FIREBASE_CLIENT_EMAIL "CLIENT_EMAIL"
const char PRIVATE_KEY[] PROGMEM = "-----BEGIN PRIVATE KEY-----XXXXXXXXXXXX-----END PRIVATE KEY-----\n";

/* 3. Define the ID token for client or device to send the message */
#define DEVICE_REGISTRATION_ID_TOKEN "DEVICE_TOKEN"

/* 4. Defined the Ethernet module connection */
#define WIZNET_RESET_PIN 26 // Connect W5500 Reset pin to GPIO 26 of ESP32
#define WIZNET_CS_PIN 5 // Connect W5500 CS pin to GPIO 5 of ESP32
#define WIZNET_MISO_PIN 19 // Connect W5500 MISO pin to GPIO 19 of ESP32
#define WIZNET_MOSI_PIN 23 // Connect W5500 MOSI pin to GPIO 23 of ESP32
#define WIZNET_SCLK_PIN 18 // Connect W5500 SCLK pin to GPIO 18 of ESP32

/* 5. Define MAC */
uint8_t Eth_MAC[] = {0x02, 0xF0, 0x0D, 0xBE, 0xEF, 0x01};

/* 6. Define IP (Optional) */
IPAddress Eth_IP(192, 168, 1, 104);

/* 7. Define the RTDB URL (only if RTDB was used)*/
#define DATABASE_URL "URL" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app

// Define Firebase Data object
FirebaseData fbdo;

FirebaseAuth auth;
FirebaseConfig config;

unsigned long sendDataPrevMillis = 0;

int count = 0;

volatile bool dataChanged = false;

// Define the basic client
// The network interface devices that can be used to handle SSL data should
// have large memory buffer up to 1k - 2k or more, otherwise the SSL/TLS handshake
// will fail.
EthernetClient basic_client;

// This is the wrapper client that utilized the basic client for io and
// provides the mean for the data encryption and decryption before sending to or after read from the io.
// The most probable failures are related to the basic client itself that may not provide the buffer
// that large enough for SSL data.
// The SSL client can do nothing for this case, you should increase the basic client buffer memory.
ESP_SSLClient ssl_client;

EthernetUDP udp_client;

unsigned long lastTime = 0;

void sendMessage();

void ResetEthernet()
{
Serial.println("Resetting WIZnet W5500 Ethernet Board... ");
pinMode(WIZNET_RESET_PIN, OUTPUT);
digitalWrite(WIZNET_RESET_PIN, HIGH);
delay(200);
digitalWrite(WIZNET_RESET_PIN, LOW);
delay(50);
digitalWrite(WIZNET_RESET_PIN, HIGH);
delay(200);
}

void networkConnection()
{
Ethernet.init(WIZNET_CS_PIN);

ResetEthernet();

Serial.println("Starting Ethernet connection...");
Ethernet.begin(Eth_MAC);

unsigned long to = millis();

while (Ethernet.linkStatus() == LinkOFF || millis() - to < 2000)
{
delay(100);
}

if (Ethernet.linkStatus() == LinkON)
{
Serial.print("Connected with IP ");
Serial.println(Ethernet.localIP());
}
else
{
Serial.println("Can't connect");
}
}

// Define the callback function to handle server status acknowledgement
void networkStatusRequestCallback()
{
// Set the network status
fbdo.setNetworkStatus(Ethernet.linkStatus() == LinkON);
}

void setup()
{

Serial.begin(115200);

networkConnection();

Serial_Printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);

/* Assign the basic Client (Ethernet) pointer to the basic Client */
ssl_client.setClient(&basic_client);

/* Similar to WiFiClientSecure */
ssl_client.setInsecure();

/* For NTP */
Firebase.setUDPClient(&udp_client, 0);

/* Assign the api key (required) */
config.api_key = API_KEY;

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

/* Assign the RTDB URL (if only RTDB was used) */
config.database_url = DATABASE_URL;

/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h

/* fbdo.setExternalClient and fbdo.setExternalClientCallbacks must be called before Firebase.begin */

/* Assign the pointer to global defined external SSL Client object */
fbdo.setExternalClient(&ssl_client);

/* Assign the required callback functions */
fbdo.setExternalClientCallbacks(networkConnection, networkStatusRequestCallback);

// Comment or pass false value when WiFi reconnection will control by your code or third party library
Firebase.reconnectWiFi(true);

Firebase.setDoubleDigits(5);

Firebase.begin(&config, &auth);
}

void loop()
{

// Firebase.ready() should be called repeatedly to handle authentication tasks.

if (Firebase.ready() && (millis() - lastTime > 60 * 1000 || lastTime == 0))
{
lastTime = millis();

sendMessage();
}
}

void sendMessage()
{

Serial.print("Send Firebase Cloud Messaging... ");

// Read more details about HTTP v1 API here https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
FCM_HTTPv1_JSON_Message msg;

msg.token = DEVICE_REGISTRATION_ID_TOKEN;

msg.notification.body = "Notification body";
msg.notification.title = "Notification title";

// For the usage of FirebaseJson, see examples/FirebaseJson/BasicUsage/Create.ino
FirebaseJson payload;

// all data key-values should be string
payload.add("temp", "28");
payload.add("unit", "celsius");
payload.add("timestamp", "1609815454");
msg.data = payload.raw();

if (Firebase.FCM.send(&fbdo, &msg)) // send message to recipient
Serial.printf("ok\n%s\n\n", Firebase.FCM.payload(&fbdo).c_str());
else
Serial.println(fbdo.errorReason());

count++;
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Firebase Arduino Client Library for ESP8266 and ESP32",
"version": "4.3.17",
"version": "4.3.18",
"keywords": "communication, REST, esp32, esp8266, arduino",
"description": "The library supports Firebase products e.g. Realtime database, Cloud Firestore database, Firebase Storage and Google Cloud Storage, Cloud Functions for Firebase and Cloud Messaging. The library also supported other Arduino devices using Clients interfaces 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=Firebase Arduino Client Library for ESP8266 and ESP32

version=4.3.17
version=4.3.18

author=Mobizt

Expand Down
2 changes: 1 addition & 1 deletion src/FB_Const.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/FB_Error.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/FB_Network.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
6 changes: 3 additions & 3 deletions src/FB_Utils.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

/**
*
* This library supports Espressif ESP8266, ESP32 and Raspberry Pi Pico (RP2040)
*
* Created July 7, 2023
* Created July 20, 2023
*
* This work is a part of Firebase ESP Client library
* Copyright (c) 2023 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -1730,7 +1730,7 @@ namespace TimeHelper
#if defined(FB_ENABLE_EXTERNAL_CLIENT) || defined(MB_ARDUINO_PICO)
tm = *mb_ts_offset + millis() / 1000;

#if defined(MB_ARDUINO_PICO)
#if defined(MB_ARDUINO_PICO) || defined(ESP32) || defined(ESP8266)
if (tm < time(nullptr))
tm = time(nullptr);
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/Firebase.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/Firebase.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/FirebaseFS.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
4 changes: 2 additions & 2 deletions src/Firebase_Client_Version.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef FIREBASE_CLIENT_VERSION
#define FIREBASE_CLIENT_VERSION "4.3.17"
#define FIREBASE_CLIENT_VERSION_NUM 40317
#define FIREBASE_CLIENT_VERSION "4.3.18"
#define FIREBASE_CLIENT_VERSION_NUM 40318

/* The inconsistent file version checking to prevent mixed versions compilation. */
#define FIREBASE_CLIENT_VERSION_CHECK(ver) (ver == FIREBASE_CLIENT_VERSION_NUM)
Expand Down
2 changes: 1 addition & 1 deletion src/firestore/FB_Firestore.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/firestore/FB_Firestore.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/functions/FB_Functions.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/functions/FB_Functions.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/functions/FunctionsConfig.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/functions/FunctionsConfig.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/functions/PocicyBuilder.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/functions/PolicyBuilder.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/gcs/GCS.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/gcs/GCS.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Firebase_Client_Version.h"
#if !FIREBASE_CLIENT_VERSION_CHECK(40317)
#if !FIREBASE_CLIENT_VERSION_CHECK(40318)
#error "Mixed versions compilation."
#endif

Expand Down
Loading

0 comments on commit ff8b3a4

Please sign in to comment.