当前位置:   article > 正文

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

esp01s

一、开发环境

开发板:野火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?命令。

代码修改如下:

  1. static void esp8266_get_netdev_info(struct rt_work *work, void *work_data)
  2. {
  3. #define AT_ADDR_LEN 32
  4. #define AT_ERR_DNS_SERVER "255.255.255.255"
  5. #define AT_DEF_DNS_SERVER "114.114.114.114"
  6. at_response_t resp = RT_NULL;
  7. char ip[AT_ADDR_LEN] = {0}, mac[AT_ADDR_LEN] = {0};
  8. char gateway[AT_ADDR_LEN] = {0}, netmask[AT_ADDR_LEN] = {0};
  9. char dns_server1[AT_ADDR_LEN] = {0}, dns_server2[AT_ADDR_LEN] = {0};
  10. const char *resp_expr = "%*[^\"]\"%[^\"]\"";
  11. const char *resp_dns = "+CIPDNS:1,%s";//
  12. ip_addr_t ip_addr;
  13. rt_uint32_t mac_addr[6] = {0};
  14. rt_uint32_t num = 0;
  15. rt_uint32_t dhcp_stat = 0;
  16. struct at_device *device = (struct at_device *)work_data;
  17. struct netdev *netdev = device->netdev;
  18. struct at_client *client = device->client;
  19. if (work != RT_NULL)
  20. {
  21. rt_free(work);
  22. }
  23. resp = at_create_resp(512, 0, rt_tick_from_millisecond(300));
  24. if (resp == RT_NULL)
  25. {
  26. LOG_E("no memory for resp create.");
  27. return;
  28. }
  29. /* send mac addr query commond "AT+CIFSR" and wait response */
  30. if (at_obj_exec_cmd(client, resp, "AT+CIFSR") < 0)
  31. {
  32. goto __exit;
  33. }
  34. if (at_resp_parse_line_args_by_kw(resp, "STAMAC", resp_expr, mac) <= 0)
  35. {
  36. LOG_E("%s device parse \"AT+CIFSR\" cmd error.", device->name);
  37. goto __exit;
  38. }
  39. /* send addr info query commond "AT+CIPSTA?" and wait response */
  40. if (at_obj_exec_cmd(client, resp, "AT+CIPSTA?") < 0)
  41. {
  42. LOG_E("%s device send \"AT+CIPSTA?\" cmd error.", device->name);
  43. goto __exit;
  44. }
  45. if (at_resp_parse_line_args_by_kw(resp, "ip", resp_expr, ip) <= 0 ||
  46. at_resp_parse_line_args_by_kw(resp, "gateway", resp_expr, gateway) <= 0 ||
  47. at_resp_parse_line_args_by_kw(resp, "netmask", resp_expr, netmask) <= 0)
  48. {
  49. LOG_E("%s device prase \"AT+CIPSTA?\" cmd error.", device->name);
  50. goto __exit;
  51. }
  52. /* set netdev info */
  53. inet_aton(gateway, &ip_addr);
  54. netdev_low_level_set_gw(netdev, &ip_addr);
  55. inet_aton(netmask, &ip_addr);
  56. netdev_low_level_set_netmask(netdev, &ip_addr);
  57. inet_aton(ip, &ip_addr);
  58. netdev_low_level_set_ipaddr(netdev, &ip_addr);
  59. sscanf(mac, "%x:%x:%x:%x:%x:%x",
  60. &mac_addr[0], &mac_addr[1], &mac_addr[2], &mac_addr[3], &mac_addr[4], &mac_addr[5]);
  61. for (num = 0; num < netdev->hwaddr_len; num++)
  62. {
  63. netdev->hwaddr[num] = mac_addr[num];
  64. }
  65. /* send dns server query commond "AT+CIPDNS?" and wait response */
  66. if (at_obj_exec_cmd(device->client, resp, "AT+CIPDNS?") < 0)
  67. {
  68. LOG_W("please check and update %s device firmware to support the \"AT+CIPDNS?\" cmd.", device->name);
  69. goto __exit;
  70. }
  71. if (at_resp_parse_line_args(resp, 1, resp_dns, dns_server1) <= 0 &&
  72. at_resp_parse_line_args(resp, 2, resp_dns, dns_server2) <= 0)
  73. {
  74. LOG_E("%d device prase \"AT+CIPDNS?\" cmd error.", device->name);
  75. goto __exit;
  76. }
  77. /* set primary DNS server address */
  78. if (rt_strlen(dns_server1) > 0 &&
  79. rt_strncmp(dns_server1, AT_ERR_DNS_SERVER, rt_strlen(AT_ERR_DNS_SERVER)) != 0)
  80. {
  81.         //inet_aton(dns_server1, &ip_addr);
  82. if(inet_aton(dns_server1, &ip_addr) == 0)
  83.         {
  84. sscanf(dns_server1,"\"%[^\"]\"",dns_server1);
  85. if(rt_strcmp(dns_server1,"8.8.8.8")==0)
  86. {
  87. inet_aton(dns_server1, &ip_addr);
  88. }
  89. else if(rt_strcmp(dns_server1,"\"8.8.8.8\"")==0)
  90. {
  91. inet_aton(dns_server1, &ip_addr);
  92. }
  93. }
  94. netdev_low_level_set_dns_server(netdev, 0, &ip_addr);
  95. }
  96. else
  97. {
  98. inet_aton(AT_DEF_DNS_SERVER, &ip_addr);
  99. esp8266_netdev_set_dns_server(netdev, 0, &ip_addr);
  100. }
  101. /* set secondary DNS server address */
  102. if (rt_strlen(dns_server2) > 0 )
  103. {
  104. inet_aton(dns_server2, &ip_addr);
  105. netdev_low_level_set_dns_server(netdev, 1, &ip_addr);
  106. }
  107. /* send DHCP query commond " AT+CWDHCP?" and wait response */
  108. if (at_obj_exec_cmd(client, resp, "AT+CWDHCP?") < 0)
  109. {
  110. goto __exit;
  111. }
  112. /* parse response data, get the DHCP status */
  113. if (at_resp_parse_line_args_by_kw(resp, "+CWDHCP:", "+CWDHCP:%d", &dhcp_stat) < 0)
  114. {
  115. LOG_E("%s device prase DHCP status error.", device->name);
  116. goto __exit;
  117. }
  118. /* Bit0 - SoftAP DHCP status, Bit1 - Station DHCP status */
  119. netdev_low_level_set_dhcp_status(netdev, dhcp_stat & 0x02 ? RT_TRUE : RT_FALSE);
  120. __exit:
  121. if (resp)
  122. {
  123. at_delete_resp(resp);
  124. }
  125. }

五、测试

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/163473
推荐阅读
相关标签
  

闽ICP备14008679号