更新了ESP8266库,添加MQTT功能
This commit is contained in:
@@ -43,16 +43,23 @@
|
|||||||
_esp esp_create(u8 (*transmit)(u8*,u32)){
|
_esp esp_create(u8 (*transmit)(u8*,u32)){
|
||||||
_esp obj=(_esp)esp_malloc(sizeof(_esp_struct));
|
_esp obj=(_esp)esp_malloc(sizeof(_esp_struct));
|
||||||
if(obj==NULL)return NULL;
|
if(obj==NULL)return NULL;
|
||||||
|
|
||||||
obj->TX_buff = (u8*)malloc(512);
|
obj->TX_buff = (u8*)malloc(512);
|
||||||
if(obj->TX_buff==NULL)return NULL;
|
if(obj->TX_buff==NULL)return NULL;
|
||||||
|
obj->title = (u8*)malloc(128);
|
||||||
|
if(obj->title==NULL)return NULL;
|
||||||
|
obj->msg = (u8*)malloc(128);
|
||||||
|
if(obj->msg==NULL)return NULL;
|
||||||
|
|
||||||
obj->transmit=transmit;
|
obj->transmit=transmit;
|
||||||
obj->semaphore_OK = rt_sem_create("OK", 0, RT_IPC_FLAG_FIFO);
|
obj->semaphore_OK = rt_sem_create("OK", 0, RT_IPC_FLAG_FIFO);
|
||||||
if(obj->semaphore_OK==NULL)return NULL;
|
if(obj->semaphore_OK==NULL)return NULL;
|
||||||
|
obj->state=ESP_UNLINK;
|
||||||
return obj;}
|
return obj;}
|
||||||
//----------------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------------
|
||||||
void esp_Callback(_esp obj,const char* str,u32 limit){
|
u8 esp_Callback(_esp obj,const char* str,u32 limit){
|
||||||
if(findSubstring(str,"OK\r\n",limit) != NULL)rt_sem_release(obj->semaphore_OK);
|
if(findSubstring(str,"OK\r\n",limit) != NULL)rt_sem_release(obj->semaphore_OK);
|
||||||
return;}
|
return obj->state;}
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//----------------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------------
|
||||||
void esp_AP_TCP_Server_Init(
|
void esp_AP_TCP_Server_Init(
|
||||||
@@ -89,7 +96,8 @@ void esp_AP_TCP_Server_Init(
|
|||||||
if(rt_sem_take(obj->semaphore_OK,1000) == 0)break;}
|
if(rt_sem_take(obj->semaphore_OK,1000) == 0)break;}
|
||||||
|
|
||||||
rt_kprintf("[Success]\r\n");
|
rt_kprintf("[Success]\r\n");
|
||||||
osDelay(1000);}
|
osDelay(1000);
|
||||||
|
obj->state=ESP_LINKING;}
|
||||||
//----------------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------------
|
||||||
void esp_AP_TCP_Client_Init(
|
void esp_AP_TCP_Client_Init(
|
||||||
_esp obj,
|
_esp obj,
|
||||||
@@ -132,7 +140,7 @@ void esp_AP_TCP_Client_Init(
|
|||||||
|
|
||||||
rt_kprintf("[Success]\r\n");
|
rt_kprintf("[Success]\r\n");
|
||||||
osDelay(1000);
|
osDelay(1000);
|
||||||
}
|
obj->state=ESP_LINKING;}
|
||||||
//----------------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------------
|
||||||
void esp_AP_UDP_Init(
|
void esp_AP_UDP_Init(
|
||||||
_esp obj,
|
_esp obj,
|
||||||
@@ -175,7 +183,7 @@ void esp_AP_UDP_Init(
|
|||||||
|
|
||||||
rt_kprintf("[Success]\r\n");
|
rt_kprintf("[Success]\r\n");
|
||||||
osDelay(1000);
|
osDelay(1000);
|
||||||
}
|
obj->state=ESP_LINKING;}
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//----------------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------------
|
||||||
void esp_STA_TCP_Client_Init(
|
void esp_STA_TCP_Client_Init(
|
||||||
@@ -216,13 +224,13 @@ void esp_STA_TCP_Client_Init(
|
|||||||
esp_print(obj,"AT+CIPMODE=1\r\n");
|
esp_print(obj,"AT+CIPMODE=1\r\n");
|
||||||
if(rt_sem_take(obj->semaphore_OK,1000) == 0)break;}
|
if(rt_sem_take(obj->semaphore_OK,1000) == 0)break;}
|
||||||
|
|
||||||
//rt_kprintf("开始传输\r\n");
|
rt_kprintf("Transmit start\r\n");
|
||||||
//esp_print(obj,"AT+CIPSEND\r\n");
|
esp_print(obj,"AT+CIPSEND\r\n");
|
||||||
//osDelay(1000);
|
osDelay(1000);
|
||||||
|
|
||||||
rt_kprintf("[Success]\r\n");
|
rt_kprintf("[Success]\r\n");
|
||||||
osDelay(1000);
|
osDelay(1000);
|
||||||
}
|
obj->state=ESP_LINKING;}
|
||||||
//----------------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------------
|
||||||
void esp_STA_TCP_Server_Init(
|
void esp_STA_TCP_Server_Init(
|
||||||
_esp obj,
|
_esp obj,
|
||||||
@@ -262,7 +270,7 @@ void esp_STA_TCP_Server_Init(
|
|||||||
|
|
||||||
rt_kprintf("[Success]\r\n");
|
rt_kprintf("[Success]\r\n");
|
||||||
osDelay(1000);
|
osDelay(1000);
|
||||||
}
|
obj->state=ESP_LINKING;}
|
||||||
//----------------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------------
|
||||||
void esp_STA_UDP_Init(
|
void esp_STA_UDP_Init(
|
||||||
_esp obj,
|
_esp obj,
|
||||||
@@ -303,7 +311,76 @@ void esp_STA_UDP_Init(
|
|||||||
|
|
||||||
rt_kprintf("[Success]\r\n");
|
rt_kprintf("[Success]\r\n");
|
||||||
osDelay(1000);
|
osDelay(1000);
|
||||||
|
obj->state=ESP_LINKING;}
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//----------------------------------------------------------------------------------------------------
|
||||||
|
void esp_MQTT_Client_Init(
|
||||||
|
_esp obj,
|
||||||
|
const char* Sname,
|
||||||
|
const char* Spswd,
|
||||||
|
const char* Cip,
|
||||||
|
const char* Cport,
|
||||||
|
const char* mqtt_ID,
|
||||||
|
const char* mqtt_user,
|
||||||
|
const char* mqtt_psw){
|
||||||
|
|
||||||
|
osDelay(2000);
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
rt_kprintf("Set WIFI Mode to STA\r\n");
|
||||||
|
esp_print(obj,"AT+CWMODE=1\r\n");
|
||||||
|
if(rt_sem_take(obj->semaphore_OK,1000) == 0)break;}
|
||||||
|
|
||||||
|
rt_kprintf("Reset\r\n");
|
||||||
|
esp_print(obj,"AT+RST\r\n");
|
||||||
|
osDelay(2000);
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
rt_kprintf("Join the WIFI\r\n");
|
||||||
|
esp_print(obj,"AT+CWJAP=\"%s\",\"%s\"\r\n",Sname,Spswd);
|
||||||
|
if(rt_sem_take(obj->semaphore_OK,5000) == 0)break;}
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
rt_kprintf("MUX is 0\r\n");
|
||||||
|
esp_print(obj,"AT+CIPMUX=0\r\n");
|
||||||
|
if(rt_sem_take(obj->semaphore_OK,1000) == 0)break;}
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
rt_kprintf("Set MQTT as %s-%s-%s\r\n",mqtt_ID,mqtt_user,mqtt_psw);
|
||||||
|
esp_print(obj,"AT+MQTTUSERCFG=0,1,\"%s\",\"%s\",\"s\",0,0,\"\"\r\n",mqtt_ID,mqtt_user,mqtt_psw);
|
||||||
|
if(rt_sem_take(obj->semaphore_OK,5000) == 0)break;}
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
rt_kprintf("Link MQTT as %s-%s\r\n",Cip,Cport);
|
||||||
|
esp_print(obj,"AT+MQTTCONN=0,\"%s\",%s,0\r\n",Cip,Cport);
|
||||||
|
if(rt_sem_take(obj->semaphore_OK,5000) == 0)break;}
|
||||||
|
|
||||||
|
rt_kprintf("[Success]\r\n");
|
||||||
|
osDelay(1000);
|
||||||
|
obj->state=ESP_LINKING;}
|
||||||
|
//----------------------------------------------------------------------------------------------------
|
||||||
|
void esp_MQTT_PUSH(
|
||||||
|
_esp obj,
|
||||||
|
const char* title,
|
||||||
|
const char* msg){
|
||||||
|
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
esp_print(obj,"AT+MQTTPUB=0,\"%s\",\"%s\",0,0\r\n",title,msg);
|
||||||
|
if(rt_sem_take(obj->semaphore_OK,5000) == 0)break;}
|
||||||
}
|
}
|
||||||
|
//----------------------------------------------------------------------------------------------------
|
||||||
|
void esp_MQTT_SUB(
|
||||||
|
_esp obj,
|
||||||
|
const char* title){
|
||||||
|
while(1){
|
||||||
|
esp_print(obj,"AT+MQTTSUB=0,\"%s\",1\r\n",title);
|
||||||
|
if(rt_sem_take(obj->semaphore_OK,1000) == 0)break;}
|
||||||
|
}
|
||||||
|
//----------------------------------------------------------------------------------------------------
|
||||||
|
u32 parseMQTTMessage(_esp obj,const char *str){
|
||||||
|
return sscanf(str, "+MQTTSUBRECV:0,\"%[^\"]\",%u,%[^\n]", obj->title, &obj->value, obj->msg);
|
||||||
|
}
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//----------------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------------
|
||||||
// 常用
|
// 常用
|
||||||
|
|||||||
@@ -53,18 +53,28 @@ extern "C"{
|
|||||||
/**
|
/**
|
||||||
* @brief TODO
|
* @brief TODO
|
||||||
*/
|
*/
|
||||||
|
enum{
|
||||||
|
ESP_LINKING,
|
||||||
|
ESP_UNLINK,
|
||||||
|
};
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
// 发送接口
|
// 发送接口
|
||||||
u8 (*transmit)(u8*,u32);
|
u8 (*transmit)(u8*,u32);
|
||||||
u8* TX_buff;
|
u8* TX_buff;
|
||||||
rt_sem_t semaphore_OK;
|
rt_sem_t semaphore_OK;
|
||||||
|
u8 state;
|
||||||
|
|
||||||
|
u8 *title;
|
||||||
|
u32 value;
|
||||||
|
u8 *msg;
|
||||||
}_esp_struct;
|
}_esp_struct;
|
||||||
typedef _esp_struct* _esp;
|
typedef _esp_struct* _esp;
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//----------------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------------
|
||||||
// 定义
|
// 定义
|
||||||
_esp esp_create(u8 (*transmit)(u8*,u32));
|
_esp esp_create(u8 (*transmit)(u8*,u32));
|
||||||
void esp_Callback(_esp obj,const char* str,u32 limit);
|
u8 esp_Callback(_esp obj,const char* str,u32 limit);
|
||||||
void esp_CIPSEND_With_ID(_esp obj,u32 id,u8* buff,u32 len);
|
void esp_CIPSEND_With_ID(_esp obj,u32 id,u8* buff,u32 len);
|
||||||
void esp_CIPSEND_Without_ID(_esp obj,u8* buff,u32 len);
|
void esp_CIPSEND_Without_ID(_esp obj,u8* buff,u32 len);
|
||||||
//----------------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------------
|
||||||
@@ -108,6 +118,27 @@ void esp_STA_UDP_Init(
|
|||||||
const char* Cip,
|
const char* Cip,
|
||||||
const char* Cport);
|
const char* Cport);
|
||||||
//----------------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------------
|
||||||
|
void esp_MQTT_Client_Init(
|
||||||
|
_esp obj,
|
||||||
|
const char* Sname,
|
||||||
|
const char* Spswd,
|
||||||
|
const char* Cip,
|
||||||
|
const char* Cport,
|
||||||
|
const char* mqtt_ID,
|
||||||
|
const char* mqtt_user,
|
||||||
|
const char* mqtt_psw);
|
||||||
|
|
||||||
|
void esp_MQTT_PUSH(
|
||||||
|
_esp obj,
|
||||||
|
const char* title,
|
||||||
|
const char* msg);
|
||||||
|
|
||||||
|
void esp_MQTT_SUB(
|
||||||
|
_esp obj,
|
||||||
|
const char* title);
|
||||||
|
|
||||||
|
u32 parseMQTTMessage(_esp obj,const char *str);
|
||||||
|
//----------------------------------------------------------------------------------------------------
|
||||||
int esp_print(_esp obj,const char* format, ...);
|
int esp_print(_esp obj,const char* format, ...);
|
||||||
const char* findSubstring(const char* haystack, const char* needle, uint32_t limit);
|
const char* findSubstring(const char* haystack, const char* needle, uint32_t limit);
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
Reference in New Issue
Block a user