Jump to content
43oh

CC3200 can't connect to Wlan after being set as AP


Recommended Posts

Hello I have a few updates,

 

1) Please ignore my earlier suggestion of including "WiFi._initialized = false;" & "WiFi._connecting = false;" in the WiFiClient.cpp file. Instead include those two lines of code in the Energia program before WiFi.begin() because they may cause some checks and balances to fail.

 

2) Be sure to include "WiFi._connecting = false;" before WiFi.begin() only when you're sure that you haven't called WiFi.begin() elsewhere in your code.

 

3) I've noticed that some routers seem to fail during the transition from STATION to AP to STATION in spite of the above mentioned solutions. When I try to transition from:

  • Belkin Router Station -> AccessPoint -> Belkin Router Station (The CC3200 almost never hangs)
  • Belkin Router Station -> AccessPoint -> TPLink Router Station (The CC3200 almost never hangs)
  • TPLink Router Station -> AccessPoint -> TPLink Router Station (Hangs almost always)
  • TPLink Router Station -> AccessPoint -> Belkin Router Station (Hangs almost always)

(I'm not saying the brands of routers are good or bad, maybe just those models. Belkin Router Model - N600 DB and TP Link model - TL WR740N)

 

4) I've also noticed that the transition from Station to AccessPoint never fails. @@spirilis is it possible that the Access Point never fails because of the following code in the WiFi.beginNetwork() in WiFi.cpp? And do you think it is safe to try to experiment with this for WiFi.begin() or rather find a way to software reset the CC3200 before doing a WiFi.begin()?

/* Restart Network processor */
    retVal = sl_Stop(30);

    role = ROLE_AP;
    return sl_Start(NULL,NULL,NULL);
Link to post
Share on other sites
  • Replies 31
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

Go ahead and try #4!  It probably won't hurt.

My application never freeze from my last fix. I tried with a netgear and tplink router and a lot of brand of WiFi repeater

 

Hello I have a few updates,

 

1) Please ignore my earlier suggestion of including "WiFi._initialized = false;" & "WiFi._connecting = false;" in the WiFiClient.cpp file. Instead include those two lines of code in the Energia program before WiFi.begin() because they may cause some checks and balances to fail.

 

2) Be sure to include "WiFi._connecting = false;" before WiFi.begin() only when you're sure that you haven't called WiFi.begin() elsewhere in your code.

 

3) I've noticed that some routers seem to fail during the transition from STATION to AP to STATION in spite of the above mentioned solutions. When I try to transition from:

  • Belkin Router Station -> AccessPoint -> Belkin Router Station (The CC3200 almost never hangs)
  • Belkin Router Station -> AccessPoint -> TPLink Router Station (The CC3200 almost never hangs)
  • TPLink Router Station -> AccessPoint -> TPLink Router Station (Hangs almost always)
  • TPLink Router Station -> AccessPoint -> Belkin Router Station (Hangs almost always)

(I'm not saying the brands of routers are good or bad, maybe just those models. Belkin Router Model - N600 DB and TP Link model - TL WR740N)

 

4) I've also noticed that the transition from Station to AccessPoint never fails. @@spirilis is it possible that the Access Point never fails because of the following code in the WiFi.beginNetwork() in WiFi.cpp? And do you think it is safe to try to experiment with this for WiFi.begin() or rather find a way to software reset the CC3200 before doing a WiFi.begin()?

/* Restart Network processor */
    retVal = sl_Stop(30);

    role = ROLE_AP;
    return sl_Start(NULL,NULL,NULL);

Go ahead and try #4!  It probably won't hurt.

Link to post
Share on other sites

Hello, I have some updates.

 

Unfortunately I haven't found a fix for my specific scenario where a particular router is exhibiting problems when the cc3200 transitions from AP to Station but another router seems to work fine, but I have a few observations and suggestions .

 

