//***************************************************************************** // //! \brief Starts Smart Configuration //! //! \param none //! //! \return void //! \note //! \warning //***************************************************************************** void SmartConfigTask(void* pValue) { long lRetVal = -1; DispatcherUartSendPacket((char*)pucUARTSmartConfigString, sizeof(pucUARTSmartConfigString)); //Turn off the Network Status LED GPIO_IF_LedOff(MCU_IP_ALLOC_IND); LedTimerConfigNStart(); //Reset the Network Status before Entering Smart Config Network_IF_UnsetMCUMachineState(STATUS_BIT_CONNECTION); Network_IF_UnsetMCUMachineState(STATUS_BIT_IP_AQUIRED); lRetVal = SmartConfigConnect(); if(lRetVal < 0) { ERR_PRINT(lRetVal); LOOP_FOREVER(); } // // Wait until IP is acquired // while (!(IS_CONNECTED(Network_IF_CurrentMCUState())) || !(IS_IP_ACQUIRED(Network_IF_CurrentMCUState()))); LedTimerDeinitStop(); // Red LED on GPIO_IF_LedOn(MCU_IP_ALLOC_IND); //Enable GPIO Interrupt Button_IF_EnableInterrupt(SW2); }
void onButton2Pressed(void *p) { UART_PRINT("[Blink] Cycle LED state\r\n"); Button_IF_EnableInterrupt(SW2); cycleLedState(); saveLedState(); }
//***************************************************************************** // //! \brief Generates Random Message and Sends Email to the preconfigured //! Recipient Email Id //! //! \param pValue - pointer to Input Data //! //! \return void //! \note //! \warning //***************************************************************************** void PushButtonMailSend(void* pValue) { int iIndex; long lRetVal = -1; // reset Orange LED GPIO_IF_LedOff(MCU_SENDING_DATA_IND); if(!IS_CONNECTED(Network_IF_CurrentMCUState())) { LedTimerConfigNStart(); lRetVal = Network_IF_ConnectAP(SSID_NAME,SecurityParams); if(lRetVal < 0) { UART_PRINT("Error: %d Connecting to an AP.\n\r",lRetVal); return; } } // // 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); MAP_UtilsDelay(10000); GenerateRandomMessage(); g_cConnectStatus = sl_NetAppEmailConnect(); // If return -1, throw connect error if(g_cConnectStatus == -1) { DispatcherUartSendPacket((char*)pucUARTErrorSocketCreateString, \ sizeof(pucUARTErrorSocketCreateString)); } // If return -2, throw socket option error if(g_cConnectStatus == -2) { DispatcherUartSendPacket((char*)pucUARTErrorSocketOptionString, \ sizeof(pucUARTErrorSocketOptionString)); } if(g_cConnectStatus == 0) { SlNetAppServerError_t sEmailErrorInfo; long lRetCode = SL_EMAIL_ERROR_FAILED; if((lRetCode = sl_NetAppEmailSend(pcEmailto,pcEmailsubject,\ pcEmailmessage, \ &sEmailErrorInfo)) == SL_EMAIL_ERROR_NONE) { // Blink LED7 to indicate email has been sent for(iIndex=0 ;iIndex<5 ;iIndex++) { MAP_UtilsDelay(6000000); GPIO_IF_LedOff(MCU_SENDING_DATA_IND); MAP_UtilsDelay(6000000); GPIO_IF_LedOn(MCU_SENDING_DATA_IND); } DispatcherUartSendPacket((char*)putUARTFinishString, \ sizeof(putUARTFinishString)); } else { lRetVal = EmailHandleERROR(lRetCode,(char*)sEmailErrorInfo.Value); } UART_PRINT("Cmd#"); } //Enable GPIO Interrupt Button_IF_EnableInterrupt(SW3); return; }
//**************************************************************************** // //! Task function implementing the OTA update functionality //! //! \param none //! //! \return None. // //**************************************************************************** int OTAUpdateTask(void *pvParameters) { int iRet; int SetCommitInt = 1; unsigned char ucVendorStr[50]; unsigned long ulVendorStrLen; static char cTaskOwnState = OTA_UPDATE_STATE_WAIT_START; switch(cTaskOwnState) { case OTA_UPDATE_STATE_WAIT_START: cTaskOwnState = OTA_UPDATE_STATE_WAIT_BTN; // // Wait for sl_Start and wlan connect to complete // TaskSyncObjWait(&g_NetStatSyncObj); break; case OTA_UPDATE_STATE_WAIT_BTN: cTaskOwnState = OTA_UPDATE_STATE_INIT; // // Wait for button press // TaskSyncObjWait(&g_OTAStatSyncObj); Button_IF_EnableInterrupt(SW3); break; case OTA_UPDATE_STATE_INIT: // // Create vendor string from NWP version and Vendor Prefix // strcpy((char *)ucVendorStr,OTA_VENDOR_STRING); ulVendorStrLen = strlen(OTA_VENDOR_STRING); sprintf((char *)&ucVendorStr[ulVendorStrLen],"%02lu", sDisplayInfo.sNwpVersion.NwpVersion[3]); // // Initializa OTA service // OTAServerInfoSet(&pvOtaApp,(char *)ucVendorStr); // // Set the OTA status and system state to RUN // sDisplayInfo.iOTAStatus = OTA_INPROGRESS; g_ulSysState = SYS_STATE_RUN; cTaskOwnState = OTA_UPDATE_STATE_RUN; DisplayRefresh(); break; case OTA_UPDATE_STATE_RUN: iRet = sl_extLib_OtaRun(pvOtaApp); if ( iRet < 0 ) { if( RUN_STAT_ERROR_CONTINUOUS_ACCESS_FAILURES == iRet ) { // // Schelude a restart. // sDisplayInfo.iOTAStatus = OTA_ERROR; g_ulSysState = SYS_STATE_REBOOT; } else { sDisplayInfo.iOTAStatus = OTA_ERROR_RETRY; } } else if( iRet == RUN_STAT_NO_UPDATES ) { sDisplayInfo.iOTAStatus = OTA_NO_UPDATES; g_ulSysState = SYS_STATE_WAIT; cTaskOwnState = OTA_UPDATE_STATE_WAIT_BTN; // // Enable button Interrupt // Button_IF_EnableInterrupt(SW3); DisplayRefresh(); } else if ((iRet & RUN_STAT_DOWNLOAD_DONE)) { // // Set OTA File for testing // iRet = sl_extLib_OtaSet(pvOtaApp, EXTLIB_OTA_SET_OPT_IMAGE_TEST, sizeof(int), (_u8 *)&SetCommitInt); sDisplayInfo.iOTAStatus = OTA_DONE; if (iRet & (OTA_ACTION_RESET_MCU| OTA_ACTION_RESET_NWP) ) { g_ulSysState = SYS_STATE_REBOOT; cTaskOwnState = OTA_UPDATE_STATE_DONE; } else { g_ulSysState = SYS_STATE_WAIT; cTaskOwnState = OTA_UPDATE_STATE_WAIT_BTN; // // Enable button Interrupt // Button_IF_EnableInterrupt(SW3); } } else if(sDisplayInfo.iOTAStatus == OTA_ERROR_RETRY) { sDisplayInfo.iOTAStatus = OTA_INPROGRESS; } DisplayRefresh(); break; default: return TASK_RET_DONE; } return TASK_RET_IN_PROG; }
//***************************************************************************** // //! 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(); }
//Initialize buttons & interrupt void ButtonInit() { Button_IF_Init(SW2Handler,SW3Handler); Button_IF_EnableInterrupt(SW2|SW3); }
void enableButtons() { Button_IF_Init(postButton2Msg, postButton3Msg); Button_IF_EnableInterrupt(SW2 | SW3); }
void onButton3Pressed(void *p) { Button_IF_EnableInterrupt(SW3); factoryReset(); }