![](/media/cache/profiles/108fe115000c147427da50c2b0c9ce31.jpg.50x50_q85.jpg)
cc3000 websocket demo for cc3000
Dependencies: NVIC_set_all_priorities WebSocketClient cc3000_hostdriver_mbedsocket mbed
main.cpp@2:398f4fa8fb4a, 2013-10-02 (annotated)
- Committer:
- SolderSplashLabs
- Date:
- Wed Oct 02 20:19:33 2013 +0000
- Revision:
- 2:398f4fa8fb4a
- Parent:
- 1:214d23448fa7
- Child:
- 3:044c6bd2d569
Compiled for DipCortex
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 1:214d23448fa7 | 1 | /* mbed Microcontroller Library |
Kojto | 1:214d23448fa7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Kojto | 1:214d23448fa7 | 3 | * |
Kojto | 1:214d23448fa7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Kojto | 1:214d23448fa7 | 5 | * you may not use this file except in compliance with the License. |
Kojto | 1:214d23448fa7 | 6 | * You may obtain a copy of the License at |
Kojto | 1:214d23448fa7 | 7 | * |
Kojto | 1:214d23448fa7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Kojto | 1:214d23448fa7 | 9 | * |
Kojto | 1:214d23448fa7 | 10 | * Unless required by applicable law or agreed to in writing, software |
Kojto | 1:214d23448fa7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Kojto | 1:214d23448fa7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Kojto | 1:214d23448fa7 | 13 | * See the License for the specific language governing permissions and |
Kojto | 1:214d23448fa7 | 14 | * limitations under the License. |
Kojto | 1:214d23448fa7 | 15 | */ |
Kojto | 1:214d23448fa7 | 16 | #include "mbed.h" |
Kojto | 1:214d23448fa7 | 17 | #include "cc3000.h" |
Kojto | 1:214d23448fa7 | 18 | #include "main.h" |
Kojto | 1:214d23448fa7 | 19 | |
Kojto | 1:214d23448fa7 | 20 | #include "Websocket.h" |
Kojto | 1:214d23448fa7 | 21 | |
Kojto | 1:214d23448fa7 | 22 | using namespace mbed_cc3000; |
Kojto | 1:214d23448fa7 | 23 | |
Kojto | 1:214d23448fa7 | 24 | tUserFS user_info; |
Kojto | 1:214d23448fa7 | 25 | |
Kojto | 1:214d23448fa7 | 26 | /* cc3000 module declaration specific for user's board. Check also init() */ |
Kojto | 1:214d23448fa7 | 27 | #if (MY_BOARD == WIGO) |
Kojto | 1:214d23448fa7 | 28 | cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), PORTA_IRQn); |
SolderSplashLabs | 2:398f4fa8fb4a | 29 | Serial pc(USBTX, USBRX); |
Kojto | 1:214d23448fa7 | 30 | #elif (MY_BOARD == WIFI_DIPCORTEX) |
Kojto | 1:214d23448fa7 | 31 | cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), PIN_INT0_IRQn); |
SolderSplashLabs | 2:398f4fa8fb4a | 32 | //Serial pc(UART_TX, UART_RX); |
SolderSplashLabs | 2:398f4fa8fb4a | 33 | Serial pc(p19, p20); |
Kojto | 1:214d23448fa7 | 34 | #else |
Kojto | 1:214d23448fa7 | 35 | |
Kojto | 1:214d23448fa7 | 36 | #endif |
Kojto | 1:214d23448fa7 | 37 | |
Kojto | 1:214d23448fa7 | 38 | #ifndef CC3000_UNENCRYPTED_SMART_CONFIG |
Kojto | 1:214d23448fa7 | 39 | const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36}; |
Kojto | 1:214d23448fa7 | 40 | #else |
Kojto | 1:214d23448fa7 | 41 | const uint8_t smartconfigkey = 0; |
Kojto | 1:214d23448fa7 | 42 | #endif |
Kojto | 1:214d23448fa7 | 43 | |
Kojto | 1:214d23448fa7 | 44 | /** |
Kojto | 1:214d23448fa7 | 45 | * \brief Print cc3000 information |
Kojto | 1:214d23448fa7 | 46 | * \param none |
Kojto | 1:214d23448fa7 | 47 | * \return none |
Kojto | 1:214d23448fa7 | 48 | */ |
Kojto | 1:214d23448fa7 | 49 | void print_cc3000_info() { |
Kojto | 1:214d23448fa7 | 50 | uint8_t myMAC[8]; |
Kojto | 1:214d23448fa7 | 51 | |
Kojto | 1:214d23448fa7 | 52 | printf("MAC address + cc3000 info \r\n"); |
Kojto | 1:214d23448fa7 | 53 | wifi.get_user_file_info((uint8_t *)&user_info, sizeof(user_info)); |
Kojto | 1:214d23448fa7 | 54 | wifi.get_mac_address(myMAC); |
Kojto | 1:214d23448fa7 | 55 | printf(" MAC address %02x:%02x:%02x:%02x:%02x:%02x \r\n \r\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]); |
Kojto | 1:214d23448fa7 | 56 | |
Kojto | 1:214d23448fa7 | 57 | printf(" FTC %i \r\n",user_info.FTC); |
Kojto | 1:214d23448fa7 | 58 | printf(" PP_version %i.%i \r\n",user_info.PP_version[0], user_info.PP_version[1]); |
Kojto | 1:214d23448fa7 | 59 | printf(" SERV_PACK %i.%i \r\n",user_info.SERV_PACK[0], user_info.SERV_PACK[1]); |
Kojto | 1:214d23448fa7 | 60 | printf(" DRV_VER %i.%i.%i \r\n",user_info.DRV_VER[0], user_info.DRV_VER[1], user_info.DRV_VER[2]); |
Kojto | 1:214d23448fa7 | 61 | printf(" FW_VER %i.%i.%i \r\n",user_info.FW_VER[0], user_info.FW_VER[1], user_info.FW_VER[2]); |
Kojto | 1:214d23448fa7 | 62 | } |
Kojto | 1:214d23448fa7 | 63 | |
Kojto | 1:214d23448fa7 | 64 | /** |
Kojto | 1:214d23448fa7 | 65 | * \brief Connect to SSID with a timeout |
Kojto | 1:214d23448fa7 | 66 | * \param ssid Name of SSID |
Kojto | 1:214d23448fa7 | 67 | * \param key Password |
Kojto | 1:214d23448fa7 | 68 | * \param sec_mode Security mode |
Kojto | 1:214d23448fa7 | 69 | * \return none |
Kojto | 1:214d23448fa7 | 70 | */ |
Kojto | 1:214d23448fa7 | 71 | void connect_to_ssid(char *ssid, char *key, unsigned char sec_mode) { |
Kojto | 1:214d23448fa7 | 72 | printf("Connecting to SSID: %s. Timeout is 10s.\n",ssid); |
Kojto | 1:214d23448fa7 | 73 | if (wifi.connect_to_AP((uint8_t *)ssid, (uint8_t *)key, sec_mode) == true) { |
Kojto | 1:214d23448fa7 | 74 | printf(" Connected\n"); |
Kojto | 1:214d23448fa7 | 75 | } else { |
Kojto | 1:214d23448fa7 | 76 | printf(" Connection timed-out (error). Please restart.\n"); |
Kojto | 1:214d23448fa7 | 77 | while(1); |
Kojto | 1:214d23448fa7 | 78 | } |
Kojto | 1:214d23448fa7 | 79 | } |
Kojto | 1:214d23448fa7 | 80 | |
Kojto | 1:214d23448fa7 | 81 | /** |
Kojto | 1:214d23448fa7 | 82 | * \brief Connect to SSID without security |
Kojto | 1:214d23448fa7 | 83 | * \param ssid Name of SSID |
Kojto | 1:214d23448fa7 | 84 | * \return none |
Kojto | 1:214d23448fa7 | 85 | */ |
Kojto | 1:214d23448fa7 | 86 | void connect_to_ssid(uint8_t *ssid) { |
Kojto | 1:214d23448fa7 | 87 | wifi.connect_open((uint8_t *)ssid); |
Kojto | 1:214d23448fa7 | 88 | } |
Kojto | 1:214d23448fa7 | 89 | |
Kojto | 1:214d23448fa7 | 90 | /** |
Kojto | 1:214d23448fa7 | 91 | * \brief First time configuration |
Kojto | 1:214d23448fa7 | 92 | * \param none |
Kojto | 1:214d23448fa7 | 93 | * \return none |
Kojto | 1:214d23448fa7 | 94 | */ |
Kojto | 1:214d23448fa7 | 95 | void do_FTC(void) { |
Kojto | 1:214d23448fa7 | 96 | printf("Running First Time Configuration \r\n"); |
Kojto | 1:214d23448fa7 | 97 | wifi.start_smart_config(smartconfigkey); |
Kojto | 1:214d23448fa7 | 98 | while (wifi.is_dhcp_configured() == false) { |
Kojto | 1:214d23448fa7 | 99 | wait_ms(500); |
Kojto | 1:214d23448fa7 | 100 | printf("Waiting for dhcp to be set. \r\n"); |
Kojto | 1:214d23448fa7 | 101 | } |
Kojto | 1:214d23448fa7 | 102 | user_info.FTC = 1; |
Kojto | 1:214d23448fa7 | 103 | wifi.set_user_file_info((uint8_t *)&user_info, sizeof(user_info)); |
Kojto | 1:214d23448fa7 | 104 | wifi._wlan.stop(); |
Kojto | 1:214d23448fa7 | 105 | printf("FTC finished.\n"); |
Kojto | 1:214d23448fa7 | 106 | } |
Kojto | 1:214d23448fa7 | 107 | |
Kojto | 1:214d23448fa7 | 108 | /** Ping a site |
Kojto | 1:214d23448fa7 | 109 | * \param none |
Kojto | 1:214d23448fa7 | 110 | * \return int |
Kojto | 1:214d23448fa7 | 111 | */ |
Kojto | 1:214d23448fa7 | 112 | int main() { |
Kojto | 1:214d23448fa7 | 113 | init(); /* board dependent init */ |
Kojto | 1:214d23448fa7 | 114 | pc.baud(115200); |
Kojto | 1:214d23448fa7 | 115 | |
Kojto | 1:214d23448fa7 | 116 | wifi.start(0); |
Kojto | 1:214d23448fa7 | 117 | printf("cc3000 Websocket demo. \r\n"); |
Kojto | 1:214d23448fa7 | 118 | print_cc3000_info(); |
Kojto | 1:214d23448fa7 | 119 | |
Kojto | 1:214d23448fa7 | 120 | printf("Attempting SSID Connection \r\n"); |
Kojto | 1:214d23448fa7 | 121 | #if (USE_SMART_CONFIG == 1) |
Kojto | 1:214d23448fa7 | 122 | if (user_info.FTC == 1) { |
Kojto | 1:214d23448fa7 | 123 | wifi._wlan.ioctl_set_connection_policy(0, 1, 1); |
Kojto | 1:214d23448fa7 | 124 | } else { |
Kojto | 1:214d23448fa7 | 125 | printf("Smart config is not set, starting configuration. \r\n"); |
Kojto | 1:214d23448fa7 | 126 | do_FTC(); |
Kojto | 1:214d23448fa7 | 127 | printf("Smart config is set. Please restart your board. \r\n"); |
Kojto | 1:214d23448fa7 | 128 | while(1); |
Kojto | 1:214d23448fa7 | 129 | } |
Kojto | 1:214d23448fa7 | 130 | #else |
Kojto | 1:214d23448fa7 | 131 | wifi._wlan.ioctl_set_connection_policy(0, 0, 0); |
Kojto | 1:214d23448fa7 | 132 | #ifndef CC3000_TINY_DRIVER |
Kojto | 1:214d23448fa7 | 133 | #ifdef AP_KEY |
Kojto | 1:214d23448fa7 | 134 | connect_to_ssid(SSID,AP_KEY,AP_SECURITY); |
Kojto | 1:214d23448fa7 | 135 | #else |
Kojto | 1:214d23448fa7 | 136 | connect_to_ssid((uint8_t *)SSID); |
Kojto | 1:214d23448fa7 | 137 | #endif |
Kojto | 1:214d23448fa7 | 138 | #else |
Kojto | 1:214d23448fa7 | 139 | connect_to_ssid((uint8_t *)SSID); |
Kojto | 1:214d23448fa7 | 140 | #endif |
Kojto | 1:214d23448fa7 | 141 | #endif |
Kojto | 1:214d23448fa7 | 142 | printf("DHCP request \r\n"); |
Kojto | 1:214d23448fa7 | 143 | while (wifi.is_dhcp_configured() == false) { |
Kojto | 1:214d23448fa7 | 144 | wait_ms(500); |
Kojto | 1:214d23448fa7 | 145 | printf(" Waiting for dhcp to be set. \r\n"); |
Kojto | 1:214d23448fa7 | 146 | } |
Kojto | 1:214d23448fa7 | 147 | |
Kojto | 1:214d23448fa7 | 148 | tNetappIpconfigRetArgs ipinfo2; |
Kojto | 1:214d23448fa7 | 149 | wifi.get_ip_config(&ipinfo2); // data is returned in the ipinfo2 structure |
Kojto | 1:214d23448fa7 | 150 | printf("DHCP assigned IP Address = %d.%d.%d.%d \r\n", ipinfo2.aucIP[3], ipinfo2.aucIP[2], ipinfo2.aucIP[1], ipinfo2.aucIP[0]); |
Kojto | 1:214d23448fa7 | 151 | |
Kojto | 1:214d23448fa7 | 152 | Websocket ws("ws://sockets.mbed.org/ws/Kojto/rw"); |
Kojto | 1:214d23448fa7 | 153 | while (!ws.connect()); |
Kojto | 1:214d23448fa7 | 154 | |
Kojto | 1:214d23448fa7 | 155 | printf("Connected to websocket server. \r\n"); |
Kojto | 1:214d23448fa7 | 156 | while (1) { |
Kojto | 1:214d23448fa7 | 157 | ws.send("WebSocket Hello World over cc3000"); |
Kojto | 1:214d23448fa7 | 158 | wait(1.0); |
Kojto | 1:214d23448fa7 | 159 | } |
Kojto | 1:214d23448fa7 | 160 | |
Kojto | 1:214d23448fa7 | 161 | wifi.disconnect(); |
Kojto | 1:214d23448fa7 | 162 | } |