API  2.3.0
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
mqtt_wifi_subscriber.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 be subscribed to a topic with QoS(quality of
* service) 0. 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 can be subscribed to a 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.
*
*
* @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 151).
*
* - 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 153).
*
* - 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 155)
* - WLAN_SEC_WEP -----> WEP (line 156)
* - WLAN_SEC_WPA -----> WPA (line 157)
* - WLAN_SEC_WPA2 ----> WPA2 (line 158)
*
* - Local IP address ------------------------> Set your IP address.
* - LOCAL_IP0 - 1 value of the Local IP address (line 131).
* - LOCAL_IP1 - 2 value of the Local IP address (line 132).
* - LOCAL_IP2 - 3 value of the Local IP address (line 133).
* - LOCAL_IP3 - 4 value of the Local IP address (line 134).
* ----> 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 136).
* - SUBNET_MASK1 - 2 value of the subnet mask (line 137).
* - SUBNET_MASK2 - 3 value of the subnet mask (line 138).
* - SUBNET_MASK3 - 4 value of the subnet mask (line 139).
* ----> 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 141).
* - GW_IP1 - 2 value of the gateway IP address (line 142).
* - GW_IP2 - 3 value of the gateway IP address (line 143).
* - GW_IP3 - 4 value of the gateway IP address (line 144).
* ----> 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 146).
* - DNS_IP_SERVER1 - 2 value of the DNS IP server address (line 147).
* - DNS_IP_SERVER2 - 3 value of the DNS IP server address (line 148).
* - DNS_IP_SERVER3 - 4 value of the DNS IP server address (line 149).
* ----> 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 161).
* - MQTT_SERVER_IP1_ADDRESS --> 2 value of the MQTT server IP address (line 162).
* - MQTT_SERVER_IP2_ADDRESS --> 3 value of the MQTT server IP address (line 163).
* - MQTT_SERVER_IP3_ADDRESS --> 4 value of the MQTT server IP address (line 164).
*
* - MQTT port server --------> The MQTT port server enabled to connect.
* - MQTT_PORT - Port number (line 160).
*
* - MQTT Client parameters --> MQTT parameters to stablish a MQTT connection:
* - MQTT_CLIENT_ID -----------> Client identifier (line 165).
* - MQTT_USER ----------------> User name (line 166).
* - MQTT_PASSWORD ------------> Password (line 167).
*
*
* @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_S\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 */
/* 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"
/* SUBSCRIPTION VALUES */
#define NUMBER_OF_TOPIC_TO_BE_SUBSCRIPTED 1
/*-------------------------------------------------------- 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.
* It will connect with the MQTT server and it will subscribe to a
* specific topic with QoS: 0. After that, the example will wait for new
* publications as consequence of the subscription.
*
* @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;
uint32_t reception_timeout = 0xFFF;
int ret;
int socket_mqtt = 0;
int length_mqtt_frame = 0;
int connack_code = 0;
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
int rcvd_packetid;
int rcvd_count;
int rcvd_grantedQoSs[NUMBER_OF_TOPIC_TO_BE_SUBSCRIPTED];
uint8_t s_publish_lenght;
int s_dup;
int s_retained;
int s_packetid;
int s_qos;
MQTTString s_received_topic[NUMBER_OF_TOPIC_TO_BE_SUBSCRIPTED];
MQTTString s_topic[NUMBER_OF_TOPIC_TO_BE_SUBSCRIPTED];
int s_requested_qoss[NUMBER_OF_TOPIC_TO_BE_SUBSCRIPTED];
int s_payloadlen_in;
char* s_payload_in;
uint8_t topic_recived_buffer[30];
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 -----------------------------------------------------*/
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 SUBSCRIBE QoS: 0 */
/* ******************************************************************************************************************* */
TSMART_UART_Send(&DEBUG_UART, " MQTT: Subscribe with QoS: 0...", strlen(" MQTT: Subscribe with QoS: 0..."), 1000/portTICK_RATE_MS);
/* MQTT Subscriptions */
s_topic[0].cstring = MQTT_TOPIC;
s_topic[0].lenstring.len = 0;
s_topic[0].lenstring.data = NULL;
s_requested_qoss[0] = 0;
s_packetid = 1;
s_dup = 0;
length_mqtt_frame = MQTTSerialize_subscribe(mqtt_serial_buffer_tx, sizeof(mqtt_serial_buffer_rx), s_dup, s_packetid, NUMBER_OF_TOPIC_TO_BE_SUBSCRIPTED, s_topic, s_requested_qoss);
/*------------------------------------------------ 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);
/*-----------------------------------------------------------------------------------------------------------*/
if(MQTTDeserialize_suback(&rcvd_packetid, NUMBER_OF_TOPIC_TO_BE_SUBSCRIPTED, &rcvd_count, rcvd_grantedQoSs, mqtt_serial_buffer_rx, ret) != 1){
TSMART_UART_Send(&DEBUG_UART, "SUBACK ERROR\r\n", strlen("SUBACK ERROR\r\n"), 1000/portTICK_RATE_MS);
for(;;);
}
/* Check SUBACK */
if(rcvd_packetid != s_packetid){
sprintf(debug_buffer, "SUBACK ERROR\r\n");
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* ******************************************************************************************************************* */
/* MQTT WAIT FOR SUBSCRIPTION QoS: 0 */
/* ******************************************************************************************************************* */
TSMART_UART_Send(&DEBUG_UART, "Setting socket timeout...", strlen("Setting socket timeout..."), 1000/portTICK_RATE_MS);
if(setsockopt( socket_mqtt, SOL_SOCKET, SOCKOPT_RECV_TIMEOUT , &reception_timeout, sizeof(reception_timeout))){
TSMART_UART_Send(&DEBUG_UART, "fail\r\n", strlen("fail\r\n"), 1000/portTICK_RATE_MS);
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
while(1){
TSMART_UART_Send(&DEBUG_UART, "\r\nWaiting for a new message...\r\n", strlen("\r\nWaiting for a new message...\r\n"), 1000/portTICK_RATE_MS);
/*------------------------------------------------ WIFI Interface -------------------------------------------*/
/* 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);
/*-----------------------------------------------------------------------------------------------------------*/
if(ret > 0){
if(mqtt_serial_buffer_rx[0]>>4 == PUBLISH){
/* Get subscription payload length */
s_publish_lenght = mqtt_serial_buffer_rx[1];
/* Deserialize publish subscription */
if(MQTTDeserialize_publish(&s_dup, &s_qos, &s_retained, &s_packetid, s_received_topic, &s_payload_in, &s_payloadlen_in, mqtt_serial_buffer_rx, s_publish_lenght) == 1){
/* Get topic */
memset(topic_recived_buffer, 0x00, sizeof(topic_recived_buffer));
memcpy(topic_recived_buffer, s_received_topic->lenstring.data, s_received_topic->lenstring.len);
/* Display information received */
sprintf(debug_buffer,
"---- message received ----\r\n"
"lenght: %d DUP: %d Retain: %d\r\n"
"PacketId: %d QoS: %d Topic: %s\r\n"
"Data(%d bytes): %s \r\n"
"-------------------------------\r\n",
s_publish_lenght, s_dup, s_retained,
s_packetid, s_qos, topic_recived_buffer,
s_payloadlen_in, s_payload_in);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 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;
}