1) When the cc3200 is connected/connecting to a router as a Station the WiFi._connecting flag is set to 'true'. But when it transitions into an Access Point, then the WiFi._connecting flag is NOT reset back to 'false'

  • Suggestion: "_connecting = false;" should be included at the end of WiFi.beginNetwork()
    /* Restart Network processor */
    retVal = sl_Stop(30);

    role = ROLE_AP;
	
    _connecting = false;    // AP FIX
	
    return sl_Start(NULL,NULL,NULL);

2) When transitioning from AP to Station, the NWP must be restarted as mentioned in Wlan.h

 

\warning   After setting the mode the system must be restarted for activating the new mode  

    \par       Example:
    \code
                //Switch from any role  to STA:
                sl_WlanSetMode(ROLE_STA);
                sl_Stop(0);
                sl_Start(NULL,NULL,NULL);
    \endcode
  • Suggestion: Include the following code which is within "AP FIX" TAGS into WiFi.begin() method of WiFi.cpp file
    //
    //initialize the simplelink driver and make sure it was a success
    //
    bool init_success = init();
    if (!init_success) {
        return WL_CONNECT_FAILED;
    }


    // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< AP FIX
    if(role != ROLE_STA){
	sl_WlanSetMode(ROLE_STA);
	sl_Stop(30);
	sl_Start(NULL,NULL,NULL);
        role = ROLE_STA;
    }
    // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< AP FIX END


    //
    // Set IP address configuration to DHCP if needed
    //
    setIpDefaults();

3) @@PaoloIT , the fixes you mentioned before do exactly the same things as I mentioned above but they also call WiFi.init() and sl_WlanConnect() everytime you call WiFi.begin(). This is not good practice since not everyone is going to be careful in calling WiFi.begin(). I'd really appreciate it if you could test my fixes on your end.

 

4) The fixes that I mentioned are in no way exhaustive. I don't know enough of such indepth networking to be sure of what all needs to be done before and after transition from AP to STA mode. As mentioned in one of the functions of Wlan_station example of CC3200 CCS SDK the following are some possibilities.

//*****************************************************************************
//! \brief This function puts the device in its default state. It:
//!           - Set the mode to STATION
//!           - Configures connection policy to Auto and AutoSmartConfig
//!           - Deletes all the stored profiles
//!           - Enables DHCP
//!           - Disables Scan policy
//!           - Sets Tx power to maximum
//!           - Sets power policy to normal
//!           - TBD - Unregister mDNS services
//!
//! \param   none
//! \return  On success, zero is returned. On error, negative is returned
//*****************************************************************************

