API  2.3.0
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
mqtt_cellular_2g_publisher.c

This is an example of how to manage the MQTT protocol using the cellular 2g device.

/* @file mqtt_cellular_2g_publisher.c
* @author TST
* @version 0.2
* @date 10/04/2014
* @brief This is an example of how to manage the MQTT library using the
* cellular 2g interface.
*
*
* This example is based on using a cellular 2g network using a cellular 2g
* interface. The cellular 2G in use is a SL6087 2G module manufactured by
* Sierra wireless.. This device has an embedded TCP/IP controller that enables
* easier internet connection.
*
* This example shows users how to publish data with different QoS(quality of
* service) in a MQTT server. To achive it, the example uses a MQTT open-source
* library based on the embedded C version of Paho from IBM and Eclipse
* foundation.
*
* Finally, to run this example users must have a server running so that this
* example publish data in an specific topic.
* This topic is "tst/tsmart", located in the root of your MQTT server, but
* users can change it, changing the proper definition: MQTT_TOPIC. In
* addition, users can send different data changing the predefined messages
* (MQTT_PAYLOAD_QOS_0, MQTT_PAYLOAD_QOS_1, MQTT_PAYLOAD_QOS_2) by custom
* messages.
*
*
* @note Users shall configure the MQTT connection parameters:
* - PIN ------------> Set your personal identification number, PIN code (line 90).
* - APN ------------> Set your access point name (line 92).
* - LOGIN ----------> Set your network user name (line 93).
* - PASSWORD -------> set your network password (line 94).
*
* User shall set them in the definitions below.
*
* @note Users shall configure the MQTT connection parameters:
* - MQTT IP server ------> Set your MQTT IP server address in numeric format.
* - MQTT_SERVER_IP0_ADDRESS --> 1 value of the MQTT server IP address (line 101).
* - MQTT_SERVER_IP1_ADDRESS --> 2 value of the MQTT server IP address (line 102).
* - MQTT_SERVER_IP2_ADDRESS --> 3 value of the MQTT server IP address (line 103).
* - MQTT_SERVER_IP3_ADDRESS --> 4 value of the MQTT server IP address (line 104).
*
* - MQTT port server --------> The MQTT port server enabled to connect.
* - MQTT_PORT - Port number (line 100).
*
* - MQTT Client parameters --> MQTT parameters to stablish a MQTT connection:
* - MQTT_CLIENT_ID -----------> Client identifier (line 105).
* - MQTT_USER ----------------> User name (line 106).
* - MQTT_PASSWORD ------------> Password (line 107).
*
*
* @note This example uses a debug UART to display some debug information like
* ip address asigned.
* The Debug UART is located in the UART 1 of the TSmarT platform.
* All users who want to see these debug messages shall use a serial
* communication program. In addition, the serial communication
* program shall be set as follows:
* - Baud rate -----> 115200
* - Word length ---> 8 bits
* - Parity --------> none
* - Stop bit ------> 1
* - Flow control --> none
*
*/
/*-------------------------------------------------------- System headers -------------------------------------------------*/
#include "tsmart.h"
/* MQTT protocol headers */
#include "mqtt/include/MQTTPacket.h"
#include "mqtt/include/MQTTConnect.h"
#include "mqtt/include/MQTTPublish.h"
#include "mqtt/include/MQTTSubscribe.h"
#include "mqtt/include/MQTTUnsubscribe.h"
#include "mqtt/include/StackTrace.h"
/*-------------------------------------------------------- UART Debug -----------------------------------------------------*/
#define DEBUG_UART tsmart_uart1
#define APP_RX_BUFFER_SZ 1
static uint8_t app_rx_buf[APP_RX_BUFFER_SZ];
static uint8_t debug_buffer[2000];
/*-------------------------------------------------------- Cellular 2G network parameters ------------------------------------*/
/* Set SIM configuration */
#define PIN_CODE "\0" /* PIN code */
/* Access point network parameters */
#define APN "gprs.pepephone.com\0" /* APN */
#define LOGIN "\0" /* Login */
#define PASSWORD "\0" /* Password */
/* Cellular state machine */
#define CEL_2G_ON 0
#define CEL_2G_OFF 1
#define CEL_2G_ATTACH 2
/*-------------------------------------------------------- MQTT parameters ------------------------------------------------*/
#define MQTT_PORT 1883 /* MQTT port number */
#define MQTT_SERVER_IP0_ADDRESS 0 /* 1 value of the MQTT server IP address */
#define MQTT_SERVER_IP1_ADDRESS 0 /* 2 value of the MQTT server IP address */
#define MQTT_SERVER_IP2_ADDRESS 0 /* 3 value of the MQTT server IP address */
#define MQTT_SERVER_IP3_ADDRESS 0 /* 4 value of the MQTT server IP address */
#define MQTT_CLIENT_ID "2G_P\0" /* Client identifier */
#define MQTT_USER "\0" /* User name */
#define MQTT_PASSWORD "\0" /* Password */
#define MQTT_TOPIC "tst/tsmart\0" /* Topic name */
#define MQTT_KEEP_ALIVE 0 /* Keep alive: 30 seconds */
#define MQTT_CLEAN_SESSION 1 /* Clean session: enabled */
#define MQTT_MQTT_VERSION 3 /* MQTT version in use: 3 */
#define MQTT_PAYLOAD_QOS_0 "TSMART --> Publish: QoS 0\0" /* MQTT payload to publish with QoS 0 */
#define MQTT_PAYLOAD_QOS_1 "TSMART --> Publish: QoS 1\0" /* MQTT payload to publish with QoS 0 */
#define MQTT_PAYLOAD_QOS_2 "TSMART --> Publish: QoS 2\0" /* MQTT payload to publish with QoS 0 */
/* MQTT Connection states */
#define CONNECTION_ACCEPTED 0x00
#define CONNECTION_REFUSE_UNACCEPTABLE_PROTOCOL_VERSION 0x01
#define CONNECTION_REFUSE_IDENTIFIER_REJECTED 0x02
#define CONNECTION_REFUSE_SERVER_UNAVAILABLE 0x03
#define CONNECTION_REFUSE_BAD_USER_NAME_OR_PASSWORD 0x04
#define CONNECTION_REFUSE_NOT_AUTORIZED 0x05
/* MQTT_Connetion state Messages */
#define CONNECTION_ACCEPTED_M " MQTT: Connection Accepted\r\n\0"
#define CONNECTION_REFUSE_UNACCEPTABLE_PROTOCOL_VERSION_M " MQTT: Connestion Refused: unacceptabl protocol version\r\n\0"
#define CONNECTION_REFUSE_IDENTIFIER_REJECTED_M " MQTT: Connection Refused: identifier rejected\r\n\0"
#define CONNECTION_REFUSE_SERVER_UNAVAILABLE_M " MQTT: Connection Refused: server unavailable\r\n\0"
#define CONNECTION_REFUSE_BAD_USER_NAME_OR_PASSWORD_M " MQTT: Connection Refused: bad user name or passwod\r\n\0"
#define CONNECTION_REFUSE_NOT_AUTORIZED_M " MQTT: Connection Refused: not authorized\r\n\0"
#define CONNECTION_DEFAULT_M " MQTT: Bad Connection State\r\n\0"
/*-------------------------------------------------------- MQTT variables -------------------------------------------------*/
static char mqtt_serial_buffer_tx[200];
static char mqtt_serial_buffer_rx[200];
/*-------------------------------------------------------- Application Task -----------------------------------------------*/
/* @brief CELLULAR_2G task.
*
* This task initiliazes the cellular 2g device with the user network
* parameters. After that, it will connect with the MQTT server and it will
* start to publish the different payloads with different QoS: 1,2 and 3.
* Finally it will disconnect from the MQTT server and it will close the
* connection.
*
* @note: This example doesn't do a error management so if the example gets
* some error it will be blocked in a loop.
*
* @param pvParameters
*/
void vCELLULAR_2G(void *pvParameters){
/* Variables */
uint8_t state = CEL_2G_ON;
uint8_t cel_2g_ready = TSMART_FAIL;
uint8_t mqtt_remote_ip_server[30];
int8_t counter = 3;
int ret;
int length_mqtt_frame = 0;
int connack_code = 0;
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
MQTTString p_topic = MQTTString_initializer;
int p_dup = 0;
int p_qos = 0;
int p_retained = 0;
int p_packetid = 1;
char p_payload[40];
int rcvd_packet_type = 0;
int rcvd_dup = 0;
int rcvd_packetid = 0;
TSMART_UART_Send(&DEBUG_UART, "----------- MQTT(CELLULAR 2G interface) example -----------\r\n", strlen("----------- MQTT(CELLULAR 2G interface) example -----------\r\n"), 1000/portTICK_RATE_MS);
/* ******************************************************************************************************************* */
/* INITIALIZE CELLULAR 2G INTERFACE */
/* ******************************************************************************************************************* */
for(;;){
switch(state){
case CEL_2G_ON:
/* Turn On */
TSMART_UART_Send(&DEBUG_UART, "Turning on device 2G...", strlen("Turning on device 2G..."), 1000/portTICK_RATE_MS);
if(TSMART_CELLULAR_2G_TurnOn(&tsmart_cellular_2g, PIN_CODE) != TSMART_PASS){
/* Update state */
state = CEL_2G_OFF;
break;
}
/* Configure module */
if( TSMART_CELLULAR_2G_ConfigDevice(&tsmart_cellular_2g, &device_config) != TSMART_PASS){
/* Update state */
state = CEL_2G_OFF;
break;
}
/* Start stack */
/* Update state */
state = CEL_2G_OFF;
break;
}
/* Stack TCP/IP configuration */
tcp_ip_stack_config.ip_ttl = 64;
tcp_ip_stack_config.ip_frag_timeo = 60;
tcp_ip_stack_config.tcp_maxinitwin = 0;
tcp_ip_stack_config.tcp_rexmt_max = 64;
tcp_ip_stack_config.tcp_rexmt_maxcnt = 12;
if(TSMART_CELLULAR_2G_ConfigTcpIpStack(&tsmart_cellular_2g, &tcp_ip_stack_config) != TSMART_PASS){
/* Update state */
state = CEL_2G_OFF;
break;
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* Update state */
state = CEL_2G_ATTACH;
break;
case CEL_2G_OFF:
TSMART_UART_Send(&DEBUG_UART, "\r\nTurning off device 2G...", strlen("\r\nTurning off device 2G..."), 1000/portTICK_RATE_MS);
/* Probably unknow state: do a reset to set in known state */
}
/* Now Turn Off */
/* Wait to stabilize */
vTaskDelay(1000/portTICK_RATE_MS);
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* Update state */
state = CEL_2G_ON;
break;
case CEL_2G_ATTACH:
TSMART_UART_Send(&DEBUG_UART, "Attaching to 2G network...", strlen("Attaching 2G network..."), 1000/portTICK_RATE_MS);
do{
/* Bearer parameters */
bearer_config.apn = APN;
bearer_config.login = LOGIN;
bearer_config.password = PASSWORD;
bearer_config.time_to_connect = 1200;
if(TSMART_CELLULAR_2G_ConfigBearer(&tsmart_cellular_2g, &bearer_config) == TSMART_PASS){
break;
}
}
TSMART_UART_Send(&DEBUG_UART, "...", strlen("..."), 1000/portTICK_RATE_MS);
vTaskDelay(1000/portTICK_RATE_MS);
counter--;
}while(counter > 0);
if(counter == 0){
TSMART_UART_Send(&DEBUG_UART, "fail\r\n", strlen("fail\r\n"), 1000/portTICK_RATE_MS);
/* Re-load counter */
counter = 3;
/* Expired retries */
state = CEL_2G_OFF;
break;
}
do{
TSMART_UART_Send(&DEBUG_UART, ".", strlen("."), 1000/portTICK_RATE_MS);
/* Wait to register in GPRS network */
/* Wait for 1 second to try again */
vTaskDelay(1000/portTICK_RATE_MS);
do{
TSMART_UART_Send(&DEBUG_UART, ".", strlen("."), 1000/portTICK_RATE_MS);
/* Check Attach */
/* Wait for 1 second to try again */
vTaskDelay(1000/portTICK_RATE_MS);
}while( attach != TSMART_CELLULAR_2G_ATTACHED );
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* Read Local IP address */
if(TSMART_CELLULAR_2G_ReadIp(&tsmart_cellular_2g, &local_ip) != TSMART_PASS){
/* Wait for reading again */
vTaskDelay(500/portTICK_RATE_MS);
if(TSMART_CELLULAR_2G_ReadIp(&tsmart_cellular_2g, &local_ip) != TSMART_PASS){
/* Expired retries */
state = CEL_2G_OFF;
break;
}
}
sprintf(debug_buffer, "Local IP: %d.%d.%d.%d\r\n", local_ip.ip0, local_ip.ip1, local_ip.ip2, local_ip.ip3);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
/* Set exit condition */
cel_2g_ready = TSMART_PASS;
break;
default:
/* Unknown state */
break;
}
if(cel_2g_ready == TSMART_PASS){
/* Exit */
break;
}
}
/* ******************************************************************************************************************* */
/* MQTT CONNECT */
/* ******************************************************************************************************************* */
TSMART_UART_Send(&DEBUG_UART, "Connecting to MQTT server...", strlen("Connecting to MQTT server..."), 1000/portTICK_RATE_MS);
memset(mqtt_remote_ip_server, 0x00, sizeof(mqtt_remote_ip_server));
sprintf(mqtt_remote_ip_server, "%d.%d.%d.%d", MQTT_SERVER_IP0_ADDRESS, MQTT_SERVER_IP1_ADDRESS, MQTT_SERVER_IP2_ADDRESS, MQTT_SERVER_IP3_ADDRESS);
/*------------------------------------------------ CELULLAR 2G Interface ----------------------------------------------*/
tcp_client.id_session = 1;
tcp_client.remote_port = MQTT_PORT;
tcp_client.remote_ip = mqtt_remote_ip_server;
if( TSMART_CELLULAR_2G_CreateTcpClient(&tsmart_cellular_2g, &tcp_client, 10000) != TSMART_PASS){
TSMART_UART_Send(&DEBUG_UART, "fail 1\r\n", strlen("fail 1\r\n"), 1000/portTICK_RATE_MS);
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "fail 2\r\n", strlen("fail 2\r\n"), 1000/portTICK_RATE_MS);
for(;;);
}
/*---------------------------------------------------------------------------------------------------------------------*/
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* Fill MQTT Connect structure */
data.clientID.cstring = MQTT_CLIENT_ID;
data.username.cstring = MQTT_USER;
data.password.cstring = MQTT_PASSWORD;
data.keepAliveInterval = MQTT_KEEP_ALIVE;
data.cleansession = MQTT_CLEAN_SESSION;
data.MQTTVersion = MQTT_MQTT_VERSION;
/* Create a MQTT connect frame */
length_mqtt_frame = MQTTSerialize_connect(mqtt_serial_buffer_tx, sizeof(mqtt_serial_buffer_tx), &data);
/*------------------------------------------------ CELULLAR 2G Interface ----------------------------------------------*/
/* Send */
ret = TSMART_CELLULAR_2G_SendTcpData(&tsmart_cellular_2g, mqtt_serial_buffer_tx, length_mqtt_frame);
/* Receive */
memset(mqtt_serial_buffer_rx, 0x00, sizeof(mqtt_serial_buffer_rx));
ret = TSMART_CELLULAR_2G_ReceiveTcpData(&tsmart_cellular_2g, mqtt_serial_buffer_rx, 2000);
/*---------------------------------------------------------------------------------------------------------------------*/
/* Parse Connack */
if(MQTTDeserialize_connack(&connack_code, mqtt_serial_buffer_rx, ret) != 1){
TSMART_UART_Send(&DEBUG_UART, "BAD CONNACK RECEIVED\r\n", strlen("BAD CONNACK RECEIVED\r\n"), 1000/portTICK_RATE_MS);
for(;;);
}
/* Check connection state */
switch(connack_code){
case CONNECTION_ACCEPTED:
/* Connection Accepted */
TSMART_UART_Send(&DEBUG_UART, CONNECTION_ACCEPTED_M, strlen(CONNECTION_ACCEPTED_M), 1000/portTICK_RATE_MS);
break;
case CONNECTION_REFUSE_UNACCEPTABLE_PROTOCOL_VERSION:
/* Connestion Refused: unacceptabl protocol version */
TSMART_UART_Send(&DEBUG_UART, CONNECTION_REFUSE_UNACCEPTABLE_PROTOCOL_VERSION_M, strlen(CONNECTION_REFUSE_UNACCEPTABLE_PROTOCOL_VERSION_M), 1000/portTICK_RATE_MS);
for(;;);
break;
case CONNECTION_REFUSE_IDENTIFIER_REJECTED:
/* Connection Refused: identifier rejected */
TSMART_UART_Send(&DEBUG_UART, CONNECTION_REFUSE_IDENTIFIER_REJECTED_M, strlen(CONNECTION_REFUSE_IDENTIFIER_REJECTED_M), 1000/portTICK_RATE_MS);
for(;;);
break;
case CONNECTION_REFUSE_SERVER_UNAVAILABLE:
/* Connection Refused: server unavailable */
TSMART_UART_Send(&DEBUG_UART, CONNECTION_REFUSE_SERVER_UNAVAILABLE_M, strlen(CONNECTION_REFUSE_SERVER_UNAVAILABLE_M), 1000/portTICK_RATE_MS);
for(;;);
break;
case CONNECTION_REFUSE_BAD_USER_NAME_OR_PASSWORD:
/* Connection Refused: bad user name or passwod */
TSMART_UART_Send(&DEBUG_UART, CONNECTION_REFUSE_BAD_USER_NAME_OR_PASSWORD_M, strlen(CONNECTION_REFUSE_BAD_USER_NAME_OR_PASSWORD_M), 1000/portTICK_RATE_MS);
for(;;);
break;
case CONNECTION_REFUSE_NOT_AUTORIZED:
/* Connection Refused: not authorized */
TSMART_UART_Send(&DEBUG_UART, CONNECTION_REFUSE_NOT_AUTORIZED_M, strlen(CONNECTION_REFUSE_NOT_AUTORIZED_M), 1000/portTICK_RATE_MS);
for(;;);
break;
default:
/* Default: Reserved for future use */
TSMART_UART_Send(&DEBUG_UART, CONNECTION_DEFAULT_M, strlen(CONNECTION_DEFAULT_M), 1000/portTICK_RATE_MS);
for(;;);
break;
}
/* ******************************************************************************************************************* */
/* MQTT PUBLISH QoS 0 */
/* ******************************************************************************************************************* */
TSMART_UART_Send(&DEBUG_UART, " MQTT: Publishing with QoS: 0...", strlen(" MQTT: Publishing with QoS: 0..."), 1000/portTICK_RATE_MS);
/* Set MQTT publish options */
p_dup = 0;
p_qos = 0;
p_retained = 0;
p_packetid = 1;
p_topic.cstring = MQTT_TOPIC;
/* Create a MQTT publish frame */
length_mqtt_frame = MQTTSerialize_publish(mqtt_serial_buffer_tx, sizeof(mqtt_serial_buffer_tx), p_dup, p_qos, p_retained, p_packetid, p_topic, MQTT_PAYLOAD_QOS_0, strlen(MQTT_PAYLOAD_QOS_0));
/*------------------------------------------------ CELULLAR 2G Interface ----------------------------------------------*/
/* Send */
ret = TSMART_CELLULAR_2G_SendTcpData(&tsmart_cellular_2g, mqtt_serial_buffer_tx, length_mqtt_frame);
/*---------------------------------------------------------------------------------------------------------------------*/
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* Wait a second for next sending */
vTaskDelay(1000/portTICK_RATE_MS);
/* ******************************************************************************************************************* */
/* MQTT PUBLISH QoS 1 */
/* ******************************************************************************************************************* */
TSMART_UART_Send(&DEBUG_UART, " MQTT: Publishing with QoS: 1...", strlen(" MQTT: Publishing with QoS: 1..."), 1000/portTICK_RATE_MS);
/* Set MQTT publish options */
p_dup = 0;
p_qos = 1;
p_retained = 0;
p_packetid = 1;
p_topic.cstring = MQTT_TOPIC;
/* Create a MQTT publish frame */
length_mqtt_frame = MQTTSerialize_publish(mqtt_serial_buffer_tx, sizeof(mqtt_serial_buffer_tx), p_dup, p_qos, p_retained, p_packetid, p_topic, MQTT_PAYLOAD_QOS_1, strlen(MQTT_PAYLOAD_QOS_1));
/*------------------------------------------------ CELULLAR 2G Interface ----------------------------------------------*/
/* Send */
ret = TSMART_CELLULAR_2G_SendTcpData(&tsmart_cellular_2g, mqtt_serial_buffer_tx, length_mqtt_frame);
/* Receive */
memset(mqtt_serial_buffer_rx, 0x00, sizeof(mqtt_serial_buffer_rx));
ret = TSMART_CELLULAR_2G_ReceiveTcpData(&tsmart_cellular_2g, mqtt_serial_buffer_rx, 2000);
/*---------------------------------------------------------------------------------------------------------------------*/
/* Parse publish acknowledge(PUBACK) received */
if(MQTTDeserialize_ack(&rcvd_packet_type, &rcvd_dup, &rcvd_packetid, mqtt_serial_buffer_rx, ret) != 1){
TSMART_UART_Send(&DEBUG_UART, "PUBACK ERROR(1)\r\n", strlen("PUBACK ERROR(1)\r\n"), 1000/portTICK_RATE_MS);
for(;;);
}
/* Check PUBACK */
if((rcvd_packet_type == PUBACK) && (rcvd_dup == p_dup) && (rcvd_packetid == p_packetid)){
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
}else{
TSMART_UART_Send(&DEBUG_UART, "PUBACK ERROR(2)\r\n", strlen("PUBACK ERROR(2)\r\n"), 1000/portTICK_RATE_MS);
for(;;);
}
/* Wait a second for next sending */
vTaskDelay(1000/portTICK_RATE_MS);
/* ******************************************************************************************************************* */
/* MQTT PUBLISH QoS 2 */
/* ******************************************************************************************************************* */
TSMART_UART_Send(&DEBUG_UART, " MQTT: Publishing with QoS: 2...", strlen(" MQTT: Publishing with QoS: 2..."), 1000/portTICK_RATE_MS);
/* Set MQTT publish options */
p_dup = 0;
p_qos = 2;
p_retained = 0;
p_packetid = 1;
p_topic.cstring = MQTT_TOPIC;
/* Create a MQTT publish frame */
length_mqtt_frame = MQTTSerialize_publish(mqtt_serial_buffer_tx, sizeof(mqtt_serial_buffer_tx), p_dup, p_qos, p_retained, p_packetid, p_topic, MQTT_PAYLOAD_QOS_2, strlen(MQTT_PAYLOAD_QOS_2));
/*------------------------------------------------ CELULLAR 2G Interface ----------------------------------------------*/
/* Send */
ret = TSMART_CELLULAR_2G_SendTcpData(&tsmart_cellular_2g, mqtt_serial_buffer_tx, length_mqtt_frame);
/* Receive */
memset(mqtt_serial_buffer_rx, 0x00, sizeof(mqtt_serial_buffer_rx));
ret = TSMART_CELLULAR_2G_ReceiveTcpData(&tsmart_cellular_2g, mqtt_serial_buffer_rx, 2000);
/*---------------------------------------------------------------------------------------------------------------------*/
/* Parse Assured Publish received part-1(PUBREC) */
if(MQTTDeserialize_ack(&rcvd_packet_type, &rcvd_dup, &rcvd_packetid, mqtt_serial_buffer_rx, ret) != 1){
TSMART_UART_Send(&DEBUG_UART, "PUBREC ERROR(1)\r\n", strlen("PUBREC ERROR(1)\r\n"), 1000/portTICK_RATE_MS);
for(;;);
}
/* Check PUBREC */
if((rcvd_packet_type == PUBREC) && (rcvd_dup == p_dup) && (rcvd_packetid == p_packetid)){
TSMART_UART_Send(&DEBUG_UART, "PUBREC-OK...", strlen("PUBREC-OK..."), 1000/portTICK_RATE_MS);
}else{
TSMART_UART_Send(&DEBUG_UART, "PUBREC ERROR(2)\r\n", strlen("PUBREC ERROR(2)\r\n"), 1000/portTICK_RATE_MS);
for(;;);
}
/* Set MQTT Assured Publish Release part-2(PUBREL) options */
p_dup = 0;
p_packetid = 1;
/* Create a MQTT Publish Release frame */
length_mqtt_frame = MQTTSerialize_pubrel(mqtt_serial_buffer_tx, sizeof(mqtt_serial_buffer_tx), p_dup, p_packetid);
/*------------------------------------------------ CELULLAR 2G Interface ----------------------------------------------*/
/* Send */
ret = TSMART_CELLULAR_2G_SendTcpData(&tsmart_cellular_2g, mqtt_serial_buffer_tx, length_mqtt_frame);
/* Receive */
memset(mqtt_serial_buffer_rx, 0x00, sizeof(mqtt_serial_buffer_rx));
ret = TSMART_CELLULAR_2G_ReceiveTcpData(&tsmart_cellular_2g, mqtt_serial_buffer_rx, 2000);
/*---------------------------------------------------------------------------------------------------------------------*/
/* Parse Assured Publish Complete part-3(PUBCOMP) */
if(MQTTDeserialize_ack(&rcvd_packet_type, &rcvd_dup, &rcvd_packetid, mqtt_serial_buffer_rx, ret) != 1){
TSMART_UART_Send(&DEBUG_UART, "PUBCOMP ERROR(1)\r\n", strlen("PUBCOMP PUBCOMP(1)\r\n"), 1000/portTICK_RATE_MS);
for(;;);
}
/* Check PUBCOMP */
if((rcvd_packet_type == PUBCOMP) && (rcvd_dup == p_dup) && (rcvd_packetid == p_packetid)){
TSMART_UART_Send(&DEBUG_UART, "PUBCOMP-OK\r\n", strlen("PUBCOMP-OK\r\n"), 1000/portTICK_RATE_MS);
}else{
TSMART_UART_Send(&DEBUG_UART, "PUBCOMP ERROR(2)\r\n", strlen("PUBCOMP ERROR(2)\r\n"), 1000/portTICK_RATE_MS);
for(;;);
}
/* ******************************************************************************************************************* */
/* MQTT DISCONNECT */
/* ******************************************************************************************************************* */
TSMART_UART_Send(&DEBUG_UART, " MQTT: Disconnecting...\r\n", strlen(" MQTT: Disconnecting..."), 1000/portTICK_RATE_MS);
length_mqtt_frame = MQTTSerialize_disconnect(mqtt_serial_buffer_tx, sizeof(mqtt_serial_buffer_tx));
/*------------------------------------------------ CELULLAR 2G Interface ----------------------------------------------*/
/* Send */
ret = TSMART_CELLULAR_2G_SendTcpData(&tsmart_cellular_2g, mqtt_serial_buffer_tx, length_mqtt_frame);
/*---------------------------------------------------------------------------------------------------------------------*/
TSMART_UART_Send(&DEBUG_UART, "OK\r\nClosing connection...", strlen("OK\r\nClosing connection..."), 1000/portTICK_RATE_MS);
/*------------------------------------------------ CELULLAR 2G Interface ----------------------------------------------*/
/* Stop connection */
/* Close TCP session */
for(;;);
}
/*---------------------------------------------------------------------------------------------------------------------*/
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
TSMART_UART_Send(&DEBUG_UART, "---------------- END ----------------\r\n", strlen("--------------- END ----------------\r\n"), 1000/portTICK_RATE_MS);
/* Wait forever */
for(;;);
}/* End task */
/*
* @brief init() function
*
* This is the first thing that the user must do for using TSmarT.
* It initializes the specific hardware resources (CELLULAR, GPS, AI, DIO, MODBUS, MSA...)
* and software resources (queues, mutex, tasks...) for the user application.
*
* The way to fill in this function properly is to initialize first
* hardware resources and after that software resources.
*
* This function must return: TSMART_PASS when every thing is OK or
* TSMART_FAIL when a failure happened.
*
* @return
* @arg TSMART_PASS
* @arg TSMART_FAIL
*/
int32_t init() {
/* Variables */
tsmart_uart_config_t tsmart_uart_config;
/*----------------------- Debug Mode ----------------------------------------*/
/*----------------------- Initialize resources ------------------------------*/
/* Initilialize cellular 2g */
return TSMART_FAIL;
}
/* Initialize Debug UART */
tsmart_uart_config.baud_rate = 115200;
tsmart_uart_config.flow_control = TSMART_UART_FC_NONE;
tsmart_uart_config.parity_bit = TSMART_UART_PARITY_NONE;
tsmart_uart_config.word_length = TSMART_UART_WORDLENGTH_8B;
tsmart_uart_config.stop_bit = TSMART_UART_STOPBITS_1;
tsmart_uart_config.rx_buf_sz = APP_RX_BUFFER_SZ;
tsmart_uart_config.rx_buf = app_rx_buf;
tsmart_uart_config.use_dma = TSMART_UART_DMA_RX_TX;
tsmart_uart_config.use_tim = TSMART_UART_TIM_SW;
TSMART_UART_Init(&DEBUG_UART, &tsmart_uart_config);
/*----------------------- Application task ----------------------------------*/
/* Create cellular 2g task */
if(xTaskCreate(vCELLULAR_2G, "CELLULAR_2G", 2048, NULL, 8, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}