Hola a todos! me llamo Guillermo y desde hace muchos años he realizado varios proyectos con Arduino. Ahora estoy luchando en un proyectito para activar un interruptor domotico que cuenta con un server ESP8266 que recibe un comando GET /on o GET /off. El server funciona perfectamente con un cliente -no se como este hecho- Si necesitais el codigo de la recepcion del server es el siguiente.
/*** Start the web sever ***/
ESP8266WebServer server(80);
/*** routine to send on connect to web server ***/
void handleRoot() {
server.send(200, "text/html", "<h1>You are connected</h1>");
}
int gpio13Led = 13;
int gpio12Relay = 12;
/**** Initialise ***/
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
Serial.print("Setting soft-AP SSID & Pwd... ");
Serial.println(WiFi.softAP(ssid, password) ? "Ready" : "Failed!");
/*** Debug config OK ***/
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
pinMode(gpio13Led, OUTPUT);
digitalWrite(gpio13Led, HIGH);
pinMode(gpio12Relay, OUTPUT);
digitalWrite(gpio12Relay, LOW);
/*** Routines to handle lamp control commands ***/
/* "/on" = switch on */
server.on("/on", [](){
digitalWrite(gpio13Led, LOW);
digitalWrite(gpio12Relay, HIGH);
delay(500);
});
/* "/off" = switch off */
server.on("/off", [](){
digitalWrite(gpio13Led, HIGH);
digitalWrite(gpio12Relay, LOW);
delay(500);
});
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
Lo que estoy tratando de usar, lado cliente es ni mas ni menos que el ejemplo WIFIClient con pequeñas modificaciones. Esto:
/*
This sketch establishes a TCP connection to a "quote of the day" service.
It sends a "hello" message, and then prints received data.
*/
#include <ESP8266WiFi.h>
const char* host = "10.0.0.1";
const uint16_t port = 80;
void setup() {
Serial.begin(115200);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
boolean Flag= false;
void loop() {
static bool wait = false;
Serial.print("connecting to ");
Serial.print(host);
Serial.print(':');
Serial.println(port);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
delay(1000);
return;
}
// This will send a string to the server
Serial.println("sending data to server");
if (client.connected()) {
if(!Flag) {
client.print("GET /on HTTP/1.1");
Serial.println("GET /on HTTP/1.1");
// Close the connection
Serial.println("closing connection");
client.stop();
}
else {
client.print("GET /off HTTP/1.1");
Serial.println("GET /off HTTP/1.1");
// Close the connection
Serial.println("closing connection");
client.stop();
}
}
Flag = !Flag;
// // wait for data to be available
// unsigned long timeout = millis();
// while (client.available() == 0) {
// if (millis() - timeout > 5000) {
// Serial.println(">>> Client Timeout !");
// client.stop();
// delay(60000);
// return;
// }
// }
// // Read all the lines of the reply from server and print them to Serial
// Serial.println("receiving from remote server");
// // not testing 'client.connected()' since we do not need to send data here
// while (client.available()) {
// char ch = static_cast<char>(client.read());
// Serial.print(ch);
// }
if (wait) {
delay(30000); // execute once every 5 minutes, don't flood remote service
}
wait = true;
}
Ahora bien, el server responde a los comandos GET /on y GET / off pero con un retardo de cerca 8 segundos y no en modo instantaneo!
Me podeis orientar para hacer que el server reciba y actue immediatamente? Donde me equivoco?
Muchisimas gracias