static long ConfigureSimpleLinkToDefaultState()
{
    SlVersionFull   ver = {0};

    unsigned char ucVal = 1;
    unsigned char ucConfigOpt = 0;
    unsigned char ucConfigLen = 0;
    unsigned char ucPower = 0;

    long lRetVal = -1;
    long lMode = -1;

    lMode = sl_Start(0, 0, 0);
    ASSERT_ON_ERROR(__LINE__, lMode);

    // Get the device's version-information
    /* OFF TOPIC CODE OMITTED */

    // Set connection policy to Auto + SmartConfig 
    //      (Device's default connection policy)
    lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, 
                                SL_CONNECTION_POLICY(1, 0, 0, 0, 1), NULL, 0);
    ASSERT_ON_ERROR(__LINE__, lRetVal);

    // Remove all profiles
    lRetVal = sl_WlanProfileDel(0xFF);
    ASSERT_ON_ERROR(__LINE__, lRetVal);

    // If the device is not in station-mode, try putting it in staion-mode
    if (ROLE_STA != lMode)
    {
        if (ROLE_AP == lMode)
        {
            // If the device is in AP mode, we need to wait for this event 
            // before doing anything
            while(!IS_IP_ACQUIRED(g_ulStatus)) 
            { 
#ifndef SL_PLATFORM_MULTI_THREADED
              _SlNonOsMainLoopTask(); 
#endif
            }
        }

        // Switch to STA role and restart
        lRetVal = sl_WlanSetMode(ROLE_STA);
        ASSERT_ON_ERROR(__LINE__, lRetVal);

        lRetVal = sl_Stop(SL_STOP_TIMEOUT);
        ASSERT_ON_ERROR(__LINE__, lRetVal);

        // reset status bits
        CLR_STATUS_BIT_ALL(g_ulStatus);

        lRetVal = sl_Start(0, 0, 0);
        ASSERT_ON_ERROR(__LINE__, lRetVal);

        // Check if the device is in station again
        if (ROLE_STA != lRetVal)
        {
            // We don't want to proceed if the device is not up in STA-mode
            return DEVICE_NOT_IN_STATION_MODE;
        }
    }

    //
    // Device in station-mode. Disconnect previous connection if any
    // The function returns 0 if 'Disconnected done', negative number if already
    // disconnected Wait for 'disconnection' event if 0 is returned, Ignore 
    // other return-codes
    //
    lRetVal = sl_WlanDisconnect();
    if(0 == lRetVal)
    {
        // Wait
        while(IS_CONNECTED(g_ulStatus))
        {
#ifndef SL_PLATFORM_MULTI_THREADED
              _SlNonOsMainLoopTask(); 
#endif
        }
    }

    // Enable DHCP client
    lRetVal = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&ucVal);
    ASSERT_ON_ERROR(__LINE__, lRetVal);

    // Disable scan
    ucConfigOpt = SL_SCAN_POLICY(0);
    lRetVal = sl_WlanPolicySet(SL_POLICY_SCAN , ucConfigOpt, NULL, 0);
    ASSERT_ON_ERROR(__LINE__, lRetVal);

    // Set Tx power level for station mode
    // Number between 0-15, as dB offset from max power - 0 will set max power
    ucPower = 0;
    lRetVal = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, 
            WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *)&ucPower);
    ASSERT_ON_ERROR(__LINE__, lRetVal);

    // Set PM policy to normal
    lRetVal = sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0);
    ASSERT_ON_ERROR(__LINE__, lRetVal);

    // Unregister mDNS services
    lRetVal = sl_NetAppMDNSUnRegisterService(0, 0);
    ASSERT_ON_ERROR(__LINE__, lRetVal);

    lRetVal = sl_Stop(SL_STOP_TIMEOUT);
    ASSERT_ON_ERROR(__LINE__, lRetVal);

    InitializeAppVariables();

    return lRetVal; // Success
} 

 So there's definitely more that needs to be done to make it 'stable'.

Link to post
Share on other sites
  • 3 weeks later...

I was wondering if anybody tried to connect to the CC3200 in AP mode and THEN switch to STA mode. If I don't connect to the CC3200 in AP mode, it will switch to the STA (using what paoloIT suggested). But, if I connect to the board in AP mode (using my computer or smart phone) and then try to switch to STA, it freezes.

Link to post
Share on other sites
  • 1 year later...

I know this is an old topic but I'm facing similar issues. I started up in AP mode, started a webserver and got the network credentials from the user. Switched to STA mode (using the fixes listed earlier in this post) and then connected to my local wifi network. Now this works great till a power cycle happens. When that happens, the CC3200 is supposed to start back up in AP mode and this is where it freezes. I have to reflash the service pack and then it goes back to the AP mode. Any solutions or ideas?

Link to post
Share on other sites
  • 5 months later...

Hi everyone and PabloIT, I try to switch ap to sta mode like you but my code doesn't work right, the cc3200 doesn't connect to my home's wifi

Here is my code

 

#include "Energia.h"

 
 
// Include application, user and local libraries
 
#ifndef __CC3200R1M1RGC__
#include <SPI.h>                // Do not include SPI for CC3200 LaunchPad
#endif
#include <WiFi.h>
 
 
// Define structures and classes
 
 
// Define variables and constants
 
char wifi_name[] = "energia";
char wifi_password[] = "launchpad";
char ssid[15], passwd[15];
WiFiServer myServer(80);
uint8_t oldCountClients = 0;
uint8_t countClients = 0;
int sign = 0;
 
