Firebase - 1er pas - bug:-(

hello , débutant, je bloque sur un problème (désolé si c'est peut etre qu'une bétise )mais je ne trouve pas la solution (apres des heures de recherches )
mon matériel : Nodemcu et esp8266 wifi

voici mon code récupéré de ce site avec 2-3 modif au niveau du Wifi

mon but : arrivé à me connecter à firebase, quand j'y serai arrivé, faire ce 1er projet sonde température
ex: Send Real-Time Sensor Data to Google Firebase with ESP8266

merci pour votre aide

1er problème, quand il ne trouve pas le wifi, il ne tente pas un nouvelle connection

2eme problème, quand il se connecte au wifi, j'ai ce message d'erreur

"13:41:48.364 -> Connexion WiFi etablie
13:41:48.364 -> => Addresse IP :
13:41:48.364 -> (IP unset)
13:41:52.943 -> Firebase ok
13:41:54.161 -> FAILED
13:41:54.161 -> REASON: not found
13:41:55.427 -> FAILED
13:41:55.427 -> REASON: not found"

voici mon code (repris d'un site

#include <Arduino.h>
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif

#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 "WiFi-xxx2C9"

#define WIFI_PASSWORD "AD5xxxD"

// Insert Firebase project API Key

#define API_KEY "AIzaSyBxxxxxxxxxxxqk_cU5gD0"

// Insert RTDB URLefine the RTDB URL */

#define DATABASE_URL "console.firebase.google.com/project/nodemcu-26629/database/nodemcu-26629-default-rtdb/data/~2F"

//Define Firebase Data object

FirebaseData fbdo;

FirebaseAuth auth;

FirebaseConfig config;

unsigned long sendDataPrevMillis = 0;

int count = 0;

bool signupOK = false;

void setup(){

 

  Serial.begin(115200);

  delay(2000);

   Serial.print("Connexion a ");

  Serial.println(WIFI_SSID);

  delay(3000);

  WiFi.mode(WIFI_STA);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  delay(4000); // mini 4000 !!

   

    if (WiFi.status() != WL_CONNECTED){

    Serial.println("Connexion WiFi etablie ");

    Serial.println("=> Addresse IP : ");

    Serial.println(WiFi.localIP());

   

    delay(3000);

  }

   else

  {

    Serial.println(" Pas de Connexion WiFi - Reconnexion ");

    delay(5000);

   

    }

  /* 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("Firebase 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 (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());

    }

  }

}

Bonjour @thmare et Bienvenue !

Planquer ce genre de question dans le Bar n'est pas vraiment dans ton intérêt !!

je déplace ce fil de discussion dans la partie principale du Forum Francophone

Pour information l'excellent site RandomNerd Tutorials propose plusieurs tutoriels associant Firebase et ESP6266

8266 Firebase

Il me semble que la phase de connection au point d'accès ET obtention d'une adresse IP y est mieux gérée que dans le code que tu as trouvé, code plutôt approximatif dans sa façon de faire les choses avec comme conséquence de donner un résultat aléatoire.

Un tutoriel tel que celui-ci : ESP32/ESP8266 Firebase: Send BME280 to Realtime Database | Random Nerd Tutorials est très didactique.

bonjour , merci pour vos infos

j'ai repris le code et le tuto donné dans votre exemple
Super : le problème de connectionner est ok :slight_smile: :slight_smile:

Connecting to WiFi ......192.168.1.60
1:24:45.744 -> Token info: type = id token, status = on request
11:24:47.010 -> Token info: type = id token, status = ready
11:24:47.010 -> Getting User UID
11:24:47.010 -> User UID: nBqj4cGPrJMxXDRZg4XV21MpNEw2
11:24:47.478 -> Relative Humidity : 50.60 %RH
11:24:47.478 -> Temperature in Celsius : 19.44 C
11:24:49.494 -> Relative Humidity : 50.62 %RH

Mais, (désolé :frowning: ) je bloque sur un dernier point .. FIrebase ne réceptionne aucune donnée

je pense que c'est parce que je n'arrive pas à intégrer ces lignes :

// Get latest sensor readings
temperature = bme.readTemperature();
humidity = bme.readHumidity();

// Send readings to database:
sendFloat(tempPath, temperature);
sendFloat(humPath, humidity);

J'ai un capteur capteur temperature SHT30 2.15-5.5V, Interface HY2.0-4P , et je dois utiliser
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_Sht31.h>

Alors que dans l'exemple c'est un BME280
#include <Adafruit_BME280.h>

Mon code tourne si je de active les 4 dernières ligne mais du coup il n'envoi rien à Firebase ?!

:frowning:


// capteur temperature SHT30 2.15-5.5V, Interface HY2.0-4P

#include <Arduino.h>
#if defined(ESP32)
  #include <WiFi.h>
#elif defined(ESP8266)
  #include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h>
#include <Wire.h>
#include <Adafruit_Sensor.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"

// debut programme pour le capteur SHT30 I2C address is 0x44(68)
#define Addr 0x44

// Insert your network credentials
#define WIFI_SSID "WiFi-2.4-22C9"
#define WIFI_PASSWORD "AD5456E2BD"

// Insert Firebase project API Key
#define API_KEY "AIzaSyDcEW_iO66PS-pxDRp83NAU1puWGLjfGSg"

// Insert Authorized Email and Corresponding Password
#define USER_EMAIL "Thmarechal.iphone@gmail.com"
#define USER_PASSWORD "Djane2022"

// Insert RTDB URLefine the RTDB URL
#define DATABASE_URL "https://console.firebase.google.com/project/esp-firebase-demo-bf9ee/database/esp-firebase-demo-bf9ee-default-rtdb/data/~2F"

// Define Firebase objects
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

// Variable to save USER UID
String uid;

// Variables to save database paths
String databasePath;
String tempPath;
String humPath;



// Timer variables (send new readings every three minutes)
unsigned long sendDataPrevMillis = 0;
unsigned long timerDelay = 180000;


// Initialize WiFi
void initWiFi() {
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
  Serial.println();
}

// Write float values to the database
void sendFloat(String path, float value){
  if (Firebase.RTDB.setFloat(&fbdo, path.c_str(), value)){
    Serial.print("Writing value: ");
    Serial.print (value);
    Serial.print(" on the following path: ");
    Serial.println(path);
    Serial.println("PASSED");
    Serial.println("PATH: " + fbdo.dataPath());
    Serial.println("TYPE: " + fbdo.dataType());
  }
  else {
    Serial.println("FAILED");
    Serial.println("REASON: " + fbdo.errorReason());
  }
}

void setup(){

   // Initialise I2C communication as MASTER
  Wire.begin();

  Serial.begin(115200);

  // Initialize wifi sensor

  initWiFi();

  // 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;

  Firebase.reconnectWiFi(true);
  fbdo.setResponseSize(4096);

  // Assign the callback function for the long running token generation task */
  config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h

  // Assign the maximum retry of token generation
  config.max_token_generation_retry = 5;

  // Initialize the library with the Firebase authen and config
  Firebase.begin(&config, &auth);

  // Getting the user UID might take a few seconds
  Serial.println("Getting User UID");
  while ((auth.token.uid) == "") {
    Serial.print('.');
    delay(1000);
  }
  // Print user UID
  uid = auth.token.uid.c_str();
  Serial.print("User UID: ");
  Serial.println(uid);

  // Update database path
  databasePath = "/UsersData/" + uid;

  // Update database path for sensor readings
  tempPath = databasePath + "/temperature"; // --> UsersData/<user_uid>/temperature
  humPath = databasePath + "/humidity"; // --> UsersData/<user_uid>/humidity
  
}

void loop(){

  {
   unsigned int data[6];
  
   // Start I2C Transmission
   Wire.beginTransmission(Addr);
   // Send measurement command
   Wire.write(0x2C);
   Wire.write(0x06);
   // Stop I2C transmission
   Wire.endTransmission();
   delay(500);

   // Request 6 bytes of data
   Wire.requestFrom(Addr, 6);

   // Read 6 bytes of data
   // cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc

   if (Wire.available() == 6)
   {
     data[0] = Wire.read();
     data[1] = Wire.read();
     data[2] = Wire.read();
     data[3] = Wire.read();
     data[4] = Wire.read();
     data[5] = Wire.read();
     }

   // Convert the data
   float tempPath = ((((data[0] * 256.0) + data[1]) * 175) / 65535.0) - 47;
  
   float humidity = ((((data[3] * 256.0) + data[4]) * 100) / 65535.0);

   // Output data to serial monitor
   Serial.print("Relative Humidity : ");
   Serial.print(humidity);
   Serial.println(" %RH");
   Serial.print("Temperature in Celsius : ");
   Serial.print(tempPath);
   Serial.println(" C");
  
   delay(1500);
   }
   // fin programme capteur temperature 
{
  // Send new readings to database
  if (Firebase.ready() && (millis() - sendDataPrevMillis > timerDelay || sendDataPrevMillis == 0)){
    sendDataPrevMillis = millis();

  // Send readings to database:
  //   sendFloat(tempPath, temperature);
  //   sendFloat(humPath, humidity);
    
  }
  
  }

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.