Post topics, source code that relate to the Arduino Platform

User avatar
By AlexPilk
#25955 I've set up an access point (ESP_AP) and tried connecting to it from another module (ESP_STA). It prints an IP address, but you can't ping it or access it from a browser. However if you connect it to a router it works well, gets an ip and you can access the server, works well even with requesting a static ip. Here's the ESP_AP code:

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server(80);
boolean trigger = 1;
const char* host = "192.168.4.5";
String requested_data = "None";

void handleRoot() {
  server.send(200, "text/html", "<h1>Requested data from ESP_AP: </h1>" + requested_data);
}

void setup() {
  delay(1000);
  pinMode(2, INPUT);
  WiFi.softAP("ESPap", "thereisnospoon");
  server.on("/", handleRoot);
  server.begin();
}

void send_request() {
  WiFiClient client;
  if (!client.connect(host, 80)) {
    return;
  }
  String url = "/station_test/";
  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()) {
    requested_data = client.readStringUntil('\r');
  }
}

void loop() {
  server.handleClient();
  if (digitalRead(2) != HIGH and trigger == 1) {
    send_request();
    trigger = 0;
  } else {
    trigger = 1;
  }
}


And here's the ESP_STA code:

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server(80);
int request_number = 0;

void handleRoot(){
  server.send(200, "text/html", "<h1>Number of requests: </h1>"+String(request_number));
}
void handleRequestData(){
  server.send(200, "text/html", "<h2>I'm the requested data :)</h2>");
  request_number+=1;
}

void setup() {
  Serial.begin(115200);
  delay(10);
  WiFi.begin("ESPap", "thereisnospoon");
  //WiFi.config(IPAddress(192,168,4,5), IPAddress(192,168,4,1), IPAddress(255,255,255,0));
  pinMode(2,OUTPUT);
  digitalWrite(2,LOW);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.on("/", handleRoot);
  server.on("/station_test/", handleRequestData);
  server.begin();
  digitalWrite(2,HIGH);
}

void loop() {
  server.handleClient();
}


For now I'm trying to figure out how to connect the station to the access point, so I haven't yet tested the "request" code.
User avatar
By kolban
#25959 Howdy Alex,
I haven't studied your code in depth ... but going back to your comment earlier....

When ESP_STA connects to ESP_AP that happens at the WiFi level ... when that WiFi connection completes, to all intents and purposes, following that ... you then have two peer IP devices. Once the WiFi network has formed, bot the ESP_STA and ESP_AP are peers of each other. At that point, ESP_STA could become a TCP listener and ESP_AP a TCP client and the ESP_AP could initiate a socket or UDP network send to ESP_STA. Again, no Web Server involved.

Neil
User avatar
By AlexPilk
#25962
kolban wrote:Howdy Alex,
I haven't studied your code in depth ... but going back to your comment earlier....

When ESP_STA connects to ESP_AP that happens at the WiFi level ... when that WiFi connection completes, to all intents and purposes, following that ... you then have two peer IP devices. Once the WiFi network has formed, bot the ESP_STA and ESP_AP are peers of each other. At that point, ESP_STA could become a TCP listener and ESP_AP a TCP client and the ESP_AP could initiate a socket or UDP network send to ESP_STA. Again, no Web Server involved.

Neil


Thanks, the project is a wireless MIDI controller, so I need a server to store the data about notes and stuff. Any ideas why ESP_STA fails to connect to ESP_AP, but works well with a separate router?
User avatar
By martinayotte
#25965
AlexPilk wrote:Any ideas why ESP_STA fails to connect to ESP_AP, but works well with a separate router?


I've done some tests more than a month ago, because someone had such problem too. I've written simple MyAPServer and MyAPClient sketch, and it was working without any problem. So, in your case, you need to troubleshoot why your STA isn't able to connect to AP. Are your sure that your STA is really in STA only and not in AP+STA mode ? Because in such case, there will be routing problems where packets of your STA will be routed to itself if you have another AP on it.