// Add setup code
void setup()
{
    Serial.begin(115200);
    delay(500);
    
    Serial.println("*** LaunchPad CC3200 WiFi Web-Server in AP Mode");
    
    // Start WiFi and create a network with wifi_name as the network name
    // with wifi_password as the password.
    Serial.print("Starting AP... ");
    WiFi.beginNetwork(wifi_name, wifi_password);
    while (WiFi.localIP() == INADDR_NONE)
    {
        // print dots while we wait for the AP config to complete
        Serial.print('.');
        delay(300);
    }
    Serial.println("DONE");
    
    Serial.print("LAN name = ");
    Serial.println(wifi_name);
    Serial.print("WPA password = ");
    Serial.println(wifi_password);
    
    
    pinMode(RED_LED, OUTPUT);      // set the LED pin mode
    digitalWrite(RED_LED, LOW);
    pinMode(GREEN_LED, OUTPUT);      // set the LED pin mode
    digitalWrite(GREEN_LED, LOW);
    pinMode(YELLOW_LED, OUTPUT);      // set the LED pin mode
    digitalWrite(YELLOW_LED, LOW);
    
    IPAddress ip = WiFi.localIP();
    Serial.print("Webserver IP address = ");
    Serial.println(ip);
    
    Serial.print("Web-server port = ");
    myServer.begin();                           // start the web server on port 80
    Serial.println("80");
    Serial.println();
}
 
// Add loop code
void loop()
{
    
    countClients = WiFi.getTotalDevices();
    
    // Did a client connect/disconnect since the last time we checked?
    if (countClients != oldCountClients)
    {
        if (countClients > oldCountClients)
        {  // Client connect
            //            digitalWrite(RED_LED, !digitalRead(RED_LED));
            Serial.println("Client connected to AP");
            for (uint8_t k = 0; k < countClients; k++)
            {
                Serial.print("Client #");
                Serial.print(k);
                Serial.print(" at IP address = ");
                Serial.print(WiFi.deviceIpAddress(k));
                Serial.print(", MAC = ");
                Serial.println(WiFi.deviceMacAddress(k));
                Serial.println("CC3200 in AP mode only accepts one client.");
            }
        }
        else
        {  // Client disconnect
            //            digitalWrite(RED_LED, !digitalRead(RED_LED));
            Serial.println("Client disconnected from AP.");
            Serial.println();
        }
        oldCountClients = countClients;
    }
    
    WiFiClient myClient = myServer.available();
    
    if (myClient)
    {                             // if you get a client,
        Serial.println(". Client connected to server");           // print a message out the serial port
        char buffer[150] = {0};                 // make a buffer to hold incoming data
        int8_t i = 0;
        String mystring;
        while (myClient.connected())
        {            // loop while the client's connected
            if (myClient.available())
            {             // if there's bytes to read from the client,
                char c = myClient.read();             // read a byte, then
                Serial.write©;                    // print it out the serial monitor
                if (c == '\n') {                    // if the byte is a newline character
                    
                    // if the current line is blank, you got two newline characters in a row.
                    // that's the end of the client HTTP request, so send a response:
                    if (strlen(buffer) == 0)
                    {
                       myClient.println();
                        // break out of the while loop:
                        break;
                    }
                    else
                    {      // if you got a newline, then clear the buffer:
                        memset(buffer, 0, 150);
                        i = 0;
                    }
                }
                else if (c != '\r')
                {    // if you got anything else but a carriage return character,
                    buffer[i++] = c;      // add it to the end of the currentLine
                }
                
            mystring = buffer;
            int seperate = mystring.indexOf(' ');
            String ssid1 = mystring.substring(0, seperate);
            String passwd1 = mystring.substring(seperate + 1);
            ssid1.toCharArray(ssid, sizeof(ssid1));
            passwd1.toCharArray(passwd, sizeof(passwd1));
            Serial.println("my wifi is");    
            Serial.println(ssid);
            Serial.println(passwd);
            }
        }
        
        // close the connection:
        myClient.stop();
        Serial.println(". Client disconnected from server");
        Serial.println();
        sign = 1;
    }
    if (sign == 1) {
        WiFi._initialized = false;
        WiFi._connecting = false;
        
        WiFi.begin("haikha", "123456789");
        while ( WiFi.status() != WL_CONNECTED) {
                  // print dots while we wait to connect
                  Serial.print(".");
                  delay(300);
                  } 
    }
}
Link to post
Share on other sites
  • 2 weeks later...

