Exemple #1
0
void ICACHE_FLASH_ATTR
mqtt_tcpclient_discon_cb(void *arg)
{

	struct espconn *pespconn = (struct espconn *)arg;
	MQTT_Client* client = (MQTT_Client *)pespconn->reverse;
	INFO("TCP: Disconnected callback\r\n");
	client->connState = TCP_RECONNECT_REQ;
	if(client->disconnectedCb)
		client->disconnectedCb((uint32_t*)client);

	system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);
}
Exemple #2
0
void ICACHE_FLASH_ATTR dht22_init() {

    DHT_DBG("dht22_init");

    os_timer_disarm(&dht_timer);
    os_timer_setfn(&dht_timer, (os_timer_func_t *)dht22_timer_cb, NULL);
    os_timer_arm(&dht_timer, 5000, 1);

    //Start os task
    system_os_task(dht22_task, 1 ,taskQueue, 2);
    system_os_post(1, SIG_DHT, NULL );

}
Exemple #3
0
/**
  * @brief  Tcp client connect success callback function.
  * @param  arg: contain the ip link information
  * @retval None
  */
void ICACHE_FLASH_ATTR
mqtt_tcpclient_connect_cb(void *arg)
{
	struct espconn *pCon = (struct espconn *)arg;
	MQTT_Client* client = (MQTT_Client *)pCon->reverse;

	espconn_regist_disconcb(client->pCon, mqtt_tcpclient_discon_cb);
	espconn_regist_recvcb(client->pCon, mqtt_tcpclient_recv);////////
	espconn_regist_sentcb(client->pCon, mqtt_tcpclient_sent_cb);///////
//spam	INFO("MQTT: Connected to broker %s:%d\r\n", client->host, client->port);
	client->connState = MQTT_CONNECT_SEND;
	system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);
}
Exemple #4
0
void ICACHE_FLASH_ATTR mqtt_timer(void *arg) {
    MQTT_Client *client = (MQTT_Client *) arg;
    if (client->connState == MQTT_DATA) {
	client->keepAliveTick++;
	if (client->keepAliveTick > (client->mqtt_state.connect_info->keepalive / 2)) {
	    client->connState = MQTT_KEEPALIVE_SEND;
	    system_os_post(MQTT_TASK_PRIO, 0, (os_param_t) client);
	}

    } else if (client->connState == TCP_RECONNECT_REQ) {
	client->reconnectTick++;
	if (client->reconnectTick > MQTT_RECONNECT_TIMEOUT) {
	    client->reconnectTick = 0;
	    client->connState = TCP_RECONNECT;
	    system_os_post(MQTT_TASK_PRIO, 0, (os_param_t) client);
	    if (client->timeoutCb)
		client->timeoutCb((uint32_t *) client);
	}
    }
    if (client->sendTimeout > 0)
	client->sendTimeout--;
}
Exemple #5
0
void mg_if_connect_udp(struct mg_connection *nc) {
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
  struct udp_pcb *upcb = udp_new();
  cs->err = udp_bind(upcb, IP_ADDR_ANY, 0 /* any port */);
  DBG(("%p udp_bind %p = %d", nc, upcb, cs->err));
  if (cs->err == ERR_OK) {
    udp_recv(upcb, mg_lwip_udp_recv_cb, nc);
    cs->pcb.udp = upcb;
  } else {
    udp_remove(upcb);
  }
  system_os_post(MG_TASK_PRIORITY, MG_SIG_CONNECT_RESULT, (uint32_t) nc);
}
/**
  * @brief  Tcp client connect repeat callback function.
  * @param  arg: contain the ip link information
  * @retval None
  */
