energia / cc3200-core

6 stars 10 forks source link

Switching AP to STA still has issues. #17

Open SoundConception opened 7 years ago

SoundConception commented 7 years ago

I have written a small Energia sketch to switch back and forth between AP and STA.
Switching works initially, but always ends up failing eventually after switching a seemingly random number of times.

I've run this test multiple times and recorded the number of times it was able to switch before getting stuck: 57, 128, 62, 75, 104, 46, 30, 26, 9, 47, 52, 51. It always seems to eventually get stuck in the switch from AP to STA, but never when switching from STA back to AP.

I am using the latest commit of the WiFi library from this repo.

#include <WiFi.h>  

#define WIFI_SSID         "<Your WiFi Network SSID>"
#define WIFI_PASSWORD     "<Your WiFi Network Password>"

#define AP_SSID           "AP_Test"
#define AP_PASSWORD       "12345678"

#define MODE_CHANGING     0
#define MODE_AP           1
#define MODE_STATION      2

unsigned int currentMode;
unsigned int swapToApCount;
unsigned int swapToStationCount;

void setup() {

  Serial.begin(115200);

  setupStation();
}

void loop() {

  switch (currentMode) {

    case MODE_AP:
      delay(1000);
      setupStation();
      break;

    case MODE_STATION:
      delay(1000);
      setupAp();
      break;
  }  
}

/**
 * Set the CC3200 up as an Access Point
 */
void setupAp() {

  Serial << "Setup AP with SSID: " << AP_SSID << "\r\n";
  Serial << "AP uses WPA with Password: " << AP_PASSWORD <<  "\r\n";

  WiFi.beginNetwork(AP_SSID, AP_PASSWORD);
  while (WiFi.localIP() == INADDR_NONE) {
    Serial << ".";
    delay(300);
  }

  Serial << "\r\nAP Active!\r\n\r\n";
  currentMode = MODE_AP;
  swapToApCount++;
  printStats();
}

/**
 * Connect the CC3200 as a Client to a WiFi network.
 */
void setupStation() {

  Serial << "Connecting to network with SSID: " << WIFI_SSID << "\r\n";
  Serial << "Connecting using password: " << WIFI_PASSWORD << "\r\n";

  WiFi._initialized = false;
  WiFi._connecting = false;

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while ( WiFi.status() != WL_CONNECTED) {
    Serial << ".";
    delay(300);
  }

  Serial << "\r\nConnected to network!\r\n";
  Serial << "Waiting for an IP address\r\n";

  while (WiFi.localIP() == INADDR_NONE) {
    Serial << ".";
    delay(300);
  }

  Serial << "\r\nIP address obtained: " << WiFi.localIP() << "\r\n\r\n";
  Serial << "Attached to network: " << WiFi.SSID() << "\r\n\r\n";
  currentMode = MODE_STATION;
  swapToStationCount++;
  printStats();
}

void printStats() {

  Serial << "Stats\r\n-----\r\n";
  Serial << "Swap to AP count: " << swapToApCount << "\r\n";
  Serial << "Swap to Station count: " << swapToStationCount << "\r\n\r\n";
}

/**
 * Template to allow easier concatenation of print output.
 */
template<class T> inline Print &operator <<(Print &obj, T arg) {
  obj.print(arg);
  return obj;
}