What does "Serial2.available()" do in the following code?
#include
#include
#define JSON_SIZE 256
StaticJsonDocument jsonBuffer; // Allocate a buffer to hold the JSON object
#include // To include wifi features
#include // For interfacing Esp32 with firebase
//#include
FirebaseJson jsonData;
//Provide the token generation process info.
#include "addons/TokenHelper.h"
//Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"
// Insert your network credentials
#define WIFI_SSID "dev"
#define WIFI_PASSWORD "12345678"
// Insert Firebase project API Key
#define API_KEY "AIzaSyCArX_bjvbBZE9alXH-58RLlyZGVtXmJGE"
// Insert RTDB URLefine the RTDB URL */
#define DATABASE_URL "https://test-proj-977b8-default-rtdb.asia-southeast1.firebasedatabase.app/"
//Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
bool signupOK = false;
//#define BUTTON_PIN 2
#define UART_BAUDRATE 115200
//#define NODE_ID "esp1"
//volatile bool buttonPressed = false;
//void IRAM_ATTR buttonInterrupt() {
// buttonPressed = true;
//}
void setup() {
Serial.begin(UART_BAUDRATE);
// Firebase
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();
/* Assign the api key (required) */
config.api_key = API_KEY;
/* Assign the RTDB URL (required) */
config.database_url = DATABASE_URL;
/* Sign up */
if (Firebase.signUp(&config, &auth, "", "")) {
Serial.println("Signin successful 'ok'");
signupOK = true;
}
else {
Serial.printf("%s\n", config.signer.signupError.message.c_str());
}
/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
// UART communication
while (!Serial);
// pinMode(BUTTON_PIN, INPUT_PULLUP);
// attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonInterrupt, FALLING);
Serial2.begin(UART_BAUDRATE, SERIAL_8N1, 16 , 17); // RX: GPIO16, TX: GPIO17
}
void loop() {
// if (buttonPressed) {
// Serial.printf("The button on %s is pressed!\n", NODE_ID);
// Serial2.print(NODE_ID);
// Serial2.println(" button pressed");
// buttonPressed = false;
// delay(10);
// }
if(!Firebase.ready()) {
Serial.println("Firebase not ready");
}
if(!signupOK) {
Serial.println("Signin not successful");
}
if (Firebase.ready() && signupOK & Serial2.available()) {
// if (jsonData.readFrom(Serial2))
// {
// Serial.println("JSON Data received...");
// jsonData.toString(Serial2, true);
// Serial.println();
// }
Serial.println("Incoming Data.");
String sendData = Serial2.readStringUntil('\n');
deserializeJson(jsonBuffer, sendData); // Deserialize the JSON string to a JSON object
String chipID = jsonBuffer["chipID"].as();
int counter = jsonBuffer["counter"].as();
String message = jsonBuffer["message"].as();
Serial.printf("Data received: ChipID: %s, Counter: %d, Message: %s\n", chipID.c_str(), counter, message.c_str());
//
//
// jsonData = jsonBuffer;
if (Firebase.RTDB.setStringAsync(&fbdo, "test/string", sendData)) {
Serial.println("Data sent to DataBase");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
}
}
}