124 lines
3.4 KiB
C
124 lines
3.4 KiB
C
#include "mqtt/mqtt.h"
|
|
|
|
void MQTTTask(void* args){
|
|
LOG_TRACE("MQTT task started.");
|
|
mq_attr attr;
|
|
MQTTClient_Handle mqttClientHandle;
|
|
|
|
SPI_init();
|
|
|
|
int32_t ret = ti_net_SlNet_initConfig();
|
|
if(0 != ret)
|
|
{
|
|
LOG_ERROR("Failed to initialize SlNetSock\n\r");
|
|
}
|
|
|
|
attr.mq_maxmsg = 10;
|
|
attr.mq_msgsize = sizeof(struct msgQueue);
|
|
appQueue = mq_open("appQueue", O_CREAT, 0, &attr);
|
|
if(((int)appQueue) <= 0){
|
|
while(1);
|
|
}
|
|
|
|
// ret = WifiInit();
|
|
// if(ret < 0){
|
|
// while(1);
|
|
// }
|
|
|
|
ret = MQTT_IF_Init(mqttInitParams);
|
|
if(ret < 0){
|
|
while(1);
|
|
}
|
|
|
|
/*
|
|
* In case a persistent session is being used, subscribe is called before connect so that the module
|
|
* is aware of the topic callbacks the user is using. This is important because if the broker is holding
|
|
* messages for the client, after CONNACK the client may receive the messages before the module is aware
|
|
* of the topic callbacks. The user may still call subscribe after connect but have to be aware of this.
|
|
*/
|
|
// ret = MQTT_IF_Subscribe(mqttClientHandle, "Broker/To/cc32xx", MQTT_QOS_2, BrokerCB);
|
|
// ret |= MQTT_IF_Subscribe(mqttClientHandle, "cc32xx/ToggleLED1", MQTT_QOS_2, ToggleLED1CB);
|
|
// ret |= MQTT_IF_Subscribe(mqttClientHandle, "cc32xx/ToggleLED2", MQTT_QOS_2, ToggleLED2CB);
|
|
// ret |= MQTT_IF_Subscribe(mqttClientHandle, "cc32xx/ToggleLED3", MQTT_QOS_2, ToggleLED3CB);
|
|
if(ret < 0){
|
|
while(1);
|
|
}else{
|
|
LOG_INFO("Subscribed to all topics successfully\r\n");
|
|
}
|
|
|
|
mqttClientHandle = MQTT_IF_Connect(mqttClientParams, mqttConnParams, MQTT_EventCallback);
|
|
if(mqttClientHandle < 0){
|
|
while(1);
|
|
}
|
|
|
|
// wait for CONNACK
|
|
while(connected == 0);
|
|
|
|
|
|
struct msgQueue queueElement;
|
|
while(1){
|
|
LOG_TRACE("Running MQTT loop.");
|
|
mq_receive(appQueue, (char*)&queueElement, sizeof(struct msgQueue), NULL);
|
|
|
|
if(queueElement.event == APP_MQTT_PUBLISH){
|
|
LOG_TRACE("APP_MQTT_PUBLISH\r\n");
|
|
|
|
MQTT_IF_Publish(mqttClientHandle,
|
|
"cc32xx/ToggleLED1",
|
|
"LED 1 toggle\r\n",
|
|
strlen("LED 1 toggle\r\n"),
|
|
MQTT_QOS_2);
|
|
}
|
|
else if(queueElement.event == APP_MQTT_CON_TOGGLE){
|
|
|
|
LOG_TRACE("APP_MQTT_CON_TOGGLE %d\r\n", connected);
|
|
|
|
if(connected){
|
|
ret = MQTT_IF_Disconnect(mqttClientHandle);
|
|
if(ret >= 0){
|
|
connected = false;
|
|
}
|
|
}else{
|
|
mqttClientHandle = MQTT_IF_Connect(mqttClientParams, mqttConnParams, MQTT_EventCallback);
|
|
if((int)mqttClientHandle >= 0){
|
|
connected = true;
|
|
}
|
|
}
|
|
}
|
|
else if(queueElement.event == APP_MQTT_DEINIT){
|
|
break;
|
|
}
|
|
else if(queueElement.event == APP_BTN_HANDLER){
|
|
|
|
struct msgQueue queueElement;
|
|
|
|
// ret = detectLongPress();
|
|
// if(ret == 0){
|
|
//
|
|
//// LOG_TRACE("APP_BTN_HANDLER SHORT PRESS\r\n");
|
|
// queueElement.event = APP_MQTT_CON_TOGGLE;
|
|
// }
|
|
// else{
|
|
//
|
|
//// LOG_TRACE("APP_BTN_HANDLER LONG PRESS\r\n");
|
|
// queueElement.event = APP_MQTT_DEINIT;
|
|
// }
|
|
|
|
ret = mq_send(appQueue, (const char*)&queueElement, sizeof(struct msgQueue), 0);
|
|
if(ret < 0){
|
|
LOG_ERROR("msg queue send error %d", ret);
|
|
}
|
|
}
|
|
}
|
|
|
|
deinit = 1;
|
|
if(connected){
|
|
MQTT_IF_Disconnect(mqttClientHandle);
|
|
}
|
|
MQTT_IF_Deinit();
|
|
|
|
LOG_INFO("looping the MQTT functionality of the example for demonstration purposes only\r\n");
|
|
// sleep(2);
|
|
// goto MQTT_DEMO;
|
|
}
|