Part Number: CC3220SF-LAUNCHXL
Hello experts,
I have an application in which CC3220SF launchpad is connected to another controller via UART1,PC via UART0,SD card and Now I want to connect it to a MQTT broker "broker.hivemq.com"
First,to get acquainted to MQTT ,I tried changing the broker url in MQTT demo provided in SDK ,added a topic to publish and the code works fine.
Till now I have only implemented the part where SL device connects to a WiFi network ,and then it tries to connect to the mqtt broker as I have stated above.
- Elaborating about the warning I got:
To implement MQTT in my application I ,added the proper includes, added ARM linker include paths and library locations and the code compiles but i get warning like
" Description Resource Path Location Type
#48-D incompatible redefinition of macro "O_CREAT" (declared at line 59 of "C:/ti/ccsv8/tools/compiler/ti-cgt-arm_18.12.0.LTS/include/file.h") .ccsproject /Dahipuri line 80, external location: C:\ti\simplelink_cc32xx_sdk_3_20_00_06\source\ti\posix\ccs\mqueue.h C/C++ Problem"
There are total 5 warning all related to mqueue.h for O_CREAT , O_EXCL ,O_RDONLY, O_RDWR,O_WRONLY.
2. Now moving on to MQTTClient_connect() not returning:
Firstly, I have added all the global includes,declarations from mqtt example 's mqtt_client_app.c file in my application's mainfile.I have added network_if , and client_cbs in my project directory and have included them in my main
I have a task running in the mainfile called as dahipuriMain() which is the only task that is ready to run as soon as SYSBIOS starts (other tasks are behind a semaphore).
In that task, i have added :
SlNetIf_init(0); SlNetIf_add(SLNETIF_ID_1, "CC32xx", (const SlNetIf_Config_t *)&SlNetIfConfigWifi, SLNET_IF_WIFI_PRIO); SlNetSock_init(0); SlNetUtil_init(0);
followed by:
pthread_attr_init(&pAttrs_spawn); priParam.sched_priority = SPAWN_TASK_PRIORITY; retc = pthread_attr_setschedparam(&pAttrs_spawn, &priParam); retc |= pthread_attr_setstacksize(&pAttrs_spawn, TASKSTACKSIZE); retc |= pthread_attr_setdetachstate(&pAttrs_spawn, PTHREAD_CREATE_DETACHED); retc = pthread_create(&spawn_thread, &pAttrs_spawn, sl_Task, NULL) gApConnectionState = Mqtt_IF_Connect(); if(gApConnectionState<0){ System_printf("problem connecting to AP\r\n"); } if(!Mqtt_start()){ System_printf("Message queue screwed up\r\n"); } retc=Mqtt_client_connect(); if(!retc) { System_printf("MQTT Connection failed\r\n"); } while (1) { UART_read(uart0, &input,1); UART_read(uart1, &input1,1); System_flush(); }
I have changed some of the functions from the MQTT Demo by removing display banners.
Mqtt_IF_Connect() is as follows:
int32_t Mqtt_IF_Connect() { int32_t lRetVal; char SSID_Remote_Name[32]; int8_t Str_Length; memset(SSID_Remote_Name, '\0', sizeof(SSID_Remote_Name)); Str_Length = strlen(SSID_NAME); if(Str_Length) { /*Copy the Default SSID to the local variable */ strncpy(SSID_Remote_Name, SSID_NAME, Str_Length); } /*Display Application Banner */ // DisplayBanner(APPLICATION_NAME); /*Reset The state of the machine */ Network_IF_ResetMCUStateMachine(); /*Start the driver */ lRetVal = Network_IF_InitDriver(ROLE_STA); if(lRetVal < 0) { System_printf("Failed to start SimpleLink Device\n\r", lRetVal); return(-1); } /*switch on Board_GPIO_LED2 to indicate Simplelink is properly up. */ // GPIO_write(Board_GPIO_LED2, Board_GPIO_LED_ON); /*Start Timer to blink Board_GPIO_LED0 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_Remote_Name, SecurityParams); if(lRetVal < 0) { System_printf("Connection to an AP failed\n\r"); return(-1); } sleep(2); return(0); }
Mqtt_start() is as follows:
bool Mqtt_start(){ mq_attr attr; unsigned mode = 0; /*sync object for inter thread communication */ attr.mq_maxmsg = 10; attr.mq_msgsize = sizeof(struct msgQueue); g_PBQueue = mq_open("g_PBQueue", O_CREAT, mode, &attr); if(g_PBQueue == NULL) { System_printf("MQTT Message Queue create fail\n\r"); //gInitState &= ~MQTT_INIT_STATE; return FALSE; } return TRUE; }
Mqtt_client_connect() is as follows:
int32_t Mqtt_client_connect(){ int32_t lRetVal = -1; int32_t iCount = 0; MqttClientExmple_params.clientId = ClientId; MqttClientExmple_params.connParams = &Mqtt_ClientCtx; MqttClientExmple_params.mqttMode31 = MQTT_3_1; MqttClientExmple_params.blockingSend = true; gMqttClient = MQTTClient_create(MqttClientCallback, &MqttClientExmple_params); if(gMqttClient == NULL) { System_printf("Mqtt connection failed\r\n"); return(-1); } MQTTClient_set(gMqttClient, MQTTClient_WILL_PARAM, &will_param, sizeof(will_param)); System_flush(); lRetVal = MQTTClient_connect(gMqttClient); UART_PRINT("lRetVal = %d",lRetVal); if(0 > lRetVal) { /*lib initialization failed */ UART_PRINT("Connection to broker failed, Error code: %d\n\r", lRetVal); gUiConnFlag = 0; } else { gUiConnFlag = 1; } /*Subscribe to topics when session is not stored by the server */ if((gUiConnFlag == 1) && (0 == lRetVal)) { uint8_t subIndex; MQTTClient_SubscribeParams subscriptionInfo[ SUBSCRIPTION_TOPIC_COUNT]; for(subIndex = 0; subIndex < SUBSCRIPTION_TOPIC_COUNT; subIndex++) { subscriptionInfo[subIndex].topic = topic[subIndex]; subscriptionInfo[subIndex].qos = qos[subIndex]; } if(MQTTClient_subscribe(gMqttClient, subscriptionInfo, SUBSCRIPTION_TOPIC_COUNT) < 0) { UART_PRINT("\n\r Subscription Error \n\r"); MQTTClient_disconnect(gMqttClient); gUiConnFlag = 0; } else { for(iCount = 0; iCount < SUBSCRIPTION_TOPIC_COUNT; iCount++) { UART_PRINT("Client subscribed on %s\n\r,", topic[iCount]); } } } return 0; }
I can successfully connect to Wifi as I get output on Console stating received IP( as I have attached)
I put a breakpoint just after MQTTClient_connect(); and it never occurs, I even put a breakpoint after Mqtt_client_connect() in my dahipurimain(main task) which is never reached.I am not sure what I have missed in this program.
I am adding mainfile of my application(it contains all the code that i have currently written) and the cio console output
Please help me out.
Thanks,
Vedant
(Please visit the site to view this file)