ESTOU TENTANDO USAR UM TEMPORIZADOR MILLIS OU OUTRO MAS NAO FUNCIONA QUANDO COLOCO NO CODIGO WIFIWEBSERVE E SOU NOVATO EM ARDUINO E ESP, E SE ALGUEM TIVER UM TEMPORIZADOR MELHOR SERIA BOM??
#include <ESP8266WiFi.h>
#ifndef STASSID
#define STASSID "KNUPFER"
#define STAPSK "0800.com"
#define pino1 D3
#define pino2 D4
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
int RELE1 = 2;
int RELE2 = 2;
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
Serial.begin(115200);
// RELE LIGA DESLIGA
pinMode (pino1, OUTPUT);
pinMode (pino2, OUTPUT);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print(F("Connecting to "));
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi connected"));
// Start the server
server.begin();
Serial.println(F("Server started"));
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.accept();
if (!client) { return; }
Serial.println(F("new client"));
client.setTimeout(5000); // default is 1000
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(F("request: "));
Serial.println(req);
//RELE 1 FUNCINANDO recebe dados app////////////////////
if (req.indexOf(F("/RELE1/ON")) != -1) {
RELE1 =1;
}
if (req.indexOf(F("/RELE1/OFF")) != -1) {
RELE1 =2;
}
//RELE 2 RECEBE DADOS///////////////////////////
if (req.indexOf(F("/RELE2/ON")) != -1) {
RELE2 =1;
}
if (req.indexOf(F("/RELE2/OFF")) != -1) {
RELE2 =2;
}
//aplica dados RELE 1//////////////////////////
if (RELE1 == 1){
digitalWrite( pino1, HIGH);
}
if (RELE1 == 2) {
digitalWrite( pino1, LOW);
}
////////////////////////////////////
//aplica dados RELE 2//////////////////////////
if (RELE2 == 1){
digitalWrite( pino2, HIGH);
}
if (RELE2 == 2) {
digitalWrite( pino2, LOW);
}
/////////////////////////////////////
// read/ignore the rest of the request
// do not client.flush(): it is for output only, see below
while (client.available()) {
// byte by byte is not very efficient
client.read();
}
// Send the response to the client
// it is OK for multiple small client.print/write,
// because nagle algorithm will group them into one single packet
client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nRAI SERVER "));
client.print(F("<br><br>Click <a href='http://"));
client.print(WiFi.localIP());
client.print(F("/gpio/1'>here</a> to switch LED GPIO on, or <a href='http://"));
client.print(WiFi.localIP());
client.print(F("/gpio/0'>here</a> to switch LED GPIO off.</html>"));
// client.print(type="text/javascript"> var teste = "99");
//client.print("<h1><center>" + String(xx) + "</center></h1>");
// The client will actually be *flushed* then disconnected
// when the function returns and 'client' object is destroyed (out-of-scope)
// flush = ensure written data are received by the other side
Serial.println(F("Disconnecting from client"));
///////////TESTE ENVIO
}