Sample project to connect to AT&T M2X from the STM32 Nucleo + MTS WiFi shield
Dependencies: M2XStreamClient SocketModem jsonlite mbed
Fork of STM32_MTS_Wifi_Connect_M2X by
main.cpp@8:fa93a2d07116, 2014-01-04 (annotated)
- Committer:
- mfiore
- Date:
- Sat Jan 04 21:37:02 2014 +0000
- Revision:
- 8:fa93a2d07116
- Parent:
- 6:7c2bdcf9d302
- Child:
- 11:a9117a40bde7
put Wifi::Connect call in loop for robustness
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 | |
jengbrecht | 5:19044863e45c | 6 | /* This example shows how to do a basic connectivity test using |
jengbrecht | 5:19044863e45c | 7 | * the MTS Wifi shield board. You will need to change the network |
jengbrecht | 5:19044863e45c | 8 | * SSID and security key, unless you are at the 2014 AT&T hackathon |
jengbrecht | 5:19044863e45c | 9 | * where you can use the defaults below. |
jengbrecht | 5:19044863e45c | 10 | */ |
jengbrecht | 5:19044863e45c | 11 | |
sam_grove | 0:23c1654d70e4 | 12 | int main() |
sam_grove | 0:23c1654d70e4 | 13 | { |
jengbrecht | 5:19044863e45c | 14 | //Set the network parameters |
sam_grove | 4:6daf448c5b1c | 15 | std::string ssid = "belkin54g"; |
jengbrecht | 5:19044863e45c | 16 | std::string securityKey = "hackathon"; |
mfiore | 6:7c2bdcf9d302 | 17 | Wifi::SecurityType securityType = Wifi::WPA; |
sam_grove | 0:23c1654d70e4 | 18 | |
sam_grove | 0:23c1654d70e4 | 19 | //Wait for wifi module to boot up |
sam_grove | 0:23c1654d70e4 | 20 | for (int i = 10; i >= 0; i = i - 2) { |
sam_grove | 0:23c1654d70e4 | 21 | wait(2); |
sam_grove | 0:23c1654d70e4 | 22 | printf("Waiting %d seconds...\n\r", i); |
sam_grove | 0:23c1654d70e4 | 23 | } |
sam_grove | 0:23c1654d70e4 | 24 | |
sam_grove | 0:23c1654d70e4 | 25 | //Setup serial interface to WiFi module |
sam_grove | 0:23c1654d70e4 | 26 | MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256); |
sam_grove | 0:23c1654d70e4 | 27 | serial->baud(9600); |
sam_grove | 0:23c1654d70e4 | 28 | |
sam_grove | 0:23c1654d70e4 | 29 | //Setup Wifi class |
sam_grove | 0:23c1654d70e4 | 30 | Wifi* wifi = Wifi::getInstance(); |
sam_grove | 0:23c1654d70e4 | 31 | printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE"); |
sam_grove | 0:23c1654d70e4 | 32 | |
sam_grove | 0:23c1654d70e4 | 33 | //Setup and check connection |
sam_grove | 0:23c1654d70e4 | 34 | printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, securityType, securityKey)).c_str()); |
sam_grove | 0:23c1654d70e4 | 35 | printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str()); |
mfiore | 8:fa93a2d07116 | 36 | while (! wifi->connect()) { |
mfiore | 8:fa93a2d07116 | 37 | printf("Connect: Failure\r\n"); |
mfiore | 8:fa93a2d07116 | 38 | wait(1); |
mfiore | 8:fa93a2d07116 | 39 | } |
mfiore | 8:fa93a2d07116 | 40 | printf("Connect: Success\r\n"); |
sam_grove | 0:23c1654d70e4 | 41 | printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); |
mfiore | 6:7c2bdcf9d302 | 42 | |
mfiore | 6:7c2bdcf9d302 | 43 | //Ping |
mfiore | 6:7c2bdcf9d302 | 44 | while (true) { |
mfiore | 6:7c2bdcf9d302 | 45 | printf("Ping Server: %s\n\r", wifi->ping("8.8.8.8") ? "Success" : "Failed"); |
mfiore | 6:7c2bdcf9d302 | 46 | wait(3); |
mfiore | 6:7c2bdcf9d302 | 47 | } |
sam_grove | 0:23c1654d70e4 | 48 | } |