PlatformIO Community

ESP32 access point. To be or not to be?


#1

Good day friends. I have a problem. I used to use the ESP 8266 un module I programmed it in the LUA language in the ESPlorer application. I created an access point and connected with a mobile application and performed various actions. Now I have become closely with ESP8266 and I begin to work with ESP32 in the design of PlatformIO. There was a problem: After creating an access point on ESP32 and connecting a mobile phone to this access point, an inscription appears on the mobile phone screen that there is no internet connection. My mobile app is not working. I see that it creates a connection and ESP32 sees a new client, but does not want to communicate.
HELP!!!


#2

If you connect the phone to ESP AP, then you have a Wifi connection between the 2 devices, NOT to the internet.
That’s why you get “No Internet Connection” message on the phone.
Because you are connected just to ESP32 AP.
NOT to internet.

As for connection, if you are hosting a http page on ESP32, make sure you enter the IP address of the ESP on the phone browser. Usually that IP is 192.168.4.1


#3
  1. ESP8266 did not issue such a message.
  2. My program on the smartphone with ESP8266 works correctly (just telnet is used).
  3. With ESP32 problems. Access point rises. The mobile application connects (I see it), and then, you need to make a delay of 100ms to read the line. It does not even read everything.
    4.http server on ESP32 does not start.

#4

Can you share your code?
This way will be much easier to find the problem.


#5
#include <Arduino.h>

#include <WiFi.h>
#include <WiFiClient.h>
#include <IPAddress.h>
#include "string"

String inputString="";
String inputStringWiFi="";

String hello="hello\r";
String fadd="fadd\r";
String fdel="fdel\r";

int isp=0;
uint8_t tmpraed;
uint32_t hsread;


const char* ssidDev="TEST";
const char* pwdDev="test";

WiFiServer wserver;
WiFiClient wsclient;
uint16_t trPort=567;

IPAddress ipDev="192.168.24.16";
IPAddress nmDev="255.255.255.0";
IPAddress gwDev="192.168.24.16";

extern configurator_wifi wifiAP;

char c;

void testWiFiConnect (void){
    Serial.println("Start test connecting....");
    //Serial.print("Connected, IP adress: "); Serial.println(WiFi.localIP());
    Serial.print("Mode = "); Serial.print(WiFi.getMode());
    Serial.print(" IP = "); Serial.println(WiFi.softAPIP().toString());
    Serial.println("...End test connected");
}
void createWIFI (void){
    //create WIFI AP
    Serial.println("Create WIFI AP");
    Serial.println("Parametr to create");
    Serial.print("SSID DEVICE="); Serial.println(ssidDev);
    Serial.print("PASSWORD DEVICE="); Serial.println(pwdDev);
    Serial.print("CONFIG IP="); Serial.println(ipDev);
    Serial.print("GW="); Serial.println(gwDev);
    Serial.print("NM="); Serial.println(nmDev);
    //WiFi.mode(WIFI_AP);
    //delay(10);
    WiFi.softAP(ssidDev,pwdDev,1,0,4);
    delay(10);
    WiFi.softAPConfig(ipDev, gwDev, nmDev);
    delay(10);
    testWiFiConnect();
    Serial.println("WiFi AP is UP");
}
void disconnectWIFI (void){
    //disconecting WIFI AP
    WiFi.disconnect();
    WiFi.mode(WIFI_OFF);
}
void createServer (void){
    Serial.println("Created WIFI SERVER");
    trPort=wifiAP.portAP.toInt();
    //WiFiServer wserver(trPort);
    //WiFiClient wsclient(1);
    wserver.begin(trPort);
    //wserver.setNoDelay(true);
    Serial.println("WiFi Server is cteate");
}


void setup() {
  // put your setup code here, to run once:
  //WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG,0);
  Serial.begin(115200);
  Serial.println("Hello Alex!!!");
  Serial.print("Temperatyre - ");
  Serial.println(temperatureRead());
  Serial.print("HAll - ");
  Serial.println(hallRead());
  Serial.println();
  //**************************
  //**** Setup WIFI **********
  disconnectWIFI();
  delay(100);
  createWIFI();
  delay(100);
  createServer();
  delay(100);
  
}