void ICACHE_FLASH_ATTR
mqtt_tcpclient_recon_cb(void *arg, sint8 errType)
{
	struct espconn *pCon = (struct espconn *)arg;
	MQTT_Client* client = (MQTT_Client *)pCon->reverse;

	INFO("TCP: Reconnect to %s:%d\r\n", client->host, client->port);

	client->connState = TCP_RECONNECT_REQ;

	system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);

}
Exemple #7
0
static err_t mg_lwip_tcp_sent_cb(void *arg, struct tcp_pcb *tpcb,
                                 u16_t num_sent) {
  struct mg_connection *nc = (struct mg_connection *) arg;
  DBG(("%p %p %u", nc, tpcb, num_sent));
  if (nc == NULL) {
    tcp_abort(tpcb);
    return ERR_ABRT;
  }
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
  cs->num_sent += num_sent;
  system_os_post(MG_TASK_PRIORITY, MG_SIG_SENT_CB, (uint32_t) nc);
  return ERR_OK;
}
Exemple #8
0
// for use in interrupts
int8_t esp_queue_daint_8(esp_queue_obj_t *queue_in, uint8_t value) {
	if (queue_in->items >= queue_in->max_items) {
        // printf("full\n");
        return -1;
    } 
    queue_in->obj_instances[queue_in->last] = MP_OBJ_NEW_SMALL_INT(value);
    queue_in->items++;
    queue_in->last = (queue_in->last + 1) % queue_in->max_items;
    if (queue_in->os_task != mp_const_none) {
        system_os_post(SENSOR_TASK_ID, 1, (os_param_t)queue_in->os_task);
    }
    return 0;
}
Exemple #9
0
/**
  * @brief  Client send over callback function.
  * @param  arg: contain the ip link information
  * @retval None
  */
