TCP echo client using the WiConnect library and mbed TCP Socket API.

Dependencies:   WiConnect mbed

main.cpp

Committer:
dan_ackme
Date:
2014-08-24
Revision:
2:2f1df6a7ed12
Parent:
0:447a1bbce2ca

File content as of revision 2:2f1df6a7ed12:


#include "Wiconnect.h"
#include "target_config.h"

#define NETWORK_SSID "ASUS"
#define NETWORK_PASSWORD "Y@nkee123"

#define ECHO_SERVER_ADDRESS  "192.168.1.110"
#define ECHO_SERVER_PORT 7


static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX);
static char buf[256];


int main()
{
    WiconnectResult result;
    SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256);
    Wiconnect wiconnect(serialConfig, 256, NULL, WICONNECT_RESET_PIN);

    consoleSerial.baud(115200);

    printf("Initializing WiConnect...\r\n");
    if(WICONNECT_FAILED(result, wiconnect.init(true)))
    {
        printf("Failed to initialize Wiconnect: %s\r\n", Wiconnect::getWiconnectResultStr(result));
        for(;;);
    }
    
    printf("Joining network: %s\r\n", NETWORK_SSID);
    if(WICONNECT_FAILED(result, wiconnect.join(NETWORK_SSID, NETWORK_PASSWORD)))
    {
        printf("Failed to join network: %s\r\n", Wiconnect::getWiconnectResultStr(result));
        for(;;);
    }

    printf("IP Address is %s\r\n", wiconnect.getIpAddress());

    TCPSocketConnection socket;
    while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0)
    {
        printf("Unable to connect to (%s) on port (%d) (is the TCP server python script running?\r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
        wait(1);
    }

    printf("Sending msg to server...\r\n");
    char hello[] = "Hello World\r\n";
    if(socket.send_all(hello, sizeof(hello) - 1) == -1)
    {
        printf("Failed to send data\r\n");
        for(;;);
    }

    int n = socket.receive(buf, 256);
    if(n == -1)
    {
        printf("Failed to receive data\r\n");
        for(;;);
    }
    buf[n] = '\0';
    printf("Received msg from server: %s", buf);

    socket.close();
    wiconnect.deinit();

    printf("\r\n\r\n*** Finished!");
    while(true) {}
}