my copy of the websocket demo
Dependencies: NVIC_set_all_priorities WebSocketClient cc3000_hostdriver_mbedsocket mbed
Fork of cc3000_websocket_demo by
main.cpp@3:044c6bd2d569, 2013-10-02 (annotated)
- Committer:
- SolderSplashLabs
- Date:
- Wed Oct 02 20:23:39 2013 +0000
- Revision:
- 3:044c6bd2d569
- Parent:
- 2:398f4fa8fb4a
Reverted to Martins defaults in main.h
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 | 3:044c6bd2d569 | 32 | Serial pc(UART_TX, UART_RX); |
Kojto | 1:214d23448fa7 | 33 | #else |
Kojto | 1:214d23448fa7 | 34 | |
Kojto | 1:214d23448fa7 | 35 | #endif |
Kojto | 1:214d23448fa7 | 36 | |
Kojto | 1:214d23448fa7 | 37 | #ifndef CC3000_UNENCRYPTED_SMART_CONFIG |
Kojto | 1:214d23448fa7 | 38 | const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36}; |
Kojto | 1:214d23448fa7 | 39 | #else |
Kojto | 1:214d23448fa7 | 40 | const uint8_t smartconfigkey = 0; |
Kojto | 1:214d23448fa7 | 41 | #endif |
Kojto | 1:214d23448fa7 | 42 | |
Kojto | 1:214d23448fa7 | 43 | /** |
Kojto | 1:214d23448fa7 | 44 | * \brief Print cc3000 information |
Kojto | 1:214d23448fa7 | 45 | * \param none |
Kojto | 1:214d23448fa7 | 46 | * \return none |
Kojto | 1:214d23448fa7 | 47 | */ |
Kojto | 1:214d23448fa7 | 48 | void print_cc3000_info() { |
Kojto | 1:214d23448fa7 | 49 | uint8_t myMAC[8]; |
Kojto | 1:214d23448fa7 | 50 | |
Kojto | 1:214d23448fa7 | 51 | printf("MAC address + cc3000 info \r\n"); |
Kojto | 1:214d23448fa7 | 52 | wifi.get_user_file_info((uint8_t *)&user_info, sizeof(user_info)); |
Kojto | 1:214d23448fa7 | 53 | wifi.get_mac_address(myMAC); |
Kojto | 1:214d23448fa7 | 54 | 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 | 55 | |
Kojto | 1:214d23448fa7 | 56 | printf(" FTC %i \r\n",user_info.FTC); |
Kojto | 1:214d23448fa7 | 57 | printf(" PP_version %i.%i \r\n",user_info.PP_version[0], user_info.PP_version[1]); |
Kojto | 1:214d23448fa7 | 58 | printf(" SERV_PACK %i.%i \r\n",user_info.SERV_PACK[0], user_info.SERV_PACK[1]); |
Kojto | 1:214d23448fa7 | 59 | 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 | 60 | 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 | 61 | } |
Kojto | 1:214d23448fa7 | 62 | |
Kojto | 1:214d23448fa7 | 63 | /** |
Kojto | 1:214d23448fa7 | 64 | * \brief Connect to SSID with a timeout |
Kojto | 1:214d23448fa7 | 65 | * \param ssid Name of SSID |
Kojto | 1:214d23448fa7 | 66 | * \param key Password |
Kojto | 1:214d23448fa7 | 67 | * \param sec_mode Security mode |
Kojto | 1:214d23448fa7 | 68 | * \return none |
Kojto | 1:214d23448fa7 | 69 | */ |
Kojto | 1:214d23448fa7 | 70 | void connect_to_ssid(char *ssid, char *key, unsigned char sec_mode) { |
Kojto | 1:214d23448fa7 | 71 | printf("Connecting to SSID: %s. Timeout is 10s.\n",ssid); |
Kojto | 1:214d23448fa7 | 72 | if (wifi.connect_to_AP((uint8_t *)ssid, (uint8_t *)key, sec_mode) == true) { |
Kojto | 1:214d23448fa7 | 73 | printf(" Connected\n"); |
Kojto | 1:214d23448fa7 | 74 | } else { |
Kojto | 1:214d23448fa7 | 75 | printf(" Connection timed-out (error). Please restart.\n"); |
Kojto | 1:214d23448fa7 | 76 | while(1); |
Kojto | 1:214d23448fa7 | 77 | } |
Kojto | 1:214d23448fa7 | 78 | } |
Kojto | 1:214d23448fa7 | 79 | |
Kojto | 1:214d23448fa7 | 80 | /** |
Kojto | 1:214d23448fa7 | 81 | * \brief Connect to SSID without security |
Kojto | 1:214d23448fa7 | 82 | * \param ssid Name of SSID |
Kojto | 1:214d23448fa7 | 83 | * \return none |
Kojto | 1:214d23448fa7 | 84 | */ |
Kojto | 1:214d23448fa7 | 85 | void connect_to_ssid(uint8_t *ssid) { |
Kojto | 1:214d23448fa7 | 86 | wifi.connect_open((uint8_t *)ssid); |
Kojto | 1:214d23448fa7 | 87 | } |
Kojto | 1:214d23448fa7 | 88 | |
Kojto | 1:214d23448fa7 | 89 | /** |
Kojto | 1:214d23448fa7 | 90 | * \brief First time configuration |
Kojto | 1:214d23448fa7 | 91 | * \param none |
Kojto | 1:214d23448fa7 | 92 | * \return none |
Kojto | 1:214d23448fa7 | 93 | */ |
Kojto | 1:214d23448fa7 | 94 | void do_FTC(void) { |
Kojto | 1:214d23448fa7 | 95 | printf("Running First Time Configuration \r\n"); |
Kojto | 1:214d23448fa7 | 96 | wifi.start_smart_config(smartconfigkey); |
Kojto | 1:214d23448fa7 | 97 | while (wifi.is_dhcp_configured() == false) { |
Kojto | 1:214d23448fa7 | 98 | wait_ms(500); |
Kojto | 1:214d23448fa7 | 99 | printf("Waiting for dhcp to be set. \r\n"); |
Kojto | 1:214d23448fa7 | 100 | } |
Kojto | 1:214d23448fa7 | 101 | user_info.FTC = 1; |
Kojto | 1:214d23448fa7 | 102 | wifi.set_user_file_info((uint8_t *)&user_info, sizeof(user_info)); |
Kojto | 1:214d23448fa7 | 103 | wifi._wlan.stop(); |
Kojto | 1:214d23448fa7 | 104 | printf("FTC finished.\n"); |
Kojto | 1:214d23448fa7 | 105 | } |
Kojto | 1:214d23448fa7 | 106 | |
Kojto | 1:214d23448fa7 | 107 | /** Ping a site |
Kojto | 1:214d23448fa7 | 108 | * \param none |
Kojto | 1:214d23448fa7 | 109 | * \return int |
Kojto | 1:214d23448fa7 | 110 | */ |
Kojto | 1:214d23448fa7 | 111 | int main() { |
Kojto | 1:214d23448fa7 | 112 | init(); /* board dependent init */ |
Kojto | 1:214d23448fa7 | 113 | pc.baud(115200); |
Kojto | 1:214d23448fa7 | 114 | |
Kojto | 1:214d23448fa7 | 115 | wifi.start(0); |
Kojto | 1:214d23448fa7 | 116 | printf("cc3000 Websocket demo. \r\n"); |
Kojto | 1:214d23448fa7 | 117 | print_cc3000_info(); |
Kojto | 1:214d23448fa7 | 118 | |
Kojto | 1:214d23448fa7 | 119 | printf("Attempting SSID Connection \r\n"); |
Kojto | 1:214d23448fa7 | 120 | #if (USE_SMART_CONFIG == 1) |
Kojto | 1:214d23448fa7 | 121 | if (user_info.FTC == 1) { |
Kojto | 1:214d23448fa7 | 122 | wifi._wlan.ioctl_set_connection_policy(0, 1, 1); |
Kojto | 1:214d23448fa7 | 123 | } else { |
Kojto | 1:214d23448fa7 | 124 | printf("Smart config is not set, starting configuration. \r\n"); |
Kojto | 1:214d23448fa7 | 125 | do_FTC(); |
Kojto | 1:214d23448fa7 | 126 | printf("Smart config is set. Please restart your board. \r\n"); |
Kojto | 1:214d23448fa7 | 127 | while(1); |
Kojto | 1:214d23448fa7 | 128 | } |
Kojto | 1:214d23448fa7 | 129 | #else |
Kojto | 1:214d23448fa7 | 130 | wifi._wlan.ioctl_set_connection_policy(0, 0, 0); |
Kojto | 1:214d23448fa7 | 131 | #ifndef CC3000_TINY_DRIVER |
Kojto | 1:214d23448fa7 | 132 | #ifdef AP_KEY |
Kojto | 1:214d23448fa7 | 133 | connect_to_ssid(SSID,AP_KEY,AP_SECURITY); |
Kojto | 1:214d23448fa7 | 134 | #else |
Kojto | 1:214d23448fa7 | 135 | connect_to_ssid((uint8_t *)SSID); |
Kojto | 1:214d23448fa7 | 136 | #endif |
Kojto | 1:214d23448fa7 | 137 | #else |
Kojto | 1:214d23448fa7 | 138 | connect_to_ssid((uint8_t *)SSID); |
Kojto | 1:214d23448fa7 | 139 | #endif |
Kojto | 1:214d23448fa7 | 140 | #endif |
Kojto | 1:214d23448fa7 | 141 | printf("DHCP request \r\n"); |
Kojto | 1:214d23448fa7 | 142 | while (wifi.is_dhcp_configured() == false) { |
Kojto | 1:214d23448fa7 | 143 | wait_ms(500); |
Kojto | 1:214d23448fa7 | 144 | printf(" Waiting for dhcp to be set. \r\n"); |
Kojto | 1:214d23448fa7 | 145 | } |
Kojto | 1:214d23448fa7 | 146 | |
Kojto | 1:214d23448fa7 | 147 | tNetappIpconfigRetArgs ipinfo2; |
Kojto | 1:214d23448fa7 | 148 | wifi.get_ip_config(&ipinfo2); // data is returned in the ipinfo2 structure |
Kojto | 1:214d23448fa7 | 149 | 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 | 150 | |
Kojto | 1:214d23448fa7 | 151 | Websocket ws("ws://sockets.mbed.org/ws/Kojto/rw"); |
Kojto | 1:214d23448fa7 | 152 | while (!ws.connect()); |
Kojto | 1:214d23448fa7 | 153 | |
Kojto | 1:214d23448fa7 | 154 | printf("Connected to websocket server. \r\n"); |
Kojto | 1:214d23448fa7 | 155 | while (1) { |
Kojto | 1:214d23448fa7 | 156 | ws.send("WebSocket Hello World over cc3000"); |
Kojto | 1:214d23448fa7 | 157 | wait(1.0); |
Kojto | 1:214d23448fa7 | 158 | } |
Kojto | 1:214d23448fa7 | 159 | |
Kojto | 1:214d23448fa7 | 160 | wifi.disconnect(); |
Kojto | 1:214d23448fa7 | 161 | } |