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