Water usage and Valve control system Over wifi

Hello,

I am currently working on a project that monitors water usage using Hall effect water flow sensor and when there are abnormalities in the usage the solenoid valve would automatically close. Now, the problem is I want to monitor the water usage and control the solenoid valve online, and I cannot seem to connect to the internet. I am having problem with the esp8266 I cant seem to make it work, And I cant find any code for reference regarding sending data and controlling valves.

Any tips on how to make this work?

Materials used:
Arduino Mega 2560
4 x Hall-effect water Flow sensor
4 x Solenoid valve
3 x Water Pressure sensor
Esp8266

Here's my mediocre code:


void setup()
{
  Serial.begin(9600);        // open serial port, set the baud rate to 9600 bps
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), flow1, RISING);  //DIGITAL Pin 2: Interrupt 0
  attachInterrupt(digitalPinToInterrupt(3), flow2, RISING);  //DIGITAL Pin 3: Interrupt 1
//  attachInterrupt(2, flow, RISING);  //DIGITAL Pin 21: Interrupt 2
//  attachInterrupt(3, flow, RISING);  //DIGITAL Pin 20: Interrupt 3
   sei(); // Enable interrupts
 
  
}
void loop()
{
static unsigned long cloopTime = millis();
if (millis() - cloopTime >= 100UL){
  cloopTime += 1000UL;
  water_Flow(2); 
  water_Flow(3);
}
//  PressureSensor(0);
//  PressureSensor(1);
//  SolenoidValve(3);

}


void SolenoidValve(int m_pin) {
//  pinMode(m_pin, OUTPUT);
//  off();
   pinMode(m_pin, OUTPUT); // solenoid valve 
  if(Serial.read()=='o' ){

     digitalWrite(m_pin, 1);   //Turn on 
//     m_state = true; 
     Serial.println("close");
//     delay(100);
  }
   if(Serial.read()=='f'){

     digitalWrite(m_pin, 0);   //Turn off   
//     m_state = false;
     Serial.println("open");
//     delay(100);
  }
}



const float  OffSet = 0.488 ;
float V, P;
void PressureSensor (int analogpin){
  //Connect sensor to Analog 0
  V = analogRead(analogpin) * 5.00 / 1024;     //Sensor output voltage
  P = (V - OffSet) * 400;             //Calculate water pressure

  Serial.print("WaterPressure: ");
  Serial.println(analogpin + 1);
  Serial.print("Voltage:");
  Serial.print(V, 3);
  Serial.println("V");
  Serial.print(" Pressure:");
  Serial.print(P, 1);
  Serial.println(" KPa");
  Serial.println();

  delay(500);
  
}
volatile unsigned long flow_frequency1 = 0;
volatile unsigned long flow_frequency2 = 0;


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

void water_Flow(bool flowsensor){
  static unsigned long l_hour = 0;
  static unsigned long flow_frequency = 0;
  flow_frequency = flowsensor ? flow_frequency2 : flow_frequency1;
  flowsensor ? flow_frequency2 = 0 : flow_frequency1 = 0;
 
  // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
  l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour

  Serial.print( "WaterFLow: ");
  Serial.println(flowsensor + 1);
  Serial.print(l_hour); // Print litres/hour
  Serial.println(" L/hour");
     
   }

My project looks like this

this is rather vauge - have you tested the ESP8266 by itself with some basic programs, e.g. a simple webserver, etc to check it can connect to you local network etc
how is the ESP8266 connected to the Mega? e.g. upload a schemtic showing connections

sorry for being vague :sweat_smile:. I am still trying to work my esp8266 with some basic programs but I am really having a hard time. I just laid out my project to explain my plan properly but it made it confusing hahahaha sorry my bad :laughing:

I just laid out the code because I am confused on how to integrate the code of the esp8266.

did you try some of the 8266 examples
e.g. in IDE File>Examples>ESP8266WebServer>HelloSever
to get the hang of it working by itself?

how have you connected the Mega to the ESP8266, e.g. serial, ?

what do you want the esp8266 to do? e.g. run a webserver to display the state of your system?

as of now I am trying the examples to get the hang of it.
Yes I have connected the Mega to the Esp8266
I want to make an app to monitor the water usage and turn the solenoid valve on and off and I am thinking of doing it over wifi. But i am still figuring out on how to make this happen :laughing:

have a look at ESP8266 example: Wi-Fi Access point, static IP, web-server and remote GPIO control
how have you connected the Mega to the ESP8266? remember the Mega uses 5V logic the ESP8266 3.3V logic
do you require the Mega? the ESP8266 could probably run the whole project

yeah I need the mega since I need 4 water flow sensor so it needs multiple interrupt pins

is it possible to run the whole project using esp8266?
what should I do to make it?

I'm not using the nodemcu type of esp8266 I'm using the esp8266-01 :sweat_smile:

you would need to interface the sensor and solenoid value to the ESP8266 - how do you drive the solenoid valve, e.g. a relay?

the ESP8266-01 is very limited in the number of IO pins available
I also find having move the device between the programmer (to program it) and dev board (to run it) a pain - I tend to write code on an NodeMCU and then port it to the ESP-01 when working

Ps *I'm not using the logic level converter

this is my reference of what my project would look like. Is this feasible? or should I just use the node mcu ?

the reason why i'm not using the nodemcu because I am still new to arduino and I don't know how to wire the components to the nodemcu

at least you should use potential dividers on any signal from the Mega to the ESP or you could damage the ESP
how many solenoids do you have? what voltage do they require to drive them?
can you give links to datasheets on the sensors and solenoids?
you could probably do the whole project on a NodeMCU

I've got 4 solenoids and they require 12v each and i use mosfet and a diode to control it just like the circuito.io diagram showed

Yf-S201 (water flow sensor):

Water Pressure sensor:

Solenoid valve:( I dont know if this the link you're looking for)

the 3.3V ESP8266 probably would not drive the MOS FETs and would require an additional driver

as you already have the system working with the Mega and just require the webpage interface it may be simpler to add the EPS8266-01 communicating over a serial line
Edit: this photo shows an arduino Mega connected to an ESP-01

The Mega is a 5.0V device so a potential divider is required on the Tx line to the ESP Rx (yellow wire)

an alternative may be a Mega-WiFi_R3_ATmega2560_ESP8266 which is a mega with a builtin ESP8266 so the hardware interfaceing problems are removed - however, having to switch the DIL switches ON/OFF between loading and running code is awkward

It would certainly simplify matters to use the NodeMCU - if that is what you have.

If you do not have one, then get the cheaper WeMOS D1 Mini instead - it has all the same usable functionality.

Unfortunately, your schematic on "circuito.io" is indecipherable, but interfacing should be quite practical and the clumsy Mega 2560 would not be necessary.

can I use the bi-logical converter?

Hi,
You might be better off using an ESP32 to do all the work of the Mega and ESP01.

Tom.... :smiley: :+1: :coffee: :australia:

Yeah I just researched about it yesterday and it seemed much easier to use the nodemcu than the mega but I am pressed for time since I need to finish this by wednesday and usually the delivery time for the materials takes at least two weeks to arrive in my place