[Résolu][ESP32 + DHT11 + YL69] tout fonctionne séparement mais pas ensemble

Forum dédié aux bricolages d'objets connectés IOT et leurs communications avec DomoticZ :
- Remplacement de firmware des prises, modules relais ...
- Fabrication à base d'ESP8266, de NodeMCU, de Weemos et consorts ...
phrenos
Messages : 2
Inscription : 22 nov. 2018, 18:56

[Résolu][ESP32 + DHT11 + YL69] tout fonctionne séparement mais pas ensemble

Message par phrenos »

Bonsoir,

Je débute avec les nodemcu et j'ai réalisé mon premier montage en suivant les différents tuto/retour d'expérience.

Donc ce qui fonctionne :
  • esp32 + YL69
  • esp32 + DHT11 + domoticz
N'ayant qu'un seul esp32, j'ai tenté de mettre dessus le YL69 et le DHT11 ; mais là rien ne sort via le moniteur de série.

Voici le code que j'utilise (qui est une concaténation de ce que j'ai trouvé sur le web) :

Code : Tout sélectionner

#include <WiFi.h>
#include <SimpleDHT.h>
#include <HTTPClient.h>

// for DHT11, 
//      VCC: 5V or 3V
//      GND: GND
//      DATA: 2

int pinDHT11 = 2;

int PinYL69 = 15;
int ValeurSensor = 0;
int pourcentage = 0;

SimpleDHT11 dht11;
HTTPClient http; 

const char* ssid = "XXXXXXXXX";
const char* password =  "XXXXXXXXXXX";
const char* host = "XXXXXXXXX";
const int port = 8080;
const int   watchdog = 60000; // Fréquence d'envoi des données à Domoticz - Frequency of sending data to Domoticz
unsigned long previousMillis = millis(); 


void sendDomoticz(String url){
  Serial.print("connecting to ");
  Serial.println(host);
  Serial.print("Requesting URL: ");
  Serial.println(url);

  String complete_url = "http://";
  complete_url += String(host);
  complete_url += ":";
  complete_url += String(port);
  complete_url += String(url); 
  http.begin(host,port,url);
  int httpCode = http.GET();
    if (httpCode) {
      if (httpCode == 200) {
        String payload = http.getString();
        Serial.println("Domoticz response "); 
        Serial.println(payload);
      }else{
        String payload = http.getString();
        Serial.println("Domoticz not response ");
        Serial.println(String(httpCode));
        // pour interprétation des codes d'erreur : https://github.com/espressif/arduino-esp32/blob/master/libraries/HTTPClient/src/HTTPClient.h
        Serial.println(complete_url);
      }
    }
  Serial.println("closing connection");
  http.end();
}

int ConvertEnPercent(int value){
 int ValeurPorcentage = 0;
 ValeurPorcentage = map(value, 4095, 800, 0, 100);
 return ValeurPorcentage;
}

void AfficheValeurEtPourcentage(){
 Serial.println("-----------");
 Serial.print("Valeur: ");
 Serial.println(ValeurSensor);
 Serial.print("Pourcentage: ");
 Serial.print(pourcentage);
 Serial.println("%");

}

void connectToNetwork() {
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Establishing connection to WiFi..");
  } 
  Serial.println("Connected to network");
}

//#######################################################

void setup() {
  Serial.begin(9600);
  connectToNetwork();
 
  Serial.println(WiFi.macAddress());
  Serial.println(WiFi.localIP());
 
  //WiFi.disconnect(true);
  Serial.println(WiFi.localIP());
}

//#######################################################
void loop() {
  // start working...
  Serial.println("=================================");
  Serial.println(WiFi.localIP());

  ValeurSensor = analogRead(PinYL69);
  pourcentage = ConvertEnPercent(ValeurSensor);
  AfficheValeurEtPourcentage();
  
  // read with raw sample data.
  byte temperature = 0;
  byte humidity = 0;
  byte data[40] = {0};
  if (dht11.read(pinDHT11, &temperature, &humidity, data)) {
    Serial.print("Read DHT11 failed");
    return;
  }
  
  float t = int(temperature);
  float h = int(humidity);

  if ( isnan(t) || isnan(h) ) {
        Serial.println("DHT KO");
  } else {
        int hum_stat;
        if ( h > 70 ) {
          hum_stat = 3;
        } else if ( h < 30 ) {
          hum_stat = 2; 
        } else if ( h >= 30 & h <= 45 ) {
          hum_stat = 0;
        } else if ( h > 45 & h <= 70 ) {
          hum_stat = 1;
        }

        // modèle : /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT
        // https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s#Temperature.2Fhumidity
        
        String url = "/json.htm?type=command&param=udevice&idx=2&nvalue=0&svalue=";
        url += String(t); url += ";";
        url += String(h); url += ";";
        url += String(hum_stat); url += ";";
        sendDomoticz(url);
  }
  
  Serial.print((int)temperature); Serial.print(" *C, ");
  Serial.print((int)humidity); Serial.println(" %");
  
  // DHT11 sampling rate is 1HZ.
  delay(6000);
}
Au niveau des tests avec ce code :
  • esp32 + YL69 => pas de retour, ou plutot une valeur qui n'a pas de sens (4095 à sec et même valeur dans l'eau) : montage classique (directement connecté à l'esp32)
  • esp32 + YL69 => sans les bouts de code concernant le wifi et le DHT11, impeccable, j'ai des valeurs qui occillent entre 800 et 1300 : montage classique (pas de modification par rapport au précédent
  • esp32 + YL69 + DHT11 => même résultat que le point 2 pour le YL69 et pas de connexion pour le DHT11 : usage d'un breadboard pour le 3,3v et le GRND, connexion aux pin 15 et 2 (cf en-tête du code)
Voilà, j'ai testé ce que je connais, il doit y avoir une explication simple, mais là je sèche. Merci par avance pour votre aide.
phrenos
Messages : 2
Inscription : 22 nov. 2018, 18:56

Re: [Résolu][ESP32 + DHT11 + YL69] tout fonctionne séparement mais pas ensemble

Message par phrenos »

Bonjour,

Après moult essai voici ce qui fonctionne (j'explique pas pourquoi, je ne sais pas) :

Voici le code :

Code : Tout sélectionner

#include <WiFi.h>
#include <SimpleDHT.h>
#include <HTTPClient.h>


// Pour le dht11 
int pinDHT11 = 15;
SimpleDHT11 dht11;

// Pour le YL69
int analog_pin = 34;  // lecture analogique
int ValeurSensor = 0;
int pourcentage = 0;

// constante de connexion
HTTPClient http;
const char* ssid = "";
const char* password =  "";
const char* host = "";
const int port = 8080;
const int   watchdog = 60000; // Fréquence d'envoi des données à Domoticz - Frequency of sending data to Domoticz
unsigned long previousMillis = millis(); 

//#######################################################

void connectToNetwork() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Establishing connection to WiFi..");
  } 
  Serial.println("Connected to network");
}


