Two sensors on same ESP-32 board

Hi everybody.

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.

Attached is the code I have where I added the flow_frequency2/3 and flowsensor2/3 but now the code does not work.

Can anybody help me out on this?

flow_mqtt.ino (4.96 KB)

You need three different topics. One for each sensor so you can tell the difference.

Please specify what does not work. What do you expect to happen?

You also want three different interrupt service routines to count each sensor separately.

Klaus_K:
Please specify what does not work. What do you expect to happen?

What does not work is the messages sent via MQTT. I open my terminal run a python script to see if I get the data but nothing shows with the code I attached on my initial post.

Klaus_K:
You need three different topics. One for each sensor so you can tell the difference.

You also want three different interrupt service routines to count each sensor separately.

Can you specify more on this two? I'm really a rookie in Arduino and can't quickly decipher what is said

jmdjac:
Can you specify more on this two? I'm really a rookie in Arduino and can't quickly decipher what is said

Your MQTT messages could look like this

topic: "sensor001/water"
value: 80
topic: "sensor002/water"
value: 50

Right now you you cannot tell the difference between the sensors.

attachInterrupt(34, flow, RISING); // this says if voltage at pin 34 rises run the function named flow

// This function increases all three variables by one every time any of the three sensor signal goes HIGH
// If water flows through sensor2 only you still measure water flow in 1 & 3
void flow () { // Interrupt function
  flow_frequency1++;
  flow_frequency2++;
  flow_frequency3++;
}


// It should look like this

attachInterrupt(34, flow1, RISING);

void flow1 () { // Interrupt function
  flow_frequency1++;
}

I also just realized, the following statement is executed only half the time.

if (digitalRead(flowsensor1) == HIGH) {}

This pin toggles between HIGH and LOW and you only read it when it is HIGH. If the pin is low every 5 seconds you will not get any result.

Also these lines should not be necessary and might even be bad.

digitalWrite(flowsensor1, HIGH); // Optional Internal Pull-Up
digitalWrite(flowsensor2, HIGH); // Optional Internal Pull-Up
digitalWrite(flowsensor3, HIGH); // Optional Internal Pull-Up

If the sensor drives the input LOW it will need to drive the voltage to ground against the internal pull-up.

Additionally, I recommend you do not send the water flow per hour but send the water value and never reset the value to zero. The reason is if you loose a MQTT message you will loose accuracy, but if you send the total value you will be able to recover. e.g.

1 + 1 + 1 + 1 = 4 if you loose any message the result is wrong
1,2, missing value, 4 the total value will be correct

Because you are using a wireless connection you will loose messages at some point.