cc3000 websocket demo for cc3000

Dependencies:   NVIC_set_all_priorities WebSocketClient cc3000_hostdriver_mbedsocket mbed

Committer:
Kojto
Date:
Thu Oct 03 17:33:38 2013 +0200
Revision:
6:62b6aa700f85
Parent:
3:044c6bd2d569
Child:
7:a671c9a84867
ending - unix, removed unreachable line

Who changed what in which revision?

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