#include "mqttcli.h"
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "user_interface.h"
#include "espconn.h"
#include "gpio.h"
#include "uart.h"
#include "dht11.h"
/******************************************************************************
*此区域为编辑区,请需要添加设计的请到此区域编辑!
*******************************************************************************/
at_recvTask(){}//此函数在所有工程中只能创建一次，可插入任意地方
//WQTT客户端起始
LOCAL struct espconn MQTTclient;
static ip_addr_t host_ip;
os_timer_t  MQTTclienttimer;
void MQTTclienttimer_star(void);
void  MQTTclienttimeractiv(void);
uint8_t device_id[32];
/***透传模块放置区域******/



/***透传模块放置区域******/

void ICACHE_FLASH_ATTR
MQTTclient_recv(void *arg, char *pdata, unsigned short len)//接收函数
{  struct espconn *pespconn = (struct espconn *)arg;
/********连接应答**********/
if(pdata[0]==0x20&&pdata[1]==0x2){ uart0_sendStr("连接成功!");  mqtt_sub(pespconn,"DHT",0);
os_timer_setfn(&MQTTclienttimer, (os_timer_func_t *)MQTTclienttimeractiv, NULL);
os_timer_arm(&MQTTclienttimer,3000,1);
}
/********订阅应答**********/
if(pdata[0]==0x90&&pdata[1]==0x3) {uart0_sendStr("订阅成功!");}

/********心跳包应答**********/
if(pdata[0]==0xD0) { uart0_sendStr("心跳包建立成功!");}

if(pdata[0]==0x32)//离线接收
{ pdata+=2;int sum,i;sum=pdata[0]*256+pdata[1];
char topic[sum+1];pdata+=2;
for(i=0;i<sum;i++){topic[i]=*pdata;pdata++;}
topic[i]=0;//pdata+=4;
  uart0_sendStr("主题:");uart0_sendStr(topic);
  uart0_sendStr("\n消息:");uart0_sendStr(pdata);
/***********离线消息处理区*****topic为主题标题*******pdata为接收的消息******/




/******************/
}


if(pdata[0]==0x30)//即时接收
   { pdata+=2;int sum,i;sum=pdata[0]*256+pdata[1];
char topic[sum+1];pdata+=2;
for(i=0;i<sum;i++){topic[i]=*pdata;pdata++;}topic[i]=0;//pdata+=2;
  uart0_sendStr("主题:");uart0_sendStr(topic);
  uart0_sendStr("\n消息:");uart0_sendStr(pdata);
/***********消息处理区*****topic为主题标题*******pdata为接收的消息******/




/******************/ 
}}

static void ICACHE_FLASH_ATTR
MQTTclient_discon_cb(void *arg)//断开回调
{
  struct espconn *pespconn = (struct espconn *)arg;
  os_timer_disarm (&MQTTclienttimer) ;
  MQTTclienttimer_star();
    //uart0_sendStr("Unlink\r\n");

  }

static void ICACHE_FLASH_ATTR
MQTTclient_sent_cb(void *arg)//发送回调
{
struct espconn *pespconn = (struct espconn *)arg;
  
}

static void ICACHE_FLASH_ATTR
MQTTclient_connect_cb(void *arg) //连接回调
{  
  struct espconn *pespconn = (struct espconn *)arg;
  espconn_regist_disconcb(pespconn, MQTTclient_discon_cb);
  espconn_regist_recvcb(pespconn,MQTTclient_recv);
  espconn_regist_sentcb(pespconn, MQTTclient_sent_cb);
os_timer_disarm (&MQTTclienttimer) ;
os_sprintf(device_id, "DVES_%08X", system_get_chip_id());
 uart0_sendStr(device_id);//串口输出matt clientID	
mqtt_conn(pespconn,0,device_id,NULL,NULL);//与MQTT建立连接
}

static void ICACHE_FLASH_ATTR
MQTTclient_recon_cb(void *arg, sint8 errType)//重连回调,或连接失败回调
{  uart0_sendStr("连接失败！\r\n");
   os_timer_disarm (&MQTTclienttimer) ;
  MQTTclienttimer_star();
  }


 LOCAL void ICACHE_FLASH_ATTR
 MQTTclient_init(){
   uint32_t ip = 0;
  char ipTemp[128]="192.168.0.183";
  ip = ipaddr_addr(ipTemp);
MQTTclient.type = ESPCONN_TCP;
MQTTclient.proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
MQTTclient.proto.tcp->local_port = espconn_port();
MQTTclient.proto.tcp->remote_port = 1883;
os_memcpy(MQTTclient.proto.tcp->remote_ip, &ip, 4);
 espconn_regist_connectcb(&MQTTclient, MQTTclient_connect_cb); //连接回调
 espconn_regist_reconcb(&MQTTclient, MQTTclient_recon_cb);//重连回调或连接结果回调
espconn_connect(&MQTTclient);
   }

void ICACHE_FLASH_ATTR
MQTTclienttimer_exe(void) 
{MQTTclient_init();}

void ICACHE_FLASH_ATTR
MQTTclienttimeractiv(void) //发送心跳包函数
{
char temp[60];
os_sprintf(temp, "温度%d，湿度%d",readwdu(),readsdu());
mqtt_pub(&MQTTclient,"DHT",temp);
}

void ICACHE_FLASH_ATTR
MQTTclienttimer_star(void) //连接服务器调用
{
os_timer_setfn(&MQTTclienttimer, (os_timer_func_t *)MQTTclienttimer_exe, NULL);
os_timer_arm(&MQTTclienttimer,3000,1);
}
//WQTT客户端结束

 /************************/ //模块连接路由器起始,在调用wifi模式的时，设置波特率一定要在设置wifi模式前
conntwifi(void){//模块连接路由器函数调用
struct station_config stationConf;
  strcpy(stationConf.ssid, "jzzd");
   strcpy(stationConf.password,"53243690");
 ETS_UART_INTR_DISABLE();
    wifi_station_set_config(&stationConf);
    ETS_UART_INTR_ENABLE();
    wifi_station_connect();}

/*******************************/ //模块连接路由器结束

/******************************************************************************
*编辑区结尾,下面是唯一个子程序,也叫接口程序,请不要动其位置!
*******************************************************************************/
void ICACHE_FLASH_ATTR
mqttcli_init(void)
{

conntwifi();

MQTTclienttimer_star();

}
