Débit mètre YF-B1

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 ...
bartras
Messages : 140
Inscription : 17 mars 2017, 17:44
Localisation : Saint Etienne (42)

Débit mètre YF-B1

Message par bartras »

Bonjour,

Je souhaite mesure un débit d'eau avec une sonde YF-B1 (aliexpress) relié à un Wemos D1 mini pour envoyer l'information sur un capteur "Waterflow" virtuel sous Domoticz.

Jusque là j'ai déjà fait avec un capteur de distance et tout fonctionne nickel.

J'ai repris mon code et je l'ai adapté pour le débit mètre mais j'ai une erreur car mon capteur virtuel n'est pas mis à jour...

Je pense fortement que ça à quelque chose à voir avec le fait qu'il y ait des interruptions...

Ci-dessous mon code:

Code : Tout sélectionner

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid     = x
const char* password = x
const char* host = x
const int   port = 8080;
const char*   id = x
const char*   mdp = x
const int   idx = 10498;
const int   watchdog = 5000; // Fréquence d'envoi (ms) 
unsigned long previousMillis = millis();

int NbTopsFan;
int Calc;
int hallsensor = D6;

void ICACHE_RAM_ATTR rpm()
{
  NbTopsFan++;
}

HTTPClient http;

void setup() {

  pinMode (hallsensor, INPUT_PULLUP);
  
  Serial.begin(115200);
  delay(10);

  Serial.setDebugOutput(true);  
  Serial.println("Connecting Wifi...");

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
   
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.print(WiFi.localIP()); 

  attachInterrupt(digitalPinToInterrupt(hallsensor), rpm, FALLING);
}

int value = 0;

void loop() {
  unsigned long currentMillis = millis();

  if ( currentMillis - previousMillis > watchdog ) {
    previousMillis = currentMillis;

    if(WiFi.status() != WL_CONNECTED) {
      Serial.println("WiFi not connected !");
    } else {  
      Serial.println("Send data to Domoticz");

      NbTopsFan = 0;
      sei();
      delay(1000);
      cli();
      Calc = (NbTopsFan * 2.2);
      Serial.print (Calc, DEC);
      Serial.println("L/h");
      
      String url = "/json.htm?username=";
      url += String(id);
      url += String("=&password=");
      url += String(mdp);
      url += String("=&type=command&param=udevice&idx=");
      url += String(idx);
      url += String("&nvalue=0&svalue=");
      url += String(Calc);
         
      sendDomoticz(url);
      }
   }
 }

void sendDomoticz(String url){
  Serial.print("connecting to ");
  Serial.println(host);
  Serial.print("Requesting URL: ");
  Serial.println(url);
  http.begin(host,port,url);
  int httpCode = http.GET();
  Serial.println(httpCode);
    if (httpCode) {
      if (httpCode == 200) {
        String payload = http.getString();
        Serial.println("Domoticz response "); 
        Serial.println(payload);
      }
    }
  Serial.println("closing connection");
  http.end();
}
Le httpCode est -1


Et la valeur "Calc" du débitmètre est bien affichée dans le moniteur série.


Pouvez vous m'aider ?

Merci.
vil1driver
Messages : 5661
Inscription : 30 janv. 2015, 11:07
Localisation : Rennes (35)

Re: Débit mètre YF-B1

Message par vil1driver »

Salut,

Sans approfondir,
Je dirais que les = avant les & sont de trop (à 2 reprises dans ton code)
MAJ = VIDER LE CACHE(<-Clicable)
/!\Les mises à jour de Domoticz sont souvent sources de difficultés, ne sautez pas dessus
modules.lua

Un ex domoticzien
bartras
Messages : 140
Inscription : 17 mars 2017, 17:44
Localisation : Saint Etienne (42)

Re: Débit mètre YF-B1

Message par bartras »

Salut,

Je suppose que tu veux parler de ce morceau du code:

Code : Tout sélectionner

url += String("=&password=");
url += String(mdp);
url += String("=&type=command&param=udevice&idx=");
J'ai enlevé les "=" et j'ai toujours la même erreur (httpCode -1).
bartras
Messages : 140
Inscription : 17 mars 2017, 17:44
Localisation : Saint Etienne (42)

Re: Débit mètre YF-B1

Message par bartras »

Un petit up, pour m'aider ?
vil1driver
Messages : 5661
Inscription : 30 janv. 2015, 11:07
Localisation : Rennes (35)

Re: Débit mètre YF-B1

Message par vil1driver »

Domoticz est il bavard au moment de l'envoi ?

L'idx est bon ? Le nombre me semble trop grand pour être juste.

La commande passée manuellement dans le navigateur apporte le résultat escompté ?
MAJ = VIDER LE CACHE(<-Clicable)
/!\Les mises à jour de Domoticz sont souvent sources de difficultés, ne sautez pas dessus
modules.lua

Un ex domoticzien
fking
Messages : 166
Inscription : 03 nov. 2017, 10:19
Localisation : Liverdun (54)
Contact :

Re: Débit mètre YF-B1

Message par fking »

L'index me semble gros aussi...
Tu as essayé en virant le user et password de la chaine json ?

Voilà le code que j'utilise pour un capteur MQ7 (monoxyde de carbone)

Code : Tout sélectionner

float valeur=(mq7.getPPM());
// Construction de la chaine pour le capteur et envoi à Domoticz
String url1 = "/json.htm?type=command&param=udevice&idx=";
url1 += String(IDX_MQ7);
url1 += "&nvalue=";   
url1 += String(valeur); 
url1 += "&svalue=";
url1 += String(valeur);
sendToDomoticz(url1);
Fais le ou ne le fais pas , essayer ne veut rien dire.
bartras
Messages : 140
Inscription : 17 mars 2017, 17:44
Localisation : Saint Etienne (42)

