5.3.1 Execution
main.c – Initialize the ATWINC15x0 and download the file from the URL.
- 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 */
- Configure the HTTP URL
file to be downloaded into the SD
card.
/** Content URI for download. */ #define MAIN_HTTP_FILE_URL "http://www.atmel.com/Images/Atmel-42502-SmartConnect-WINC1500-MR210PB_Datasheet.pdf"
- Configure the HTTP
client.
configure_http_client();
- Get the default config
data and specify the user configuration. The
http_client_init()
andhttp_client_register_callback()
functions execute sequentially.static void configure_http_client(void) { ... http_client_get_config_defaults(&httpc_conf); httpc_conf.recv_buffer_size = MAIN_BUFFER_MAX_SIZE; httpc_conf.timer_inst = &swt_module_inst; ret = http_client_init(&http_client_module_inst, &httpc_conf); if (ret < 0) { while (1) { } /* Loop forever. */ } http_client_register_callback(&http_client_module_inst, http_client_callback); }
- Initialize the socket
module and register the socket callback functions.
socketInit(); registerSocketCallback(socket_cb, resolve_cb);
- 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, an HTTP request will be sent.
static void wifi_callback(uint8_t msg_type, void *msg_data) { ... case M2M_WIFI_REQ_DHCP_CONF: { start_download(); }
static void start_download(void) { ... /* Send the HTTP request. */ printf("start_download: sending HTTP request...\r\n"); http_client_send_request(&http_client_module_inst, MAIN_HTTP_FILE_URL, HTTP_METHOD_GET, NULL, NULL); }
- Four operations will
execute sequentially. The files are stored in the SD card when they are
received by the HTTP request and the
callback.
static void http_client_callback(...) { switch (type) { case HTTP_CLIENT_CALLBACK_SOCK_CONNECTED: printf("Connected\r\n"); break; case HTTP_CLIENT_CALLBACK_REQUESTED: printf("Request complete\r\n"); break; case HTTP_CLIENT_CALLBACK_RECV_RESPONSE: printf("http_client_callback: received response %u data size %u\r\n", (unsigned int)data->recv_response.response_code, (unsigned int)data->recv_response.content_length); if ((unsigned int)data->recv_response.response_code == 200) { http_file_size = data->recv_response.content_length; received_file_size = 0; } if (data->recv_response.content_length <= MAIN_BUFFER_MAX_SIZE) { store_file_packet(data->recv_response.content, data->recv_response.content_length); } break; case HTTP_CLIENT_CALLBACK_RECV_CHUNKED_DATA: store_file_packet(data->recv_chunked_data.data, data->recv_chunked_data.length); case HTTP_CLIENT_CALLBACK_DISCONNECTED: printf("Disconnected reason:%d\r\n", data->disconnected.reason); ... } }
- The first sequence begins with the socket connected. After the request completes, the third sequence is executed and the file header is followed by the file data.
- Configure the network
parameters in main.h.
- Build the program and download it into the board.
- Start the application.
Note:
- If the disconnect reason is equal to -ECONNRESET(-104), it means the server disconnected your connection due to the keep alive timeout. This operation is normal.
- If the server connection is unstable, this example may not operate normally.