3

Need some help on the project I'm currently developing.

I have 3 YF-B2 sensors that I need to connect to the same ESP-32 board and read the data from the sensors and send it via MQTT.

I had it working for 1 sensor but now I want to add the other two and keep sending the messages via MQTT but now with 3 sensors and each with its different value.

Below is the code I have where I added the flow_frequency2/3 and flowsensor2/3 but now the code does not work and I do not get the messages via MQTT.

Can anybody help me out on this?

#include <PubSubClient.h>
#include <EEPROM.h>

/****** Replace the next variables with your SSID/Password combination ********/
const char* ssid = "xxx";
const char* password = "xxx";

/****** Add your MQTT Broker IP address, example: ********/
const char* mqtt_server = "xxx.com";
const char* mqtt_user = "aaa";
const char* mqtt_pass = "aaa";
const char* input_topic = "teste/xxxx/in";
const char* output_topic = "teste/xxxx/out";

/****** FLUXO DE ÁGUA-YF-B2 ********/
volatile int flow_frequency1; // Measures flow sensor pulses
volatile int flow_frequency2; // Measures flow sensor pulses
volatile int flow_frequency3; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowsensor1 = 34; // Sensor Input
unsigned char flowsensor2 = 35; // Sensor Input
unsigned char flowsensor3 = 32; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void flow () { // Interrupt function
  flow_frequency1++;
  flow_frequency2++;
  flow_frequency3++;
}

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

  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

  pinMode(flowsensor1, INPUT);
  pinMode(flowsensor2, INPUT);
  pinMode(flowsensor3, INPUT);
  digitalWrite(flowsensor1, HIGH); // Optional Internal Pull-Up
  digitalWrite(flowsensor2, HIGH); // Optional Internal Pull-Up
  digitalWrite(flowsensor3, HIGH); // Optional Internal Pull-Up
  attachInterrupt(34, flow, RISING);
  attachInterrupt(35, flow, RISING);
  attachInterrupt(32, flow, RISING);// Setup Interrupt
  sei(); // Enable interrupts
  currentTime = millis();
  cloopTime = currentTime;
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 5000) {
    lastMsg = now;

    if (digitalRead(flowsensor1) == HIGH) {
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
      l_hour = (flow_frequency1 * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
      flow_frequency1 = 0; // Reset Counter

      String water = String(l_hour);

      Serial.print("Water flow of sensor 1 is: " + water + " L/hour");
      water = water + " , waterflow1";
      client.publish(output_topic, water.c_str());
    }

    if (digitalRead(flowsensor2) == HIGH) {
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
      l_hour = (flow_frequency2 * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
      flow_frequency2 = 0; // Reset Counter

      String water = String(l_hour);

      Serial.print("Water flow of sensor 2 is: " + water + " L/hour");
      water = water + " , waterflow2";
      client.publish(output_topic, water.c_str());
    }

    if (digitalRead(flowsensor3) == HIGH) {
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
      l_hour = (flow_frequency3 * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
      flow_frequency3 = 0; // Reset Counter

      String water = String(l_hour);

      Serial.print("Water flow of sensor 3 is: " + water + " L/hour");
      water = water + " , waterflow3";
      client.publish(output_topic, water.c_str());
    }

  }
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;

  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  // Feel free to add more if statements to control more GPIOs with MQTT

  // If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
  // Changes the output state according to the message
  if (String(topic) == input_topic) {
    Serial.print("Changing output to ");
    if (messageTemp == "on") {
      Serial.println("on");
      //digitalWrite(ledPin, HIGH);
    }
    else if (messageTemp == "off") {
      Serial.println("off");
      //digitalWrite(ledPin, LOW);
    }
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client", mqtt_user, mqtt_pass)) {
      Serial.println("connected");
      // Subscribe
      client.subscribe(input_topic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
 ´´´
1
  • use an array for the flow sensor pins ... use flowsensor[1] instead of flowsensor1 ... then you can use a for loop to iterate through the sensor setup and sensor read – jsotola Sep 23 '20 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.