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

Dependencies:   NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Fork of cc3000_ping_demo by Martin Kojtal

Committer:
erezi
Date:
Wed Jun 25 06:12:22 2014 +0000
Revision:
10:b98f2d6e6da5
Parent:
7:fbc17364ea63
version_2.0

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)
erezi 10:b98f2d6e6da5 30 //cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7), "ssid", "key", WPA2, false);
erezi 10:b98f2d6e6da5 31 //Serial pc(USBTX, USBRX);
erezi 10:b98f2d6e6da5 32 cc3000 wifi(D3, D5, D10, SPI(SPI_MOSI, SPI_MISO, SPI_SCK), "ssid", "key", WPA2, false);
Kojto 7:fbc17364ea63 33 Serial pc(USBTX, USBRX);
erezi 10:b98f2d6e6da5 34
erezi 10:b98f2d6e6da5 35 //printf("pin selection Ok. \r\n");
erezi 10:b98f2d6e6da5 36
Kojto 3:f38499c4000e 37 #else
Kojto 3:f38499c4000e 38
erezi 10:b98f2d6e6da5 39
erezi 10:b98f2d6e6da5 40
Kojto 0:26d9788555b6 41 #endif
Kojto 0:26d9788555b6 42
Kojto 5:864a193bd778 43 /**
Kojto 5:864a193bd778 44 * \brief Ping a site
Kojto 5:864a193bd778 45 * \param none
Kojto 0:26d9788555b6 46 * \return int
Kojto 0:26d9788555b6 47 */
Kojto 0:26d9788555b6 48 int main() {
Kojto 3:f38499c4000e 49 init(); /* board dependent init */
erezi 10:b98f2d6e6da5 50 // pc.baud(115200);
erezi 10:b98f2d6e6da5 51 pc.baud(9600);
Kojto 0:26d9788555b6 52
Kojto 7:fbc17364ea63 53 printf("CC3000 ping demo. \r\n");
Kojto 7:fbc17364ea63 54 wifi.init();
erezi 10:b98f2d6e6da5 55 printf("Wifi init done. \r\n");
Kojto 7:fbc17364ea63 56 if (wifi.connect() == -1) {
Kojto 7:fbc17364ea63 57 printf("Failed to connect. Please verify connection details and try again. \r\n");
Kojto 1:1ef047ffeef4 58 } else {
Kojto 7:fbc17364ea63 59 printf("IP address: %s \r\n",wifi.getIPAddress());
Kojto 1:1ef047ffeef4 60 }
Kojto 7:fbc17364ea63 61
Kojto 0:26d9788555b6 62 uint32_t ip;
Kojto 0:26d9788555b6 63 uint8_t *site = (uint8_t *)"google.com";
Kojto 3:f38499c4000e 64 printf("Get an IP address of %s \r\n",site);
Kojto 4:52e1684bf322 65 if (wifi._socket.gethostbyname(site,strlen((const char *)site), &ip)) {
Kojto 0:26d9788555b6 66 uint8_t add0 = (ip >> 24);
Kojto 0:26d9788555b6 67 uint8_t add1 = (ip >> 16);
Kojto 0:26d9788555b6 68 uint8_t add2 = (ip >> 8);
Kojto 0:26d9788555b6 69 uint8_t add3 = (ip >> 0);
Kojto 3:f38499c4000e 70 printf("IP address of %s: %d:%d:%d:%d \r\n", site, add0, add1, add2, add3);
Kojto 0:26d9788555b6 71 } else {
Kojto 5:864a193bd778 72 printf("Failed. \r\n");
Kojto 0:26d9788555b6 73 }
Kojto 0:26d9788555b6 74
Kojto 3:f38499c4000e 75 printf("Starting sending ping. \r\n");
Kojto 4:52e1684bf322 76 uint32_t reply_count = wifi.ping(ip, 5, 500, 32);
Kojto 3:f38499c4000e 77 printf("Received %d replies. \r\n", reply_count);
Kojto 3:f38499c4000e 78 printf("Ping demo completed. \r\n");
Kojto 4:52e1684bf322 79 wifi.disconnect();
Kojto 0:26d9788555b6 80 }