void loop() {
  //********************************
  //***** DEBUG UART port **********
  if (Serial.available()) {
      char c = Serial.read();
      Serial.print(c);
      //read line and parsing
      if (c==10) {
          Serial.print ("inputString ="); Serial.println(inputString);

          if (inputString.equals(hello)) {
              Serial.println ("Hello my friends!");
          }
          Serial.println("Obnulili line string");
          inputString = "";
      } else {
          inputString +=c;
      }
  }
  //************ END DEBUG UART PORT ***************
  delay(100);
  //*************************************************
  //*********** WIFI PORT ************************
  wsclient = wserver.available();
  if (wsclient) {
      Serial.println ("new client");
      String currentLine = "";
      while (wsclient.connected()) {
          if (wsclient.available()) {
              c = wsclient.read();
              Serial.write(c);
              if (c == 10) {
                  //Serial.println("end string");
                  //wsclient.println("end command");
                  if (currentLine.equals(hello)){
                      Serial.print("Command - "); Serial.println(currentLine);
                      wsclient.println("Hello my WIFI friend!");
                  }
                  if (currentLine.substring(0).equals(fadd)){
                      Serial.print("Command-"); Serial.println(currentLine);
                       wsclient.print("Working - ");
                       wsclient.println(currentLine);
                      isp=1;
                      if (isp==1){
                          wsclient.println("fadd:OK");
                      } else {
                          wsclient.println("fadd:BAD");
                      }
                  }
                  if (currentLine.substring(9).equals(fdel)){
                      Serial.print("Command-"); Serial.println(currentLine);
                      isp=1;
                      if (isp==1){
                          wsclient.println("fdel:OK");
                      } else {
                          wsclient.println("fdel:BAD");
                      }
                  }
                  currentLine = "";
              } else {
                  currentLine +=c;
              }
              
          }

      }
      wsclient.stop();
      Serial.println("Client disconnected.");
  }


  
  //************ END  WIFI PORT ******************
}

#6
  • You should call WiFi.softAPConfig before WiFi.softAP

  • configurator_wifi is undefined so you should define it or remove it.

  • Port 567 is a non standard port. If you are using this port, make sure your client can connect to this port.
    The default port is 80. Try that one first.

  • You don’t need to set the wifi to off by calling WiFi.mode(WIFI_OFF). Is enough to disconnect.

  • If you want to use custom IP, you should define it as a int array: IPAddress ipDev={192,168,24,16};
    Not as a string like you did.
    Check the IPAddress class constructor:

IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);

  • About strings: You should NOT include “string” into the project. String is already defined in Wstring.h that is included into Arduino framework.

  • Also you don’t need to include “IPAddress.h” since is already included into WiFi.h. Check inclusion tree…

  • My advice is to start from the WifiAccessPoint example, see it work, then you can expand from that.
    Below you can find this example:

/*
  WiFiAccessPoint creates a WiFi access point and provides a web server on it.

  Steps:
  1. Connect to the access point "yourAp"
  2. Point your web browser to http://192.168.4.1/H to turn the LED on or http://192.168.4.1/L to turn it off
     OR
     Run raw TCP "GET /H" and "GET /L" on PuTTY terminal with 192.168.4.1 as IP address and 80 as port

  Created for arduino-esp32 on 04 July, 2018
  by Elochukwu Ifediora (fedy0)
*/

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>

#define LED_BUILTIN 2 // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED

// Set these to your desired credentials.
const char *ssid = "yourAP";
const char *password = "yourPassword";

WiFiServer server(80);

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring access point...");

  // You can remove the password parameter if you want the AP to be open.
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.begin();

  Serial.println("Server started");
}

void loop()
{
  WiFiClient 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
    String currentLine = "";       // make a String to hold incoming data from the client
    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
        Serial.write(c);        // print it out the serial monitor
        if (c == '\n')
        { // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0)
          {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
            client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else
          { // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r')
        {                   // if you got anything else but a carriage return character,
          currentLine += c; // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H"))
        {
          digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L"))
        {
          digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

#7

How to do it?

Yes. That’s what I need.

I tried this method. The result is equally negative.

// Constructors
IPAddress();
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
IPAddress(uint32_t address);
IPAddress(const uint8_t *address);
virtual ~IPAddress() {}

bool fromString(const char *address);
bool fromString(const String &address) { return fromString(address.c_str()); }

In fact, using this method it turns out to set the network parameters and they are clearly visible when scanning. You can freely connect to the AP with a password and name. To the server on the desired port, you can freely connect. But if the mobile application has more than one screen with buttons, then commands are not accepted from the second window. The server on the ESP32 does not see them and does not see off.


#8

Try this: https://gist.github.com/pfeerick/28a5aa0b00b98344f99443040b1752f9

I basically just put WiFi.softAPConfig(ipDev, gwDev, nmDev); before WiFi.softAP(ssidDev,pwdDev,1,0,4);, formatted the IP addresses properly (here’s the documentation if you’re still confused on that point), and commented out trPort=wifiAP.portAP.toInt();.

Not sure what the point of that line was anyway, as trPort was already an integer, and anyway, it was blocking the code compiling.

I suspect you’re going around this completely wrong, as you seem to want AP-STA mode like on the ESP8266, so you can have a AP to connect to, which also acts as a station which connects to your WiFi network? Maybethis guide will help somewhat, as the ESP32 has that mode also…