diff --git a/bsp_Device/bsp_UART/mb_ModBusRTU/mbs_ModBusRTU.h b/bsp_Device/bsp_UART/mb_ModBusRTU/mbs_ModBusRTU.h index 1f3cb34..9dc69a4 100644 --- a/bsp_Device/bsp_UART/mb_ModBusRTU/mbs_ModBusRTU.h +++ b/bsp_Device/bsp_UART/mb_ModBusRTU/mbs_ModBusRTU.h @@ -102,6 +102,10 @@ u8 yr_Slave_WR_In_REGS_Ask(_mb_slave obj,u8 ID,u16 Addr,u16 Mem,u16* Data); u8 yr_Slave_WR_Hold_REGS_Ask(_mb_slave obj,u8 ID,u16 Addr,u16 Mem,u16* Data); u8 yr_Slave_WR_In_COILS_Ask(_mb_slave obj,u8 ID,u16 Addr,u16 Mem,u8* Data); u8 yr_Slave_WR_Out_COILS_Ask(_mb_slave obj,u8 ID,u16 Addr,u16 Mem,u8* Data); +#define yr_Slave_WR_Hold_REGS_ADDR_Ask(obj,ID,Addr,Mem,Data) yr_Slave_WR_Hold_REGS_Ask(obj,ID,Addr,Mem,(Data)+(Addr)) +#define yr_Slave_WR_In_REGS_ADDR_Ask(obj,ID,Addr,Mem,Data) yr_Slave_WR_In_REGS_Ask(obj,ID,Addr,Mem,(Data)+(Addr)) +#define yr_Slave_WR_Out_COILS_ADDR_Ask(obj,ID,Addr,Mem,Data) yr_Slave_WR_Out_COILS_Ask(obj,ID,Addr,Mem,(Data)+(Addr)) +#define yr_Slave_WR_In_COILS_ADDR_Ask(obj,ID,Addr,Mem,Data) yr_Slave_WR_In_COILS_Ask(obj,ID,Addr,Mem,(Data)+(Addr)) //---------------------------------------------------------------------------------------------------- #define mb_Slave_get(x,y) mb_Slave_Unpack(x,y,mb_Slave_Lencount(y)) u8 mb_Slave_Unpack(_mb_slave obj,u8* Buff,u32 len); diff --git a/bsp_Device/bsp_esp/ESP8266-AT_MQTT-1M.bin b/bsp_Device/bsp_esp/ESP8266-AT_MQTT-1M.bin new file mode 100644 index 0000000..a5c2978 Binary files /dev/null and b/bsp_Device/bsp_esp/ESP8266-AT_MQTT-1M.bin differ diff --git a/bsp_Device/bsp_esp/bsp_ESP8266.c b/bsp_Device/bsp_esp/bsp_ESP8266.c new file mode 100644 index 0000000..691a7d1 --- /dev/null +++ b/bsp_Device/bsp_esp/bsp_ESP8266.c @@ -0,0 +1,338 @@ +/*---------------------------------------------------------------------------------------------------- + # + # Copyright (c) 2022 Yuankang Liang(XerolySkinner) + # + # 本软件按原样提供,无任何明示或暗示 + # 在任何情况下,作者都不承担任何损害赔偿责任 + # + # 使用的许可声明: + # 1. 不得歪曲本软件的来源,你不能声称你编写了原始软件. + # 2. 免费授予以任何目的,前提是版权声明出现在所有副本中. + # 并且版权声明和许可声明同时出现. + # 3. 你有使用,复制,修改,分发,和销售本软件的许可. + # 4. 如果你在产品中使用,产品文档中的声明是赞赏的但不是必须的. + # 5. 本通知不得从任何来源删除或更改. + # + # Yuankang Liang(XerolySkinner) + # E-mail:zabbcccbbaz@163.com + # QQ:2715099320 + # Mobile Phone:13005636215 + # + # All rights reserved. + */ + + /** + * @file bsp_ESP8266.c + * @brief ESP8266 + * @mainpage 主要信息 + * @author Yuankang Liang(XerolySkinner) + * @email zabbcccbbaz@163.com + * @version V1.0.0 + * @date Fri Dec 29 16:39:52 2023 + + */ +#include "bsp_ESP8266.h" +////////////////////////////////////////////////////////////////////////////////////////////////////// +//---------------------------------------------------------------------------------------------------- +// 函数 +/** + * @brief title + * @param var vars + * @return res + */ +_esp esp_create(u8 (*transmit)(u8*,u32)){ + _esp obj=(_esp)esp_malloc(sizeof(_esp_struct)); + if(obj==NULL)return NULL; + obj->TX_buff = (u8*)malloc(512); + if(obj->TX_buff==NULL)return NULL; + obj->transmit=transmit; + obj->semaphore_OK = rt_sem_create("OK", 0, RT_IPC_FLAG_FIFO); + if(obj->semaphore_OK==NULL)return NULL; + return obj;} +//---------------------------------------------------------------------------------------------------- +void esp_Callback(_esp obj,const char* str,u32 limit){ + if(findSubstring(str,"OK\r\n",limit) != NULL)rt_sem_release(obj->semaphore_OK); + return;} +////////////////////////////////////////////////////////////////////////////////////////////////////// +//---------------------------------------------------------------------------------------------------- +void esp_AP_TCP_Server_Init( + _esp obj, + const char* Sname, + const char* Spswd, + const char* Sport){ + osDelay(2000); + + while(1){ + rt_kprintf("Set WIFI Mode to AP\r\n"); + esp_print(obj,"AT+CWMODE=2\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("Create the WIFI\r\n"); + esp_print(obj,"AT+CWSAP=\"%s\",\"%s\",1,4\r\n",Sname,Spswd); + if(rt_sem_take(obj->semaphore_OK,5000) == 0)break;} + + osDelay(1000); + + while(1){ + rt_kprintf("MUX is 1\r\n"); + esp_print(obj,"AT+CIPMUX=1\r\n"); + if(rt_sem_take(obj->semaphore_OK,1000) == 0)break;} + + while(1){ + rt_kprintf("Port in %s\r\n",Sport); + esp_print(obj,"AT+CIPSERVER=1,%s\r\n",Sport); + if(rt_sem_take(obj->semaphore_OK,1000) == 0)break;} + + rt_kprintf("[Success]\r\n"); + osDelay(1000);} +//---------------------------------------------------------------------------------------------------- +void esp_AP_TCP_Client_Init( + _esp obj, + const char* Sname, + const char* Spswd, + const char* Cip, + const char* Cport){ + osDelay(2000); + + while(1){ + rt_kprintf("Set WIFI Mode to AP\r\n"); + esp_print(obj,"AT+CWMODE=2\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("Create the WIFI\r\n"); + esp_print(obj,"AT+CWSAP=\"%s\",\"%s\",1,4\r\n",Sname,Spswd); + if(rt_sem_take(obj->semaphore_OK,5000) == 0)break;} + + osDelay(1000); + + 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("Link TCP as %s:%s\r\n",Cip,Cport); + esp_print(obj,"AT+CIPSTART=\"TCP\",\"%s\",%s\r\n",Cip,Cport); + if(rt_sem_take(obj->semaphore_OK,5000) == 0)break;} + + while(1){ + rt_kprintf("Transparent transmission mode\r\n"); + esp_print(obj,"AT+CIPMODE=1\r\n"); + if(rt_sem_take(obj->semaphore_OK,1000) == 0)break;} + + rt_kprintf("[Success]\r\n"); + osDelay(1000); + } +//---------------------------------------------------------------------------------------------------- +void esp_AP_UDP_Init( + _esp obj, + const char* Sname, + const char* Spswd, + const char* Cip, + const char* Cport){ + osDelay(2000); + + while(1){ + rt_kprintf("Set WIFI Mode to AP\r\n"); + esp_print(obj,"AT+CWMODE=2\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("Create the WIFI\r\n"); + esp_print(obj,"AT+CWSAP=\"%s\",\"%s\",1,4\r\n",Sname,Spswd); + if(rt_sem_take(obj->semaphore_OK,5000) == 0)break;} + + osDelay(1000); + + 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("Link UDP as %s:%s\r\n",Cip,Cport); + esp_print(obj,"AT+CIPSTART=\"UDP\",\"%s\",%s\r\n",Cip,Cport); + if(rt_sem_take(obj->semaphore_OK,5000) == 0)break;} + + while(1){ + rt_kprintf("Transparent transmission mode\r\n"); + esp_print(obj,"AT+CIPMODE=1\r\n"); + if(rt_sem_take(obj->semaphore_OK,1000) == 0)break;} + + rt_kprintf("[Success]\r\n"); + osDelay(1000); + } +////////////////////////////////////////////////////////////////////////////////////////////////////// +//---------------------------------------------------------------------------------------------------- +void esp_STA_TCP_Client_Init( + _esp obj, + const char* Sname, + const char* Spswd, + const char* Cip, + const char* Cport){ + + 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("Link TCP as %s:%s\r\n",Cip,Cport); + esp_print(obj,"AT+CIPSTART=\"TCP\",\"%s\",%s\r\n",Cip,Cport); + if(rt_sem_take(obj->semaphore_OK,5000) == 0)break;} + + while(1){ + rt_kprintf("Transparent transmission mode\r\n"); + esp_print(obj,"AT+CIPMODE=1\r\n"); + if(rt_sem_take(obj->semaphore_OK,1000) == 0)break;} + + //rt_kprintf("开始传输\r\n"); + //esp_print(obj,"AT+CIPSEND\r\n"); + //osDelay(1000); + + rt_kprintf("[Success]\r\n"); + osDelay(1000); + } +//---------------------------------------------------------------------------------------------------- +void esp_STA_TCP_Server_Init( + _esp obj, + const char* Cname, + const char* Cpswd, + const char* Sport){ + + osDelay(2000); + + while(1){ + rt_kprintf("Set WIFI to STA mode\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("Link to WIFI\r\n"); + esp_print(obj,"AT+CWJAP=\"%s\",\"%s\"\r\n",Cname,Cpswd); + if(rt_sem_take(obj->semaphore_OK,5000) == 0)break;} + + osDelay(2000); + + while(1){ + rt_kprintf("MUX is 1\r\n"); + esp_print(obj,"AT+CIPMUX=1\r\n"); + if(rt_sem_take(obj->semaphore_OK,1000) == 0)break;} + + osDelay(2000); + + while(1){ + rt_kprintf("Port in %s\r\n",Sport); + esp_print(obj,"AT+CIPSERVER=1,%s\r\n",Sport); + if(rt_sem_take(obj->semaphore_OK,1000) == 0)break;} + + rt_kprintf("[Success]\r\n"); + osDelay(1000); + } +//---------------------------------------------------------------------------------------------------- +void esp_STA_UDP_Init( + _esp obj, + const char* Cname, + const char* Cpswd, + const char* Cip, + const char* Cport){ + + osDelay(2000); + + while(1){ + rt_kprintf("Set WIFI to STA mode\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("Link to WIFI\r\n"); + esp_print(obj,"AT+CWJAP=\"%s\",\"%s\"\r\n",Cname,Cpswd); + if(rt_sem_take(obj->semaphore_OK,5000) == 0)break;} + + osDelay(1000); + + 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;} + + osDelay(1000); + + while(1){ + rt_kprintf("Link UDP as %s:%s\r\n",Cip,Cport); + esp_print(obj,"AT+CIPSTART=\"UDP\",\"%s\",%s\r\n",Cip,Cport); + if(rt_sem_take(obj->semaphore_OK,5000) == 0)break;} + + rt_kprintf("[Success]\r\n"); + osDelay(1000); + } +////////////////////////////////////////////////////////////////////////////////////////////////////// +//---------------------------------------------------------------------------------------------------- +// 常用 +void esp_CIPSEND_With_ID(_esp obj,u32 id,u8* buff,u32 len){ + esp_print(obj,"AT+CIPSEND=%d,%d\r\n",id,len); + osDelay(100); + obj->transmit(buff,len);} + +void esp_CIPSEND_Without_ID(_esp obj,u8* buff,u32 len){ + esp_print(obj,"AT+CIPSEND=%d\r\n",len); + obj->transmit(buff,len);} +////////////////////////////////////////////////////////////////////////////////////////////////////// +const char* findSubstring(const char* haystack, const char* needle, uint32_t limit) { + if (*needle == '\0') return haystack; + while (*haystack != '\0' && limit > 0) { + const char* h = haystack; + const char* n = needle; + while (*n != '\0' && *h == *n)h++,n++; + if (*n == '\0')return haystack; + haystack++; + limit--;} + return NULL;} +//---------------------------------------------------------------------------------------------------- +int esp_print(_esp obj,const char* format, ...) { + u32 res; // 返回状态码 + va_list args; // 不定长变量 + va_start(args, format); // 初始化不定长变量 + res=vsprintf((char*)(obj->TX_buff),(const char*)format, args); // 组合 + obj->transmit((obj->TX_buff), strlen((const char*)(obj->TX_buff))); // 发送 + va_end(args); // 收尸 + return res;} +////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/bsp_Device/bsp_esp/bsp_ESP8266.h b/bsp_Device/bsp_esp/bsp_ESP8266.h new file mode 100644 index 0000000..ecb45e4 --- /dev/null +++ b/bsp_Device/bsp_esp/bsp_ESP8266.h @@ -0,0 +1,117 @@ +/*---------------------------------------------------------------------------------------------------- + # + # Copyright (c) 2022 Yuankang Liang(XerolySkinner) + # + # 本软件按原样提供,无任何明示或暗示 + # 在任何情况下,作者都不承担任何损害赔偿责任 + # + # 使用的许可声明: + # 1. 不得歪曲本软件的来源,你不能声称你编写了原始软件. + # 2. 免费授予以任何目的,前提是版权声明出现在所有副本中. + # 并且版权声明和许可声明同时出现. + # 3. 你有使用,复制,修改,分发,和销售本软件的许可. + # 4. 如果你在产品中使用,产品文档中的声明是赞赏的但不是必须的. + # 5. 本通知不得从任何来源删除或更改. + # + # Yuankang Liang(XerolySkinner) + # E-mail:zabbcccbbaz@163.com + # QQ:2715099320 + # Mobile Phone:13005636215 + # + # All rights reserved. + */ + + /** + * @file bsp_ESP8266.h + * @brief ESP8266 + * @mainpage 主要信息 + * @author Yuankang Liang(XerolySkinner) + * @email zabbcccbbaz@163.com + * @version V1.0.0 + * @date Fri Dec 29 16:39:52 2023 + + */ +#ifndef _bsp_ESP8266_H +#define _bsp_ESP8266_H +#include "stdlib.h" +#include "varint.h" +#include "stdarg.h" +#include "stdio.h" +#include "string.h" +#include + +#define esp_malloc malloc +#define osDelay(x) rt_thread_delay(x) +#define ESP_DEBUG_PRINT + +#ifdef __cplusplus +extern "C"{ +#endif +////////////////////////////////////////////////////////////////////////////////////////////////////// +//---------------------------------------------------------------------------------------------------- +// 定义 +/** + * @brief TODO + */ +typedef struct{ + // 发送接口 + u8 (*transmit)(u8*,u32); + u8* TX_buff; + rt_sem_t semaphore_OK; +}_esp_struct; +typedef _esp_struct* _esp; +////////////////////////////////////////////////////////////////////////////////////////////////////// +//---------------------------------------------------------------------------------------------------- +// 定义 +_esp esp_create(u8 (*transmit)(u8*,u32)); +void 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_Without_ID(_esp obj,u8* buff,u32 len); +//---------------------------------------------------------------------------------------------------- +void esp_AP_TCP_Server_Init( + _esp obj, + const char* Sname, + const char* Spswd, + const char* Sport); + +void esp_AP_TCP_Client_Init( + _esp obj, + const char* Sname, + const char* Spswd, + const char* Cip, + const char* Cport); + +void esp_AP_UDP_Init( + _esp obj, + const char* Sname, + const char* Spswd, + const char* Cip, + const char* Cport); +//---------------------------------------------------------------------------------------------------- +void esp_STA_TCP_Client_Init( + _esp obj, + const char* Cname, + const char* Cpswd, + const char* Cip, + const char* Cport); + +void esp_STA_TCP_Server_Init( + _esp obj, + const char* Cname, + const char* Cpswd, + const char* Sport); + +void esp_STA_UDP_Init( + _esp obj, + const char* Cname, + const char* Cpswd, + const char* Cip, + const char* Cport); +//---------------------------------------------------------------------------------------------------- +int esp_print(_esp obj,const char* format, ...); +const char* findSubstring(const char* haystack, const char* needle, uint32_t limit); +////////////////////////////////////////////////////////////////////////////////////////////////////// +#ifdef __cplusplus +} +#endif +#endif