bportaluri / WiFiEsp

Arduino WiFi library for ESP8266 modules
GNU General Public License v3.0
548 stars 210 forks source link

Unable to connect to server IP via browser #190

Open vtolenti89 opened 4 years ago

vtolenti89 commented 4 years ago

Hello,

I've just uploaded the WebServerAp.ino to an arduino connected to a ESP8266. The connection to my network seems to work alright, and the server starts and provides me with an Ip. However, whenever I try to access this ip in the browser, it times out.

Here is the code I am using:

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(3, 2); // RX, TX
#endif

char ssid[] = "-----";            // your network SSID (name)
char pass[] = "-----";        // your network password
int status = WL_IDLE_STATUS;
int reqCount = 0;                // number of requests received

WiFiEspServer server(80);

// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);

void setup()
{
  Serial.begin(115200);   // initialize serial for debugging
  Serial1.begin(9600);    // initialize serial for ESP module
  WiFi.init(&Serial1);    // initialize ESP module

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true); // don't continue
  }

  Serial.print("Attempting to start AP ");
  Serial.println(ssid);

  // uncomment these two lines if you want to set the IP address of the AP
  //IPAddress localIp(192, 168, 111, 111);
  //WiFi.configAP(localIp);

  // start access point
  status = WiFi.beginAP(ssid, 10, pass, ENC_TYPE_WPA2_PSK);

  Serial.println("Access point started");
  printWifiStatus();

  // start the web server on port 80
  server.begin();
  Serial.println("Server started");
}

void loop()
{
  WiFiEspClient client = server.available();  // listen for incoming clients

  if (client) {                               // if you get a client,
    Serial.println("New client");             // print a message out the serial port
    buf.init();                               // initialize the circular buffer
    while (client.connected()) {              // loop while the client's connected
      if (client.available()) {               // if there's bytes to read from the client,
        char c = client.read();               // read a byte, then
        buf.push(c);                          // push it to the ring buffer

        // you got two newline characters in a row
        // that's the end of the HTTP request, so send a response
        if (buf.endsWith("\r\n\r\n")) {
          sendHttpResponse(client);
          break;
        }
      }
    }

    // give the web browser time to receive the data
    delay(10);

    // close the connection
    client.stop();
    Serial.println("Client disconnected");
  }
}

void sendHttpResponse(WiFiEspClient client)
{
  client.print(
    "HTTP/1.1 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Connection: close\r\n"  // the connection will be closed after completion of the response
    "Refresh: 20\r\n"        // refresh the page automatically every 20 sec
    "\r\n");
  client.print("<!DOCTYPE HTML>\r\n");
  client.print("<html>\r\n");
  client.print("<h1>Hello World!</h1>\r\n");
  client.print("Requests received: ");
  client.print(++reqCount);
  client.print("<br>\r\n");
  client.print("Analog input A0: ");
  client.print(analogRead(0));
  client.print("<br>\r\n");
  client.print("</html>\r\n");
}

void printWifiStatus()
{
  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print where to go in the browser
  Serial.println();
  Serial.print("To see this page in action, connect to ");
  Serial.print(ssid);
  Serial.print(" and open a browser to http://");
  Serial.println(ip);
  Serial.println();
}

And this is what the serial console shows:

14:03:53.141 -> [WiFiEsp] Server started on port 80
14:04:49.234 -> [WiFiEsp] Initializing ESP module
14:04:52.902 -> [WiFiEsp] Initilization successful - 2.0.0
14:04:52.936 -> Attempting to start AP ------
14:04:53.924 -> [WiFiEsp] Access point started --------
14:04:53.958 -> Access point started
14:04:54.026 -> IP Address: 192.168.4.1
14:04:54.026 -> 
14:04:54.026 -> To see this page in action, connect to ----- and open a browser to http://192.168.4.1
14:04:54.060 -> 
14:04:54.060 -> [WiFiEsp] Server started on port 80
14:04:54.060 -> Server started

Whenever I try to navigate to http://192.168.4.1, the page cannot be found. Any ideas what went wrong?

Thanks in advance.

JAndrassy commented 4 years ago

did you connect the WiFi of your computer to the SSID of the esp8266 SoftAP?

vtolenti89 commented 4 years ago

Yes, they are in the same network. I also tried with my phone, to no avail.

vtolenti89 commented 4 years ago

I just tried the example WebServerLed.ino and it seems to work fine. I did not go through the source code, but I assume there is a difference between Wifi.beginAP() and Wifi.begin(), where the latter is used in WebServerLed.ino.

Do you know whether it is also possible to set a fixed ip address for the Wifi.begin() method? For the Wifi.beginAp() it is done via:

IPAddress localIp(192, 168, 111, 111);
WiFi.configAP(localIp);
vtolenti89 commented 4 years ago

Finally I figured out what was "wrong". I just failed to understand the difference between both methods. For the AP one, I thought that the ssid to be inserted was the my Wifi's one, which makes no sense. Now everything works perfectly.

JAndrassy commented 4 years ago

WiFi.config(ip);

and take a look at https://github.com/jandrassy/WiFiEspAT#Why-a-new-wifiesp-library

vtolenti89 commented 4 years ago

Thanks for the reply @jandrassy. Is it necessary though, to disable DCHP in my modem and make sure that the given static ip address matches the one from the modem?

JAndrassy commented 4 years ago

WiFi.config is AT+CIPSTA. this command disables the DHCP client