void ICACHE_FLASH_ATTR
mqtt_tcpclient_sent_cb(void *arg)
{
	struct espconn *pCon = (struct espconn *)arg;
	MQTT_Client* client = (MQTT_Client *)pCon->reverse;
	INFO("TCP: Sent\r\n");
	client->sendTimeout = 0;
	if(client->connState == MQTT_DATA && client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_PUBLISH){
		if(client->publishedCb)
			client->publishedCb((uint32_t*)client);
	}
	system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);
}
Exemple #10
0
// ----------------------------------------------------------------------------
// Init function
// ----------------------------------------------------------------------------
void user_init(void)
{
    ioInit();               // initialize UART, GPIO and crap like that
    os_delay_us(10000);
    for(int i = 0; i < 30; i++) os_printf(" \n");
    menu();

    // ------------------------------------------------------------------------
    // Start os task
    // ------------------------------------------------------------------------
    system_os_task(loop, user_procTaskPrio, user_procTaskQueue, user_procTaskQueueLen);
    system_os_post(user_procTaskPrio, 0, 0 );
}
Exemple #11
0
//Init function
void ICACHE_FLASH_ATTR
user_init()
{
	uart_init(BIT_RATE_115200, BIT_RATE_115200);
	os_delay_us(100000);

	wifiInit();
	
	//Start os task
	system_os_task(loop, user_procTaskPrio, user_procTaskQueue, user_procTaskQueueLen);
	
	system_os_post(user_procTaskPrio, 0, 0);
}
Exemple #12
0
static void ICACHE_FLASH_ATTR
procTask(os_event_t *events)
{
	system_os_post(procTaskPrio, 0, 0 );

	if( events->sig == 0 && events->par == 0 )
	{
		//Idle Event.
	}
//	printf( "+" );
//	ets_delay_us( 20000 );
//	printf( "-" );
}
Exemple #13
0
//Init function 
void ICACHE_FLASH_ATTR user_init() {

    //uart_init(BIT_RATE_115200,BIT_RATE_115200);
    flashData = &_flashData;
    IPStation = ipstation;

    stdoutInit();
    char ssid[32] = SSID;
    char password[64] = SSID_PASSWORD;
    struct station_config stationConf;


    os_printf("\ninit\n");


    user_init_gpio();
    //Set station mode & AP mode
    // wifi_set_opmode(STATION_MODE);
    wifi_set_opmode(STATIONAP_MODE);

    initFlash();
    flash_read();

    //if (flashData->magic != MAGIC_NUM)
    {
    //Set ap settings
        os_memcpy(&stationConf.ssid, ssid, 32);
        os_memcpy(&stationConf.password, password, 64);
    }
//    else
//    {
//        os_memcpy(&stationConf.ssid, flashData->ssid, 32);
//        os_memcpy(&stationConf.password, flashData->password, 64);
//
//    }
    wifi_station_set_config(&stationConf);
    os_printf("\nConnecting to %s, %s\n", stationConf.ssid, stationConf.password);

    SetSetverMode();


    //Start os task
    system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen);

    system_os_post(user_procTaskPrio, 0, 0 );

    os_printf("\nInitializing Network\n");
    network_init(); 


}
/*******************************************************************************
* 名称:LesInit
* 功能:Les系统
* 形参:
* 返回: 无
* 说明:
******************************************************************************/
void ICACHE_FLASH_ATTR
LesTask(void)
{

#ifdef CloudKey
	if(!SysTask.m_CloudOk && IsConnect)
		Cloud_Client_Send();        //发送数据-》云端
#endif

//	Le_CheckStationState();
    SysTask_Tick();

	system_os_post(LesTaskPrio, 0, 0);
}
void mg_if_connect_tcp(struct mg_connection *nc,
                       const union socket_address *sa) {
  struct tcp_pcb *tpcb = tcp_new();
  nc->sock = (int) tpcb;
  ip_addr_t *ip = (ip_addr_t *) &sa->sin.sin_addr.s_addr;
  u16_t port = ntohs(sa->sin.sin_port);
  tcp_arg(tpcb, nc);
  tcp_err(tpcb, mg_lwip_tcp_error_cb);
  tcp_sent(tpcb, mg_lwip_tcp_sent_cb);
  tcp_recv(tpcb, mg_lwip_tcp_recv_cb);
  nc->err = tcp_bind(tpcb, IP_ADDR_ANY, 0 /* any port */);
  DBG(("%p tcp_bind = %d", nc, nc->err));
  if (nc->err != ERR_OK) {
    system_os_post(MG_TASK_PRIORITY, MG_SIG_CONNECT_RESULT, (uint32_t) nc);
    return;
  }
  nc->err = tcp_connect(tpcb, ip, port, mg_lwip_tcp_conn_cb);
  DBG(("%p tcp_connect = %d", nc, nc->err));
  if (nc->err != ERR_OK) {
    system_os_post(MG_TASK_PRIORITY, MG_SIG_CONNECT_RESULT, (uint32_t) nc);
    return;
  }
}
Exemple #16
0
STATIC ICACHE_FLASH_ATTR mp_obj_t mod_esp_queue_put(mp_obj_t self_in, mp_obj_t add_obj) {
    esp_queue_obj_t *self = self_in;

	if (self->items >= self->max_items) {
         nlr_raise(mp_obj_new_exception(&mp_type_Full));
	}
    self->items++;
    self->obj_instances[self->last] = add_obj;
    self->last = (self->last + 1) % self->max_items;
    if (self->os_task != mp_const_none) {
        system_os_post(SENSOR_TASK_ID, 1, (os_param_t)self->os_task);
    }
    return mp_const_none;
}
Exemple #17
0
void ICACHE_FLASH_ATTR MQTT_DeleteClient(MQTT_Client * mqttClient) {
    if (NULL == mqttClient)
	return;

    mqttClient->connState = MQTT_DELETED;
    // if(TCP_DISCONNECTED == mqttClient->connState) {
    //  mqttClient->connState = MQTT_DELETED;
    // } else if(MQTT_DELETED != mqttClient->connState) {
    //  mqttClient->connState = MQTT_DELETING;
    // }

    system_os_post(MQTT_TASK_PRIO, 0, (os_param_t) mqttClient);
    os_timer_disarm(&mqttClient->mqttTimer);
}
Exemple #18
0
LOCAL void ICACHE_FLASH_ATTR
mqtt_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
{
	struct espconn *pConn = (struct espconn *)arg;
	MQTT_Client* client = (MQTT_Client *)pConn->reverse;
	DEBUG("mqtt_dns_found: enter");


#if 0
        DEBUG("About to log IP address ...");
	delay(2000);
	DEBUG("&ip_addr address %p", &ipaddr);
	delay(2000);
	DEBUG("ip_addr address %p", ipaddr);
	delay(2000);
#endif

	if(ipaddr == NULL)
	{
		WARN("DNS: Found, but got no ip, try to reconnect");
		client->connState = TCP_RECONNECT_REQ;
		return;
	}
	INFO("DNS: found ip %d.%d.%d.%d\n",
			*((uint8 *) &ipaddr->addr),
			*((uint8 *) &ipaddr->addr + 1),
			*((uint8 *) &ipaddr->addr + 2),
			*((uint8 *) &ipaddr->addr + 3));

	if(client->ip.addr == 0 && ipaddr->addr != 0)
	{

		DEBUG("let's connect");
		os_memcpy(client->pCon->proto.tcp->remote_ip, &ipaddr->addr, 4);
		if(client->security){
			espconn_secure_connect(client->pCon);
		}
		else {
			//delay(1000);
			espconn_connect(client->pCon);
		}

		client->connState = TCP_CONNECTING;
		INFO("TCP: connecting...");
	}

	DEBUG("os post");
	system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);
	DEBUG("dns found exit");
}
//Init function 
void ICACHE_FLASH_ATTR
user_init()
{
    char ssid[32] = SSID;
    char password[64] = SSID_PASSWORD;
    struct station_config stationConf;

    //Set output debugger bandwidth
    uart_div_modify(0, UART_CLK_FREQ / 115200);

    //Set station mode
    wifi_set_opmode( 0x1 );

    //Set ap settings
    os_memcpy(&stationConf.ssid, ssid, 32);
    os_memcpy(&stationConf.password, password, 64);
    if ( wifi_station_set_config(&stationConf) )
    {
        os_printf("Wi-fi configured\n");
    }
    else
    {
        os_printf("ERROR:Wi-fi NOT configured\n");
    }

    struct espconn connection;
    ip_addr_t ipaddr;
    ipaddr.addr = 3105625166U; // hardcoded IP of golink.besaba.com   
    int rtr_val;
    if ( (rtr_val = espconn_gethostbyname(&connection, HOSTNAME, &ipaddr, 
            gethost_callback)) != ESPCONN_OK )
    {
        if (rtr_val == ESPCONN_INPROGRESS)
        {
            os_printf("ERROR:espconn_gethostbyname INPROGRESS\n");
        } else if (rtr_val == ESPCONN_ARG)
        {
            os_printf("ERROR:espconn_gethostbyname ARG\n");
        } else 
        {
            os_printf("ERROR:espconn_gethostbyname UNKNOWN ERROR\n");
        }
    }

    //Start os task
    system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen);

    system_os_post(user_procTaskPrio, 0, 0 );
}
Exemple #20
0
void ICACHE_FLASH_ATTR mqtt_send_keepalive(MQTT_Client * client) {
    MQTT_INFO("\r\nMQTT: Send keepalive packet to %s:%d!\r\n", client->host, client->port);
    client->mqtt_state.outbound_message = mqtt_msg_pingreq(&client->mqtt_state.mqtt_connection);
    client->mqtt_state.pending_msg_type = MQTT_MSG_TYPE_PINGREQ;
    client->mqtt_state.pending_msg_type = mqtt_get_type(client->mqtt_state.outbound_message->data);
    client->mqtt_state.pending_msg_id =
	mqtt_get_id(client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length);

    client->sendTimeout = MQTT_SEND_TIMOUT;
    MQTT_INFO("MQTT: Sending, type: %d, id: %04X\r\n", client->mqtt_state.pending_msg_type,
	      client->mqtt_state.pending_msg_id);
    err_t result = ESPCONN_OK;
    if (client->security) {
#ifdef MQTT_SSL_ENABLE
	result =
	    espconn_secure_send(client->pCon, client->mqtt_state.outbound_message->data,
				client->mqtt_state.outbound_message->length);
#else
	MQTT_INFO("TCP: Do not support SSL\r\n");
#endif
    } else {
	result =
	    espconn_send(client->pCon, client->mqtt_state.outbound_message->data,
			 client->mqtt_state.outbound_message->length);
    }

    client->mqtt_state.outbound_message = NULL;
    if (ESPCONN_OK == result) {
	client->keepAliveTick = 0;
	client->connState = MQTT_DATA;
	system_os_post(MQTT_TASK_PRIO, 0, (os_param_t) client);
    } else {
	client->connState = TCP_RECONNECT_DISCONNECTING;
	system_os_post(MQTT_TASK_PRIO, 0, (os_param_t) client);
    }
}
Exemple #21
0
static void ICACHE_FLASH_ATTR
at_procTask(os_event_t *events)
{
//	uart0_sendStr("ATPRoc");
	system_os_post(at_procTaskPrio, 0, 0 );
	if( events->sig == 0 && events->par == 0 )
	{
		//Idle Event.
/*		if( connections[0].pespconn && connections[0].cansend )
		{
		    espconn_sent( connections[0].pespconn, "hello\r\n", 7 );
		}
*/
	}
}
Exemple #22
0
/**
 * The receive callback called when data is received.
 */
