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

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

/* @file mqtt_wifi_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
* wifi interface.
*
*
* This example is based on using a wireless network using a wifi interface.
* The wifi device in use is a CC3000 from Texas Instrument. This device has an
* embedded TCP/IP 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". 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:
*
* - Service Set IDentifier ------------------> Set the network name.
* - SSID - "tsmart_net\0" could be an example of network name (line 155).
*
* - Network key -----------> Set your wifi network key.
* - KEY - This is the values of your network key, "tsmart\0" could be
* an example. Leave empty if you don't have one (line 157).
*
* - Type of network key ---> Several types of keys are supported. Set yours.
* - KEY_TYPE - User shall define this definition with one of the
* following definitions:
* - WLAN_SEC_UNSEC ---> Open (line 159)
* - WLAN_SEC_WEP -----> WEP (line 160)
* - WLAN_SEC_WPA -----> WPA (line 161)
* - WLAN_SEC_WPA2 ----> WPA2 (line 162)
*
* - Local IP address ------------------------> Set your IP address.
* - LOCAL_IP0 - 1 value of the Local IP address (line 135).
* - LOCAL_IP1 - 2 value of the Local IP address (line 136).
* - LOCAL_IP2 - 3 value of the Local IP address (line 137).
* - LOCAL_IP3 - 4 value of the Local IP address (line 138).
* ----> Setting them to 0, implies to request for a dynamic IP to the DHCP.
*
* - Subnet mask -----------------------------> Set your subnet mask.
* - SUBNET_MASK0 - 1 value of the subnet mask (line 140).
* - SUBNET_MASK1 - 2 value of the subnet mask (line 141).
* - SUBNET_MASK2 - 3 value of the subnet mask (line 142).
* - SUBNET_MASK3 - 4 value of the subnet mask (line 143).
* ----> Setting them to 0, implies to request for a dynamic IP to the DHCP.
*
* - Gateway IP ------------------------------> Set your gateway IP address.
* - GW_IP0 - 1 value of the gateway IP address (line 145).
* - GW_IP1 - 2 value of the gateway IP address (line 146).
* - GW_IP2 - 3 value of the gateway IP address (line 147).
* - GW_IP3 - 4 value of the gateway IP address (line 148).
* ----> Setting them to 0, implies to request for a dynamic IP to the DHCP.
*
* - DNS IP server address ------------------> Set your DNS IP server address.
* - DNS_IP_SERVER0 - 1 value of the DNS IP server address (line 150).
* - DNS_IP_SERVER1 - 2 value of the DNS IP server address (line 151).
* - DNS_IP_SERVER2 - 3 value of the DNS IP server address (line 152).
* - DNS_IP_SERVER3 - 4 value of the DNS IP server address (line 153).
* ----> Setting them to 0, implies to request for a dynamic IP to the DHCP.
*
* 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 165).
* - MQTT_SERVER_IP1_ADDRESS --> 2 value of the MQTT server IP address (line 166).
* - MQTT_SERVER_IP2_ADDRESS --> 3 value of the MQTT server IP address (line 167).
* - MQTT_SERVER_IP3_ADDRESS --> 4 value of the MQTT server IP address (line 168).
*
* - MQTT port server --------> The MQTT port server enabled to connect.
* - MQTT_PORT - Port number (line 164).
*
* - MQTT Client parameters --> MQTT parameters to stablish a MQTT connection:
* - MQTT_CLIENT_ID -----------> Client identifier (line 169).
* - MQTT_USER ----------------> User name (line 170).
* - MQTT_PASSWORD ------------> Password (line 171).
*
*
* @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"
/* Wifi headers */
#include "tsmart_wifi_headers.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];
/*-------------------------------------------------------- Global variables -----------------------------------------------*/
volatile unsigned long ulSmartConfigFinished;
volatile unsigned long ulCC3000Connected;
volatile unsigned long ulCC3000DHCP;
volatile unsigned long OkToDoShutDown;
/*-------------------------------------------------------- Wireless network parameters ------------------------------------*/
/* Local IP address */
#define LOCAL_IP0 0 /* 1 value of the Local IP address */
#define LOCAL_IP1 0 /* 2 value of the Local IP address */
#define LOCAL_IP2 0 /* 3 value of the Local IP address */
#define LOCAL_IP3 0 /* 4 value of the Local IP address */
/* Subnet mask */
#define SUBNET_MASK0 0 /* 1 value of the subnet mask */
#define SUBNET_MASK1 0 /* 2 value of the subnet mask */
#define SUBNET_MASK2 0 /* 3 value of the subnet mask */
#define SUBNET_MASK3 0 /* 4 value of the subnet mask */
/* Gateway IP address */
#define GW_IP0 0 /* 1 value of the gateway IP address */
#define GW_IP1 0 /* 2 value of the gateway IP address */
#define GW_IP2 0 /* 3 value of the gateway IP address */
#define GW_IP3 0 /* 4 value of the gateway IP address */
/* DNS IP server */
#define DNS_IP_SERVER0 0 /* 1 value of the DNS IP server address */
#define DNS_IP_SERVER1 0 /* 2 value of the DNS IP server address */
#define DNS_IP_SERVER2 0 /* 3 value of the DNS IP server address */
#define DNS_IP_SERVER3 0 /* 4 value of the DNS IP server address */
/* SSID */
#define SSID "SANTANDER\0" /* Network name */
/* Network key */
#define KEY "tsmote\0" /* Network Key */
/* Type network key: select one of them */
//#define KEY_TYPE WLAN_SEC_UNSEC /* Network key type: open */
//#define TSMART_KEY_TYPE WLAN_SEC_WEP /* Network key type: WEP */
//#define TSMART_KEY_TYPE WLAN_SEC_WPA /* Network key type: WPA */
#define KEY_TYPE WLAN_SEC_WPA2 /* Network key type: WPA2 */
/*-------------------------------------------------------- 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 "WIFI_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];
/*-------------------------------------------------------- WIFI Handlers --------------------------------------------------*/
/* sendDriverPatch
*
*\param pointer to the length
*
*\return none
*
*\brief The function returns a pointer to the driver patch: since there is no patch yet -
* it returns 0
*/
char *sendDriverPatch(unsigned long *Length)
{
*Length = 0;
return NULL;
}
/*
*sendBootLoaderPatch
*
*\param pointer to the length
*
*\return none
*
*\brief The function returns a pointer to the boot loader patch: since there is no patch yet -
* it returns 0
*/
char *sendBootLoaderPatch(unsigned long *Length)
{
*Length = 0;
return NULL;
}
/*
* sendWLFWPatch
*
* \param pointer to the length
*
* \return none
*
* \brief The function returns a pointer to the FW patch: since there is no patch yet - it returns 0
*/
char *sendWLFWPatch(unsigned long *Length)
{
*Length = 0;
return NULL;
}
/*
*CC3000_UsynchCallback
*
* \param Event type
*
*\return none
*
*\brief The function handles asynchronous events that come from CC3000 device
*
*/
void CC3000_UsynchCallback(long lEventType, char * data, unsigned char length)
{
if (lEventType == HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE)
{
/* Create handler */
ulSmartConfigFinished = 1;
}
if (lEventType == HCI_EVNT_WLAN_UNSOL_CONNECT)
{
/* Create handler */
ulCC3000Connected = 1;
}
if (lEventType == HCI_EVNT_WLAN_UNSOL_DISCONNECT)
{
/* Create handler */
}
if (lEventType == HCI_EVNT_WLAN_UNSOL_DHCP)
{
/* Create handler */
ulCC3000DHCP = 1;
}
if (lEventType == HCI_EVENT_CC3000_CAN_SHUT_DOWN)
{
/* Create handler */
OkToDoShutDown = 1;
}
}
/*-------------------------------------------------------- Application Task -----------------------------------------------*/
/* @brief WIFI task.
*
* This task initiliazes the wifi 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 vWIFI(void *pvParameters){
/* Variables */
unsigned char pucIP_Addr[4];
unsigned char pucIP_DefaultGWAddr[4];
unsigned char pucSubnetMask[4];
unsigned char pucDNS[4];
long network_status;
sockaddr socket_address;
tNetappIpconfigRetArgs ipconfig;
int ret;
int socket_mqtt = 0;
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(WIFI interface) example -----------\r\n", strlen("----------- MQTT(WIFI interface) example -----------\r\n"), 1000/portTICK_RATE_MS);
/* ******************************************************************************************************************* */
/* INITIALIZE WIFI INTERFACE */
/* ******************************************************************************************************************* */
TSMART_UART_Send(&DEBUG_UART, "Configuring network...", strlen("Configuring network..."), 1000/portTICK_RATE_MS);
/* WLAN On API Implementation */
/* Trigger a WLAN device */
/* Mask out all non-required events from CC3000 */
if(wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE|HCI_EVNT_WLAN_UNSOL_INIT|HCI_EVNT_WLAN_ASYNC_PING_REPORT) != 0){
TSMART_UART_Send(&DEBUG_UART, "fail 1\r\n", strlen("fail 1\r\n"), 1000/portTICK_RATE_MS);
for(;;);
}
/*------------------------------------------------ Set the IP configuration -------------------------------------------*/
/* Local IP */
pucIP_Addr[0] = LOCAL_IP0;
pucIP_Addr[1] = LOCAL_IP1;
pucIP_Addr[2] = LOCAL_IP2;
pucIP_Addr[3] = LOCAL_IP3;
/* Network mask */
pucSubnetMask[0] = SUBNET_MASK0;
pucSubnetMask[1] = SUBNET_MASK1;
pucSubnetMask[2] = SUBNET_MASK2;
pucSubnetMask[3] = SUBNET_MASK3;
/* Gateway IP */
pucIP_DefaultGWAddr[0] = GW_IP0;
pucIP_DefaultGWAddr[1] = GW_IP1;
pucIP_DefaultGWAddr[2] = GW_IP2;
pucIP_DefaultGWAddr[3] = GW_IP3;
/* DNS */
pucDNS[0] = DNS_IP_SERVER0;
pucDNS[1] = DNS_IP_SERVER1;
pucDNS[2] = DNS_IP_SERVER2;
pucDNS[3] = DNS_IP_SERVER3;
if(netapp_dhcp((unsigned long *)pucIP_Addr, (unsigned long *)pucSubnetMask, (unsigned long *)pucIP_DefaultGWAddr, (unsigned long *)pucDNS) != 0){
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);
/*------------------------------------------------ Connect to the Access Point ----------------------------------------*/
TSMART_UART_Send(&DEBUG_UART, "Connecting to AP", strlen("Connecting to AP"), 1000/portTICK_RATE_MS);
/* Start a WLAN Connect process */
if(wlan_connect(KEY_TYPE, SSID, strlen(SSID), 0, KEY, strlen(KEY)) != 0){
TSMART_UART_Send(&DEBUG_UART, "fail\r\n", strlen("fail\r\n"), 1000/portTICK_RATE_MS);
for(;;);
}
/* Wait for connecting is OK */
do{
network_status = wlan_ioctl_statusget();
TSMART_UART_Send(&DEBUG_UART, ".", strlen("."), 1000/portTICK_RATE_MS);
vTaskDelay(80/portTICK_RATE_MS);
}while(network_status != 3);
/* Wait for DHCP process to finish (if you are using a static IP address please delete the wait for DHCP event - ulCC3000DHCP */
while ((ulCC3000DHCP == 0) || (ulCC3000Connected == 0)){
TSMART_UART_Send(&DEBUG_UART, ".", strlen("."), 1000/portTICK_RATE_MS);
hci_unsolicited_event_handler();
vTaskDelay(1000/portTICK_RATE_MS);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/*------------------------------------------------ Get and display the current IP configuration -----------------------*/
netapp_ipconfig(&ipconfig);
/* Show netowrk info */
TSMART_UART_Send(&DEBUG_UART, "--------------------------------------- \r\n", strlen("--------------------------------------- \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, "--------------------------------------- \r\n", strlen("--------------------------------------- \r\n"), 1000/portTICK_RATE_MS);
sprintf(debug_buffer, "Local IP : %d.%d.%d.%d\r\n", ipconfig.aucIP[3], ipconfig.aucIP[2],ipconfig.aucIP[1], ipconfig.aucIP[0]);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
sprintf(debug_buffer, "Local MASK: %d.%d.%d.%d\r\n", ipconfig.aucSubnetMask[3], ipconfig.aucSubnetMask[2], ipconfig.aucSubnetMask[1], ipconfig.aucSubnetMask[0]);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
sprintf(debug_buffer, "GW IP : %d.%d.%d.%d\r\n", ipconfig.aucDefaultGateway[3], ipconfig.aucDefaultGateway[2], ipconfig.aucDefaultGateway[1], ipconfig.aucDefaultGateway[0]);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
sprintf(debug_buffer, "DNS : %d.%d.%d.%d\r\n", ipconfig.aucDNSServer[3], ipconfig.aucDNSServer[2], ipconfig.aucDNSServer[1], ipconfig.aucDNSServer[0]);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
sprintf(debug_buffer, "DHCP : %d.%d.%d.%d\r\n", ipconfig.aucDHCPServer[3], ipconfig.aucDHCPServer[2], ipconfig.aucDHCPServer[1], ipconfig.aucDHCPServer[0]);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
sprintf(debug_buffer, "SSID : %s\r\n", ipconfig.uaSSID);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
/* ******************************************************************************************************************* */
/* MQTT CONNECT */
/* ******************************************************************************************************************* */
TSMART_UART_Send(&DEBUG_UART, "Connecting to MQTT server...", strlen("Connecting to MQTT server..."), 1000/portTICK_RATE_MS);
/*------------------------------------------------ WIFI Interface -----------------------------------------------------*/
/* Request a TCP/IP socket */
socket_mqtt = socket(AF_INET, SOCK_STREAM, 0);
if(socket_mqtt < 0){
TSMART_UART_Send(&DEBUG_UART, "fail 1\r\n", strlen("fail 1\r\n"), 1000/portTICK_RATE_MS);
for(;;);
}
/* Fill out the socke structure */
socket_address.sa_family = AF_INET;
/* MQTT Port */
socket_address.sa_data[0] = (MQTT_PORT&0xFF00)>>8;
socket_address.sa_data[1] = (MQTT_PORT&0x00FF);
/* IP MQTT Server */
socket_address.sa_data[2] = MQTT_SERVER_IP0_ADDRESS;
socket_address.sa_data[3] = MQTT_SERVER_IP1_ADDRESS;
socket_address.sa_data[4] = MQTT_SERVER_IP2_ADDRESS;
socket_address.sa_data[5] = MQTT_SERVER_IP3_ADDRESS;
if(connect(socket_mqtt, &socket_address, sizeof(socket_address)) < 0){
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);
/*------------------------------------------------ WIFI Interface -----------------------------------------------------*/
/* Send */
ret = send(socket_mqtt, mqtt_serial_buffer_tx, length_mqtt_frame, 0);
/* Receive */
memset(mqtt_serial_buffer_rx, 0x00, sizeof(mqtt_serial_buffer_rx));
ret = recv(socket_mqtt, mqtt_serial_buffer_rx, sizeof(mqtt_serial_buffer_rx) - 1, 0);
/*---------------------------------------------------------------------------------------------------------------------*/
/* 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 */
/* ******************************************************************************************************************* */
sprintf(debug_buffer," MQTT: Publishing with QoS: 0...");
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 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));
/*------------------------------------------------ WIFI Interface -----------------------------------------------------*/
/* Send */
ret = send(socket_mqtt, mqtt_serial_buffer_tx, length_mqtt_frame, 0);
/*---------------------------------------------------------------------------------------------------------------------*/
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 */
/* ******************************************************************************************************************* */
sprintf(debug_buffer," MQTT: Publishing with QoS: 1...");
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 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));
/*------------------------------------------------ WIFI Interface -----------------------------------------------------*/
/* Send */
ret = send(socket_mqtt, mqtt_serial_buffer_tx, length_mqtt_frame, 0);
/* Receive */
memset(mqtt_serial_buffer_rx, 0x00, sizeof(mqtt_serial_buffer_rx));
ret = recv(socket_mqtt, mqtt_serial_buffer_rx, sizeof(mqtt_serial_buffer_rx) - 1, 0);
/*---------------------------------------------------------------------------------------------------------------------*/
/* 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 */
/* ******************************************************************************************************************* */
sprintf(debug_buffer," MQTT: Publishing with QoS: 2...");
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 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));
/*------------------------------------------------ WIFI Interface -----------------------------------------------------*/
/* Send */
ret = send(socket_mqtt, mqtt_serial_buffer_tx, length_mqtt_frame, 0);
/* Receive */
memset(mqtt_serial_buffer_rx, 0x00, sizeof(mqtt_serial_buffer_rx));
ret = recv(socket_mqtt, mqtt_serial_buffer_rx, sizeof(mqtt_serial_buffer_rx) - 1, 0);
/*---------------------------------------------------------------------------------------------------------------------*/
/* 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);
/*------------------------------------------------ WIFI Interface -----------------------------------------------------*/
/* Send */
ret = send(socket_mqtt, mqtt_serial_buffer_tx, length_mqtt_frame, 0);
/* Receive */
memset(mqtt_serial_buffer_rx, 0x00, sizeof(mqtt_serial_buffer_rx));
ret = recv(socket_mqtt, mqtt_serial_buffer_rx, sizeof(mqtt_serial_buffer_rx) - 1, 0);
/*---------------------------------------------------------------------------------------------------------------------*/
/* 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));
/*------------------------------------------------ WIFI Interface -----------------------------------------------------*/
/* Send */
ret = send(socket_mqtt, mqtt_serial_buffer_tx, length_mqtt_frame, 0);
/*---------------------------------------------------------------------------------------------------------------------*/
TSMART_UART_Send(&DEBUG_UART, "OK\r\nClosing connection...", strlen("OK\r\nClosing connection..."), 1000/portTICK_RATE_MS);
/*------------------------------------------------ WIFI Interface -----------------------------------------------------*/
/* Close socket */
ret = close(socket_mqtt);
/*---------------------------------------------------------------------------------------------------------------------*/
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 wifi */
if(TSMART_WIFI_Init() != TSMART_PASS){
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 wifi task */
if(xTaskCreate(vWIFI, "WIFI", 2048, NULL, 8, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}