denk dat het nu werkt, maar ik kan niet testen :(

This commit is contained in:
Mats van Reenen 2020-10-27 16:11:53 +01:00
parent 24ccf86b7c
commit 7b722b8992
2 changed files with 59 additions and 61 deletions

View File

@ -1,43 +1,16 @@
#include "mqtt/mqtt.h"
void MQTTTask(void * args){
void MQTTTask(void* args){
mq_attr attr;
Timer_Params params;
// UART_Handle uartHandle;
MQTTClient_Handle mqttClientHandle;
// uartHandle = InitTerm();
// UART_control(uartHandle, UART_CMD_RXDISABLE, NULL);
// GPIO_init();
SPI_init();
// Timer_init();
int32_t ret = ti_net_SlNet_initConfig();
if(0 != ret)
{
LOG_ERROR("Failed to initialize SlNetSock\n\r");
}
// GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_OFF);
// GPIO_write(CONFIG_GPIO_LED_1, CONFIG_GPIO_LED_OFF);
// GPIO_write(CONFIG_GPIO_LED_2, CONFIG_GPIO_LED_OFF);
// GPIO_setCallback(CONFIG_GPIO_BUTTON_0, pushButtonPublishHandler);
// GPIO_setCallback(CONFIG_GPIO_BUTTON_1, pushButtonConnectionHandler);
// configuring the timer to toggle an LED until the AP is connected
// Timer_Params_init(&params);
// params.period = 1000000;
// params.periodUnits = Timer_PERIOD_US;
// params.timerMode = Timer_CONTINUOUS_CALLBACK;
// params.timerCallback = (Timer_CallBackFxn)timerLEDCallback;
// timer0 = Timer_open(CONFIG_TIMER_0, &params);
// if (timer0 == NULL) {
// LOG_ERROR("failed to initialize timer\r\n");
// while(1);
// }
attr.mq_maxmsg = 10;
attr.mq_msgsize = sizeof(struct msgQueue);
@ -51,23 +24,6 @@ void MQTTTask(void * args){
while(1);
}
// GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_OFF);
// GPIO_write(CONFIG_GPIO_LED_1, CONFIG_GPIO_LED_OFF);
// GPIO_write(CONFIG_GPIO_LED_2, CONFIG_GPIO_LED_OFF);
// params.period = 1500000;
// params.periodUnits = Timer_PERIOD_US;
// params.timerMode = Timer_ONESHOT_CALLBACK;
// params.timerCallback = (Timer_CallBackFxn)timerCallback;
// timer0 = Timer_open(CONFIG_TIMER_0, &params);
// if (timer0 == NULL) {
// LOG_ERROR("failed to initialize timer\r\n");
// while(1);
// }
// MQTT_DEMO:
ret = MQTT_IF_Init(mqttInitParams);
if(ret < 0){
while(1);
@ -79,10 +35,10 @@ void MQTTTask(void * args){
* 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);
// 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);
}
@ -98,8 +54,6 @@ void MQTTTask(void * args){
// wait for CONNACK
while(connected == 0);
// GPIO_enableInt(CONFIG_GPIO_BUTTON_0);
struct msgQueue queueElement;
while(1){
@ -124,12 +78,12 @@ void MQTTTask(void * args){
if(connected){
ret = MQTT_IF_Disconnect(mqttClientHandle);
if(ret >= 0){
connected = 0;
connected = false;
}
}else{
mqttClientHandle = MQTT_IF_Connect(mqttClientParams, mqttConnParams, MQTT_EventCallback);
if((int)mqttClientHandle >= 0){
connected = 1;
connected = true;
}
}
}

View File

@ -1,4 +1,12 @@
#include "./mqtt_if.h"
#include "mqtt_if.h"
#include <pthread.h>
#include <unistd.h>
#include <mqueue.h>
#include <ti/drivers/SPI.h>
#include <ti/net/mqtt/mqttclient.h>
// #define SL_TASKSTACKSIZE 2048
// #define SPAWN_TASK_PRIORITY 9
@ -25,7 +33,7 @@
mqd_t appQueue;
int connected;
int deinit;
Timer_Handle timer0;
//Timer_Handle timer0;
int longPress = 0;
/* Client ID */
@ -46,11 +54,11 @@ struct msgQueue
char* payload;
};
// MQTT_IF_InitParams_t mqttInitParams =
// {
// MQTT_MODULE_TASK_STACK_SIZE, // stack size for mqtt module - default is 2048
// MQTT_MODULE_TASK_PRIORITY // thread priority for MQTT - default is 2
// };
MQTT_IF_InitParams_t mqttInitParams =
{
2048, // stack size for mqtt module - default is 2048
2 // thread priority for MQTT - default is 2
};
MQTTClient_Will mqttWillParams =
{
@ -81,4 +89,40 @@ MQTTClient_ConnParams mqttConnParams =
0, // cipher for secure socket
0, // number of files for secure connection
NULL // secure files
};
};
void MQTT_EventCallback(int32_t event){
struct msgQueue queueElement;
switch(event){
case MQTT_EVENT_CONNACK:
deinit = 0;
connected = true;
LOG_INFO("MQTT_EVENT_CONNACK\r\n");
break;
case MQTT_EVENT_SUBACK:
LOG_INFO("MQTT_EVENT_SUBACK\r\n");
break;
case MQTT_EVENT_PUBACK:
LOG_INFO("MQTT_EVENT_PUBACK\r\n");
break;
case MQTT_EVENT_UNSUBACK:
LOG_INFO("MQTT_EVENT_UNSUBACK\r\n");
break;
case MQTT_EVENT_CLIENT_DISCONNECT:
connected = false;
LOG_INFO("MQTT_EVENT_CLIENT_DISCONNECT\r\n");
break;
case MQTT_EVENT_SERVER_DISCONNECT:
connected = false;
LOG_INFO("MQTT_EVENT_SERVER_DISCONNECT\r\n");
queueElement.event = APP_MQTT_CON_TOGGLE;
int res = mq_send(appQueue, (const char*)&queueElement, sizeof(struct msgQueue), 0);
if(res < 0){
LOG_ERROR("msg queue send error %d", res);
}
break;
case MQTT_EVENT_DESTROY:
LOG_INFO("MQTT_EVENT_DESTROY\r\n");
break;
}
}