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
main.cpp@1:1ef047ffeef4, 2013-09-15 (annotated)
- Committer:
- Kojto
- Date:
- Sun Sep 15 14:41:26 2013 +0000
- Revision:
- 1:1ef047ffeef4
- Parent:
- 0:26d9788555b6
- Child:
- 3:f38499c4000e
Smart config addition, removed non-used macros from the main header file
Who changed what in which revision?
User | Revision | Line number | New 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 "KL25Z_irq_prio.h" |
Kojto | 0:26d9788555b6 | 18 | #include "cc3000.h" |
Kojto | 0:26d9788555b6 | 19 | #include "main.h" |
Kojto | 0:26d9788555b6 | 20 | |
Kojto | 0:26d9788555b6 | 21 | using namespace mbed_cc3000; |
Kojto | 0:26d9788555b6 | 22 | |
Kojto | 0:26d9788555b6 | 23 | #if (WIGO_BOARD) |
Kojto | 0:26d9788555b6 | 24 | // Wi-Go battery charger control |
Kojto | 0:26d9788555b6 | 25 | DigitalOut PWR_EN1(PTB2); |
Kojto | 0:26d9788555b6 | 26 | DigitalOut PWR_EN2(PTB3); |
Kojto | 0:26d9788555b6 | 27 | #endif |
Kojto | 0:26d9788555b6 | 28 | |
Kojto | 0:26d9788555b6 | 29 | Serial pc(USBTX, USBRX); |
Kojto | 0:26d9788555b6 | 30 | cc3000 wigo(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), PORTA_IRQn); |
Kojto | 1:1ef047ffeef4 | 31 | tUserFS user_info; |
Kojto | 1:1ef047ffeef4 | 32 | |
Kojto | 1:1ef047ffeef4 | 33 | #ifndef CC3000_UNENCRYPTED_SMART_CONFIG |
Kojto | 1:1ef047ffeef4 | 34 | const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36}; |
Kojto | 1:1ef047ffeef4 | 35 | #else |
Kojto | 1:1ef047ffeef4 | 36 | const uint8_t smartconfigkey = 0; |
Kojto | 1:1ef047ffeef4 | 37 | #endif |
Kojto | 0:26d9788555b6 | 38 | |
Kojto | 0:26d9788555b6 | 39 | /** Print cc3000 information |
Kojto | 0:26d9788555b6 | 40 | * \param none |
Kojto | 0:26d9788555b6 | 41 | * \return none |
Kojto | 0:26d9788555b6 | 42 | */ |
Kojto | 0:26d9788555b6 | 43 | void print_cc3000_info() { |
Kojto | 0:26d9788555b6 | 44 | uint8_t myMAC[8]; |
Kojto | 0:26d9788555b6 | 45 | |
Kojto | 0:26d9788555b6 | 46 | printf("MAC address + cc3000 info\n"); |
Kojto | 0:26d9788555b6 | 47 | wigo.get_user_file_info((uint8_t *)&user_info, sizeof(user_info)); |
Kojto | 0:26d9788555b6 | 48 | wigo.get_mac_address(myMAC); |
Kojto | 0:26d9788555b6 | 49 | printf(" MAC address %02x:%02x:%02x:%02x:%02x:%02x\n\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]); |
Kojto | 0:26d9788555b6 | 50 | |
Kojto | 0:26d9788555b6 | 51 | printf(" FTC %i\n",user_info.FTC); |
Kojto | 0:26d9788555b6 | 52 | printf(" PP_version %i.%i\n",user_info.PP_version[0], user_info.PP_version[1]); |
Kojto | 0:26d9788555b6 | 53 | printf(" SERV_PACK %i.%i\n",user_info.SERV_PACK[0], user_info.SERV_PACK[1]); |
Kojto | 0:26d9788555b6 | 54 | printf(" DRV_VER %i.%i.%i\n",user_info.DRV_VER[0], user_info.DRV_VER[1], user_info.DRV_VER[2]); |
Kojto | 0:26d9788555b6 | 55 | printf(" FW_VER %i.%i.%i\n",user_info.FW_VER[0], user_info.FW_VER[1], user_info.FW_VER[2]); |
Kojto | 0:26d9788555b6 | 56 | } |
Kojto | 0:26d9788555b6 | 57 | |
Kojto | 0:26d9788555b6 | 58 | /** Connect to SSID with a timeout |
Kojto | 0:26d9788555b6 | 59 | * \param ssid Name of SSID |
Kojto | 0:26d9788555b6 | 60 | * \param key Password |
Kojto | 0:26d9788555b6 | 61 | * \param sec_mode Security mode |
Kojto | 0:26d9788555b6 | 62 | * \return none |
Kojto | 0:26d9788555b6 | 63 | */ |
Kojto | 0:26d9788555b6 | 64 | void connect_to_ssid(uint8_t *ssid, uint8_t *key, uint8_t sec_mode) { |
Kojto | 0:26d9788555b6 | 65 | printf("Connecting to SSID: %s. Timeout is 10s.\n",ssid); |
Kojto | 0:26d9788555b6 | 66 | if (wigo.connect_to_AP(ssid, key, sec_mode) == true) { |
Kojto | 0:26d9788555b6 | 67 | printf(" Connected\n"); |
Kojto | 0:26d9788555b6 | 68 | } else { |
Kojto | 0:26d9788555b6 | 69 | printf(" Connection timed-out (error). Please restart.\n"); |
Kojto | 0:26d9788555b6 | 70 | while(1); |
Kojto | 0:26d9788555b6 | 71 | } |
Kojto | 0:26d9788555b6 | 72 | } |
Kojto | 0:26d9788555b6 | 73 | |
Kojto | 0:26d9788555b6 | 74 | /** Connect to SSID without security |
Kojto | 0:26d9788555b6 | 75 | * \param ssid Name of SSID |
Kojto | 0:26d9788555b6 | 76 | * \return none |
Kojto | 0:26d9788555b6 | 77 | */ |
Kojto | 0:26d9788555b6 | 78 | void connect_to_ssid(uint8_t *ssid) { |
Kojto | 0:26d9788555b6 | 79 | wigo.connect_open(ssid); |
Kojto | 0:26d9788555b6 | 80 | } |
Kojto | 0:26d9788555b6 | 81 | |
Kojto | 0:26d9788555b6 | 82 | void wigo_init(void) { |
Kojto | 0:26d9788555b6 | 83 | /* KL25 specific code */ |
Kojto | 0:26d9788555b6 | 84 | // Wi-Go set current to 500mA since we're turning on the Wi-Fi |
Kojto | 0:26d9788555b6 | 85 | SET_PWR_EN1; |
Kojto | 0:26d9788555b6 | 86 | SET_PWR_EN2; |
Kojto | 1:1ef047ffeef4 | 87 | |
Kojto | 0:26d9788555b6 | 88 | NVIC_SetAllPriority(3); |
Kojto | 0:26d9788555b6 | 89 | NVIC_SetPriority(SPI0_IRQn, 0x0); // Wi-Fi SPI interrupt must be higher priority than SysTick |
Kojto | 0:26d9788555b6 | 90 | NVIC_SetPriority(PORTA_IRQn, 0x1); |
Kojto | 0:26d9788555b6 | 91 | NVIC_SetPriority(SysTick_IRQn, 0x2); // SysTick set to lower priority than Wi-Fi SPI bus interrupt |
Kojto | 0:26d9788555b6 | 92 | CLEAR_PCR_INTERRUPT; |
Kojto | 0:26d9788555b6 | 93 | CLEAN_PORT_INTERRUPT; |
Kojto | 0:26d9788555b6 | 94 | } |
Kojto | 0:26d9788555b6 | 95 | |
Kojto | 1:1ef047ffeef4 | 96 | /** First time configuration |
Kojto | 1:1ef047ffeef4 | 97 | * \param none |
Kojto | 1:1ef047ffeef4 | 98 | * \return none |
Kojto | 1:1ef047ffeef4 | 99 | */ |
Kojto | 1:1ef047ffeef4 | 100 | void do_FTC(void) { |
Kojto | 1:1ef047ffeef4 | 101 | printf("Running First Time Configuration\n"); |
Kojto | 1:1ef047ffeef4 | 102 | wigo.start_smart_config(smartconfigkey); |
Kojto | 1:1ef047ffeef4 | 103 | while (wigo.is_dhcp_configured() == false) { |
Kojto | 1:1ef047ffeef4 | 104 | wait_ms(500); |
Kojto | 1:1ef047ffeef4 | 105 | printf("Waiting for dhcp to be set.\n"); |
Kojto | 1:1ef047ffeef4 | 106 | } |
Kojto | 1:1ef047ffeef4 | 107 | user_info.FTC = 1; |
Kojto | 1:1ef047ffeef4 | 108 | wigo.set_user_file_info((uint8_t *)&user_info, sizeof(user_info)); |
Kojto | 1:1ef047ffeef4 | 109 | wigo._wlan.stop(); |
Kojto | 1:1ef047ffeef4 | 110 | printf("FTC finished.\n"); |
Kojto | 1:1ef047ffeef4 | 111 | } |
Kojto | 1:1ef047ffeef4 | 112 | |
Kojto | 1:1ef047ffeef4 | 113 | /** Ping a site |
Kojto | 0:26d9788555b6 | 114 | * \param none |
Kojto | 0:26d9788555b6 | 115 | * \return int |
Kojto | 0:26d9788555b6 | 116 | */ |
Kojto | 0:26d9788555b6 | 117 | int main() { |
Kojto | 0:26d9788555b6 | 118 | wigo_init(); |
Kojto | 0:26d9788555b6 | 119 | pc.baud(115200); |
Kojto | 0:26d9788555b6 | 120 | |
Kojto | 0:26d9788555b6 | 121 | wigo.start(0); |
Kojto | 0:26d9788555b6 | 122 | printf("CC3000 ping demo.\n"); |
Kojto | 0:26d9788555b6 | 123 | print_cc3000_info(); |
Kojto | 0:26d9788555b6 | 124 | |
Kojto | 0:26d9788555b6 | 125 | printf("Attempting SSID Connection\n"); |
Kojto | 1:1ef047ffeef4 | 126 | #if (USE_SMART_CONFIG == 1) |
Kojto | 1:1ef047ffeef4 | 127 | if (user_info.FTC == 1) { |
Kojto | 1:1ef047ffeef4 | 128 | wigo._wlan.ioctl_set_connection_policy(0, 1, 1); |
Kojto | 1:1ef047ffeef4 | 129 | } else { |
Kojto | 1:1ef047ffeef4 | 130 | printf("Smart config is not set, starting configuration\n"); |
Kojto | 1:1ef047ffeef4 | 131 | do_FTC(); |
Kojto | 1:1ef047ffeef4 | 132 | printf("Smart config is set. Please restart your board.\n"); |
Kojto | 1:1ef047ffeef4 | 133 | while(1); |
Kojto | 1:1ef047ffeef4 | 134 | } |
Kojto | 1:1ef047ffeef4 | 135 | #else |
Kojto | 1:1ef047ffeef4 | 136 | wigo._wlan.ioctl_set_connection_policy(0, 0, 0); |
Kojto | 1:1ef047ffeef4 | 137 | |
Kojto | 1:1ef047ffeef4 | 138 | |
Kojto | 0:26d9788555b6 | 139 | #ifndef CC3000_TINY_DRIVER |
Kojto | 1:1ef047ffeef4 | 140 | #ifdef AP_KEY |
Kojto | 1:1ef047ffeef4 | 141 | connect_to_ssid((uint8_t *)SSID,(uint8_t *)AP_KEY,AP_SECURITY); |
Kojto | 0:26d9788555b6 | 142 | #else |
Kojto | 1:1ef047ffeef4 | 143 | connect_to_ssid((uint8_t *)SSID); |
Kojto | 0:26d9788555b6 | 144 | #endif |
Kojto | 1:1ef047ffeef4 | 145 | #else |
Kojto | 1:1ef047ffeef4 | 146 | connect_to_ssid((uint8_t *)SSID); |
Kojto | 1:1ef047ffeef4 | 147 | #endif |
Kojto | 1:1ef047ffeef4 | 148 | #endif |
Kojto | 0:26d9788555b6 | 149 | printf("DHCP request\n"); |
Kojto | 0:26d9788555b6 | 150 | while (wigo.is_dhcp_configured() == false) { |
Kojto | 0:26d9788555b6 | 151 | wait_ms(500); |
Kojto | 0:26d9788555b6 | 152 | printf(" Waiting for dhcp to be set.\n"); |
Kojto | 0:26d9788555b6 | 153 | } |
Kojto | 0:26d9788555b6 | 154 | |
Kojto | 0:26d9788555b6 | 155 | uint32_t ip; |
Kojto | 0:26d9788555b6 | 156 | uint8_t *site = (uint8_t *)"google.com"; |
Kojto | 0:26d9788555b6 | 157 | printf("Get an IP address of %s\n",site); |
Kojto | 0:26d9788555b6 | 158 | if (wigo._socket.gethostbyname(site,strlen((const char *)site), &ip)) { |
Kojto | 0:26d9788555b6 | 159 | uint8_t add0 = (ip >> 24); |
Kojto | 0:26d9788555b6 | 160 | uint8_t add1 = (ip >> 16); |
Kojto | 0:26d9788555b6 | 161 | uint8_t add2 = (ip >> 8); |
Kojto | 0:26d9788555b6 | 162 | uint8_t add3 = (ip >> 0); |
Kojto | 0:26d9788555b6 | 163 | printf("IP address of %s: %d:%d:%d:%d \n", site, add0, add1, add2, add3); |
Kojto | 0:26d9788555b6 | 164 | } else { |
Kojto | 0:26d9788555b6 | 165 | printf("Failed"); |
Kojto | 0:26d9788555b6 | 166 | } |
Kojto | 0:26d9788555b6 | 167 | |
Kojto | 0:26d9788555b6 | 168 | printf("Starting sending ping\n"); |
Kojto | 0:26d9788555b6 | 169 | uint32_t reply_count = wigo.ping(ip, 5, 500, 32); |
Kojto | 0:26d9788555b6 | 170 | printf("Received %d replies\n", reply_count); |
Kojto | 0:26d9788555b6 | 171 | printf("Ping demo completed.\n"); |
Kojto | 0:26d9788555b6 | 172 | wigo.disconnect(); |
Kojto | 0:26d9788555b6 | 173 | } |