Hi,

I am working with cc3200.

I want my device to be able to connect to any network.

I want to enable ap mode on energia and then enter networks ssid and password and switch to station mode??

I am new to cc3200 ?

can somebody post his/her code of energia here along with changes required in header files or cpp files.

 

Please reply asap.

Link to post
Share on other sites

Hi All,

 

My Ap mode to STA mode is working on CC3200. I made the changes in the WiFi.cpp file.

Now I tried to integrate the simplewbserver code.

But it is not working.

Web page is not opening at that ip.

 

I am sharing my code here.I have used udp packets to send new ssid and password to cc3200.

AP mode to sta mode is working but webserver code not working.

Can anyone see my code and tell what is the mistake that I have made.

 

 
#ifndef __CC3200R1M1RGC__
#include <SPI.h>
#endif
#include <WiFi.h>
const char ssid[] = "CC3200";
const char wifipw[] = "password";
 
const char s[2] = "-";
char *token;
 
String y = "";
char Rssid[100],Rpsk[100];//store new ssid & password
 
unsigned int Port =2390;
char packetBuffer[255];
IPAddress Ip(255,255,255,255);
 
char b[1024];
int j=0;
WiFiServer server(80);
WiFiUDP Udp;
 
void setup()
{
  Serial.begin(115200);
  pinMode(RED_LED, OUTPUT);  // LED will toggle when clients connect/disconnect
  digitalWrite(RED_LED, LOW);
  
  Serial.print("Setting up Access Point named: ");
  Serial.println(ssid);
  Serial.print("AP uses WPA and password is: ");
  Serial.println(wifipw);
  
  WiFi.beginNetwork((char *)ssid, (char *)wifipw);
 
  while (WiFi.localIP() == INADDR_NONE) 
  {
    // print dots while we wait for the AP config to complete
    Serial.print('.');
    delay(300);
  }
  Serial.println();
  Serial.println("AP active.");
  printWifiStatus();
  Udp.begin(Port);
}
 
 
 
void loop()
{
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    int len = Udp.read(packetBuffer, 255);
    if (len > 0)packetBuffer[len] = 0;
    //unit=atoi(packetBuffer);
    Serial.println(packetBuffer);
         
                 /* get the first token */
                token = strtok(packetBuffer, s);
                int w = 0;
   
                while( token != NULL ) 
               {
                  
                  if(w == 0){
                  strcpy(Rssid,token);
                  Serial.println(Rssid);
                  w++;
                  }else if(w == 1){
                    strcpy(Rpsk,token);
                    Serial.println(Rpsk);
                  }
                  token = strtok(NULL, s);
               }
 
                int retVal;
                retVal = sl_WlanSetMode(ROLE_STA);
                retVal = sl_Stop(30);
                retVal= sl_Start(NULL, NULL, NULL);
 
 
                // attempt to connect to Wifi network:
                Serial.print("Attempting to connect to Network named: ");
                delay(5000);
                // print the network name (SSID);
                Serial.println(Rssid);
                // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
                WiFi._initialized = false;
                WiFi._connecting = false;
                WiFi.begin(Rssid, Rpsk);
                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");
 
                // you're connected now, so print out the status
                printWifiStatus();
                Serial.println("Starting webserver on port 80");
                server.begin();                           // start the web server on port 80
                Serial.println("Webserver started!");
   }
   int i = 0;
  WiFiClient client = server.available();   // listen for incoming clients
 
  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    char buffer[150] = {0};                 // make a buffer to hold incoming data
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write©;                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character
 
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (strlen(buffer) == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
 
            // the content of the HTTP response follows the header:
            client.println("<html><head><title>Energia CC3200 WiFi Web Server</title></head><body align=center>");
            client.println("<h1 align=center><font color=\"red\">Welcome to the CC3200 WiFi Web Server</font></h1>");
            client.print("RED LED <button onclick=\"location.href='/H'\">HIGH</button>");
            client.println(" <button onclick=\"location.href='/L'\">LOW</button><br>");
 
            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear the buffer:
            memset(buffer, 0, 150);
            i = 0;
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          buffer[i++] = c;      // add it to the end of the currentLine
        }
 
        // Check to see if the client request was "GET /H" or "GET /L":
        if (endsWith(buffer, "GET /H")) {
          digitalWrite(RED_LED, HIGH);               // GET /H turns the LED on
        }
        if (endsWith(buffer, "GET /L")) {
          digitalWrite(RED_LED, LOW);                // GET /L turns the LED off
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
 
}
 