//#######################################################

void setup() {
  Serial.begin(9600);
  connectToNetwork();
  
  //pinMode(PinYL69alim, OUTPUT);
  //digitalWrite(PinYL69alim, LOW);

  Serial.println(WiFi.macAddress());
  Serial.println(WiFi.localIP());
 
  //WiFi.disconnect(true);
  Serial.println(WiFi.localIP());
}

//#######################################################
void loop() {
  // start working...
  Serial.println("=================================");
  Serial.println(WiFi.localIP());
  
  ValeurSensor = analogRead(analog_pin);

  pourcentage = ConvertEnPercent(ValeurSensor);
  AfficheValeurEtPourcentage();
  
  // read with raw sample data.
  byte temperature = 0;
  byte humidity = 0;
  byte data[40] = {0};
  if (dht11.read(pinDHT11, &temperature, &humidity, data)) {
    Serial.print("Read DHT11 failed");
    return;
  }
  
  float t = int(temperature);
  float h = int(humidity);

  if ( isnan(t) || isnan(h) ) {
        Serial.println("DHT KO");
  } else {
        int hum_stat;
        if ( h > 70 ) {
          hum_stat = 3;
        } else if ( h < 30 ) {
          hum_stat = 2; 
        } else if ( h >= 30 & h <= 45 ) {
          hum_stat = 0;
        } else if ( h > 45 & h <= 70 ) {
          hum_stat = 1;
        }

        // modèle : /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT
        // https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s#Temperature.2Fhumidity
        
        String url = "/json.htm?type=command&param=udevice&idx=14&nvalue=0&svalue=";
        url += String(t); url += ";";
        url += String(h); url += ";";
        url += String(hum_stat); url += ";";
        sendDomoticz(url);
  }
  
  Serial.print((int)temperature); Serial.print(" *C, ");
  Serial.print((int)humidity); Serial.println(" %");
  
  // DHT11 sampling rate is 1HZ.
  delay(6000);
}

//#######################################################

void sendDomoticz(String url){
  Serial.print("connecting to ");
  Serial.println(host);
  Serial.print("Requesting URL: ");
  Serial.println(url);

  String complete_url = "http://";
  complete_url += String(host);
  complete_url += ":";
  complete_url += String(port);
  complete_url += String(url); 
  http.begin(host,port,url);
  int httpCode = http.GET();
    if (httpCode) {
      if (httpCode == 200) {
        String payload = http.getString();
        Serial.println("Domoticz response "); 
        Serial.println(payload);
      }else{
        String payload = http.getString();
        Serial.println("Domoticz not response ");
        Serial.println(String(httpCode));
        // pour interprétation des codes d'erreur : https://github.com/espressif/arduino-esp32/blob/master/libraries/HTTPClient/src/HTTPClient.h
        Serial.println(complete_url);
      }
    }
  Serial.println("closing connection");
  http.end();
}

//#######################################################
// pour l'humidité en terre
int ConvertEnPercent(int value){
 int ValeurPorcentage = 0;
 ValeurPorcentage = map(value, 4095, 1813, 0, 100);
 return ValeurPorcentage;
}

// pour l'humidité en terre
void AfficheValeurEtPourcentage(){
 Serial.println("-----------");
 Serial.print("Valeur: ");
 Serial.println(ValeurSensor);
 Serial.print("Pourcentage: ");
 Serial.print(pourcentage);
 Serial.println("%");
}
Je suspecte néanmoins un pb d'alimention du YL69, du coup via un breadboard j'ai mis en serie le pin d'alim ; autre pbmatique, il me semble que la lecture analogique il faudrait que j'ai des résultats entre 0 et 1024 hors là j'ai entre 4095 (sec) et 1813 (dans l'eau).

Voilà
Répondre