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

This is an example of how to manage the MQTT protocol using the wiznet device.

/* @file mqtt_wiznet_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
* wiznet interface.
*
* This example is based on using a wired network using an ethernet interface.
* The ethernet interface is manage by the Wiznet chip. This chip is a
* Hardwired TCP/IP embedded Ethernet controller that enables easier internet
* connection using SPI (Serial Peripheral Interface).
*
* 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 To run this example, it's necessary to set some network parameters:
*
* - Media access control(MAC) address -------> Set your MAC address.
* - MAC0 - 1 value of the MAC address (line 122).
* - MAC1 - 2 value of the MAC address (line 123).
* - MAC2 - 3 value of the MAC address (line 124).
* - MAC3 - 4 value of the MAC address (line 125).
* - MAC4 - 5 value of the MAC address (line 126).
* - MAC5 - 6 value of the MAC address (line 127).
*
* - Local IP address ------------------------> Set your IP address.
* - LOCAL_IP0 - 1 value of the Local IP address (line 129).
* - LOCAL_IP1 - 2 value of the Local IP address (line 130).
* - LOCAL_IP2 - 3 value of the Local IP address (line 131).
* - LOCAL_IP3 - 4 value of the Local IP address (line 132).
*
* - Subnet mask -----------------------------> Set your subnet mask.
* - SUBNET_MASK0 - 1 value of the subnet mask (line 134).
* - SUBNET_MASK1 - 2 value of the subnet mask (line 135).
* - SUBNET_MASK2 - 3 value of the subnet mask (line 136).
* - SUBNET_MASK3 - 4 value of the subnet mask (line 137).
*
* - Gateway IP ------------------------------> Set your gateway IP address.
* - GW_IP0 - 1 value of the gateway IP address (line 139).
* - GW_IP1 - 2 value of the gateway IP address (line 140).
* - GW_IP2 - 3 value of the gateway IP address (line 141).
* - GW_IP3 - 4 value of the gateway IP address (line 142).
* - DNS IP server address ------------------> Set your DNS IP server address.
* - DNS_IP_SERVER0 - 1 value of the DNS IP server address (line 144).
* - DNS_IP_SERVER1 - 2 value of the DNS IP server address (line 145).
* - DNS_IP_SERVER2 - 3 value of the DNS IP server address (line 146).
* - DNS_IP_SERVER3 - 4 value of the DNS IP server address (line 147).
*
* 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 119).
* - MQTT_SERVER_IP1_ADDRESS --> 2 value of the MQTT server IP address (line 119).
* - MQTT_SERVER_IP2_ADDRESS --> 3 value of the MQTT server IP address (line 119).
* - MQTT_SERVER_IP3_ADDRESS --> 4 value of the MQTT server IP address (line 119).
*
* - MQTT port server --------> The MQTT port server enabled to connect.
* - MQTT_PORT - Port number (line 119).
*
* - MQTT Client parameters --> MQTT parameters to stablish a MQTT connection:
* - MQTT_CLIENT_ID -----------> Client identifier (line 119).
* - MQTT_USER ----------------> User name (line 119).
* - MQTT_PASSWORD ------------> Password (line 119).
*
*
* @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"
/* Wiznet headers */
#include "wiznet\W5200\include\w5200.h"
#include "wiznet\W5200\include\socket.h"
#include "wiznet\W5200\include\spi.h"
#include "wiznet\W5200\include\config.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];
/*-------------------------------------------------------- Ethernet network parameters ------------------------------------*/
/* MAC address */
#define MAC0 0x00 /* 1 value of the MAC address */
#define MAC1 0x11 /* 2 value of the MAC address */
#define MAC2 0x22 /* 3 value of the MAC address */
#define MAC3 0x33 /* 4 value of the MAC address */
#define MAC4 0x44 /* 5 value of the MAC address */
#define MAC5 0x55 /* 6 value of the MAC address */
/* Local IP address */
#define LOCAL_IP0 192 /* 1 value of the Local IP address */
#define LOCAL_IP1 168 /* 2 value of the Local IP address */
#define LOCAL_IP2 10 /* 3 value of the Local IP address */
#define LOCAL_IP3 150 /* 4 value of the Local IP address */
/* Subnet mask */
#define SUBNET_MASK0 255 /* 1 value of the subnet mask */
#define SUBNET_MASK1 255 /* 2 value of the subnet mask */
#define SUBNET_MASK2 255 /* 3 value of the subnet mask */
#define SUBNET_MASK3 0 /* 4 value of the subnet mask */
/* Gateway IP address */
#define GW_IP0 192 /* 1 value of the gateway IP address */
#define GW_IP1 168 /* 2 value of the gateway IP address */
#define GW_IP2 10 /* 3 value of the gateway IP address */
#define GW_IP3 1 /* 4 value of the gateway IP address */
/* DNS IP server */
#define DNS_IP_SERVER0 192 /* 1 value of the DNS IP server address */
#define DNS_IP_SERVER1 168 /* 2 value of the DNS IP server address */
#define DNS_IP_SERVER2 10 /* 3 value of the DNS IP server address */
#define DNS_IP_SERVER3 1 /* 4 value of the DNS IP server address */
/* Client socket index */
#define TCP_C_SOCKET 5
/*-------------------------------------------------------- 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 "ETH_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: the client won't be diconnected */
#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];
/*-------------------------------------------------------- Auxiliary Functions --------------------------------------------*/
/* @brief Wiznet config function
*
* This function configures wiznet with the user network
* parameters and shows them using the debug UART.
*
*/
void WIZ_Config(void){
/* Variables */
wiz_NetInfo netinfo;
/* HW Reset */
/* Initialize Wiznet */
/* Enable IRQ socket to work in IRQ mode */
/* Set MAC address */
netinfo.Mac[0] = MAC0;
netinfo.Mac[1] = MAC1;
netinfo.Mac[2] = MAC2;
netinfo.Mac[3] = MAC3;
netinfo.Mac[4] = MAC4;
netinfo.Mac[5] = MAC5;
/* Set IP address */
netinfo.IP[0] = LOCAL_IP0;
netinfo.IP[1] = LOCAL_IP1;
netinfo.IP[2] = LOCAL_IP2;
netinfo.IP[3] = LOCAL_IP3;
/* Set Subnet mask */
netinfo.Subnet[0] = SUBNET_MASK0;
netinfo.Subnet[1] = SUBNET_MASK1;
netinfo.Subnet[2] = SUBNET_MASK2;
netinfo.Subnet[3] = SUBNET_MASK3;
/* Set IP Gateway */
netinfo.Gateway[0] = GW_IP0;
netinfo.Gateway[1] = GW_IP1;
netinfo.Gateway[2] = GW_IP2;
netinfo.Gateway[3] = GW_IP3;
/* Set DNS */
netinfo.DNSServerIP[0] = DNS_IP_SERVER0;
netinfo.DNSServerIP[1] = DNS_IP_SERVER1;
netinfo.DNSServerIP[2] = DNS_IP_SERVER2;
netinfo.DNSServerIP[3] = DNS_IP_SERVER3;
/* Set network info */
SetNetInfo(&netinfo);
/* Show netowrk info */
TSMART_UART_Send(&DEBUG_UART, "\r\n--------------------------------------- \r\n", strlen("\r\n--------------------------------------- \r\n"), 1000/portTICK_RATE_MS);
TSMART_UART_Send(&DEBUG_UART, "Network Configuration Information \r\n", strlen("Network Configuration Information \r\n"), 1000/portTICK_RATE_MS);
TSMART_UART_Send(&DEBUG_UART, "--------------------------------------- ", strlen("--------------------------------------- "), 1000/portTICK_RATE_MS);
sprintf(debug_buffer, "\r\nMAC: %02X.%02X.%02X.%02X.%02X.%02X", netinfo.Mac[0],netinfo.Mac[1],netinfo.Mac[2],netinfo.Mac[3],netinfo.Mac[4],netinfo.Mac[5]);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
sprintf(debug_buffer, "\r\nIP : %d.%d.%d.%d", netinfo.IP[0],netinfo.IP[1],netinfo.IP[2],netinfo.IP[3]);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
sprintf(debug_buffer, "\r\nSN : %d.%d.%d.%d", netinfo.Subnet[0],netinfo.Subnet[1],netinfo.Subnet[2],netinfo.Subnet[3]);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
sprintf(debug_buffer, "\r\nGW : %d.%d.%d.%d", netinfo.Gateway[0],netinfo.Gateway[1],netinfo.Gateway[2],netinfo.Gateway[3]);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
sprintf(debug_buffer, "\r\nDNS: %d.%d.%d.%d \r\n", netinfo.DNSServerIP[0],netinfo.DNSServerIP[1],netinfo.DNSServerIP[2],netinfo.DNSServerIP[3]);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
}
/*-------------------------------------------------------- Application Task -----------------------------------------------*/
/* @brief WIZNET task.
*
* This task initiliazes the Wiznet chip 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 vWIZNET(void *pvParameters){
/* Variables */
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;
uint8 mqtt_server_ip_address[4]= {MQTT_SERVER_IP0_ADDRESS, MQTT_SERVER_IP1_ADDRESS, MQTT_SERVER_IP2_ADDRESS, MQTT_SERVER_IP3_ADDRESS};
TSMART_UART_Send(&DEBUG_UART, "----------- MQTT(Wiznet interface) example -----------\r\n", strlen("----------- MQTT(Wiznet interface) example -----------\r\n"), 1000/portTICK_RATE_MS);
/* ******************************************************************************************************************* */
/* INITIALIZE WIZNET INTERFACE */
/* ******************************************************************************************************************* */
TSMART_UART_Send(&DEBUG_UART, "Configuring network...", strlen("Configuring network..."), 1000/portTICK_RATE_MS);
/* Configure network */
WIZ_Config();
/* Time to stabilize */
vTaskDelay(1000/portTICK_RATE_MS);
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* ******************************************************************************************************************* */
/* MQTT CONNECT */
/* ******************************************************************************************************************* */
TSMART_UART_Send(&DEBUG_UART, "Connecting to MQTT server...", strlen("Connecting to MQTT server..."), 1000/portTICK_RATE_MS);
/*------------------------------------------------ WIZNET Interface -----------------------------------------------------*/
/* Open TCP client */
mqtt_server_ip_address[0] = MQTT_SERVER_IP0_ADDRESS;
mqtt_server_ip_address[1] = MQTT_SERVER_IP1_ADDRESS;
mqtt_server_ip_address[2] = MQTT_SERVER_IP2_ADDRESS;
mqtt_server_ip_address[3] = MQTT_SERVER_IP3_ADDRESS;
if(TCPClientOpen(TCP_C_SOCKET, 5000, mqtt_server_ip_address, (uint16_t)MQTT_PORT) != 1){
for(;;);
}
/* Wait for being connected */
xQueueReceive(socket_queue[TCP_C_SOCKET], &event, portMAX_DELAY);
/*---------------------------------------------------------------------------------------------------------------------*/
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);
/*------------------------------------------------ WIZNET Interface -----------------------------------------------------*/
/* Send */
ret = TCPSend(TCP_C_SOCKET, mqtt_serial_buffer_tx, length_mqtt_frame);
/* Wait for SEND OK */
xQueueReceive(socket_queue[TCP_C_SOCKET], &event, portMAX_DELAY);
/* Wait for DATA */
xQueueReceive(socket_queue[TCP_C_SOCKET], &event, portMAX_DELAY);
/* Get data received */
memset(mqtt_serial_buffer_rx, 0x00, sizeof(mqtt_serial_buffer_rx));
ret = TCPRecv(TCP_C_SOCKET, mqtt_serial_buffer_rx, sizeof(mqtt_serial_buffer_rx));
/*---------------------------------------------------------------------------------------------------------------------*/
/* 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));
/*------------------------------------------------ WIZNET Interface -----------------------------------------------------*/
/* Send */
ret = TCPSend(TCP_C_SOCKET, mqtt_serial_buffer_tx, length_mqtt_frame);
/* Wait for SEND OK */
xQueueReceive(socket_queue[TCP_C_SOCKET], &event, portMAX_DELAY);
/*---------------------------------------------------------------------------------------------------------------------*/
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));
/*------------------------------------------------ WIZNET Interface -----------------------------------------------------*/
/* Send */
ret = TCPSend(TCP_C_SOCKET, mqtt_serial_buffer_tx, length_mqtt_frame);
/* Wait for SEND OK */
xQueueReceive(socket_queue[TCP_C_SOCKET], &event, portMAX_DELAY);
/* Wait for DATA */
xQueueReceive(socket_queue[TCP_C_SOCKET], &event, portMAX_DELAY);
/* Get data received */
memset(mqtt_serial_buffer_rx, 0x00, sizeof(mqtt_serial_buffer_rx));
ret = TCPRecv(TCP_C_SOCKET, mqtt_serial_buffer_rx, sizeof(mqtt_serial_buffer_rx));
/*---------------------------------------------------------------------------------------------------------------------*/
/* 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));
/*------------------------------------------------ WIZNET Interface -----------------------------------------------------*/
/* Send */
ret = TCPSend(TCP_C_SOCKET, mqtt_serial_buffer_tx, length_mqtt_frame);
/* Wait for SEND OK */
xQueueReceive(socket_queue[TCP_C_SOCKET], &event, portMAX_DELAY);
/* Wait for DATA */
xQueueReceive(socket_queue[TCP_C_SOCKET], &event, portMAX_DELAY);
/* Get data received */
memset(mqtt_serial_buffer_rx, 0x00, sizeof(mqtt_serial_buffer_rx));
ret = TCPRecv(TCP_C_SOCKET, mqtt_serial_buffer_rx, sizeof(mqtt_serial_buffer_rx));
/*---------------------------------------------------------------------------------------------------------------------*/
/* 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);
/*------------------------------------------------ WIZNET Interface -----------------------------------------------------*/
/* Send */
ret = TCPSend(TCP_C_SOCKET, mqtt_serial_buffer_tx, length_mqtt_frame);
/* Wait for SEND OK */
xQueueReceive(socket_queue[TCP_C_SOCKET], &event, portMAX_DELAY);
/* Wait for DATA */
xQueueReceive(socket_queue[TCP_C_SOCKET], &event, portMAX_DELAY);
/* Get data received */
memset(mqtt_serial_buffer_rx, 0x00, sizeof(mqtt_serial_buffer_rx));
ret = TCPRecv(TCP_C_SOCKET, mqtt_serial_buffer_rx, sizeof(mqtt_serial_buffer_rx));
/*---------------------------------------------------------------------------------------------------------------------*/
/* 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));
/*------------------------------------------------ WIZNET Interface -----------------------------------------------------*/
/* Send */
ret = TCPSend(TCP_C_SOCKET, mqtt_serial_buffer_tx, length_mqtt_frame);
/* Wait for SEND OK */
xQueueReceive(socket_queue[TCP_C_SOCKET], &event, portMAX_DELAY);
/*---------------------------------------------------------------------------------------------------------------------*/
TSMART_UART_Send(&DEBUG_UART, "OK\r\nClosing connection...", strlen("OK\r\nClosing connection..."), 1000/portTICK_RATE_MS);
/*------------------------------------------------ WIZNET Interface -------------------------------------------------*/
/* Close socket */
if(TCPClose(TCP_C_SOCKET) != 1){
for(;;);
}
/* Wait for Disconnect */
xQueueReceive(socket_queue[TCP_C_SOCKET], &event, portMAX_DELAY);
/*---------------------------------------------------------------------------------------------------------------------*/
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 ------------------------------*/
/* Initialize wiznet */
/* 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 wiznet task */
if(xTaskCreate(vWIZNET, "WIZNET", 2048, NULL, 8, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}