boolean endsWith(char* inString, char* compString) {
  int compLength = strlen(compString);
  int strLength = strlen(inString);
  
  //compare the last "compLength" values of the inString
  int i;
  for (i = 0; i < compLength; i++) {
    char a = inString[(strLength - 1) - i];
    char b = compString[(compLength - 1) - i];
    if (a !=b ) {
      return false;
    }
  }
  return true;
}
 
void printWifiStatus() {
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  sprintf(b,"%d.%d.%d.%d,",ip[0],ip[1],ip[2],ip[3] );
  Serial.print("IP Address: ");
  Serial.println(ip);
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}
Link to post
Share on other sites

Hi All,


 


My Ap mode to STA mode is working on CC3200. I made the changes in the WiFi.cpp file.


Now I tried to integrate the simplewbserver code.


But it is not working.


Web page is not opening at that ip.


 


I am sharing my code here.I have used udp packets to send new ssid and password to cc3200.


AP mode to sta mode is working but webserver code not working.


Can anyone see my code and tell what is the mistake that I have made.


 


 

#ifndef __CC3200R1M1RGC__

#include <SPI.h>

#endif

#include <WiFi.h>

const char ssid[] = "CC3200";

const char wifipw[] = "password";

 

const char s[2] = "-";

char *token;

 

String y = "";

char Rssid[100],Rpsk[100];//store new ssid & password

 

unsigned int Port =2390;

char packetBuffer[255];

IPAddress Ip(255,255,255,255);

 

char b[1024];

int j=0;

WiFiServer server(80);

WiFiUDP Udp;

 

void setup()

{

  Serial.begin(115200);

  pinMode(RED_LED, OUTPUT);  // LED will toggle when clients connect/disconnect

  digitalWrite(RED_LED, LOW);

  

  Serial.print("Setting up Access Point named: ");

  Serial.println(ssid);

  Serial.print("AP uses WPA and password is: ");

  Serial.println(wifipw);

  

  WiFi.beginNetwork((char *)ssid, (char *)wifipw);

 

  while (WiFi.localIP() == INADDR_NONE) 

  {

    // print dots while we wait for the AP config to complete

    Serial.print('.');

    delay(300);

  }

  Serial.println();

  Serial.println("AP active.");

  printWifiStatus();

  Udp.begin(Port);

}

 

 

 

void loop()

