微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 无线和射频 > 射频无线通信设计 > Open1081 softap+TCP UDP echo

Open1081 softap+TCP UDP echo

时间:10-02 整理:3721RD 点击:


为下面的项目做准备,调通Open1081的softap和TCP,UDP收发。
发现一个问题,softap不能设置密码(wifi_key),设置之后StartNetwork返回-1。这个有谁知道的?

把wifi_key参数留空,电脑就能顺利的找到热点。下面是我设置的Open1081热点。




试了一下TCP连接,最多7个,看代码应该是8个才对,可能是因为监听用去了一个socket




测试log




感觉官方自带的代码有点乱,我作了一些调整,看起来舒服一点。

  1. #include "stdio.h"
  2. #include "ctype.h"

  3. #include "stm32f2xx.h"
  4. #include "platform.h"
  5. #include "mxchipWNET.h"


  6. #define AP_NAME "TP-604"
  7. #define AP_PASSWORD "robot604"
  8. #define APP_INFO "------ Open1081 ------"

  9. #define SOFTAP_NAME "Open1081"
  10. #define SOFTAP_PASSWD "123456"

  11. int wifi_up = 0;
  12. int webserverTest = 1;
  13. int cloud_ip_addr = -1;
  14. int cloud_connected = 0;
  15. int dns_pending = 0;
  16. extern u32 MS_TIMER;


  17. /* ========================================
  18. User provide callback functions
  19. ======================================== */
  20. void system_version(char *str, int len)
  21. {
  22. snprintf(str, len, "%s", APP_INFO);
  23. }

  24. void WifiStatusHandler(int event)
  25. {
  26. switch (event) {
  27. case MXCHIP_WIFI_UP:
  28. wifi_up = 1;
  29. printf("Station up \r\n");
  30. break;
  31. case MXCHIP_WIFI_DOWN:
  32. printf("Station down \r\n");
  33. break;
  34. case MXCHIP_UAP_UP:
  35. break;
  36. case MXCHIP_UAP_DOWN:
  37. break;
  38. default:
  39. break;
  40. }
  41. }

  42. void NetCallback(net_para_st *pnet)
  43. {
  44. printf("IP address: %s \r\n", pnet->ip);
  45. printf("NetMask address: %s \r\n", pnet->mask);
  46. printf("Gateway address: %s \r\n", pnet->gate);
  47. printf("DNS server address: %s \r\n", pnet->dns);
  48. printf("MAC address: %s \r\n", pnet->mac);
  49. }

  50. void Open1081stationModeStart(void)
  51. {
  52. network_InitTypeDef_st wNetConfig;
  53. int ret;

  54. memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));

  55. wNetConfig.wifi_mode = Station;
  56. strcpy((char*)wNetConfig.wifi_ssid, AP_NAME);
  57. strcpy((char*)wNetConfig.wifi_key, AP_PASSWORD);
  58. wNetConfig.dhcpMode = DHCP_Client;

  59. ret = StartNetwork(&wNetConfig);

  60. printf("connect to %s.....[%d]\r\n", wNetConfig.wifi_ssid, ret);
  61. }


  62. void Open1081softAPModeStart(void)
  63. {
  64. static network_InitTypeDef_st wNetConfig;
  65. int ret;

  66. memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));

  67. wNetConfig.wifi_mode = Soft_AP;
  68. strcpy((char*)wNetConfig.wifi_ssid, SOFTAP_NAME);
  69. strcpy((char*)wNetConfig.wifi_key, "");
  70. strcpy((char*)wNetConfig.local_ip_addr, "192.168.1.1");
  71. strcpy((char*)wNetConfig.net_mask, "255.255.255.0");
  72. strcpy((char*)wNetConfig.address_pool_start,"192.168.1.10");
  73. strcpy((char*)wNetConfig.address_pool_end, "192.168.1.250");
  74. wNetConfig.dhcpMode = DHCP_Server;

  75. ret = StartNetwork(&wNetConfig);
  76. printf("Setup soft AP: %s[%d]\r\n", wNetConfig.wifi_ssid, ret);
  77. }


  78. void Open1081Init()
  79. {
  80. lib_config_t libConfig;

  81. libConfig.tcp_buf_dynamic = mxEnable;
  82. libConfig.tcp_max_connection_num = 12;
  83. libConfig.tcp_rx_size = 2048;
  84. libConfig.tcp_tx_size = 2048;
  85. libConfig.hw_watchdog = 0;
  86. libConfig.wifi_channel = WIFI_CHANNEL_1_13;
  87. lib_config(&libConfig);

  88. mxchipInit();
  89. UART_Init();

  90. printf("\r\n%s\r\n", APP_INFO);
  91. printf("library version: %s\r\n", system_lib_version());

  92. ps_enable();
  93. set_tcp_keepalive(3, 60);

  94. Open1081softAPModeStart();

  95. }

  96. int Open1081TCPUDPInit(int* fd_listen, int* fd_udp)
  97. {
  98. int bufferSize;
  99. struct sockaddr_t addr;

  100. /* Establish a TCP server that accept the tcp clients connections */
  101. *fd_listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  102. bufferSize = 5*1024;
  103. setsockopt(*fd_listen, 0, SO_RDBUFLEN, &bufferSize, 4);
  104. bufferSize = 5*1024;
  105. setsockopt(*fd_listen, 0, SO_WRBUFLEN, &bufferSize, 4);
  106. addr.s_port = 8080;
  107. bind(*fd_listen, &addr, sizeof(addr));
  108. listen(*fd_listen, 0);

  109. printf("TCP server established at port: %d \r\n", addr.s_port);

  110. /* Establish a UDP port to receive any data sent to this port */
  111. *fd_udp = socket(AF_INET, SOCK_DGRM, IPPROTO_UDP);
  112. addr.s_port = 8090;
  113. bind(*fd_udp, &addr, sizeof(addr));

  114. printf("Open UDP port %d\r\n", addr.s_port);

  115. return 0;
  116. }

  117. int main(void)
  118. {
  119. int tcp_client;
  120. int i, fd_listen = -1, fd_udp = -1, fd_client = -1;
  121. char *buf, ip_address[16];
  122. int len;
  123. int con = -1;
  124. int clientfd[8];
  125. fd_set readfds, exceptfds;
  126. struct timeval_t t;
  127. struct sockaddr_t addr;
  128. socklen_t addrLen;

  129. Open1081Init();

  130. for(i = 0; i < 8; i++)
  131. clientfd[i] = -1;

  132. t.tv_sec = 0;
  133. t.tv_usec = 100;

  134. // tcp and udo recv data buffer 3KB
  135. buf = NULL;
  136. buf = (char*)malloc(3*1024);
  137. if (!buf) {
  138. printf("malloc error\n");
  139. return 1;
  140. }

  141. Open1081TCPUDPInit(&fd_listen, &fd_udp);

  142. for (;;) {

  143. mxchipTick();

  144. /* Check status on every sockets */
  145. FD_ZERO(&readfds);
  146. FD_SET(fd_listen, &readfds);
  147. FD_SET(fd_udp, &readfds);

  148. if(fd_client != -1)
  149. FD_SET(fd_client, &readfds);

  150. for(i = 0; i < 8; i++) {
  151. if (clientfd[i] != -1)
  152. FD_SET(clientfd[i], &readfds);
  153. }

  154. select(1, &readfds, NULL, &exceptfds, &t);

  155. /* Check tcp connection requests */
  156. if(FD_ISSET(fd_listen, &readfds)) {
  157. tcp_client = accept(fd_listen, &addr, &len);
  158. if (tcp_client > 0) {
  159. inet_ntoa(ip_address, addr.s_ip);
  160. printf("Client %s:%d connected[%d]\r\n", ip_address, addr.s_port, tcp_client);

  161. for(i = 0; i < 8; i++) {
  162. if (clientfd[i] == -1) {
  163. clientfd[i] = tcp_client;
  164. break;
  165. }
  166. }
  167. }
  168. }

  169. /* Read data from tcp clients and send data back */
  170. for(i = 0; i < 8; i++) {
  171. if (clientfd[i] != -1) {
  172. if (FD_ISSET(clientfd[i], &readfds)) {
  173. con = recv(clientfd[i], buf, 1*1024, 0);
  174. if (con > 0)
  175. send(clientfd[i], buf, con, 0);
  176. else {
  177. printf("clientfd[%d]: %d closed\r\n", i, clientfd[i]);
  178. close(clientfd[i]);
  179. clientfd[i] = -1;
  180. }
  181. }
  182. else if (FD_ISSET(clientfd[i], &exceptfds))
  183. clientfd[i] = -1;
  184. }
  185. }

  186. /* Read data from udp and send data back */
  187. if (FD_ISSET(fd_udp, &readfds)) {
  188. con = recvfrom(fd_udp, buf, 3*1024, 0, &addr, &addrLen);
  189. sendto(fd_udp, buf, con, 0, &addr, sizeof(struct sockaddr_t));
  190. }
  191. }
  192. }

复制代码

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top