static void recvCB(void *arg, char *pData, unsigned short len) {
	LOG("recvCB\n");
	struct espconn *pEspconn;
	pEspconn = (struct espconn *) arg;
	ESP_RestClient *pESP_RestClient = (ESP_RestClient *) pEspconn->reverse;
	LOG("Data: length received = %d\n", len);
	char *pBuf = (char *)malloc(len + 1);
	memcpy(pBuf, pData, len);
	pBuf[len] = '\0';
	pESP_RestClient->m_response = String(pBuf);
	free(pBuf);
	// Don't try and print the data here ... too much...
	pESP_RestClient->_close();
	system_os_post(REST_TASK_PRI, ESP_REST_CLIENT_RESPONSE_SIG, (os_param_t)pESP_RestClient);
} // End of recvCB
Exemple #23
0
/**
  * @brief  MQTT subscibe function.
  * @param  client: 	MQTT_Client reference
  * @param  topic: 		string topic will subscribe
  * @param  qos:		qos
  * @retval TRUE if success queue
  */
BOOL ICACHE_FLASH_ATTR
MQTT_Subscribe(MQTT_Client *client, char* topic, uint8_t qos)
{


	client->mqtt_state.outbound_message = mqtt_msg_subscribe(&client->mqtt_state.mqtt_connection,
											topic, 0,
											&client->mqtt_state.pending_msg_id);
	INFO("MQTT: queue subscribe, topic\"%s\", id: %d\r\n",topic, client->mqtt_state.pending_msg_id);
	if(QUEUE_Puts(&client->msgQueue, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length) == -1){
		INFO("MQTT: Exceed the amount of queues\r\n");
		return FALSE;
	}
	system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);
	return TRUE;
}
Exemple #24
0
LOCAL void ICACHE_FLASH_ATTR
mqtt_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
{
    struct espconn *pConn = (struct espconn *)arg;
    MQTT_Client* client = (MQTT_Client *)pConn->reverse;


    if (ipaddr == NULL)
    {
        INFO("DNS: Found, but got no ip, try to reconnect\r\n");
        client->connState = TCP_RECONNECT_REQ;
        return;
    }

    INFO("DNS: found ip %d.%d.%d.%d\n",
         *((uint8 *) &ipaddr->addr),
         *((uint8 *) &ipaddr->addr + 1),
         *((uint8 *) &ipaddr->addr + 2),
         *((uint8 *) &ipaddr->addr + 3));

    if (client->ip.addr == 0 && ipaddr->addr != 0)
    {
        os_memcpy(client->pCon->proto.tcp->remote_ip, &ipaddr->addr, 4);
        if (client->security) {
#ifdef MQTT_SSL_ENABLE
            if(DEFAULT_SECURITY >= ONE_WAY_ANTHENTICATION ) {
                espconn_secure_ca_enable(ESPCONN_CLIENT,CA_CERT_FLASH_ADDRESS);
            }
            if(DEFAULT_SECURITY >= TWO_WAY_ANTHENTICATION) {
                espconn_secure_cert_req_enable(ESPCONN_CLIENT,CLIENT_CERT_FLASH_ADDRESS);
            }

            espconn_secure_connect(client->pCon);
#else
            INFO("TCP: Do not support SSL\r\n");
#endif
        }
        else {
            espconn_connect(client->pCon);
        }

        client->connState = TCP_CONNECTING;
        INFO("TCP: connecting...\r\n");
    }

    system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);
}
Exemple #25
0
void mg_dispatch_v7_callback(struct v7 *v7, v7_val_t func, v7_val_t this_obj,
                             v7_val_t args) {
  struct v7_callback_args *cba =
      (struct v7_callback_args *) calloc(1, sizeof(*cba));
  if (cba == NULL) {
    DBG(("OOM"));
    return;
  }
  cba->v7 = v7;
  cba->func = func;
  cba->this_obj = this_obj;
  cba->args = args;
  v7_own(v7, &cba->func);
  v7_own(v7, &cba->this_obj);
  v7_own(v7, &cba->args);
  system_os_post(MG_TASK_PRIORITY, MG_SIG_V7_CALLBACK, (uint32_t) cba);
}
Exemple #26
0
static void mg_lwip_send_more(struct mg_connection *nc) {
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
  if (nc->sock == INVALID_SOCKET || cs->pcb.tcp == NULL) {
    DBG(("%p invalid socket", nc));
    return;
  }
  struct tcp_pcb *tpcb = cs->pcb.tcp;
  int num_written =
      mg_lwip_tcp_write(tpcb, nc->send_mbuf.buf, nc->send_mbuf.len);
  DBG(("%p mg_lwip_tcp_write %u = %d", nc, nc->send_mbuf.len, num_written));
  if (num_written == 0) return;
  if (num_written < 0) {
    system_os_post(MG_TASK_PRIORITY, MG_SIG_CLOSE_CONN, (uint32_t) nc);
  }
  mbuf_remove(&nc->send_mbuf, num_written);
  mbuf_trim(&nc->send_mbuf);
}
int tcpserver_send(struct espconn *conn, void *data, uint16 len, enum Memtype mem)
{
	DEBUG("enter tcpserver_send");
	ets_intr_lock();

	if (MessageQueue_push(&sendq, conn, data, len, mem) != 0) {
		ets_intr_unlock();
		ets_uart_printf("Failed to push data into send queue.\n");
		DEBUG("exit tcpserver_send");
		return -1;
	}

	ets_intr_unlock();
	system_os_post(SERVER_TASK_PRIO, SERVER_SIG_TX, 0);
	DEBUG("exit tcpserver_send");
	return 0;
}
Exemple #28
0
void ESP_RestClient::_connect() {
	LOG("_connect: %i\n", &m_serverIP);
	memcpy(m_conn.proto.tcp->remote_ip, &m_serverIP, 4);
	m_conn.proto.tcp->remote_port = m_port;
	dumpEspConn(&m_conn);
	if (m_secure == false) {
		int rc = espconn_connect(&m_conn);
		if (rc != 0) {
			// Error
			LOG("Error with connect\n");
			system_os_post(REST_TASK_PRI, ESP_REST_CLIENT_ERROR_SIG, (os_param_t)this);
			return;
		}
	} else {
		//int rc = espconn_secure_connect(&m_conn);
	}
} // End of m_connect
void user_init(void)
{
	uart_init(BIT_RATE_115200, BIT_RATE_115200);

	//Add a process
	uart0_sendStr("Registering idle task...\r\n");
	system_os_task(procTask, procTaskPrio, procTaskQueue, procTaskQueueLen);

	//Timer example
	uart0_sendStr("Starting timer task...\r\n");
	os_timer_disarm(&some_timer);
	os_timer_setfn(&some_timer, (os_timer_func_t *)myProber, NULL);
	os_timer_arm(&some_timer, 50, 1); // Do this every 50ms
 
	uart0_sendStr("Starting idle task...\r\n");
	system_os_post(procTaskPrio, 0, 0 );
}
Exemple #30
0
/**
  * @brief  MQTT initialization mqtt client function
  * @param  client:     MQTT_Client reference
  * @param  clientid:     MQTT client id
  * @param  client_user:MQTT client user
  * @param  client_pass:MQTT client password
  * @param  client_pass:MQTT keep alive timer, in second
  * @retval None
  */
