This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Problem in reconnecting to wifi in cc3200

Other Parts Discussed in Thread: CC3200, ENERGIA

Hello all,

I am using energia and cc3200 for an application. In my application I have to disconnect the wifi inside the void loop or another function and reconnect again.

Up to disconnect it works fine but when I try to reconnect again,it's not happening.

Can anyone please tell me what am I missing ?

This is the disconnect and reconnect function. 

if(WiFi.status() == WL_CONNECTED)
{
Serial.print("Disconnecting from WiFi");
sl_WlanPolicySet(SL_POLICY_CONNECTION , SL_CONNECTION_POLICY(0,0,0,0,0), 0, 0);
// delay(500);
sl_WlanDisconnect();
}
while(WiFi.status() == WL_CONNECTED) {
#ifndef SL_PLATFORM_MULTI_THREADED
sl_Task();
#endif
Serial.print(".");
delay(10);
}

Serial.println();
Serial.println("Disconnected");
ssid:
Serial.println("Enter ssid");
read_count=0;
read_status=true;
// Serial.println(Serial.available());
while(!(Serial.available()));
if(!(Serial.read() == '%'))
goto ssid;
while(read_status)
{
// readchar=Serial.read();
if(Serial.available())
{
if(Serial.peek() == '%')
read_status=false;
else
ssid_new[read_count]=Serial.read();
Serial.print(ssid_new[read_count]);
read_count=read_count+1;
}
}
Serial.println(ssid_new);
while(Serial.available())
int clear = Serial.read();
read_status=true;
password:
Serial.println("Enter password");
ssidnew_len=read_count;
read_count=0;
while(!(Serial.available()));
if(!(Serial.read() == '%'))
goto password;
while(read_status)
{
if(Serial.available())
{
if(Serial.peek() == '%')
read_status=false;
else
password_new[read_count]=Serial.read();
Serial.print(password_new[read_count]);
read_count=read_count+1;
}
}
delay(300);
passwordnew_len=read_count;
Serial.println(password_new);
Serial.println(ssidnew_len);
Serial.println(passwordnew_len);
while(Serial.available())
int clear = Serial.read();
read_count=0;
rc = FatFs.open("T_READ.TXT");
if (rc) die(rc);
// Serial.println("Write a text data. (10 x Hello world!)");
delay(100);
bw=0;
rc = FatFs.write(ssid_new, ssidnew_len-1, &bw);
//rc = FatFs.write(ssid_new+'%', 2, &bw);
if (rc) die(rc);
rc = FatFs.write("%", 1, &bw);
if (rc) die(rc);
rc = FatFs.write(password_new, passwordnew_len-1, &bw);
if (rc) die(rc);
rc = FatFs.write("%", 1, &bw);
if (rc) die(rc);
rc = FatFs.write(0, 0, &bw); //Finalize write
if (rc) die(rc);
apflag=false;
Serial.print("Attempting to connect to Network named: ");
// print the network name (SSID);
Serial.println(ssid_new);
Serial.print("Password: ");
// print the network name (SSID);
Serial.println(password_new);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
WiFi.begin(ssid_new, password_new);
while ( WiFi.status() != WL_CONNECTED) {
// print dots while we wait to connect
Serial.print(".");
delay(300);
if(apflag)
APMode();
}
Serial.println("connected!");
}

  • Hi Rajib,

    What you are missing is to set the connection policy to AUTO/FAST with   sl_WlanPolicySet(SL_POLICY_CONNECTION , SL_CONNECTION_POLICY(1,1,0,0,0), 0, 0);

    Below is a Sketch that demonstrates how to connect/disconnect every 10 sec.

    #include <WiFi.h>
    
    // your network name also called SSID
    char ssid[] = "energia1";
    // your network password
    char password[] = "launchpad";
    
    // Connect / Disconnect every 10 sec;
    #define CONNECTION_INTERVAL_MS 10000
    uint32_t lastConnected = 0;
    
    void setup() {
      Serial.begin(115200);
      WiFi.init();
      connectToWiFi();
      lastConnected = millis();
    }
    
    void loop() {
      Serial.print("Waiting... ");
      Serial.println(CONNECTION_INTERVAL_MS - (millis() - lastConnected));
      delay(1000);
      
      if(millis() - lastConnected > CONNECTION_INTERVAL_MS) {
        lastConnected = millis();
        if(WiFi.status() == WL_CONNECTED) {
          disconnectFromWiFi();
        } else {
          connectToWiFi();
        }
      }
    }
    
    void connectToWiFi() {
      sl_WlanPolicySet(SL_POLICY_CONNECTION , SL_CONNECTION_POLICY(1,1,0,0,0), 0, 0);
        // attempt to connect to Wifi network:
      Serial.print("Attempting to connect to Network named: ");
      // print the network name (SSID);
      Serial.println(ssid);
      // Connect to WPA/WPA2 network.
      WiFi.begin(ssid, password);
      while ( WiFi.status() != WL_CONNECTED) {
        // print dots while we wait to connect
        Serial.print(".");
        delay(300);
      }
    
      Serial.println("\nYou're connected to the network");
      Serial.println("Waiting for an ip address");
      
      while (WiFi.localIP() == INADDR_NONE) {
        // print dots while we wait for an ip addresss
        Serial.print(".");
        delay(300);
      }
    
      Serial.println("\nIP Address obtained");
      // We are connected and have an IP address.
      // Print the WiFi status.
      printWifiStatus();
    }
    
    // ----------------------------
    void disconnectFromWiFi() {
      Serial.print("Disconnecting from WiFi....");
      sl_WlanPolicySet(SL_POLICY_CONNECTION , SL_CONNECTION_POLICY(0,0,0,0,0), 0, 0);
      delay(500);
      sl_WlanDisconnect();
    
    #ifndef SL_PLATFORM_MULTI_THREADED
      while(WiFi.status() == WL_CONNECTED) {
        sl_Task();
      }
    #endif
    
      Serial.println("Disconnected");
    }
    
    // ----------------------------
    void printWifiStatus() {
      // print the SSID of the network you're attached to:
      Serial.print("SSID: ");
      Serial.println(WiFi.SSID());
    
      // print your WiFi IP address:
      IPAddress ip = WiFi.localIP();
      Serial.print("IP Address: ");
      Serial.println(ip);
    
      // print the received signal strength:
      long rssi = WiFi.RSSI();
      Serial.print("signal strength (RSSI):");
      Serial.print(rssi);
      Serial.println(" dBm");
    }

    Robert

  • Hi robert,

    That's correct. Now it's working. Thanks
  • I have also same problem but After disconnecting I want to connect to two new password and ssid but it was connected to old network I have used same program.