-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix time issue for ESP devices when using external client with OAuth2…
… authentication.
- Loading branch information
Showing
48 changed files
with
285 additions
and
50 deletions.
There are no files selected for viewing
235 changes: 235 additions & 0 deletions
235
examples/ExternalClient/FCM/Ethernet/ESP32/HTTPv1/SendMessage/SendMessage.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.