void ICACHE_FLASH_ATTR
MQTT_InitClient(MQTT_Client *mqttClient, uint8_t* client_id, uint8_t* client_user, uint8_t* client_pass, uint32_t keepAliveTime, uint8_t cleanSession)
{
    uint32_t temp;
    INFO("MQTT_InitClient\r\n");

    os_memset(&mqttClient->connect_info, 0, sizeof(mqtt_connect_info_t));

    temp = os_strlen(client_id);
    mqttClient->connect_info.client_id = (uint8_t*)os_zalloc(temp + 1);
    os_strcpy(mqttClient->connect_info.client_id, client_id);
    mqttClient->connect_info.client_id[temp] = 0;

    if (client_user)
    {
        temp = os_strlen(client_user);
        mqttClient->connect_info.username = (uint8_t*)os_zalloc(temp + 1);
        os_strcpy(mqttClient->connect_info.username, client_user);
        mqttClient->connect_info.username[temp] = 0;
    }

    if (client_pass)
    {
        temp = os_strlen(client_pass);
        mqttClient->connect_info.password = (uint8_t*)os_zalloc(temp + 1);
        os_strcpy(mqttClient->connect_info.password, client_pass);
        mqttClient->connect_info.password[temp] = 0;
    }


    mqttClient->connect_info.keepalive = keepAliveTime;
    mqttClient->connect_info.clean_session = cleanSession;

    mqttClient->mqtt_state.in_buffer = (uint8_t *)os_zalloc(MQTT_BUF_SIZE);
    mqttClient->mqtt_state.in_buffer_length = MQTT_BUF_SIZE;
    mqttClient->mqtt_state.out_buffer =  (uint8_t *)os_zalloc(MQTT_BUF_SIZE);
    mqttClient->mqtt_state.out_buffer_length = MQTT_BUF_SIZE;
    mqttClient->mqtt_state.connect_info = &mqttClient->connect_info;

    mqtt_msg_init(&mqttClient->mqtt_state.mqtt_connection, mqttClient->mqtt_state.out_buffer, mqttClient->mqtt_state.out_buffer_length);

    QUEUE_Init(&mqttClient->msgQueue, QUEUE_BUFFER_SIZE);

    system_os_task(MQTT_Task, MQTT_TASK_PRIO, mqtt_procTaskQueue, MQTT_TASK_QUEUE_SIZE);
    system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)mqttClient);
}