Example program for MTS WiFi Shield. Test that you can connect to a network.
Dependencies: SocketModem mbed
main.cpp@2:5f1645f60a3b, 2014-01-03 (annotated)
- Committer:
- sam_grove
- Date:
- Fri Jan 03 05:05:52 2014 +0000
- Revision:
- 2:5f1645f60a3b
- Parent:
- 1:b2a530079150
- Child:
- 4:6daf448c5b1c
edits...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove | 0:23c1654d70e4 | 1 | #include "mbed.h" |
sam_grove | 0:23c1654d70e4 | 2 | #include "MTSSerial.h" |
sam_grove | 0:23c1654d70e4 | 3 | #include "Wifi.h" |
sam_grove | 0:23c1654d70e4 | 4 | using namespace mts; |
sam_grove | 0:23c1654d70e4 | 5 | |
sam_grove | 0:23c1654d70e4 | 6 | int main() |
sam_grove | 0:23c1654d70e4 | 7 | { |
sam_grove | 2:5f1645f60a3b | 8 | std::string ssid = "your network name"; |
sam_grove | 2:5f1645f60a3b | 9 | std::string securityKey = "your pass phrase"; |
sam_grove | 0:23c1654d70e4 | 10 | Wifi::SecurityType securityType = Wifi::WPA2; |
sam_grove | 0:23c1654d70e4 | 11 | |
sam_grove | 0:23c1654d70e4 | 12 | //Wait for wifi module to boot up |
sam_grove | 0:23c1654d70e4 | 13 | for (int i = 10; i >= 0; i = i - 2) { |
sam_grove | 0:23c1654d70e4 | 14 | wait(2); |
sam_grove | 0:23c1654d70e4 | 15 | printf("Waiting %d seconds...\n\r", i); |
sam_grove | 0:23c1654d70e4 | 16 | } |
sam_grove | 0:23c1654d70e4 | 17 | |
sam_grove | 0:23c1654d70e4 | 18 | //Setup serial interface to WiFi module |
sam_grove | 0:23c1654d70e4 | 19 | MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256); |
sam_grove | 0:23c1654d70e4 | 20 | serial->baud(9600); |
sam_grove | 0:23c1654d70e4 | 21 | |
sam_grove | 0:23c1654d70e4 | 22 | //Setup Wifi class |
sam_grove | 0:23c1654d70e4 | 23 | Wifi* wifi = Wifi::getInstance(); |
sam_grove | 0:23c1654d70e4 | 24 | printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE"); |
sam_grove | 0:23c1654d70e4 | 25 | |
sam_grove | 0:23c1654d70e4 | 26 | //Setup and check connection |
sam_grove | 0:23c1654d70e4 | 27 | printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, securityType, securityKey)).c_str()); |
sam_grove | 0:23c1654d70e4 | 28 | printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str()); |
sam_grove | 0:23c1654d70e4 | 29 | printf("Connect: %s\n\r", wifi->connect() ? "Success" : "Failure"); |
sam_grove | 0:23c1654d70e4 | 30 | printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); |
sam_grove | 0:23c1654d70e4 | 31 | printf("Ping Server: %s\n\r", wifi->ping("8.8.8.8") ? "Success" : "Failed"); |
sam_grove | 0:23c1654d70e4 | 32 | |
sam_grove | 0:23c1654d70e4 | 33 | //Disconnect from network |
sam_grove | 0:23c1654d70e4 | 34 | printf("Disconnecting...\n\r"); |
sam_grove | 0:23c1654d70e4 | 35 | wifi->disconnect(); |
sam_grove | 0:23c1654d70e4 | 36 | printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); |
sam_grove | 0:23c1654d70e4 | 37 | |
sam_grove | 0:23c1654d70e4 | 38 | printf("End Program\n\r"); |
sam_grove | 0:23c1654d70e4 | 39 | while(1); |
sam_grove | 0:23c1654d70e4 | 40 | } |