//!!Ignore key index!!// int WiFiClass::begin(char* ssid, uint8_t key_idx, char* key) { // // If we already called begin and are already connecting // then return the status. This prevents sl_WlanConnect() // from being called repeatedly. // if(_connecting) { delay(500); return status(); } // //initialize the simplelink driver and make sure it was a success // bool init_success = WiFiClass::init(); if (!init_success) { return WL_CONNECT_FAILED; } sl_WlanProfileDel(WLAN_DEL_ALL_PROFILES); // // Set IP address configuration to DHCP if needed // setIpDefaults(); sl_WlanPolicySet(SL_POLICY_CONNECTION , SL_CONNECTION_POLICY(1,1,0,0,0), 0, 0); // //get name length and set security type to WEP //add key and keylength to security parameters // int NameLen = strlen(ssid); SlSecParams_t SecParams = {0}; SecParams.Type = SL_SEC_TYPE_WEP; SecParams.Key = (signed char *)key; SecParams.KeyLen = strlen(key); // //Connect to the access point (non enterprise, so 5th argument is NULL); //also mac address parameter set as null (3rd argument) // int iRet = sl_WlanConnect((signed char*)ssid, NameLen, NULL, &SecParams, NULL); // //return appropriate status as described by arduino wifi library //the WiFiClass:WiFi_status is handled by the WlanEvenHandler //in SimpleLinkCallbacks.cpp. However, if iRet < 0, there was an error // if (iRet == 0) { sl_WlanProfileAdd((signed char*)ssid, NameLen, 0, &SecParams, 0, 6, 0); _connecting = true; return status(); } else { return WL_CONNECT_FAILED; } }
void WlanConnect() { //Add Profile sl_WlanProfileAdd(g_cWlanSSID, strlen((char*)g_cWlanSSID), 0, &g_SecParams, 0,g_ucPriority,0); //Connecting to the Access point sl_WlanConnect(g_cWlanSSID, strlen((char*)g_cWlanSSID), 0, &g_SecParams, 0); //waiting for the device to connect to the AP and obtain ip address while (g_uiConnectTimeoutCnt<CONNECTION_TIMEOUT_COUNT && ((g_ucConnectionStatus == 0) || (g_ucIpObtained == 0))) { osi_Sleep(1); //Sleep 1 millisecond g_uiConnectTimeoutCnt++; } g_uiConnectTimeoutCnt = 0; }
/* * Application's entry point */ int main(int argc, char** argv) { SlSecParams_t secParams = {0}; _i32 retVal = -1; retVal = initializeAppVariables(); ASSERT_ON_ERROR(retVal); /* Stop WDT and initialize the system-clock of the MCU */ stopWDT(); initClk(); /* Configure command line interface */ CLI_Configure(); displayBanner(); /* * Following function configures the device to default state by cleaning * the persistent settings stored in NVMEM (viz. connection profiles & * policies, power policy etc.) * * Applications may choose to skip this step if the developer is sure * that the device is in its default state at start of application * * Note that all profiles and persistent settings that were done on the * device will be lost */ retVal = configureSimpleLinkToDefaultState(); if(retVal < 0) { if (DEVICE_NOT_IN_STATION_MODE == retVal) CLI_Write(" Failed to configure the device in its default state \n\r"); LOOP_FOREVER(); } CLI_Write(" Device is configured in default state \n\r"); /* * Initializing the CC3100 device * Assumption is that the device is configured in station mode already * and it is in its default state */ retVal = sl_Start(0, 0, 0); if ((retVal < 0) || (ROLE_STA != retVal) ) { CLI_Write(" Failed to start the device \n\r"); LOOP_FOREVER(); } CLI_Write(" Device started as STATION \n\r"); /* Delete all profiles (0xFF) stored and reset policy settings */ retVal = sl_WlanProfileDel(0xFF); if(retVal < 0) { LOOP_FOREVER(); } retVal = sl_WlanPolicySet(SL_POLICY_CONNECTION , SL_CONNECTION_POLICY(0, 0, 0, 0, 0), 0, 0); if(retVal < 0) { LOOP_FOREVER(); } /* Add unsecured AP profile with priority 6 (7 is highest) */ retVal = sl_WlanProfileAdd((_i8 *)UNSEC_SSID_NAME, pal_Strlen(UNSEC_SSID_NAME), g_BSSID, 0, 0, 6, 0); if(retVal < 0) { LOOP_FOREVER(); } /* Add WPA2 secured AP profile with priority 7 (highest) */ secParams.Type = SL_SEC_TYPE_WPA; secParams.Key = SEC_SSID_KEY; secParams.KeyLen = pal_Strlen(SEC_SSID_KEY); retVal = sl_WlanProfileAdd((_i8 *)SEC_SSID_NAME, pal_Strlen(SEC_SSID_NAME), g_BSSID, &secParams, 0, 7, 0); if(retVal < 0) { LOOP_FOREVER(); } /* * Enable auto connect (connection to stored profiles according to priority) * Connection should first be established to higher (secured) profile as in * this example */ retVal = sl_WlanPolicySet(SL_POLICY_CONNECTION , SL_CONNECTION_POLICY(1,0,0,0,0), 0, 0); if(retVal < 0) { LOOP_FOREVER(); } /* Wait for the connection to be established */ while((!IS_CONNECTED(g_Status)) || (!IS_IP_ACQUIRED(g_Status))) { _SlNonOsMainLoopTask(); } CLI_Write(" Device connected to the AP using 'auto' connection policy\n\r"); /* Delete all profiles (0xFF) stored */ retVal = sl_WlanProfileDel(0xFF); if(retVal < 0) { LOOP_FOREVER(); } g_Status = 0; /* Restart the device */ retVal = sl_Stop(SL_STOP_TIMEOUT); if(retVal < 0) { LOOP_FOREVER(); } retVal = sl_Start(0, 0, 0); if ((retVal < 0) || (ROLE_STA != retVal) ) { CLI_Write(" Failed to start the device \n\r"); LOOP_FOREVER(); } /* * Set connection policy to Fast - Device will connect to the last connected AP. * This feature can be used to reconnect to AP */ retVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 1, 0, 0, 0), 0, 0); if(retVal < 0) { LOOP_FOREVER(); } /* Connect to the open AP */ retVal = establishConnectionWithAP(); if(retVal < 0) { CLI_Write(" Failed to establish connection w/ an AP \n\r"); LOOP_FOREVER(); } CLI_Write(" Device was connected to the AP using host-driver API \n\r"); g_Status = 0; /* Status variable */ /* Restart the Device */ retVal = sl_Stop(SL_STOP_TIMEOUT); if(retVal < 0) { LOOP_FOREVER(); } retVal = sl_Start(0, 0, 0); if ((retVal < 0) || (ROLE_STA != retVal) ) { CLI_Write(" Failed to start the device \n\r"); LOOP_FOREVER(); } /* Wait for the connection to be established */ while((!IS_CONNECTED(g_Status)) || (!IS_IP_ACQUIRED(g_Status))) { _SlNonOsMainLoopTask(); } CLI_Write(" Device connected to the AP using 'fast' connection policy\n\r"); /* Delete all profiles (0xFF) stored */ retVal = sl_WlanProfileDel(0xFF); if(retVal < 0) { LOOP_FOREVER(); } /* Set Auto SmartConfig policy */ retVal = sl_WlanPolicySet(SL_POLICY_CONNECTION , SL_CONNECTION_POLICY(1,0,0,0,1), 0, 0); if(retVal < 0) { LOOP_FOREVER(); } /* Restart the Device */ g_Status = 0; retVal = sl_Stop(SL_STOP_TIMEOUT); if(retVal < 0) { LOOP_FOREVER(); } retVal = sl_Start(0, 0, 0); if ((retVal < 0) || (ROLE_STA != retVal) ) { CLI_Write(" Failed to start the device \n\r"); LOOP_FOREVER(); } /* * After restarting , device will wait for a command for 2 second and start * the SmartConfig process if no command is received */ CLI_Write(" Device waiting to be connected using SmartConfig technology \n\r"); while((!IS_CONNECTED(g_Status)) || (!IS_IP_ACQUIRED(g_Status))) { _SlNonOsMainLoopTask(); } CLI_Write(" Device connected to the AP \n\r"); /* * Cleaning all profiles and setting the default connection policy before exiting the application * Set connection policy to Auto + SmartConfig (Device's default connection policy) */ retVal = sl_WlanProfileDel(0xFF); if(retVal < 0) { LOOP_FOREVER(); } retVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 0, 0, 0, 1), NULL, 0); if(retVal < 0) { LOOP_FOREVER(); } return 0; }
//**************************************************************************** // //! \brief Configuring the Connection Policy //! //! This function configures different Connection Policy such as FAST,AUTO,OPEN //! //! \param[in] None //! //! \return 0 on success else error code //! //! \note // //**************************************************************************** static long SetConnectionPolicy() { unsigned char policyVal; long lRetVal = -1; /* Clear all stored profiles and reset the policies */ lRetVal = sl_WlanProfileDel(0xFF); ASSERT_ON_ERROR(lRetVal); lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(0,0,0,0,0), 0, 0); ASSERT_ON_ERROR(lRetVal); //Add Profile /* user needs to change SSID_NAME = "<Secured AP>" SECURITY_TYPE = SL_SEC_TYPE_WPA SECURITY_KEY = "<password>" and set the priority as per requirement to connect with a secured AP */ SlSecParams_t secParams; secParams.Key = SECURITY_KEY; secParams.KeyLen = strlen(SECURITY_KEY); secParams.Type = SECURITY_TYPE; lRetVal = sl_WlanProfileAdd(SSID_NAME,strlen(SSID_NAME),0,&secParams,0,1,0); ASSERT_ON_ERROR(lRetVal); //set AUTO policy lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1,0,0,0,0), &policyVal, 1 /*PolicyValLen*/); ASSERT_ON_ERROR(lRetVal); //wait until IP is acquired while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) { _SlNonOsMainLoopTask(); } CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION); CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED); // // ****** Put breakpoint here ****** // If control comes here- means device connected to AP in auto mode // // Disconnect from AP lRetVal = sl_WlanDisconnect(); if(0 == lRetVal) { // Wait while(IS_CONNECTED(g_ulStatus)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } //delete profiles lRetVal = sl_WlanProfileDel(0xFF); ASSERT_ON_ERROR(lRetVal); //set Auto SmartConfig policy lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1,0,0,0,1), &policyVal,0); ASSERT_ON_ERROR(lRetVal); // reset the NWP lRetVal = ResetNwp(); ASSERT_ON_ERROR(lRetVal); // wait for smart config to complete and device to connect to an AP //wait until IP is acquired while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) { _SlNonOsMainLoopTask(); } CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION); CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED); // // ****** Put breakpoint here ****** // If control comes here- means device connected to AP in smartconfig mode // /* Delete all profiles (0xFF) stored */ lRetVal = sl_WlanProfileDel(0xFF); ASSERT_ON_ERROR(lRetVal); // reset the NWP lRetVal = ResetNwp(); ASSERT_ON_ERROR(lRetVal); /* Set connection policy to Fast, Device will connect to last connected AP. * This feature can be used to reconnect to AP */ lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION , \ SL_CONNECTION_POLICY(1,1,0,0,0), 0, 0); ASSERT_ON_ERROR(lRetVal); /* Connect to the open AP */ lRetVal = sl_WlanConnect(SSID_NAME, strlen(SSID_NAME), 0, 0, 0); ASSERT_ON_ERROR(lRetVal); //wait until IP is acquired while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) { _SlNonOsMainLoopTask(); } CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION); CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED); // Add unsecured AP profile with priority 6 (7 is highest) // Because of a limitation in SDK-0.5 which prevents the automatic addition // of the profile (fast), the application is required to explicitly add it. // The limitation shall be addressed in subsequent SDK release, and the below // lines for adding the profile can be removed then...! secParams.Key = ""; secParams.KeyLen = 0; secParams.Type = SL_SEC_TYPE_OPEN; lRetVal = sl_WlanProfileAdd((signed char*)SSID_NAME, strlen(SSID_NAME), 0, &secParams, 0, 6, 0); ASSERT_ON_ERROR(lRetVal); // reset the NWP lRetVal = ResetNwp(); ASSERT_ON_ERROR(lRetVal); /* Wait for the connection to be established */ while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) { _SlNonOsMainLoopTask(); } // // ****** Put breakpoint here ****** // If control comes here- means device connected to AP in FAST mode // //remove all policies lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(0,0,0,0,0), &policyVal,0); ASSERT_ON_ERROR(lRetVal); return SUCCESS; }
//***************************************************************************** // //! Task implementing MQTT client communication to other web client through //! a broker //! //! \param none //! //! This function //! 1. Initializes network driver and connects to the default AP //! 2. Initializes the mqtt library and set up MQTT connection configurations //! 3. set up the button events and their callbacks(for publishing) //! 4. handles the callback signals //! //! \return None //! //***************************************************************************** void MqttClient(void *pvParameters) { long lRetVal = -1; int iCount = 0; int iNumBroker = 0; int iConnBroker = 0; event_msg RecvQue; unsigned char policyVal; connect_config *local_con_conf = (connect_config *)app_hndl; // // Configure LED // GPIO_IF_LedConfigure(LED1|LED2|LED3); GPIO_IF_LedOff(MCU_RED_LED_GPIO); GPIO_IF_LedOff(MCU_GREEN_LED_GPIO); // // Reset The state of the machine // Network_IF_ResetMCUStateMachine(); // // Start the driver // lRetVal = Network_IF_InitDriver(ROLE_STA); if(lRetVal < 0) { UART_PRINT("Failed to start SimpleLink Device\n\r",lRetVal); LOOP_FOREVER(); } // switch on Green LED to indicate Simplelink is properly up GPIO_IF_LedOn(MCU_ON_IND); // Start Timer to blink Red LED till AP connection LedTimerConfigNStart(); // Initialize AP security params SecurityParams.Key = (signed char *)SECURITY_KEY; SecurityParams.KeyLen = strlen(SECURITY_KEY); SecurityParams.Type = SECURITY_TYPE; // // Connect to the Access Point // lRetVal = Network_IF_ConnectAP(SSID_NAME, SecurityParams); if(lRetVal < 0) { UART_PRINT("Connection to an AP failed\n\r"); LOOP_FOREVER(); } lRetVal = sl_WlanProfileAdd(SSID_NAME,strlen(SSID_NAME),0,&SecurityParams,0,1,0); //set AUTO policy lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1,0,0,0,0), &policyVal, 1 /*PolicyValLen*/); // // Disable the LED blinking Timer as Device is connected to AP // LedTimerDeinitStop(); // // Switch ON RED LED to indicate that Device acquired an IP // GPIO_IF_LedOn(MCU_IP_ALLOC_IND); UtilsDelay(20000000); GPIO_IF_LedOff(MCU_RED_LED_GPIO); GPIO_IF_LedOff(MCU_ORANGE_LED_GPIO); GPIO_IF_LedOff(MCU_GREEN_LED_GPIO); // // Register Push Button Handlers // Button_IF_Init(pushButtonInterruptHandler2,pushButtonInterruptHandler3); // // Initialze MQTT client lib // lRetVal = sl_ExtLib_MqttClientInit(&Mqtt_Client); if(lRetVal != 0) { // lib initialization failed UART_PRINT("MQTT Client lib initialization failed\n\r"); LOOP_FOREVER(); } /******************* connection to the broker ***************************/ iNumBroker = sizeof(usr_connect_config)/sizeof(connect_config); if(iNumBroker > MAX_BROKER_CONN) { UART_PRINT("Num of brokers are more then max num of brokers\n\r"); LOOP_FOREVER(); } connect_to_broker: while(iCount < iNumBroker) { //create client context local_con_conf[iCount].clt_ctx = sl_ExtLib_MqttClientCtxCreate(&local_con_conf[iCount].broker_config, &local_con_conf[iCount].CallBAcks, &(local_con_conf[iCount])); // // Set Client ID // sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx, SL_MQTT_PARAM_CLIENT_ID, local_con_conf[iCount].client_id, strlen((char*)(local_con_conf[iCount].client_id))); // // Set will Params // if(local_con_conf[iCount].will_params.will_topic != NULL) { sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx, SL_MQTT_PARAM_WILL_PARAM, &(local_con_conf[iCount].will_params), sizeof(SlMqttWill_t)); } // // setting username and password // if(local_con_conf[iCount].usr_name != NULL) { sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx, SL_MQTT_PARAM_USER_NAME, local_con_conf[iCount].usr_name, strlen((char*)local_con_conf[iCount].usr_name)); if(local_con_conf[iCount].usr_pwd != NULL) { sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx, SL_MQTT_PARAM_PASS_WORD, local_con_conf[iCount].usr_pwd, strlen((char*)local_con_conf[iCount].usr_pwd)); } } // // connectin to the broker // if((sl_ExtLib_MqttClientConnect((void*)local_con_conf[iCount].clt_ctx, local_con_conf[iCount].is_clean, local_con_conf[iCount].keep_alive_time) & 0xFF) != 0) { UART_PRINT("\n\rBroker connect fail for conn no. %d \n\r",iCount+1); //delete the context for this connection sl_ExtLib_MqttClientCtxDelete(local_con_conf[iCount].clt_ctx); break; } else { UART_PRINT("\n\rSuccess: conn to Broker no. %d\n\r ", iCount+1); local_con_conf[iCount].is_connected = true; iConnBroker++; } // // Subscribe to topics // if(sl_ExtLib_MqttClientSub((void*)local_con_conf[iCount].clt_ctx, local_con_conf[iCount].topic, local_con_conf[iCount].qos, TOPIC_COUNT) < 0) { UART_PRINT("\n\r Subscription Error for conn no. %d\n\r", iCount+1); UART_PRINT("Disconnecting from the broker\r\n"); sl_ExtLib_MqttClientDisconnect(local_con_conf[iCount].clt_ctx); local_con_conf[iCount].is_connected = false; //delete the context for this connection sl_ExtLib_MqttClientCtxDelete(local_con_conf[iCount].clt_ctx); iConnBroker--; break; } else { int iSub; UART_PRINT("Client subscribed on following topics:\n\r"); for(iSub = 0; iSub < local_con_conf[iCount].num_topics; iSub++) { UART_PRINT("%s\n\r", local_con_conf[iCount].topic[iSub]); } } iCount++; } if(iConnBroker < 1) { // // no succesful connection to broker // goto end; } iCount = 0; for(;;) { osi_MsgQRead( &g_PBQueue, &RecvQue, OSI_WAIT_FOREVER); if(PUSH_BUTTON_SW2_PRESSED == RecvQue.event) { Button_IF_EnableInterrupt(SW2); // // send publish message // sl_ExtLib_MqttClientSend((void*)local_con_conf[iCount].clt_ctx, pub_topic_sw2,data_sw2,strlen((char*)data_sw2),QOS2,RETAIN); UART_PRINT("\n\r CC3200 Publishes the following message \n\r"); UART_PRINT("Topic: %s\n\r",pub_topic_sw2); UART_PRINT("Data: %s\n\r",data_sw2); } else if(PUSH_BUTTON_SW3_PRESSED == RecvQue.event) { Button_IF_EnableInterrupt(SW3); // // send publish message // sl_ExtLib_MqttClientSend((void*)local_con_conf[iCount].clt_ctx, pub_topic_sw3,data_sw3,strlen((char*)data_sw3),QOS2,RETAIN); UART_PRINT("\n\r CC3200 Publishes the following message \n\r"); UART_PRINT("Topic: %s\n\r",pub_topic_sw3); UART_PRINT("Data: %s\n\r",data_sw3); } else if(BROKER_DISCONNECTION == RecvQue.event) { iConnBroker--; /* Derive the value of the local_con_conf or clt_ctx from the message */ sl_ExtLib_MqttClientCtxDelete(((connect_config*)(RecvQue.hndl))->clt_ctx); if(!IS_CONNECTED(g_ulStatus)) { UART_PRINT("device has disconnected from AP \n\r"); UART_PRINT("retry connection to the AP\n\r"); while(!(IS_CONNECTED(g_ulStatus)) || !(IS_IP_ACQUIRED(g_ulStatus))) { osi_Sleep(10); } goto connect_to_broker; } if(iConnBroker < 1) { // // device not connected to any broker // goto end; } } } end: // // Deinitializating the client library // sl_ExtLib_MqttClientExit(); UART_PRINT("\n\r Exiting the Application\n\r"); LOOP_FOREVER(); }