3

I am trying to reach the webserver running in my Wemos D1 Mini compatible board.

I am trying one esp8266 example using Arduino examples "WifiManualServer" code below. I have replaced the SSI and password for my wifi network in the lines

#define STASSID "my wifi ssid"
#define STAPSK  "my secret"
/*
    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>

#ifndef STASSID
#define STASSID "my wifi ssid"
#define STAPSK  "my secret"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

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

  // prepare LED
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 0);

  // 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());
  server = WiFiServer(80);
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  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);

  // Match the request
  int val;
  if (req.indexOf(F("/gpio/0")) != -1) {
    val = 0;
  } else if (req.indexOf(F("/gpio/1")) != -1) {
    val = 1;
  } else {
    Serial.println(F("invalid request"));
    val = digitalRead(LED_BUILTIN);
  }

  // Set LED according to the request
  digitalWrite(LED_BUILTIN, val);

  // 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\nGPIO is now "));
  client.print((val) ? F("high") : F("low"));
  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>"));

  // 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"));
}

Once I compile and upload the sketch, the sketch is able to connect to my wifi network. The following is the output in the serial monitor.

20:45:12.633 -> Connecting to <text replaced xxxxxxx>
20:45:13.247 -> ........
20:45:17.551 -> WiFi connected
20:45:17.551 -> Server started
20:45:17.551 -> 192.168.0.39

I am able to verify the client connection in my wifi routers network management UI. The espboard is connected and assigned the IP as in the output (192.168.0.39).

When I launch browser and try to get http://192.168.0.39/gpio/0, the request times out. from my windows 10 pc, tried ping on 192.168.0.39, the Request timed out.

From the sketch I see the following lines in the loop() function:

  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.println(F("new client"));

The execution never reached the line Serial.println(F("new client"));.

I had tried inserting the line server = WiFiServer(80); after the wifi connection is established.

Please share your thoughts on why the server.available() is not successful?

5
  • Is your windows 10 PC on the same subnet? Does your WiFi router impose WiFi Isolation?
    – Majenko
    Commented May 16, 2021 at 10:59
  • 1
    remove the line server = WiFiServer(80);
    – Juraj
    Commented May 16, 2021 at 11:28
  • I think you have to call server.on() somewhere so the server does actually serve an url. Which library do you use btw (there is nothing behind #include for me)?
    – Sim Son
    Commented May 16, 2021 at 12:12
  • @SimSon, this is not ESP8266WebServer library
    – Juraj
    Commented May 16, 2021 at 14:48
  • @Juraj, thanks for the suggestion. I have now removed the line server = WiFiServer(80). The issue is resovled. The server is now reachable. Thanks your help. Commented May 17, 2021 at 7:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.