{

  int packetSize = Udp.parsePacket();

  if (packetSize)

  {

    int len = Udp.read(packetBuffer, 255);

    if (len > 0)packetBuffer[len] = 0;

    //unit=atoi(packetBuffer);

    Serial.println(packetBuffer);

         

                 /* get the first token */

                token = strtok(packetBuffer, s);

                int w = 0;

   

                while( token != NULL ) 

               {

                  

                  if(w == 0){

                  strcpy(Rssid,token);

                  Serial.println(Rssid);

                  w++;

                  }else if(w == 1){

                    strcpy(Rpsk,token);

                    Serial.println(Rpsk);

                  }

                  token = strtok(NULL, s);

               }

 

                int retVal;

                retVal = sl_WlanSetMode(ROLE_STA);

                retVal = sl_Stop(30);

                retVal= sl_Start(NULL, NULL, NULL);

 

 

                // attempt to connect to Wifi network:

                Serial.print("Attempting to connect to Network named: ");

                delay(5000);

                // print the network name (SSID);

                Serial.println(Rssid);

                // Connect to WPA/WPA2 network. Change this line if using open or WEP network:

                WiFi._initialized = false;

                WiFi._connecting = false;

                WiFi.begin(Rssid, Rpsk);

                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");

 

                // you're connected now, so print out the status

                printWifiStatus();

                Serial.println("Starting webserver on port 80");

                server.begin();                           // start the web server on port 80

                Serial.println("Webserver started!");

   }

   int i = 0;

  WiFiClient client = server.available();   // listen for incoming clients

 

  if (client) {                             // if you get a client,

    Serial.println("new client");           // print a message out the serial port

    char buffer[150] = {0};                 // make a buffer to hold incoming data

    while (client.connected()) {            // loop while the client's connected

      if (client.available()) {             // if there's bytes to read from the client,

        char c = client.read();             // read a byte, then

        Serial.write
Link to post
Share on other sites

@@PaoloIT

 

Hi,

I was facing issues with moving from AP mode to STA mode. But I fixed it.

I am using Energia and I am using UDP packets to send ssid and password. Now the problem that I face is that I want my cc3200 to remember the last ssid.

At present when a power recycle is done again the AP mode starts.

How can I store the last ssid. Kindly provide some help in the form of energia code.

 

Here is the code.

 

 
#ifndef __CC3200R1M1RGC__
#include <SPI.h>
#endif
#include <WiFi.h>
const char ssid[] = "CC3200";
const char wifipw[] = "password";
 
const char s[2] = "-";
char *token;
 
String y = "";
char Rssid[100],Rpsk[100];
 
unsigned int Port =2390;
char packetBuffer[255];
IPAddress Ip(255,255,255,255);
 
char b[1024];
int j=0;
 
WiFiUDP Udp;
 
void setup()
{
  Serial.begin(115200);
  pinMode(RED_LED, OUTPUT);  // LED will toggle when clients connect/disconnect
  digitalWrite(RED_LED, LOW);
  
  Serial.print("Setting up Access Point named: ");
  Serial.println(ssid);
  Serial.print("AP uses WPA and password is: ");
  Serial.println(wifipw);
  
  WiFi.beginNetwork((char *)ssid, (char *)wifipw);
 
  while (WiFi.localIP() == INADDR_NONE) 
  {
    // print dots while we wait for the AP config to complete
    Serial.print('.');
    delay(300);
  }
  Serial.println();
  Serial.println("AP active.");
  printWifiStatus();
  Udp.begin(Port);
}
 
unsigned int num_clients = 0;
 
void loop()
{
  
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    int len = Udp.read(packetBuffer, 255);
    if (len > 0)packetBuffer[len] = 0;
    //unit=atoi(packetBuffer);
    Serial.println(packetBuffer);
         
                 /* get the first token */
                token = strtok(packetBuffer, s);
                int w = 0;
   
                while( token != NULL ) 
               {
                  
                  if(w == 0){
                  strcpy(Rssid,token);
                  Serial.println(Rssid);
                  w++;
                  }else if(w == 1){
                    strcpy(Rpsk,token);
                    Serial.println(Rpsk);
                  }
                  token = strtok(NULL, s);
               }
 
                int retVal;
                retVal = sl_WlanSetMode(ROLE_STA);
                retVal = sl_Stop(30);
                retVal= sl_Start(NULL, NULL, NULL);
 
 
                // attempt to connect to Wifi network:
                Serial.print("Attempting to connect to Network named: ");
                delay(5000);
                // print the network name (SSID);
                Serial.println(Rssid);
                // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
                WiFi.begin(Rssid, Rpsk);
                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");
 
                // you're connected now, so print out the status
                printWifiStatus();
   }
 
}
 
void printWifiStatus() {
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  sprintf(b,"%d.%d.%d.%d,",ip[0],ip[1],ip[2],ip[3] );
  Serial.print("IP Address: ");
  Serial.println(ip);
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...