Example program for MTS WiFi Shield. Test that you can connect to a network.

Dependencies:   SocketModem mbed

main.cpp

Committer:
mfiore
Date:
2014-07-11
Revision:
11:a9117a40bde7
Parent:
8:fa93a2d07116

File content as of revision 11:a9117a40bde7:

#include "mbed.h"
#include "MTSSerial.h"
#include "Wifi.h"
using namespace mts;

/* This example shows how to do a basic connectivity test using
* the MTS Wifi shield board. You will need to change the network
* SSID and security key. You may need to chage the security type.
*/

int main()
{
    //Set the network parameters
    std::string ssid = "your_wifi_ssid";
    std::string securityKey = "your_wifi_password";
    Wifi::SecurityType securityType = Wifi::WPA2;

    //Wait for wifi module to boot up
    for (int i = 10; i >= 0; i = i - 2) {
        wait(2);
        printf("Waiting %d seconds...\n\r", i);
    }

    //Setup serial interface to WiFi module
    MTSSerial* serial = new MTSSerial(D8, D2, 256, 256);
    serial->baud(9600);

    //Setup Wifi class
    Wifi* wifi = Wifi::getInstance();
    printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE");

    //Setup and check connection
    printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, securityType, securityKey)).c_str());
    printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str());
    while (! wifi->connect()) {
        printf("Connect: Failure\r\n");
        wait(1);
    }
    printf("Connect: Success\r\n");
    printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False");
    
    //Ping 
    while (true) {
        printf("Ping Server: %s\n\r", wifi->ping("8.8.8.8") ? "Success" : "Failed");
        wait(3);
    }
}