Replies: 2 comments 2 replies
-
If you don't want to use native WiFi and Ethernet interfaces, you need to set external SSL client that works with your network module to use with library. For tinyGSM library, it provides both SSL client via You can use For ESP and Pico devices: https://github.com/mobizt/ESP_SSLClient We will use Here the example. #include <FirebaseESP32.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>
/* Define the API Key */
#define API_KEY "API_KEY"
/* Define the RTDB URL */
#define DATABASE_URL "URL" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
/* Define the user Email and password that alreadey registerd or added in your project */
#define USER_EMAIL "USER_EMAIL"
#define USER_PASSWORD "USER_PASSWORD"
// Select your modem:
#define TINY_GSM_MODEM_SIM800
// #define TINY_GSM_MODEM_SIM808
// #define TINY_GSM_MODEM_SIM868
// #define TINY_GSM_MODEM_SIM7000SSL
// #define TINY_GSM_MODEM_SIM7080
// #define TINY_GSM_MODEM_UBLOX
// #define TINY_GSM_MODEM_SARAR4
// #define TINY_GSM_MODEM_ESP8266
// #define TINY_GSM_MODEM_XBEE
// #define TINY_GSM_MODEM_SEQUANS_MONARCH
// Set serial for AT commands (to the module)
// Use Hardware Serial on Mega, Leonardo, Micro
#ifndef __AVR_ATmega328P__
#define SerialAT Serial1
// or Software Serial on Uno, Nano
#else
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(2, 3); // RX, TX
#endif
// Increase RX buffer to capture the entire response
// Chips without internal buffering (A6/A7, ESP8266, M590)
// need enough space in the buffer for the entire response
// else data will be lost (and the http library will fail).
#if !defined(TINY_GSM_RX_BUFFER)
#define TINY_GSM_RX_BUFFER 650
#endif
// See all AT commands, if wanted
// #define DUMP_AT_COMMANDS
// Define the serial console for debug prints, if needed
#define TINY_GSM_DEBUG Serial
// #define LOGGING // <- Logging is for the HTTP library
// Range to attempt to autobaud
// NOTE: DO NOT AUTOBAUD in production code. Once you've established
// communication, set a fixed baud rate using modem.setBaud(#).
#define GSM_AUTOBAUD_MIN 9600
#define GSM_AUTOBAUD_MAX 115200
// Add a reception delay, if needed.
// This may be needed for a fast processor at a slow baud rate.
// #define TINY_GSM_YIELD() { delay(2); }
// Define how you're planning to connect to the internet.
// This is only needed for this example, not in other code.
#define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false
// set GSM PIN, if any
#define GSM_PIN ""
// flag to force SSL client authentication, if needed
// #define TINY_GSM_SSL_CLIENT_AUTHENTICATION
// Your GPRS credentials, if any
#define APN "YourAPN"
#define GPRS_USER ""
#define GPRS_PASSWORD ""
#include <TinyGsmClient.h>
// Just in case someone defined the wrong thing..
#if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
#undef TINY_GSM_USE_GPRS
#undef TINY_GSM_USE_WIFI
#define TINY_GSM_USE_GPRS false
#define TINY_GSM_USE_WIFI true
#endif
#if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
#undef TINY_GSM_USE_GPRS
#undef TINY_GSM_USE_WIFI
#define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false
#endif
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, Serial);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
TinyGsmClientSecure ssl_client(modem);
// Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
unsigned long count = 0;
void networkConnection()
{
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
modem.restart();
// modem.init();
String modemInfo = modem.getModemInfo();
Serial.print("Modem Info: ");
Serial.println(modemInfo);
#if TINY_GSM_USE_GPRS
// Unlock your SIM card with a PIN if needed
if (GSM_PIN && modem.getSimStatus() != 3)
{
modem.simUnlock(GSM_PIN);
}
#endif
Serial.print("Waiting for network...");
if (!modem.waitForNetwork())
{
Serial.println(" fail");
delay(10000);
return;
}
Serial.println(" success");
if (modem.isNetworkConnected())
{
Serial.println("Network connected");
}
#if TINY_GSM_USE_GPRS
// GPRS connection parameters are usually set after network registration
Serial.print(F("Connecting to "));
Serial.print(APN);
if (!modem.gprsConnect(APN, GPRS_USER, GPRS_PASSWORD))
{
Serial.println(" fail");
delay(10000);
return;
}
Serial.println(" success");
if (modem.isGprsConnected())
{
Serial.println("GPRS connected");
}
#endif
}
// Define the callback function to handle server status acknowledgement
void networkStatusRequestCallback()
{
// Set the network status
fbdo.setNetworkStatus(modem.isNetworkConnected() && modem.isGprsConnected());
}
void setup()
{
// Set console baud rate
Serial.begin(115200);
Serial.println("Wait...");
// Set GSM module baud rate
TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
// SerialAT.begin(9600);
delay(6000);
networkConnection();
Serial_Printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
/* Assign the api key (required) */
config.api_key = API_KEY;
/* Assign the user sign in credentials */
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
/* Assign the RTDB URL (required) */
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() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0))
{
sendDataPrevMillis = millis();
Serial_Printf("Set bool... %s\n", Firebase.setBool(fbdo, F("/test/bool"), count % 2 == 0) ? "ok" : fbdo.errorReason().c_str());
count++;
}
}
The code example was not tested yet and adapted from the following examples. Note: You may need to increase the buffer size of |
Beta Was this translation helpful? Give feedback.
-
For For OPEnSLab's SSLClient, you should adapt and make this example works for your device. |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm working on a proyect where I upload data to firebase. I used to use Wifi conection and it worked fine, but now I need to use the TinyGsm library instead but i don't know if its compatible with this library and if it is, how can i make the conection with firebase. I've looked for examples but i just coudn't find any.
I'm using a LILYGO® TTGO T-A7670G.
Hope someone can help me :)
Beta Was this translation helpful? Give feedback.
All reactions