esp8266具有一个客户端和多个AP

时间:2018-07-04 17:45:17

标签: wifi arduino-esp8266

硬件和软件

具有esp8622开发板库的Arduino IDE

NodeMCU 8266 v0.9 devkit开发板

问题

连接到第一个AP时,一切正常,到端口9001 tcp服务器的连接成功,并发送了数据。但是,当客户端eps8266因超出范围而失去与第一个AP的连接并成功连接至第二个AP时,它将不连接至TCP服务器TKDClient.connect();。只会返回0。

主要代码

服务器

  ...
**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
  {
    ...
  }
}  

客户

  ...
**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();
  } 
}

服务器调试序列

第一个AP

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
Client
Client
Client
<SEND>
Sending XYZ
<X55665.5646;Y4536.455;Z5453.453>
<SEND>

第二个AP

wifi 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

忽略Mac是相同的事实,因为我仅在一个esp8266上进行过调试,因此在将其用作第一个AP并在其作为第二个AP时进行了调试

客户端调试序列

18: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 

第二个AP

18: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

我真的不知道要花几天时间才能完成这项工作需要什么。

0 个答案:

没有答案