PROJECT BACKGROUND
My home irrigation system consists of a system of 12 water circuits, each of which is controlled by a solenoid operated valve. The valves, in turn are controlled by an electrical controller/timer which is programmed to run the system at night. Most of the water distribution points are drippers.
A problem occurs when occasionally some dirt will lodge in the seat of one of the valves, causing the valve to leak water (under pressure) until detection and correction. It can be several days before the problem can be detected.
SOLUTION
My solution was to install a water flow sensor in the main water line and to check the system flow rate each morning when there should be no water flowing through the system. I initially connected the sensor to a small OLED display to ensure its operation. The problem was that the display had to be located in an inaccessible part of my garage. So I modified the system to transmit the data to a web site via my WiFi network. For this I used an ESP8266-01S microprocessor directly rather than my Arduino Uno. Voltage supplied to the ESP8266 is 3.3v. This worked well until I encountered the following problem.
PROBLEM
The water flow sensor is a Hall Effect type sensor. When water flows through the pipe a series of electrical pulses in the form of a square wave is transmitted. The frequency of these pulses is a direct function of the flow rate of the water. The operational power to the sensor is 5 volts. When water is flowing, the voltage in the data line is HIGH at 2.5 volts. When no water is flowing the data lead voltage should be zero.
My sketch uses the "pulsein" function to interpret the data. The "pulsein" function waits for the signal to rise to HIGH, then measures the duration of the HIGH portion of the pulse and returns that value. If the signal does not go HIGH after a certain period of time (as in zero flow), "pulsein" returns a value of zero. And all should be cool.
THE ISSUE
While water is flowing through the sensor, the data line voltage is about 2.5 volts as stated above. But when the water flow ceases, the voltage does not always drop to zero; instead it sometimes goes to a high of 4.9 volts. This apparently posed no problem for the Uno/OLED setup, but the high voltage caused my ESP8266 to "go ape".
SOLUTION WANTED
Is there any way by which I can salvage my project by either a circuit modification or a sketch code change, or a combination of both?
I have attached a circuit diagram as well as my sketch code. Please note that I have a 60 minute delay embedded in the void loop.
/*
H2O_Flo_WiFiMeasures the flow rate of water flowing through a 3/4-inch PVC pipe using a DIGITEN Hall effect sensor and displaying results on thingspeak.com using an ESP8266-01S
Sensor date wire connected to Uno pin D2
The sensor output is in the form of the duration in milliseconds of a square wave pulse and the relationship to the water flowrate is:F = 4.8 * Q ( in liters per minute)
*/
#include <ESP8266WiFi.h>// WiFi SSD and Password
char ssid[] = "tracy";
char password[] = "Mange161";
char server[] = "api.thingspeak.com";
//Initialize the client library
WiFiClient client;#include <SPI.h>
const int sensdatapin = 2;
String apiKey = "0ZUS9CDG08ZU6VJ2";
unsigned long myChannelNumber = 255517;
void setup() {
unsigned long duration;
float dursec;
float pulsefreq;
float flolit;
float flolitr;
float flogal;
float flocuft;pinMode(sensdatapin, INPUT);
Serial.begin(115200);
delay(10);
// init done
WiFi.begin(ssid, password);
Serial.println();
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");}
void loop() {
const unsigned long duration = pulseIn(sensdatapin, HIGH);
//Pulse duration in microsecondsfloat dursec = duration / 1000000.0;
//Pulse duration in secondsfloat pulsefreq = 10.0;
if ( duration != 0 )
{
pulsefreq = 1.0 / dursec;
}
else
{
pulsefreq = 0.0;
}//Pulse frequency in Hz
float flolit = (pulsefreq / 4.8);
float flolitr = 60.0 * flolit;
//Flow rate in Liters/hourfloat flogal = flolitr * .26417;
//Flow rate in Gallons/hour
float flocuft = flolitr * .035315;
//Flow rate in cu ft/hourif (client.connect(server, 80)) { // "184.106.153.149" or api.thingspeak.com
String postStr = apiKey;
postStr += "&field1=";
postStr += String(flogal);
postStr += "\r\n\r\n";client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");client.println(postStr);
Serial.print(flogal);
Serial.print("Gal/hr");}
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.delay(3600000);
// 3600000 = 1 hour
// ThingSpeak will only accept updates every 15 seconds.}
WiFi-2 Schematic.pdf (112 KB)