Simple WiFi Connect program using Multi-Tech SocketModem with Freescale FRDM-K64F. Adjust network settings to suit yours.
Dependencies: SocketModem mbed
main.cpp@0:8622ae02fc44, 2014-08-26 (annotated)
- Committer:
- mmaas
- Date:
- Tue Aug 26 04:24:01 2014 +0000
- Revision:
- 0:8622ae02fc44
Configured for FRDM-K64F on WPA secure network CloudNet.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mmaas | 0:8622ae02fc44 | 1 | // This program is borrowed from the MTS Multi-Hackers page and configured |
mmaas | 0:8622ae02fc44 | 2 | // to work with the Freescale FRDM-K64F platform. |
mmaas | 0:8622ae02fc44 | 3 | |
mmaas | 0:8622ae02fc44 | 4 | #include "mbed.h" |
mmaas | 0:8622ae02fc44 | 5 | #include "MTSSerial.h" |
mmaas | 0:8622ae02fc44 | 6 | #include "Wifi.h" |
mmaas | 0:8622ae02fc44 | 7 | |
mmaas | 0:8622ae02fc44 | 8 | DigitalOut gpo(D0); |
mmaas | 0:8622ae02fc44 | 9 | DigitalOut led(LED_BLUE); |
mmaas | 0:8622ae02fc44 | 10 | |
mmaas | 0:8622ae02fc44 | 11 | int main() |
mmaas | 0:8622ae02fc44 | 12 | { |
mmaas | 0:8622ae02fc44 | 13 | std::string ssid = "CloudNet"; |
mmaas | 0:8622ae02fc44 | 14 | std::string securityKey = "CloudNetPass"; |
mmaas | 0:8622ae02fc44 | 15 | Wifi::SecurityType securityType = Wifi::WPA; |
mmaas | 0:8622ae02fc44 | 16 | |
mmaas | 0:8622ae02fc44 | 17 | //Wait for wifi module to boot up |
mmaas | 0:8622ae02fc44 | 18 | for (int i = 10; i >= 0; i = i - 2) { |
mmaas | 0:8622ae02fc44 | 19 | wait(2); |
mmaas | 0:8622ae02fc44 | 20 | printf("Waiting %d seconds...\n\r", i); |
mmaas | 0:8622ae02fc44 | 21 | } |
mmaas | 0:8622ae02fc44 | 22 | |
mmaas | 0:8622ae02fc44 | 23 | //Setup serial interface to WiFi module - FRDM-K64F |
mmaas | 0:8622ae02fc44 | 24 | MTSSerial* serial = new MTSSerial(PTC17, PTC16, 256, 256); |
mmaas | 0:8622ae02fc44 | 25 | serial->baud(9600); |
mmaas | 0:8622ae02fc44 | 26 | |
mmaas | 0:8622ae02fc44 | 27 | //Setup Wifi class |
mmaas | 0:8622ae02fc44 | 28 | Wifi* wifi = Wifi::getInstance(); |
mmaas | 0:8622ae02fc44 | 29 | printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE"); |
mmaas | 0:8622ae02fc44 | 30 | |
mmaas | 0:8622ae02fc44 | 31 | //Setup and check connection |
mmaas | 0:8622ae02fc44 | 32 | printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, securityType, securityKey)).c_str()); |
mmaas | 0:8622ae02fc44 | 33 | printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str()); |
mmaas | 0:8622ae02fc44 | 34 | printf("Connect: %s\n\r", wifi->connect() ? "Success" : "Failure"); |
mmaas | 0:8622ae02fc44 | 35 | printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); |
mmaas | 0:8622ae02fc44 | 36 | printf("Ping Server: %s\n\r", wifi->ping("8.8.8.8") ? "Success" : "Failed"); |
mmaas | 0:8622ae02fc44 | 37 | |
mmaas | 0:8622ae02fc44 | 38 | //Disconnect from network |
mmaas | 0:8622ae02fc44 | 39 | printf("Disconnecting...\n\r"); |
mmaas | 0:8622ae02fc44 | 40 | wifi->disconnect(); |
mmaas | 0:8622ae02fc44 | 41 | printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); |
mmaas | 0:8622ae02fc44 | 42 | |
mmaas | 0:8622ae02fc44 | 43 | printf("End Program\n\r"); |
mmaas | 0:8622ae02fc44 | 44 | while (true) { |
mmaas | 0:8622ae02fc44 | 45 | gpo = !gpo; // toggle pin |
mmaas | 0:8622ae02fc44 | 46 | led = !led; // toggle led |
mmaas | 0:8622ae02fc44 | 47 | wait(0.2f); |
mmaas | 0:8622ae02fc44 | 48 | } |
mmaas | 0:8622ae02fc44 | 49 | } |