#include <Arduino.h>
#include <WiFi.h>
#include <FirebaseESP32.h>
#include <DHT.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 DHTPIN 15
#define DHTTYPE DHT22

// Replace with your network credentials
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";

// Replace with your Firebase project details
#define FIREBASE_HOST "https://data-esp32-c6ae2-default-rtdb.europe-west1.firebasedatabase.app/"
#define FIREBASE_API_KEY "AIzaSyBotY7sHUOAHutyOKhHV7VRtTYt16IIPQ4"

DHT dht(DHTPIN, DHTTYPE);

FirebaseData firebaseData;

FirebaseConfig firebaseConfig;
FirebaseAuth firebaseAuth;

unsigned long sendDataPrevMillis = 0;
int count = 0;
bool signupOK = false;
int valeurs[] = {46, 55, 66, 78, 87};  // Exemple de tableau de données

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();

  // Set Firebase configuration and authentication details
  firebaseConfig.database_url = FIREBASE_HOST;
  firebaseConfig.api_key = FIREBASE_API_KEY;

  /* Sign up */
  if (Firebase.signUp(&firebaseConfig, &firebaseAuth, "", "")){
    Serial.println("Firebase ok ");
    signupOK = true;
  }
  else{
    Serial.printf("%s\n", firebaseConfig.signer.signupError.message.c_str());
  }

  /* Assign the callback function for the long running token generation task */
  firebaseConfig.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
  
  // Connect to Firebase
  Firebase.begin(&firebaseConfig, &firebaseAuth);
  Firebase.reconnectWiFi(true);

  // Initialize DHT sensor
  dht.begin();
}

void loop() {
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  // Exemple de tableau de données
 
 //********************************
 int valeur = random(0, 100);  // Génère une valeur aléatoire entre 0 et 100
   if (Firebase.setInt(firebaseData, "/test/valeur", valeur)) {
    Serial.println("Donnée envoyée avec succès : " + String(valeur));
  } else {
    Serial.println("Erreur lors de l'envoi : " + firebaseData.errorReason());
  }

  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Send data to Firebase
  if (Firebase.setFloat(firebaseData, "/temperature", temperature)) {
    Serial.println("Temperature data sent successfully!");
  } else {
    Serial.println("Failed to send temperature data");
    Serial.println(firebaseData.errorReason());
  }

  if (Firebase.setFloat(firebaseData, "/humidity", humidity)) {
    Serial.println("Humidity data sent successfully!");
  } else {
    Serial.println("Failed to send humidity data");
    Serial.println(firebaseData.errorReason());
  }

  delay(2000);  // Delay for 2 seconds before the next loop
}