Hola, buenas tardes…me encuentro con un pequeño problema y necesito ayuda…Estoy haciendo un contador de aceite que tiene una pistola surtidora, y se debe enviar lo que cuenta por WiFi…Tiene una relación de que cada 12 pulsos es 1 litro de aceite…Logro contar los pulsos sin ruido alguno…pero necesito que al menos dure 10 segundos esperando por si vuelven a presionar el gatillo de la pistola, espero que me puedan entender…aquí coloco mi código …Gracias!
#include <ESP8266WiFi.h>
#include <Bounce2.h>
#include <WiFiClient.h>
// Variables will change:
int buttonPin = D2;
int mL = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
float Litros_Consumidos = 0;
// Instantiate a Bounce object
Bounce debouncer = Bounce();
//-------------------VARIABLES GLOBALES--------------------------
int contconexion = 0;
String Hidraulico = "Hidraulico";
float Litros;
const char *ssid = "AMYMWIFIGALPON2";
const char *password = "Lafayette1954";
char host[48];
String strhost = "192.168.0.225";
String strurl = "/Cuchillito/Contador_Aceite.php";
String IDLolin = "";
//-------Función para Enviar Datos a la Base de Datos SQL--------
String enviardatos(String datos) {
String linea = "error";
WiFiClient client;
strhost.toCharArray(host, 49);
if (!client.connect(host, 80)) {
Serial.println("Fallo de conexion");
return linea;
}
client.print(String("POST ") + strurl + " HTTP/1.1" + "\r\n" +
"Host: " + strhost + "\r\n" +
"Accept: */*" + "*\r\n" +
"Content-Length: " + datos.length() + "\r\n" +
"Content-Type: application/x-www-form-urlencoded" + "\r\n" +
"\r\n" + datos);
delay(10);
Serial.print("Enviando datos...");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println("Cliente fuera de tiempo!");
client.stop();
return linea;
}
}
// Lee todas las lineas que recibe del servidro y las imprime por la terminal serial
while(client.available()){
linea = client.readStringUntil('\r');
}
Serial.println(linea);
return linea;
}
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT_PULLUP);
debouncer.attach(buttonPin);
debouncer.interval(5); // interval in ms
// initialize serial communication:
Serial.begin(9600);
Serial.println("");
if (contconexion <50) {
//para usar con ip fija
IPAddress ip(192,168,0,247);
IPAddress gateway(192,168,0,254);
IPAddress subnet(255,255,255,0);
WiFi.config(ip, gateway, subnet);
Serial.println("");
Serial.println("WiFi conectado");
Serial.println(WiFi.localIP());
}
else {
Serial.println("");
Serial.println("Error de conexion");
}
//////////////////////////////////////////////////////////////ID LoLiN/////////////////////////////////////////
Serial.print("IDLolin: ");
IDLolin = String(ESP.getChipId());
if(IDLolin != Hidraulico)
{
IDLolin = Hidraulico;
}
Serial.println(IDLolin);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Conexión WIFI
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED and contconexion <50) { //Cuenta hasta 50 si no se puede conectar lo cancela
++contconexion;
delay(500);
Serial.print(".");
}
}
void loop() {
// Get the updated value :
debouncer.update();
// read the pushbutton input pin:
buttonState = debouncer.read();
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
mL++;
//Serial.println("on");
Serial.print("mL: ");
Serial.println(mL);
}
// Delay a little bit to avoid bouncing
delay(100);
// save the current state as the last state, for next time through the loop
}
lastButtonState = buttonState;
}