基于stm32单片机的ESP-01S模块(wifi模块)的使用

  • Post author:
  • Post category:其他


一、开发环境

开发板:野火stm32f407开发板

wifi模块:ESP-01S(核心处理器 ESP8266 ),固件版本(MQTT固件)

操作系统:rt-thread操作系统

二、ESP-01S模块介绍

ESP-01S 是由安信可科技开发的 Wi-Fi 模块,该模块核心处理器 ESP8266 在较小尺寸封装中集成了业界领先的 Tensilica L106 超低功耗 32 位微型 MCU,带有 16 位精简模式,主频支持 80 MHz 和 160 MHz,支持 RTOS,集成 Wi-Fi MAC/ BB/RF/PA/LNA。

ESP-01S Wi-Fi 模块支持标准的 IEEE802.11 b/g/n 协议,完整的 TCP/IP 协议栈。用户可以使用该模块为现有的设备添加联网功能,也可以构建独立的网络控制器。

ESP8266 是高性能无线 SoC,以最低成本提供最大实用性,为 Wi-Fi 功能嵌入其他系统提供无限可能。

6b16b610eaa9cfe3e419c177dc967d5c.png

连接方式:开发板和ESP-01S模块通过串口2连接。单片机引脚是PA2和PA3。

供电方式:3.3V供电。

三、工程创建

基于rt-thread系统bsp工程创建,bsp工程使用的是stm32f407-atk-explorer。

使用env工具配置工程,使能usart2和增加at-device软件包。

fa051b6d09463962dc8a96cd8d904f38.png

81be0040a26582937d8d9232344e2744.png

保存,下载软件包,重新生成工程。

9b8467cbde3da6de63f3eef76094f68a.png

四、工程修改

1、时钟修改;

2、增加串口2的初始化配置

8e63608f447f182f7e164886611d8739.png

适配固件信息,如果不修改的话会提示不支持AT+CIPDNS_CUR?命令。

代码修改如下:

