Connect to SSID, get ip by dhcp, ping google.com, display statistics

Dependencies:   NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Committer:
Kojto
Date:
Tue Jun 03 08:43:07 2014 +0000
Revision:
9:a0610da52eed
Parent:
7:fbc17364ea63
update mbed lib to r84

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 0:26d9788555b6 1 /* mbed Microcontroller Library
Kojto 0:26d9788555b6 2 * Copyright (c) 2006-2013 ARM Limited
Kojto 0:26d9788555b6 3 *
Kojto 0:26d9788555b6 4 * Licensed under the Apache License, Version 2.0 (the "License");
Kojto 0:26d9788555b6 5 * you may not use this file except in compliance with the License.
Kojto 0:26d9788555b6 6 * You may obtain a copy of the License at
Kojto 0:26d9788555b6 7 *
Kojto 0:26d9788555b6 8 * http://www.apache.org/licenses/LICENSE-2.0
Kojto 0:26d9788555b6 9 *
Kojto 0:26d9788555b6 10 * Unless required by applicable law or agreed to in writing, software
Kojto 0:26d9788555b6 11 * distributed under the License is distributed on an "AS IS" BASIS,
Kojto 0:26d9788555b6 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Kojto 0:26d9788555b6 13 * See the License for the specific language governing permissions and
Kojto 0:26d9788555b6 14 * limitations under the License.
Kojto 0:26d9788555b6 15 */
Kojto 0:26d9788555b6 16 #include "mbed.h"
Kojto 0:26d9788555b6 17 #include "cc3000.h"
Kojto 0:26d9788555b6 18 #include "main.h"
Kojto 0:26d9788555b6 19
Kojto 0:26d9788555b6 20 using namespace mbed_cc3000;
Kojto 0:26d9788555b6 21
Kojto 3:f38499c4000e 22 /* cc3000 module declaration specific for user's board. Check also init() */
Kojto 3:f38499c4000e 23 #if (MY_BOARD == WIGO)
Kojto 7:fbc17364ea63 24 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), "ssid", "key", WPA2, false);
Kojto 3:f38499c4000e 25 Serial pc(USBTX, USBRX);
Kojto 3:f38499c4000e 26 #elif (MY_BOARD == WIFI_DIPCORTEX)
Kojto 7:fbc17364ea63 27 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), "ssid", "key", WPA2, false);
Kojto 3:f38499c4000e 28 Serial pc(UART_TX, UART_RX);
Kojto 7:fbc17364ea63 29 #elif (MY_BOARD == MBED_BOARD_EXAMPLE)
Kojto 7:fbc17364ea63 30 cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7), "ssid", "key", WPA2, false);
Kojto 7:fbc17364ea63 31 Serial pc(USBTX, USBRX);
Kojto 3:f38499c4000e 32 #else
Kojto 3:f38499c4000e 33
Kojto 0:26d9788555b6 34 #endif
Kojto 0:26d9788555b6 35
Kojto 5:864a193bd778 36 /**
Kojto 5:864a193bd778 37 * \brief Ping a site
Kojto 5:864a193bd778 38 * \param none
Kojto 0:26d9788555b6 39 * \return int
Kojto 0:26d9788555b6 40 */
Kojto 0:26d9788555b6 41 int main() {
Kojto 3:f38499c4000e 42 init(); /* board dependent init */
Kojto 0:26d9788555b6 43 pc.baud(115200);
Kojto 0:26d9788555b6 44
Kojto 7:fbc17364ea63 45 printf("CC3000 ping demo. \r\n");
Kojto 7:fbc17364ea63 46 wifi.init();
Kojto 7:fbc17364ea63 47 if (wifi.connect() == -1) {
Kojto 7:fbc17364ea63 48 printf("Failed to connect. Please verify connection details and try again. \r\n");
Kojto 1:1ef047ffeef4 49 } else {
Kojto 7:fbc17364ea63 50 printf("IP address: %s \r\n",wifi.getIPAddress());
Kojto 1:1ef047ffeef4 51 }
Kojto 7:fbc17364ea63 52
Kojto 0:26d9788555b6 53 uint32_t ip;
Kojto 0:26d9788555b6 54 uint8_t *site = (uint8_t *)"google.com";
Kojto 3:f38499c4000e 55 printf("Get an IP address of %s \r\n",site);
Kojto 4:52e1684bf322 56 if (wifi._socket.gethostbyname(site,strlen((const char *)site), &ip)) {
Kojto 0:26d9788555b6 57 uint8_t add0 = (ip >> 24);
Kojto 0:26d9788555b6 58 uint8_t add1 = (ip >> 16);
Kojto 0:26d9788555b6 59 uint8_t add2 = (ip >> 8);
Kojto 0:26d9788555b6 60 uint8_t add3 = (ip >> 0);
Kojto 3:f38499c4000e 61 printf("IP address of %s: %d:%d:%d:%d \r\n", site, add0, add1, add2, add3);
Kojto 0:26d9788555b6 62 } else {
Kojto 5:864a193bd778 63 printf("Failed. \r\n");
Kojto 0:26d9788555b6 64 }
Kojto 0:26d9788555b6 65
Kojto 3:f38499c4000e 66 printf("Starting sending ping. \r\n");
Kojto 4:52e1684bf322 67 uint32_t reply_count = wifi.ping(ip, 5, 500, 32);
Kojto 3:f38499c4000e 68 printf("Received %d replies. \r\n", reply_count);
Kojto 3:f38499c4000e 69 printf("Ping demo completed. \r\n");
Kojto 4:52e1684bf322 70 wifi.disconnect();
Kojto 0:26d9788555b6 71 }