5.11.1 Execution
main.c - Initialize the device and the USART interface. Create the TCP sockets, send/receive messages and display them on the terminal window.
- 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 type */ #define MAIN_WLAN_PSK "12345678" /**< Password for Destination SSID */
- Configure the USART
module to read user input data.
static void configure_console(void) { ... stdio_serial_init(&cdc_uart_module, CONF_STDIO_USART_MODULE,&usart_conf); /* Register USART callback for receiving user input. */ usart_register_callback(&cdc_uart_module, uart_callback, SART_CALLBACK_BUFFER_RECEIVED); usart_enable_callback(&cdc_uart_module, USART_CALLBACK_BUFFER_RECEIVED); usart_enable(&cdc_uart_module); }
- Initialize the socket
module and register the socket callback
function.
/* Initialize socket module */ 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, create the TCP server socket and bind it in the
main loop.
if ((tcp_server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { continue; } ... bind(tcp_server_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
- If there is user input
data, then the
handle_input_message()
function in the main loop calls theparse_command()
function to parse the user data and execute the handler function according to the command.uint8_t parse_command(char *buffer, uint8_t remote) { for (i = i_st; i < i_ed; i++) { if (!strcmp(cmd_list[i].cmd_string, cmd_buf)) { cmd_list[i].cmd_handler(buffer + strlen(cmd_list[i].cmd_string) + 1); break; } }
- The
handle_input_message()
function prints user data in the terminal window and sends it to the remote device.void handle_input_message(void) { ... if (tcp_connected == 1) { PRINT_LOCAL_MSG(uart_buffer); send(tcp_client_socket, uart_buffer, msg_len + 1, 0); }
- When receiving data from the remote device, it handles the received message to display it or execute a command.
- There are several commands for Wi-Fi serial functionality in this example.
- Configure the network
parameters in
main.h.
- Build the program and download it into the board.
- Start the application.
- Check the IP address of each
board and execute the connection on one device by typing the
<<connect xxx:xxx:xxx:xxx
command on the terminal window with the other device's address. Use prefix "<<" to execute local commands. - When connected, the
socket_cb:connect success
message will appear: - Type messages on the terminal window and you will see the sent/received messages.
- You can control the LED on the
remote device by typing the
control ledon (Or) control ledoff
command. Use prefix ">>" to execute remote commands. - The application reads data from the serial interface. User commands can be modified to execute various actions.