3.1.10.1 Execution

main.c - Initialize the ATWINC15x0 and start Provision mode until one of various APs is selected.

  1. Code summary:
    • Initialize the socket module and create the TCP server socket.
      /* Initialize socket address structure. */
      addr.sin_family = AF_INET;
      addr.sin_port = _htons((MAIN_WIFI_M2M_SERVER_PORT));
      addr.sin_addr.s_addr = 0;
      /* Initialize Socket module */
      socketInit();
      registerSocketCallback(socket_cb, NULL);
      while (1) {
          m2m_wifi_handle_events(NULL);
          if (tcp_server_socket < 0) {
          /* Open TCP server socket */
          if ((tcp_server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
          /* Bind service*/
          bind(tcp_server_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
    • Enable AP mode before the main loop. (Refer to the “How to Run AP Mode” example.)
      /* Initialize AP mode parameters structure with SSID, channel and OPEN security type. */
      	memset(&strM2MAPConfig, 0x00, sizeof(tstrM2MAPConfig));
      	strcpy((char *)&strM2MAPConfig.au8SSID, MAIN_WLAN_SSID);
      	strM2MAPConfig.u8ListenChannel = MAIN_WLAN_CHANNEL;
      	strM2MAPConfig.u8SecType = MAIN_WLAN_AUTH;
      	strM2MAPConfig.au8DHCPServerIP[0] = 0xC0; /* 192 */
      	strM2MAPConfig.au8DHCPServerIP[1] = 0xA8; /* 168 */
      	strM2MAPConfig.au8DHCPServerIP[2] = 0x01; /* 1 */
      	strM2MAPConfig.au8DHCPServerIP[3] = 0x01; /* 1 */
      
      	/* Bring up AP mode with parameters structure. */
      	ret = m2m_wifi_enable_ap(&strM2MAPConfig);
    • After your Android device is connected to the ATWINC15x0 and sends the AP configuration, disable AP mode and connect to the AP with the given information.
      static void socket_cb(SOCKET sock, uint8_t u8Msg, void *pvMsg)
      {
          case SOCKET_MSG_RECV:
          {
              printf("Disable to AP.\r\n");
      	 m2m_wifi_disable_ap();
      	 nm_bsp_sleep(500);
      	 printf("Connecting to %s.\r\n", (char *)str_ssid);
      	 m2m_wifi_connect((char *)str_ssid, strlen((char *)str_ssid), sec_type, str_pw, M2M_WIFI_CH_ALL);
    • The wifi_cb() function is called with the M2M_WIFI_REQ_DHCP_CONF message and then receives an IP address.
      static void wifi_cb(uint8_t u8MsgType, void *pvMsg)
      {
          case M2M_WIFI_REQ_DHCP_CONF:
          {
      	uint8_t *pu8IPAddress = (uint8_t *)pvMsg;
      	printf("Wi-Fi connected\r\n");
      	printf("Wi-Fi IP is %u.%u.%u.%u\r\n",
      			pu8IPAddress[0], pu8IPAddress[1], pu8IPAddress[2], pu8IPAddress[3]);
  2. Build the program and download it into the board.
  3. Start the application.