static void esp8266_get_netdev_info(struct rt_work *work, void *work_data)
{
#define AT_ADDR_LEN          32
#define AT_ERR_DNS_SERVER    "255.255.255.255"
#define AT_DEF_DNS_SERVER    "114.114.114.114"


    at_response_t resp = RT_NULL;
    char ip[AT_ADDR_LEN] = {0}, mac[AT_ADDR_LEN] = {0};
    char gateway[AT_ADDR_LEN] = {0}, netmask[AT_ADDR_LEN] = {0};
    char dns_server1[AT_ADDR_LEN] = {0}, dns_server2[AT_ADDR_LEN] = {0};
    const char *resp_expr = "%*[^\"]\"%[^\"]\"";
    const char *resp_dns = "+CIPDNS:1,%s";//
    ip_addr_t ip_addr;
    rt_uint32_t mac_addr[6] = {0};
    rt_uint32_t num = 0;
    rt_uint32_t dhcp_stat = 0;
    struct at_device *device = (struct at_device *)work_data;
    struct netdev *netdev = device->netdev;
    struct at_client *client = device->client;


    if (work != RT_NULL)
    {
        rt_free(work);
    }


    resp = at_create_resp(512, 0, rt_tick_from_millisecond(300));
    if (resp == RT_NULL)
    {
        LOG_E("no memory for resp create.");
        return;
    }


    /* send mac addr query commond "AT+CIFSR" and wait response */
    if (at_obj_exec_cmd(client, resp, "AT+CIFSR") < 0)
    {
        goto __exit;
    }


    if (at_resp_parse_line_args_by_kw(resp, "STAMAC", resp_expr, mac) <= 0)
    {
        LOG_E("%s device parse \"AT+CIFSR\" cmd error.", device->name);
        goto __exit;
    }


    /* send addr info query commond "AT+CIPSTA?" and wait response */
    if (at_obj_exec_cmd(client, resp, "AT+CIPSTA?") < 0)
    {
        LOG_E("%s device send \"AT+CIPSTA?\" cmd error.", device->name);
        goto __exit;
    }


    if (at_resp_parse_line_args_by_kw(resp, "ip", resp_expr, ip) <= 0 ||
            at_resp_parse_line_args_by_kw(resp, "gateway", resp_expr, gateway) <= 0 ||
            at_resp_parse_line_args_by_kw(resp, "netmask", resp_expr, netmask) <= 0)
    {
        LOG_E("%s device prase \"AT+CIPSTA?\" cmd error.", device->name);
        goto __exit;
    }


    /* set netdev info */
    inet_aton(gateway, &ip_addr);
    netdev_low_level_set_gw(netdev, &ip_addr);
    inet_aton(netmask, &ip_addr);
    netdev_low_level_set_netmask(netdev, &ip_addr);
    inet_aton(ip, &ip_addr);
    netdev_low_level_set_ipaddr(netdev, &ip_addr);
    sscanf(mac, "%x:%x:%x:%x:%x:%x",
            &mac_addr[0], &mac_addr[1], &mac_addr[2], &mac_addr[3], &mac_addr[4], &mac_addr[5]);
    for (num = 0; num < netdev->hwaddr_len; num++)
    {
        netdev->hwaddr[num] = mac_addr[num];
    }


    /* send dns server query commond "AT+CIPDNS?" and wait response */
    if (at_obj_exec_cmd(device->client, resp, "AT+CIPDNS?") < 0)
    {
        LOG_W("please check and update %s device firmware to support the \"AT+CIPDNS?\" cmd.", device->name);
        goto __exit;
    }


    if (at_resp_parse_line_args(resp, 1, resp_dns, dns_server1) <= 0 &&
            at_resp_parse_line_args(resp, 2, resp_dns, dns_server2) <= 0)
    {
        LOG_E("%d device prase \"AT+CIPDNS?\" cmd error.", device->name);
        goto __exit;
    }


    /* set primary DNS server address */
    if (rt_strlen(dns_server1) > 0 &&
            rt_strncmp(dns_server1, AT_ERR_DNS_SERVER, rt_strlen(AT_ERR_DNS_SERVER)) != 0)
    {
        //inet_aton(dns_server1, &ip_addr);
        if(inet_aton(dns_server1, &ip_addr) == 0)
        {
            sscanf(dns_server1,"\"%[^\"]\"",dns_server1);
            if(rt_strcmp(dns_server1,"8.8.8.8")==0)
            {
                inet_aton(dns_server1, &ip_addr);
            }
            else if(rt_strcmp(dns_server1,"\"8.8.8.8\"")==0)
            {
                inet_aton(dns_server1, &ip_addr);
            }
        }
        netdev_low_level_set_dns_server(netdev, 0, &ip_addr);
    }
    else
    {
        inet_aton(AT_DEF_DNS_SERVER, &ip_addr);
        esp8266_netdev_set_dns_server(netdev, 0, &ip_addr);
    }


    /* set secondary DNS server address */
    if (rt_strlen(dns_server2) > 0 )
    {
        inet_aton(dns_server2, &ip_addr);
        netdev_low_level_set_dns_server(netdev, 1, &ip_addr);
    }


    /* send DHCP query commond " AT+CWDHCP?" and wait response */
    if (at_obj_exec_cmd(client, resp, "AT+CWDHCP?") < 0)
    {
        goto __exit;
    }


    /* parse response data, get the DHCP status */
    if (at_resp_parse_line_args_by_kw(resp, "+CWDHCP:", "+CWDHCP:%d", &dhcp_stat) < 0)
    {
        LOG_E("%s device prase DHCP status error.", device->name);
        goto __exit;
    }


    /* Bit0 - SoftAP DHCP status, Bit1 - Station DHCP status */
    netdev_low_level_set_dhcp_status(netdev, dhcp_stat & 0x02 ? RT_TRUE : RT_FALSE);


__exit:
    if (resp)
    {
        at_delete_resp(resp);
    }
}

五、测试

将编译后的程序下载到开发板,调试信息如下:

a382ee94daae30493fa6722d3fd505f4.png

ifconfig指令查看信息,如下

807f7ba8e2dfafb00b56131de3495041.png

电脑ping设备,如下:

5de222d493c063f31d3af3cda27c3e80.png

欢迎关注个人公众号:嵌入式学习与实践

参考:

https://docs.espressif.com/projects/esp-at/zh_CN/release-v2.2.0.0_esp8266/AT_Command_Set/AT_Command_Set_Comparison.html?highlight=AT%20CIPDNS_CUR#esp-at

file:///E:/work/DTU/wifi%E6%A8%A1%E5%9D%97/esp-01s_product_specification_zh_v1.0.pdf

https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/application-note/components/at/an0014-at-client?id=%e7%ae%80%e4%bb%8b

https://club.rt-thread.org/ask/question/fa060e1514b657a1.html



版权声明:本文为weixin_46158019原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。