Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Larsen
#55554
mrburnette wrote:... can you ping the ESP8266 at the DHCP address?

If I run the ESP8266 in STD-mode, I cannot ping it on the DHCP address my router has assigned for it, but if I run the ESP8266 in AP-mode, I can ping it on the DHCP address if I connect my computer to the ESP8266 AP.

mrburnette wrote:... Your ESP8266 webserver is going to need to be set to a subnet address within the home AP subnet scope but not one that will be used by DHCP... in other words, you are going to have to limit the DHCP assignment scope to allow for a band of static assigned addresses ... then assign your webserver to one of those manual assignable addresses.


I have setup my router up to assign DHCP addresses in the range 192.168.15.100 to 192.168.15.149. To be sure that the ESP8266 always has the same address I have reserved the address 192.168.15.124 for it in the routers DHCP setup.

If I understand you correctly, I should use a static IP address outside the DHCP range - for instance 192.168.15.150 for the EPS8266.

I have tried this my adding the code:

Code: Select allIPAddress ip(192,168,15,150);
IPAddress gateway(192,168,15,1);
IPAddress subnet(255,255,255,0);

WiFi.config(ip, gateway, subnet);


but I cannot ping 192.168.15.150.

Maybe I did not understand your suggestion correctly...?
User avatar
By Larsen
#55756 Hi again,

I have found out why I could not connect to the Web Server :D

In void loop() there was this condition before reading the web request:
Code: Select all  while(!client.available())
  {
    delay(1);
  }


After looking at other Web Server examples I found out, that I also needed to check if the client is connected, so the code should be:

Code: Select all  while(client.connected() && !client.available())
  {
    delay(1);
  }


Just to sum all the code up, it is here with the change mentioned above:

Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "your-ssid";
const char* password = "your-password";

int ledPin = 2; // GPIO2

WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  WiFi.mode(WIFI_STA);          // WiFi.mode(WIFI_AP);

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

// 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.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
}

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.connected() && !client.available())
  {
    delay(1);
  }

  // Read the first line of the request
  String request = client.readStringUntil(‘\r’);
  Serial.println(request);
  client.flush();

  // Match the request
  int value = LOW;
  if (request.indexOf("/LED=ON") != -1) {
    digitalWrite(ledPin, HIGH);
    value = HIGH;
  }
  if (request.indexOf("/LED=OFF") != -1) {
    digitalWrite(ledPin, LOW);
    value = LOW;
  }

  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); // do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");

  client.print("Led pin is now: ");

  if(value == HIGH) {
    client.print("On");
  } else {
    client.print("Off");
  }
  client.println("<br><br>");
  client.println("Click <a href=\"/LED=ON\">here</a> turn the LED on pin 2 ON<br>");
  client.println("Click <a href=\"/LED=OFF\">here</a> turn the LED on pin 2 OFF<br>");
  client.println("</html>");

  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
}



Thank to you guys who spend time giving me suggestion to solve this problem ;)

Regards
Larsen