4.1.1 Execution

main.c - Initialize the Wi-Fi module and test with the UDP server.

  1. Code summary.
    • Configure the network parameters in main.h.
      /** Wi-Fi Settings */
      #define MAIN_WLAN_SSID                    "DEMO_AP" /**< Destination SSID */
      #define MAIN_WLAN_AUTH                    M2M_WIFI_SEC_WPA_PSK /**< Security manner */
      #define MAIN_WLAN_PSK                     "12345678" /**< Password for Destination SSID */
      #define MAIN_WIFI_M2M_PRODUCT_NAME        "NMCTemp"
      #define MAIN_WIFI_M2M_SERVER_IP           0xFFFFFFFF /* 255.255.255.255 */
      #define MAIN_WIFI_M2M_SERVER_PORT         (6666)
      #define MAIN_WIFI_M2M_REPORT_INTERVAL     (1000)
    • Initialize the socket module.
      /* Initialize socket address structure. */
      addr.sin_family = AF_INET;
      addr.sin_port = _htons(MAIN_WIFI_M2M_SERVER_PORT);
      addr.sin_addr.s_addr = _htonl(MAIN_WIFI_M2M_SERVER_IP);
      /* Initialize socket module */
      socketInit();
    • Connect to the AP.
      /* Connect to router. */
      m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID),
              MAIN_WLAN_AUTH, (char *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL);
    • After the device is connected to the AP, create a TX socket in the main loop.
      /* Create socket for Tx UDP */
      if (tx_socket < 0) {
          if ((tx_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
          printf("main : failed to create TX UDP client socket error!\r\n");
          continue;
          }
      }
    • Send data from the UDP client TX socket to the UDP server RX socket.
      ret = sendto(tx_socket, &msg_wifi_product_main, sizeof(t_msg_wifi_product_main),
           0, (struct sockaddr *)&addr, sizeof(addr));
  2. Build the program and download it into the board.
  3. Start the application.