Hi guys!
I'm working on communicating to a NodeMcu in Access Point mode with my phone .
my code recognizes that my phone is connected to nodemcu and shows it's ip and mac address in serial terminal ,
But when i want to connect to nodemcu's ip with my Wifi Terminal app it just doesn't connect and says "client disconnected : Socket closed !"
Please Help me ... ![]()
note that this communication works when i use nodemcu in Station mode and connect it to another Access Point .
This is my code( The Actual function of this code is to first start as AP and get the SSID and PASSWORD of a router from an WIfi terminal and then with that information connect to the specified router ! ) :
#include <ESP8266WiFi.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
extern "C" {
#include "user_interface.h"
}
char ssid[31] = "NodeMcu";
char password[63] = "227485300";
byte mac[6];
bool first=1 ;
boolean waitingDHCP=false;
char last_mac[18];Â
WiFiServer server(1337); // set port
void printWiFiStatus();
// Manage incoming device connection on ESP access point
void onNewStation(WiFiEventSoftAPModeStationConnected sta_info) {
 Serial.println("New Station :");
 sprintf(last_mac,"%02X:%02X:%02X:%02X:%02X:%02X", MAC2STR(sta_info.mac));
 Serial.printf("MAC address : %s\n",last_mac);
 Serial.printf("Id : %d\n", sta_info.aid);
 waitingDHCP=true;
}
void setup(void) {
 static WiFiEventHandler e1;
 Serial.begin(9600);
 if(first)
 {
 //WiFi.softAP(ssid, password);
 WiFi.mode(WIFI_AP_STA);
 IPAddress myIP = WiFi.softAP(ssid,password);
 //IPAddress myIP = WiFi.softAPIP();
 Serial.print("AP IP address: ");
 Serial.println(myIP);
 // Event subscription
 e1 = WiFi.onSoftAPModeStationConnected(onNewStation);
 }
Â
 // Configure GPIO2 as OUTPUT.
 pinMode(LED_BUILTIN, OUTPUT);
       Â
 // Start TCP server.
 server.begin();
 //server.begin();
}
void loop(void)
{
 bool WifiConnected = false , ClientConnected = false ;
 // Check if module is still connected to WiFi.
 if(first==0)
 {
 if (WiFi.status() != WL_CONNECTED) {
  while (WiFi.status() != WL_CONNECTED) {
   delay(500);
  }
  WifiConnected = true ;
  // Print the new IP to Serial.
  printWiFiStatus();
 }
 }
 else
 {
  if (waitingDHCP)
 {
  String cb;
  if (deviceIP(last_mac,cb))
  {
   Serial.println("Ip address :");
   Serial.println(cb); //do something
   WifiConnected = true ;
  }
  else
  {
   Serial.println("Problem during ip address request :");
   Serial.println(cb); //do something else
  }
 }
 delay(2000);
 }
WiFiClient client = server.available();
if(WifiConnected == true)
{
 if (client) {
  Serial.println("Client connected.");
  while (client.connected()) {
   ClientConnected = true ;
   if (client.available()) {
    String data ;
   Â
    char str[100], i=0 ;
    for(char j=0 ;j<20;j++)
    {
     str[j]='\0' ;
    }
    i = 0 ;
    do
    {Â
    if (client.available())
    {
     str[i] = client.read();
     i++ ;
    }
    if(i==100)break ;
    }
    while(str[i-1]!='!');
    Serial.println("! Found ") ;
    Serial.println(str) ;
    if (!strcmp(str,"LED_ON!"))
    {
     digitalWrite(LED_BUILTIN, LOW);
     Serial.println("Lamp is now on.");
    }
    if (!strcmp(str,"LED_OFF!"))
    {
     digitalWrite(LED_BUILTIN, HIGH);
     Serial.println("Lamp is now off.");
    }
    if (str[0]=='H')
    {
     unsigned char h ;
     first=0 ;
     Serial.println("Recieving new SSID & Password ...");
     i=1 ;
     while(str[i]!=':')
     {Â
     ssid[i-1] = str[i] ;
     i++ ;
     if(i==33)break ;
     }
     i++ ;
     h=i ;
     while(str[i]!='!')
     {Â
     password[i-h] = str[i] ;
     i++ ;
     if(i==65)break ;
     }
     client.stop();
     Serial.println("Client disconnected.");
     WiFi.disconnect() ;
     WiFi.mode(WIFI_STA);
     Serial.println("Access Point Terminated!");
     WiFi.begin(ssid, password);
     Serial.println("Connecting to new SSID ...");
     server.begin();
    }
    Â
    }
   }
  }
 }
 if(ClientConnected == true)
 {
 Serial.println("Client disconnected.");
 client.stop();
 }
}
void printWiFiStatus() {
 Serial.println("");
 Serial.print("Connected to ");
 Serial.println(ssid);
 Serial.print("IP address: ");
 Serial.println(WiFi.localIP());
   WiFi.macAddress(mac);
  Serial.print("MAC: ");
  Serial.print(mac[5],HEX);
  Serial.print(":");
  Serial.print(mac[4],HEX);
  Serial.print(":");
  Serial.print(mac[3],HEX);
  Serial.print(":");
  Serial.print(mac[2],HEX);
  Serial.print(":");
  Serial.print(mac[1],HEX);
  Serial.print(":");
  Serial.print(mac[0],HEX);
  Serial.println("Signal strength : ") ;
  Serial.print(WiFi.RSSI()+"dBm") ;
}
boolean deviceIP(char* mac_device, String &cb) {
 struct station_info *station_list = wifi_softap_get_station_info();
 while (station_list != NULL) {
  char station_mac[18] = {0}; sprintf(station_mac, "%02X:%02X:%02X:%02X:%02X:%02X", MAC2STR(station_list->bssid));
  String station_ip = IPAddress((&station_list->ip)->addr).toString();
  if (strcmp(mac_device,station_mac)==0) {
   waitingDHCP=false;
   cb = station_ip;
   return true;
  }
  station_list = STAILQ_NEXT(station_list, next);
 }
 wifi_softap_free_station_info();
 cb = "DHCP not ready or bad MAC address";
 return false;
}