I'm using the esp8266's webserver code from the eamples library but I can't access it from the browser in Winodows systems by IP I set.
in the serial monitor I get :
Connecting to Kobi
.
WiFi connected
Server started
10.0.0.13
new client
GET / HTTP/1.1
invalid request
"
Here is my code, strongly based on the build in example except the static IP that I integrated :
/*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a GPIO pin depending on the request
* http://server_ip/gpio/0 will set the GPIO2 low,
* http://server_ip/gpio/1 will set the GPIO2 high
* server_ip is the IP address of the ESP8266 module, will be
* printed to Serial when the module is connected.
*/
#include <ESP8266WiFi.h>
const char* ssid = "Kobi";
const char* password = "mypassword";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
// config static IP
IPAddress ip(10, 0, 0, 13); // where xx is the desired IP Address
IPAddress gateway(10, 0, 0, 138); // set gateway to match your network
Serial.print(F("Setting static ip to : "));
Serial.println(ip);
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your
//network
WiFi.config(ip, gateway, subnet);
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
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.println(WiFi.localIP());
}
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 req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val;
if (req.indexOf("/gpio/0") != -1)
val = 0;
else if (req.indexOf("/gpio/1") != -1)
val = 1;
else {
Serial.println("invalid request");
client.stop();
return;
}
// Set GPIO2 according to the request
digitalWrite(2, val);
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
s += (val)?"high":"low";
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
"Connecting to Kobi
.
WiFi connected
Server started
10.0.0.13
new client
GET /gpio/0 HTTP/1.1
Client disonnected"
and in the web :
"GPIO is now low"
Is this address I suppose to write in the browser ? How do you know it ? If so, why I'm not able to switch the pin (and so the led connected to the pin) on and off (that's the purpose of this project) ?
why does the client is "connecting" and then "disconnecting" ?
Is this address I suppose to write in the browser ? How do you know it ? If so, why I'm not able to switch the pin (and so the led connected to the pin) on and off (that's the purpose of this project) ?
You discover the URL that is required to control the led by looking at the code you wrote:
if (req.indexOf("/gpio/0") != -1)
. . .
contains the relevant part of the URL to turn off the LED.
You have connected a LED to pin GPIO2 and wired it with the correct polarity and via a current limiting resistor ?
OK, I got it
I was looking for the example with a button that you can press on and then the led is switched on and off , that's why I didn't realized why I can't see thatץ
Do you know where can I get this code ? with the switch buttons
You want to press a "button" on device A that activates a LED on device B.
Device B is presumably the ESP8266 that you have successfully installed a web server on.
But what sort of button are you talking about ? A physical tactile switch, for example or a hot spot or clickable element on a web page ?
I want to make a simple "smart home" application which is controlled from a web server and by the esp8266 device. I am going to connect devices (such as lamps etc) through relays to the different gpio pins of the ESP in order to control them. For that I want a GUI for the web like "on" and "off" switch for lamp 1 connected to gpio 5 (for example) and "on" and "off" switch for lamp 2 connected to gpio3 etc... Is there any GUI example ready to use ? Or any such code ?
There is a lot of examples. Google for: esp8266 web server switch led
You are already using the Arduino Core software on your ESP device, so ignore any examples which use 'AT' commands.
Here is one.