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

Moderator: igrr

User avatar
By OGMrStinky
#22019 I have two ESP8266 modules. One is set up as access point/server and one as a client. I can access the AP/server from a laptop and request/receive a page successfully but when I try to connect with the ESP8266, it connects to the AP but connecting the client fails. Is there a limitation I've missed seeing that prevents this?
I've tried the client device configured in both WIFI_STA and WIFI_AP_STA.
I am using IPAddress host(192,168,4,1) on the client.
Has anyone done this?
Thanks!
User avatar
By Stoney
#22069 yeh i have mine connecting ok.
but .. i find if i have an ipad connected, then i cannot get the esp to connect at the same time. yet ..
tell your phones, ipads, laptops etc to forget the AP and see how you go.
tablets also do annoying things like request the favicon.gif image for the browser tab, if you get unsolicited requests just close the connection instantly.
User avatar
By OGMrStinky
#22107 Below is the code running on my AP/server device and my client device. I always get "connection failed" when the client tries to connect to the host/port. Anyone see what would be preventing the connection? Thanks in advance.

Code: Select all#include <ESP8266WiFi.h>

const char *ssid = "ESPap";

WiFiServer server(80);

void setup() {
   delay(1000);
   Serial.begin(115200);
   Serial.println();
   Serial.print("Configuring access point...");

   WiFi.softAP(ssid);

   while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }

   Serial.println("done");
   IPAddress myIP = WiFi.softAPIP();
   Serial.print("AP IP address: ");
   Serial.println(myIP);

   server.begin();
   Serial.println("HTTP server started");
}

void loop() {

}


and

Code: Select all#include <ESP8266WiFi.h>

const char* ssid     = "ESPap";

IPAddress host(192,168,4,1);

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

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {
  delay(5000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
 
  client.print(String("GET /test/page.html HTTP/1.1\r\n") +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(10);
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("closing connection");
}
User avatar
By Stoney
#22116 well I can see a few bits missing. not sure how important they all are though..
this works, I have this as the guts in my server end.

Code: Select all
global variables ...

MDNSResponder mdns;
WiFiServer server(80);



void startAP()
{
  WiFi.disconnect();
  delay(100);
  WiFi.mode(WIFI_STA);
  WiFi.softAP(ssid,mypass);
  delay(500);
  Serial.print("Local server only @ ");
  Serial.println(WiFi.localIP());
 
  server.begin();
  Serial.println("Server started");

}


and in a function called by the loop code


  mdns.update();   // Check for any mDNS queries and send responses

  WiFiClient client = server.available();  // Check if a client has connected
  if (!client) {
    return (NOCLIENT);
  }

  Serial.println("");
  Serial.println("New client");

  // Wait for data from client to become available
  while (client.connected() && !client.available()) {
    delay(1);
  }


  // Read the first line of HTTP request
  String req = client.readStringUntil('\r');



and this is the guts of the client..

Code: Select all
globals ..

MDNSResponder mdns;
WiFiServer server(80);



void startSTA()
{
  WiFi.disconnect();
  delay(100);
  WiFi.mode(WIFI_STA);
  WiFi.begin("THE_SERVER", "thisIsAPassword");

  while ( WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println(WiFi.status());
  }

  Serial.println("WIFI connected");
  Serial.println(WiFi.softAPIP());  // get interface IP address mask

}


and the request function...


void set_position (int position)
{
  WiFiClient client;
  const char* host = "192.168.4.1";
  const int httpPort = 80;

  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  String url = "/";
  url += "?pos=";
  url += String(position);

  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(10);

  while (client.available()) {
    String line = client.readStringUntil('\r');
    //    String line = client.readStringUntil("</html>");
    Serial.println(line);

    int addr_start = line.indexOf("value=\"");
    int addr_end = line.indexOf('\"', addr_start + 7);

    if (addr_start == -1 || addr_end == -1) {
      Serial.print("no data in line");
    }
    else {
      String req = line.substring(addr_start + 7, addr_end);
      Serial.print("start: ");
      Serial.println(addr_start);
      Serial.print("end: ");
      Serial.println(addr_end);

      Serial.print("Possy: ");
      Serial.println(req);
    }

  }
  Serial.println();
  Serial.println("closing connection");
}