Hi,
I've looked all over the web, and what I found makes me think what I have should work, but it doesn't.
My project is to activate a ball valve using D1 Wemos Mini ESP8266 to cut the hot water supply (4 daugthers and 1 son).
In a nutshell:
- Valve works when the "limit switches" are not in the sketch. But this is not viable as the ball valve is activated far too long.
- Valve works for only like 1 seconds if the "limit switches" are used in the sketch. Then it waits for like 10 seconds, and move for another 1 seconds. Repeats until it is in position. But only sometimes. Sometimes it does not work at all.
The long story:
The ball valve has five wires:
- 2 wires for the 5VDC motor
- 3 wires to tell the state of the valve (fully opened or fully closed). So one common, and the common is connected internally in the valve to one of the other wires depending if the valve is fully closed or fully opened. Lets call those 3 wires the two "limit switches".
I connected 3.3 V of D1 Wemos to the common wire of the "limit switches". Then the other two wires go to my pins D5 and D6 which are set as inputs.
If I do not use the "limit switches" in my code, it works. I can activate the valve via my internet browser. But the valve is kept activated for like 20-25 seconds, which is far too long. (As a matter of fact, I do not understand why it is only activated 20-25 seconds. To my understanding, if I do not use the "limit switches", the valve should be kept activated by the board.)
The whole sketch that does not use the limit switches is:
//This example will use a static IP to control the switching of a relay. Over LAN using a web browser.
//A lot of this code have been resued from the example on the ESP8266 Learning Webpage below.
//http://www.esp8266learning.com/wemos-webserver-example.php
//CODE START
//1
#include <ESP8266WiFi.h>
// Below you will need to use your own WIFI informaiton.
//2
const char* ssid = "*******"; //WIFI Name, WeMo will only connect to a 2.4GHz network.
const char* password = "*******"; //WIFI Password
//defining the pin and setting up the "server"
//3
int openPin = D1;
int closePin = D2;
int valveOpenedPin = D5;
int valveClosedPin = D6;
WiFiServer server(80);
IPAddress ip(192, 168, 1, 199); // where xx is the desired IP Address
IPAddress gateway(192, 168, 1, 1); // set gateway to match your network
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
// void setup is where we initialize variables, pin modes, start using libraries, etc.
//The setup function will only run once, after each powerup or reset of the wemos board.
//4
void setup() {
 Serial.begin(115200);
 delay(10);
pinMode(openPin, OUTPUT);
pinMode(closePin, OUTPUT);
pinMode(valveOpenedPin, INPUT);
pinMode(valveClosedPin, INPUT);
digitalWrite(openPin, LOW);
digitalWrite(closePin, LOW);
 Serial.print(F("Setting static ip to : "));
 Serial.println(ip);
Â
 // Connect to WiFi network
 //5
 Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);
 WiFi.config(ip, gateway, subnet);
 WiFi.begin(ssid, password);
 //Trying to connect it will display dots
 while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected");
 // Start the server
 server.begin();
 Serial.println("Server started");
 // Print the IP address
 Serial.print("Use this URL : ");
 Serial.print("http://");
 Serial.print(WiFi.localIP());
 Serial.println("/");
}
//void loop is where you put all your code. it is a funtion that returns nothing and will repeat over and over again
//6
void loop() {
 // Check if a client has connected
 WiFiClient client = server.available();
 if (!client) {
  return;
 }
 // Wait until the client sends some data
 Serial.println("new client");
 while(!client.available()){
  delay(1);
 }
 // Read the first line of the request
 String request = client.readStringUntil('\r');
 Serial.println(request);
 client.flush();
 //Match the request, checking to see what the currect state is
 int value = LOW;
 if (request.indexOf("/relay=ON") != -1) {
  digitalWrite(closePin, LOW);
  digitalWrite(openPin, HIGH);
  value = HIGH;
 }
 if (request.indexOf("/relay=OFF") != -1){
  digitalWrite(openPin, LOW);
  digitalWrite(closePin, HIGH);
  value = LOW;
 }
 // Return the response, build the html page
 //7
 client.println("HTTP/1.1 200 OK");
 client.println("Content-Type: text/html");
 client.println(""); // do not forget this one
 client.println("<!DOCTYPE HTML>");
 client.println("<html>");
 if(value == HIGH) {
  client.print("Il y a de l'eau chaude");Â
 } else {
  client.print("L'eau chaude est fermée!!!");
 }
 client.println("
");
 client.println("<a href=\"/relay=ON\">Ouvrir l'eau chaude.</a>
");
 client.println("<a href=\"/relay=OFF\">Fermer l'eau chaude.</a>
");
 client.println("</html>");
 delay(1);
 Serial.println("Client disconnected");
 Serial.println("");
}//END
Within this code, the lines that control the valve are:
 if (request.indexOf("/relay=ON") != -1) {
  digitalWrite(closePin, LOW);
  digitalWrite(openPin, HIGH);
  value = HIGH;
 }
 if (request.indexOf("/relay=OFF") != -1){
  digitalWrite(openPin, LOW);
  digitalWrite(closePin, HIGH);
  value = LOW;
 }
Now, using the "limit switches" I changed the section that control the valve to:
 if (request.indexOf("/relay=ON") != -1) {
  while (digitalRead(valveOpenedPin) == LOW) {
   digitalWrite(closePin, LOW);
   digitalWrite(openPin, HIGH);
  }
   digitalWrite(openPin, LOW);
  value = HIGH;
 }
 if (request.indexOf("/relay=OFF") != -1) {
  while (digitalRead(valveClosedPin) == LOW) {
   digitalWrite(openPin, LOW);
   digitalWrite(closePin, HIGH);
  }
   digitalWrite(closePin, LOW);
  value = LOW;
 }
The way I read my second sketch that incorporates my "limit swithes" is:
- If I switch the relay ON to open the ball valve,
- As long as pin D5 (valveOpenedPin) does not receive 3.3 V;
- The ball valve will be activated through pin D1 (openPin);
The thing is the ball valve is only activated for something like 1 second. A complete cycle of the valve is around 5 seconds.
Could you help me on that please?
Thanks guys!