Re: Débit mètre YF-B1

Message par bartras »

Bonjour,

Je reviens sur ce topic après une longue absence...

J'essaye de mettre à jour un capteur virtuel de débit via ma sonde YF-B1 connecté à un Wemos D1 mini.

Ci-dessous le code simple pour lire la valeur du débitmètre quand je souffle dedans via le terminal série:
Il fonctionne parfaitement.

Code : Tout sélectionner

int NbTopsFan;
int Calc;
int hallsensor = D6;

void ICACHE_RAM_ATTR rpm()
{
  NbTopsFan++;
}

void setup() {
pinMode (hallsensor, INPUT_PULLUP);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(hallsensor), rpm, FALLING);
 
}

void loop() {
  NbTopsFan = 0;
  sei();
  delay(1000);
  cli();
  Calc = (NbTopsFan * 2.2);
  Serial.print (Calc, DEC);
  Serial.println("L/h");
}
Ici le code pour mettre à jour mon capteur virtuel via le wifi.
J'ai mis une valeur brute (300) juste pour vérifier que ça fonctionne.
Tout fonctionne très bien, c'est mis à jour.

Code : Tout sélectionner

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid     = "xxx";
const char* password = "xxx";
const char* host = "192.168.1.100";
const int   port = 8080;
const char*   id = "x";
const char*   mdp = "x";
const int   idx = 15804;
const int   watchdog = 5000; // Fréquence d'envoi (ms) des données à Domoticz - Frequency of sending data to Domoticz
unsigned long previousMillis = millis();

HTTPClient http;

void setup() {
  
  Serial.begin(115200);
  delay(10);

  Serial.setDebugOutput(true);  
  Serial.println("Connecting Wifi...");

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
   
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.print(WiFi.localIP()); 

}

void loop() {
  unsigned long currentMillis = millis();

  if ( currentMillis - previousMillis > watchdog ) {
    previousMillis = currentMillis;

    if(WiFi.status() != WL_CONNECTED) {
      Serial.println("WiFi not connected !");
    } else {  
      Serial.println("Send data to Domoticz");
      
      String url = "/json.htm?username=";
      url += String(id);
      url += String("&password=");
      url += String(mdp);
      url += String("&type=command&param=udevice&idx=");
      url += String(idx);
      url += String("&nvalue=0&svalue=");
      url += String(300);
       
      sendDomoticz(url);
      }
   }
 }

void sendDomoticz(String url){
  Serial.print("connecting to ");
  Serial.println(host);
  Serial.print("Requesting URL: ");
  Serial.print(host);
  Serial.print(":");
  Serial.print(port);
  Serial.println(url);
  http.begin(host,port,url);
  int httpCode = http.GET();
  Serial.println(httpCode);
    if (httpCode) {
      if (httpCode == 200) {
        String payload = http.getString();
        Serial.println("Domoticz response "); 
        Serial.println(payload);
      }
    }
  Serial.println("closing connection");
  http.end();
}

Du coup j'ai mixé les deux codes pour envoyer la vraie valeur du débitmètre:

Code : Tout sélectionner

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid     = "xxx";
const char* password = "xxx";
const char* host = "192.168.1.100";
const int   port = 8080;
const char*   id = "x";
const char*   mdp = "x";
const int   idx = 15804;
const int   watchdog = 5000; // Fréquence d'envoi (ms) des données à Domoticz - Frequency of sending data to Domoticz
unsigned long previousMillis = millis();

int NbTopsFan;
int Calc;
int hallsensor = D6;

int value = 0;

void ICACHE_RAM_ATTR rpm()
{
  NbTopsFan++;
}

HTTPClient http;

void setup() {

  pinMode (hallsensor, INPUT_PULLUP);
  
  Serial.begin(115200);
  delay(10);

  attachInterrupt(digitalPinToInterrupt(hallsensor), rpm, FALLING);

  Serial.setDebugOutput(true);  
  Serial.println("Connecting Wifi...");

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
   
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.print(WiFi.localIP()); 
}

void loop() {
  
      NbTopsFan = 0;
      sei();
      delay(5000);
      cli();
      Calc = (NbTopsFan * 2.2);
      Serial.print (Calc, DEC);
      Serial.println("L/h");
      
  unsigned long currentMillis = millis();

  if ( currentMillis - previousMillis > watchdog ) {
    previousMillis = currentMillis;

    if(WiFi.status() != WL_CONNECTED) {
      Serial.println("WiFi not connected !");
    } else {  
      Serial.println("Send data to Domoticz");
   
      String url = "/json.htm?username=";
      url += String(id);
      url += String("&password=");
      url += String(mdp);
      url += String("&type=command&param=udevice&idx=");
      url += String(idx);
      url += String("&nvalue=0&svalue=");
      url += String(Calc);
         
      sendDomoticz(url);
      }
   }
 }

void sendDomoticz(String url){
  Serial.print("connecting to ");
  Serial.println(host);
  Serial.print("Requesting URL: ");
  Serial.print(host);
  Serial.print(":");
  Serial.print(port);
  Serial.println(url);
  http.begin(host,port,url);
  int httpCode = http.GET();
  Serial.println(httpCode);
    if (httpCode) {
      if (httpCode == 200) {
        String payload = http.getString();
        Serial.println("Domoticz response "); 
        Serial.println(payload);
      }
    }
  Serial.println("closing connection");
  http.end();
}
Et c'est là que ça coince...

Comme toujours, le httpcode retourne -1....

Je ne comprends pas.

Pouvez vous m'aider ?

Merci.

PS: concernant la valeur de l'idx c'est normal j'avais laissé longtemps enregistrer des capteurs Rf...
Répondre