/**
Created by K. Suwatchai (Mobizt)
Email: [email protected]
Github: https://github.com/mobizt/Firebase-ESP8266
Copyright (c) 2023 mobizt
*/
/** This example will show how to access the RTDB in Test Mode (no authentication).
*/
#include <Arduino.h>
#if defined(ESP32)
#include <WiFi.h>
#include <FirebaseESP32.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>
#elif defined(ARDUINO_RASPBERRY_PI_PICO_W)
#include <WiFi.h>
#include <FirebaseESP8266.h>
#endif
#include <HTTPClient.h>
// Provide the RTDB payload printing info and other helper functions.
#include <addons/RTDBHelper.h>
/* 1. Define the WiFi credentials */
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
/* 2. Define the RTDB URL */
#define DATABASE_URL "wokwiesp32-9057f-default-rtdb.europe-west1.firebasedatabase.app" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
/* 3. Define the Firebase Data object */
FirebaseData fbdo;
/* 4, Define the FirebaseAuth data for authentication data */
FirebaseAuth auth;
/* Define the FirebaseConfig data for config data */
FirebaseConfig config;
unsigned long dataMillis = 0;
unsigned long sendDataPrevMillis = 0;
int count = 0;
int ledPin = 13;
uint32_t idleTimeForStream = 15000;
void setup()
{
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
/* Assign the certificate file (optional) */
// config.cert.file = "/cert.cer";
// config.cert.file_storage = StorageType::FLASH;
/* Assign the database URL(required) */
config.database_url = DATABASE_URL;
config.signer.test_mode = true;
/**
Set the database rules to allow public read and write.
{
"rules": {
".read": true,
".write": true
}
}
*/
Firebase.reconnectWiFi(true);
/* Initialize the library with the Firebase authen and config */
Firebase.begin(&config, &auth);
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
HTTPClient http; //Declare an object of class HTTPClient
http.begin("http://httpbin.org/get");
int httpCode = http.GET(); //Send the request
if (httpCode > 0) { //Check the returning code
String payload = http.getString(); //Get the request response payload
Serial.println(payload); //Print the response payload
}
http.end(); //Close connection
}
Serial.println("Internet connection test completed\n");
// The data under the node being stream (parent path) should keep small
// Large stream payload leads to the parsing error due to memory allocation.
if (!Firebase.beginStream(fbdo, "/devices/device001/state"))
Serial.printf("sream begin error, %s\n\n", fbdo.errorReason().c_str());
pinMode(ledPin, OUTPUT);
}
void loop()
{
// Firebase.ready() should be called repeatedly to handle authentication tasks.
if (Firebase.ready())
{
if (!Firebase.readStream(fbdo))
Serial.printf("sream read error, %s\n\n", fbdo.errorReason().c_str());
if (fbdo.streamTimeout())
{
Serial.println("stream timed out, resuming...\n");
if (!fbdo.httpConnected())
Serial.printf("error code: %d, reason: %s\n\n", fbdo.httpCode(), fbdo.errorReason().c_str());
}
if (fbdo.streamAvailable())
{
Serial.printf("sream path, %s\nevent path, %s\ndata type, %s\nevent type, %s\nvalue, %s\n\n",
fbdo.streamPath().c_str(),
fbdo.dataPath().c_str(),
fbdo.dataType().c_str(),
fbdo.eventType().c_str(),
fbdo.stringData().c_str());
if (fbdo.boolData())
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}
}
}