HoYa's MBED (Nucleo-F401RE)

Dependencies:   NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed

main.cpp

Committer:
HoYa
Date:
2017-07-20
Revision:
2:d7647babe160
Parent:
1:b2b7580e09d7
Child:
3:ad353d5bf223

File content as of revision 2:d7647babe160:

#include "mbed.h"
#include "SpwfInterface.h"
#include "TCPSocket.h"

Serial pc(USBTX, USBRX, 115200);
DigitalOut led(LED1);

SpwfSAInterface spwf(D8, D2, false);

int main() {
    pc.printf("\r\n");
    pc.printf("Start Application\r\n");
    
    // Wi-Fi
    pc.printf("\r\n");
    pc.printf("Connecting to AP...\r\n");
    if (spwf.connect("hRPi3-AP", "rlawjdgh", NSAPI_SECURITY_WPA2)) {
        pc.printf("=> Success.\r\n");
    } else {
        pc.printf("=> Failed to connect.\r\n");
        return -1;
    }    
    const char* ip = spwf.get_ip_address();
    pc.printf("=> IP Address is: %s\r\n", (ip) ? ip : "No IP");
    const char* mac = spwf.get_mac_address();   
    pc.printf("=> MAC Address is: %s\r\n", (mac) ? mac : "No MAC");
    
    // DNS
    pc.printf("\r\n");
    pc.printf("DNS Testing...\r\n");
    SocketAddress addr(&spwf, "st.com");
    pc.printf("<st.com> is resolved to: %s\r\n", addr.get_ip_address());
    
    // Unsecured TCP
    pc.printf("\r\n");
    pc.printf("Unsecured TCP Testing...\r\n");
    TCPSocket socket(&spwf);
    pc.printf("Connecting to <4.ifcfg.me>...\r\n");
    int err = socket.connect("4.ifcfg.me", 23);
    if (err) {
      pc.printf("=> Failed to connect. (%d)\r\n", err);
      return -1;
    } else {
        pc.printf("=> Success.\r\n");
    }    
    pc.printf("Receiving Data\r\n");         
    char buffer[100];
    int count = socket.recv(buffer, sizeof buffer);    
    if (count > 0) {
        buffer[count] = '\0';
        printf("=> %s\r\n", buffer);  
    }
    else {
        pc.printf("=> Nothing to receive.\r\n");
    }
    pc.printf("Close TCP socket\r\n");
    socket.close();
       
    printf ("Disconnect from AP\r\n");
    spwf.disconnect();    
    
    while (1) {
        led = !led;
        wait(0.5);
    }
}