my copy of the websocket demo

Dependencies:   NVIC_set_all_priorities WebSocketClient cc3000_hostdriver_mbedsocket mbed

Fork of cc3000_websocket_demo by Martin Kojtal

Committer:
Kojto
Date:
Tue Oct 01 15:42:21 2013 +0000
Revision:
0:7f1ab388e9c4
Child:
1:214d23448fa7
websocket demo initial version

Who changed what in which revision?

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