Estaba haciendo un sensor para una puerta, necesitaba saber si esta cerrada o abierta, he buscado algun ejemplo y encontre uno lo que pasa no que va no puedo acceder a la web, entiendo que hay que abrir el puerto en cuestion pero al poner la ip en el navegador pero no se conecta, ademas buscado IPs conectadas a mi WIFI no sale esa me podeis mirar que hago mal.
Un saludo
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
const char* ssid ="***";
const char* password = "*****";
// Remote site information
const char hostname[] = "192.168.1.33";
const int port = 3000;
WiFiClient client;
// door sensor
const int doorPin = 0;
#define CLOSED 0
#define OPENED 1
int doorState = CLOSED;
void setup() {
WiFi.begin(ssid, password); // Conecta a nuestra red WiFi
}
void loop() {
if(digitalRead(doorPin) == HIGH && doorState == OPENED) {
doorState = CLOSED;
}
if(digitalRead(doorPin) == LOW && doorState == CLOSED) {
doorState = OPENED;
client.connect(hostname, port);
// do a HTTP get request
client.println("GET /door HTTP/1.1");
client.print("Host: ");
client.println(hostname);
client.println("Connection: close");
client.println();
}
}
Cuando te ocurre eso, olvida temporalmente el programa en el que estás y vuelve a los ejemplos disponibles y repasa la situación.
Busca el ejemplo base de ESP8266WiFi.h, verifica que todo funcione, hablo de lo básico, conectarse, ver la página con el navegador.
Una vez que tienes eso funcionando, haz una copia y agrega uno a uno los elementos de este código que has presentado y siempre asegúrate de que avances. NO lo hagas en un paso, sino progresivamente.
De esta forma darás con el error y entenderás como resolverlo.
// do a HTTP get request
client.println("GET /door HTTP/1.1");
client.print("Host: ");
client.println(hostname);
client.println("Connection: close");
client.println();
nada dicen de tu actividad de puerta.
Tal como lo planteas esta mal. Debe estar disponible en todo momento del loop y leer el estado del sensor. Cuando este cambia dicho cambio debe reflejarse en la información HTTP.
Mira el ejemplo, observa que ante un requirimiento HTTP, el sistema responde, pero cuando lo hace, devuelve tambien el estado del switch o sensor.
/*--------------------------------------------------------------
Program: eth_websrv_switch
Description: Arduino web server shows the state of a switch
on a web page. Does not use the SD card.
Hardware: Arduino Uno and official Arduino Ethernet
shield. Should work with other Arduinos and
compatible Ethernet shields.
Software: Developed using Arduino 1.0.3 software
Should be compatible with Arduino 1.0 +
References: - WebServer example by David A. Mellis and
modified by Tom Igoe
- Ethernet library documentation:
http://arduino.cc/en/Reference/Ethernet
Date: 12 January 2013
Author: W.A. Smith, http://startingelectronics.org
--------------------------------------------------------------*/
#include <SPI.h>
#include <Ethernet.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 0, 0, 20); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
pinMode(3, INPUT); // input pin for switch
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
// send web page
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino Read Switch State</title>");
client.println("<meta http-equiv=\"refresh\" content=\"1\">");
client.println("</head>");
client.println("<body>");
client.println("<h1>Switch</h1>");
client.println("<p>State of switch is:</p>");
GetSwitchState(client);
client.println("</body>");
client.println("</html>");
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
void GetSwitchState(EthernetClient cl)
{
if (digitalRead(3)) {
cl.println("<p>ON</p>");
}
else {
cl.println("<p>OFF</p>");
}
}
Estas son las lineas relevantes
client.println("<body>");
client.println("<h1>Switch</h1>");
client.println("<p>State of switch is:</p>");
GetSwitchState(client);
client.println("</body>");
Pero tambien observa que el sistema responde al requerimiento del cliente HTML, y no cuando el switch esta en un estado determinado lo que invalida informar el otro estado como en tu caso.