#include <WiFi.h>
#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"
// Insert your network credentials
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Insert Firebase project API Key
#define API_KEY "AIzaSyDpGw4OyvsDGIGJ5dRJwc52Jr56zSUe3Yc"
// Insert RTDB URLefine the RTDB URL */
#define DATABASE_URL "https://iot-nodemcu-5d0e6-default-rtdb.firebaseio.com"
//Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
int count = 0;
bool signupOK = false;
const int ledPin = 13; // Pin para controlar el LED
unsigned long lastTime = 0;
unsigned long timerDelay = 100; // Intervalo de tiempo para consultar el estado en milisegundos (10 segundos)
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println("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("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);
}
void loop() {
// if ((millis() - lastTime) > timerDelay) {
// if(WiFi.status() == WL_CONNECTED){
// HTTPClient http;
// http.begin(serverName.c_str());
// int httpResponseCode = http.GET();
// if (httpResponseCode > 0) {
// String payload = http.getString();
// Serial.print("HTTP Response code: ");
// Serial.println(httpResponseCode);
// Serial.print("Payload: ");
// Serial.println(payload);
// // Verificar si el valor es "0" en el payload
// if (payload.equals("1")) {
// digitalWrite(ledPin, HIGH); // Enciende el LED si el valor es "1"
// Serial.println("LED encendido");
// } else {
// digitalWrite(ledPin, LOW); // Apaga el LED si el valor no es "1"
// Serial.println("LED apagado");
// }
// } else {
// Serial.print("Error code: ");
// Serial.println(httpResponseCode);
// }
// http.end();
// } else {
// Serial.println("WiFi Desconectado");
// }
// lastTime = millis();
// }
if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0)){
sendDataPrevMillis = millis();
// Write an Int number on the database path test/int
if (Firebase.RTDB.setInt(&fbdo, "test/int", count)){
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
}
count++;
// Write an Float number on the database path test/float
if (Firebase.RTDB.setFloat(&fbdo, "test/float", 0.01 + random(0,100))){
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
}
}
}