So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Megladon
#76874 Hardware and Software
Arduino IDE with esp8622 boards library

NodeMCU 8266 v0.9 devkit boards

Problem
When Connecting to the first AP everything works fine the connection to port 9001 tcp server is successful and data is sent. But when the client eps8266 loses connection from the 1st AP by walking out of range and connects to the 2nd AP successfully it doesn't connect to the TCP server TKDClient.connect(); just returns 0.

Main Code
Server
Code: Select all  ...
**Variables and Setup**

WiFi.disconnect();

// Setting The Wifi Mode
WiFi.mode(WIFI_AP);
WiFi.setOutputPower(0);
Serial.println("WIFI Mode : AccessPoint");

// Starting The Access Point
WiFi.softAP("Wifi", "", 12);// its an open AP so no PW

void loop()
{
  IsClients();
}

void IsClients()
{
  if(TKDServer.hasClient())
  {
    WiFiClient TKDClient = TKDServer.available();
    while(1)
    {
      if(TKDServer.hasClient())
      {
        TKDClient.stop();
        TKDClient = TKDServer.available();
        Serial.println("Client");
      }
      delay(1000);

      if(TKDClient.available())  //Message over wifi
      {
        String Message = TKDClient.readStringUntil('\r');
        Serial.println(Message);
        delay(20);
        if (Message == "<SEND>") {
        Serial.println("Sending XYZ");
        TKDClient.println(pegValue);
//      TKDClient.flush(); 
      }
    }
  }
}
else
  {
    ...
  }

Client
Code: Select all  ...
**Variables and Setup**
void loop()
{
  CheckConnectivity();
  while(TKDClient.available())
  {
    strValue = TKDClient.readStringUntil('\n');
    Serial.println(strValue);
    if (strValue.startsWith("<"))
    {
      Serial.println("Eeprom write");
      ... Eeprom write code
    }
  }
  delay(1000); 
}

void CheckConnectivity()
{
  while(WiFi.status() != WL_CONNECTED)
  {
    WiFi.mode(WIFI_STA);     
    WiFi.begin("WIFI");
    discFlag = 0;   //Disconnect flag to mark if it disconnected
    for(int i=0; i < 10; i++)
    {
      ... Blink Led
      Serial.print("."); //progress bar type of thing
    }
    Serial.println("");
  }
  if (!discFlag){
    discFlag = 1;
    ... show connect light
    Serial.println("!-- Client Device Connected --!");
    Serial.println("Connected To      : " + String(WiFi.SSID()));
    Serial.println("Signal Strenght   : " + String(WiFi.RSSI()) + " dBm");
    Serial.print  ("Server IP Address : ");
    Serial.println(TKDServer);
    Serial.print  ("Device IP Address : ");
    Serial.println(WiFi.localIP());   
    TKDRequest();
  }   
}


void TKDRequest()
{
  TKDClient.stop(); // First Make Sure You Got Disconnected
  Serial.println("DEBUG-3C");
  Serial.println(WiFi.status());
  Serial.println(TKDClient.connect(TKDServer, 9001));
  // If Sucessfully Connected Send Connection Message
  if(TKDClient.connect(TKDServer, 9001))
  {
    Serial.println    ("--CONNECTED--");
    delay(5000);
    TKDClient.println ("<SEND>");
    Serial.println    ("Sending: <SEND>");
//  TKDClient.flush();
  }
}

Server Debug Serial
First AP

Code: Select allstation: a0:20:a6:13:6e:ac leave, AID = 1
rm 1
wifi evt: 6
add 1
aid 1
station: a0:20:a6:13:6e:ac join, AID = 1
wifi evt: 5
wifi evt: 9
wifi evt: 9
Client
Client
Client
<SEND>
Sending XYZ
<X55665.5646;Y4536.455;Z5453.453>
<SEND>

Second AP

Code: Select allwifi evt: 7
wifi evt: 7
wifi evt: 7
station: a0:20:a6:13:6e:ac leave, AID = 1
rm 1
wifi evt: 6
add 1
aid 1
station: a0:20:a6:13:6e:ac join, AID = 1
wifi evt: 5
wifi evt: 9
wifi evt: 9

Ignore the fact that the mac is the same because I only had debug on one esp8266 so its debug when it was used as 1st AP and then when it was 2nd AP

Client Debug Serial
Second AP
Code: Select all18:37:22.864 ....scandone
18:37:24.724 state: 0 -> 2 (b0)
18:37:24.724 state: 2 -> 3 (0)
18:37:24.724 state: 3 -> 5 (10)
18:37:24.724 add 0
18:37:24.724 aid 1
18:37:24.724
16:41:29.919 connected with WIFI, channel 12
16:41:30.008 dhcp client start...
16:41:30.008 cnt
16:41:30.008 wifi evt: 0
16:41:30.049 .ip:192.168.4.2,mask:255.255.255.0,gw:192.168.4.1
16:41:30.208 wifi evt: 3   
18:38:05.085 .......
18:38:08.103
18:38:08.103 !-- Client Device Connected --!
18:38:08.103 Connected To      : WIFI
18:38:08.103 Signal Strenght   : -69 dBm
18:38:08.103 Server IP Address : 192.168.4.1
18:38:08.103 Device IP Address : 192.168.4.2
18:38:08.103 DEBUG-3C
18:38:08.103 3
18:38:08.103 1
18:38:08.103 --CONNECTED--
18:38:13.103 Sending: <SEND>
18:38:13.451 pm open,type:2 0
18:38:14.607 <X55665.5646;Y4536.455;Z5453.453>
18:38:14.607 Eeprom write

Second AP

Code: Select all18:38:57.476 bcn_timout,ap_probe_send_start
18:38:59.985 ap_probe_send over, rest wifi status to disassoc
18:38:59.985 state: 5 -> 0 (1)
18:38:59.985 rm 0
18:38:59.985 pm close 7
18:38:59.985 wifi evt: 1
18:38:59.985 STA disconnect: 200
18:39:00.608 scandone
18:39:00.979 scandone
18:39:00.979 state: 0 -> 2 (b0)
18:39:00.979 state: 2 -> 3 (0)
18:39:00.979 state: 3 -> 5 (10)
18:39:00.979 add 0
18:39:00.979 aid 1
18:39:00.979
18:39:00.979 connected with WIFI, channel 12
18:39:01.069 dhcp client start...
18:39:01.069 cnt
18:39:01.069 wifi evt: 0
18:39:01.109 .ip:192.168.4.2,mask:255.255.255.0,gw:192.168.4.1
18:39:01.249 wifi evt: 3
18:39:01.609 .........
18:39:05.625
18:39:05.625 !-- Client Device Connected --!
18:39:05.625 Connected To      : WIFI
18:39:05.625 Signal Strenght   : -60 dBm
18:39:05.625 Server IP Address : 192.168.4.1
18:39:05.625 Device IP Address : 192.168.4.2
18:39:05.625 DEBUG-3C
18:39:05.625 3
18:39:10.616 0
18:39:10.976 pm open,type:2 0

I really have no idea what its going to take to make this work i've been strugling for days.