5.10.1 Execution

main.c - Initialize the chip and retrieve information.

  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 */
    • Initialize the socket module and register the socket callback function.
      /* 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();
      registerSocketCallback(socket_cb, NULL);
    • Get the MAC address and set the device name with the MAC address.
      m2m_wifi_get_mac_address(gau8MacAddr);
      
      set_dev_name_to_mac((uint8_t *)gacDeviceName, gau8MacAddr);
      set_dev_name_to_mac((uint8_t *)gstrM2MAPConfig.au8SSID, gau8MacAddr);
      m2m_wifi_set_device_name((uint8_t *)gacDeviceName, ...);
    • Start the Provision mode.
      m2m_wifi_start_provision_mode((tstrM2MAPConfig *)&gstrM2MAPConfig,
              (char *)gacHttpProvDomainName, 1);
    • When the mobile device sends configuration information, the wifi_cb() function is called with the M2M_WIFI_RESP_PROVISION_INFO message and the user can connect to the AP with the given information.
      static void wifi_cb(uint8_t u8MsgType, void *pvMsg)
      {
      	case M2M_WIFI_RESP_PROVISION_INFO: 
              tstrM2MProvisionInfo *pstrProvInfo = (tstrM2MProvisionInfo *)pvMsg;        
              if (pstrProvInfo->u8Status == M2M_SUCCESS) {
      		m2m_wifi_connect((char *)pstrProvInfo->au8SSID,
                      strlen((char *)pstrProvInfo->au8SSID),        
                      pstrProvInfo->u8SecType,
                      pstrProvInfo->au8Password, M2M_WIFI_CH_ALL);
      	}
    • After the device is connected to the AP, the gethostbyname() function is called.
      static void wifi_cb(uint8_t u8MsgType, void *pvMsg)
      {
      	...
          case M2M_WIFI_REQ_DHCP_CONF:
          {
      	...
      	gbConnectedWifi = true;
      	gethostbyname((uint8_t *)MAIN_WEATHER_SERVER_NAME);
          }
          ...
      }
    • Create a TCP client socket and connect to the server in the main loop.
      if (gbConnectedWifi && !gbTcpConnection) {
          if (gbHostIpByName) {
      	if (tcp_client_socket < 0) {
      		if ((tcp_client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
      		continue;
      	        }
              }
          if (connect(tcp_client_socket, ...) != SOCK_ERR_NO_ERROR) {
              continue;
          }
          gbTcpConnection = true;
          }
      }
    • The socket callback function receives the SOCKET_MSG_CONNECT message and then it requests weather information from the server with a city name.
      static void socket_cb(SOCKET sock, uint8_t u8Msg, void *pvMsg)
      {
          case SOCKET_MSG_CONNECT:
      	sprintf(gau8ReceivedBuffer, ..., MAIN_CITY_NAME, ...);
          ...
          send(tcp_client_socket, gau8ReceivedBuffer, ...);
          recv(tcp_client_socket, (struct sockaddr *)&addr_in, sizeof(struct sockaddr_in));
          break;
      }
    • The socket callback function receives a SOCKET_MSG_RECV message with weather information. The user can also get the current temperature via an IO1 sensor board as shown in the following.
      static void socket_cb(SOCKET sock, uint8_t u8Msg, void *pvMsg)
      {
          case SOCKET_MSG_RECV:
          ...
          /** From the received Josan format buffer City Name, Temperature, Weather Number will be parsed */
          /* Get city name. */
          pcIndxPtr = strstr((char *)pstrRecv->pu8Buffer, "name=");
          printf("City: ");
          ...
          /* Get temperature. */
          pcIndxPtr = strstr(pcEndPtr + 1, "temperature value");
          printf("Temperature: ");
         ...
          /* Get weather condition. */
          pcIndxPtr = strstr(pcEndPtr + 1, "weather number");
          if (NULL != pcIndxPtr) {
              printf("Weather Condition: ");
          
      }
  2. Build the program and download it into the board.
  3. Start the application.
  4. Connect the mobile device to the ATWINC15x0/ATWINC3400 AP [WINC1500_XX:XX].
  5. Browse the web page (www.microchip.com) to setup the AP, populate the page and press Connect.
  6. The ATWINC15x0/ATWINC3400 will be connected to the selected AP.
  7. The weather information will be printed.
Note: If the server connection is unstable, this example may not operate normally.