cc3000 simple socket demo (not using EthernetInterface) !

Dependencies:   NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Info

Simple Socket demo application for the wireless CC3000 module.

Warning

The on-board Firmware must be updated to mbed enable the wireless module.
Goto the Component page to get the FirmwareUpdate tool (scroll down to the FirmwareUpdate topic).

Setup

Note

It is recommended to run an initial test WITHOUT security settings.

Changing network parameters in main.h

  • Setup a wireless router with a non-secure wireless connection using the wireless settings stored in main.h
    or set SSID to your wireless router SSID. For now, do not change USE_SMART_CONFIG and AP_KEY,
    only change AP_SECURITY to NONE.

// use smart config
#define USE_SMART_CONFIG 0

 // Default SSID Settings
#define AP_KEY       "test"
#define AP_SECURITY  NONE
#define SSID         "test"
  • By default, DHCP is used to obtain the IP address.
    When you want to use a fixed IP address, set IP_ALLOC_METHOD USE_STATIC_IP and enter your preferred values for STATIC_IP_OCT_x (device IP address) and STATIC_GW_OCT4 (4th number of your gateway IP address) .
    See the next chapter on how to use USE_SMART_CONFIG.

Setting up the Python script

  • Download Python 2.7 from http://www.python.org/download/
    Install it on a computer connected to the router you previously set up (wireless or wired).
  • Download this Python script to the Python2.7 folder (credit : Jim Carver from Avnet).
  • Compile the CC3000_Simple_Socket_demo code and save it to your board.

Running the application for the first time

  • Open a terminal program (eg: TeraTerm) and connect to your board (serial speed : 115200 baud).
  • Press the reset button on your board.
  • Following startup screen will appear :

Note

The version info can be different.
the dots in the MAC address will show your CC3000's real MAC address.

cc3000 simple socket demo.
MAC address + cc3000 info
 MAC address ..:..:..:..:..:..

 FTC        0
 PP_version 3.4
 SERV_PACK  1.11
 DRV_VER    7.14.24
 FW_VER     7.12.14
User's AP setup: SSID: "test", Password: "test", Security: 3

<0> Normal run. SmartConfig will
    start if no valid connection exists.
<1> Connect using fixed SSID without AP_KEY: test
<2> Connect using fixed SSID with AP_KEY: test
<8> Erase all stored profiles.
<9> SmartConfig.


  • For the initial test, select option <1> (Connect using fixed SSID without AP_KEY: ...).
  • If all goes well, the following screen is shown (the IP address can be different):
Attempting SSID Connection.
DHCP request
  Waiting for dhcp to be set.
  Waiting for dhcp to be set.
DHCP assigned IP Address = 192.168.1.100


  • On the computer where you installed Python2.7:
    • Make sure the connection between your computer and the wireless router is active.
    • Open a DOS prompt and go to the folder where Python2.7 is installed.
    • Type following command :
python wigo_test.py -a 192.168.0.101 -p 15000


Note

Don't forget to replace the IP address with the real IP address assigned by DHCP to the CC3000 module.

If a connection is established, the DOS window will show

-----------------
run tcp client
-----------------
connected to  remote ip=192.168.0.101 remote port=15000
Press ENTER ....


In return the controller board will send following info to the serial port:

Connection from: 192.168.1.101


When we press Enter in the DOS window, the controller board will send following info to the serial port:

Connection from: 192.168.0.10
Received: Hello Wi-Go
Sending the message to the server.


And the DOS window will show:

recv from :  data:  Hello Python


Using option <2>

  • In your wireless router, change the non-secure wireless connection to WEP, WPA or WPA2 and enter a security key.
  • In main.h, update AP_SECURITY and AP_KEY with the values you set in your wireless router.
    Valid values for AP_SECURITY are : NONE, WEP, WPA and WPA2
  • Recompile the code and save it to your board. Reconnect the terminal program and press reset on your board.
  • When the selection menu appears, choose option <2>.
    The communication sequence for option <2> is identical as described for option <1> but now a secure connection is used.

Using the application's options <0>, <8> and <9>

  • Options <0> (Normal run) and <9> (SmartConfig) are very similar.
    They both allow us to connect the CC3000 to another wireless network, without changing the pre-configured wireless settings stored in main.h.
    Option <0> will only start SmartConfig if no valid connection exists (First Time Config),
    but if the CC3000 was previously configured using SmartConfig, it will automatically connect using the stored wireless network profile.
  • Option <8> : As there are only 7 profile slots available, this option can be used to erase all stored profiles.
  • Option <9> allows the user to switch to another wireless connection.
    This connection is stored in one of the 7 profiles. More info on profile priorities is available here.


See TI's pages on how to use the SmartConfig tool:

Committer:
Kojto
Date:
Sat Sep 14 15:06:37 2013 +0000
Revision:
3:aab73431f03b
Parent:
1:99076f2d9408
error handling inside tcp_connection

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 1:99076f2d9408 1 /* mbed Microcontroller Library
Kojto 1:99076f2d9408 2 * Copyright (c) 2006-2013 ARM Limited
Kojto 1:99076f2d9408 3 *
Kojto 1:99076f2d9408 4 * Licensed under the Apache License, Version 2.0 (the "License");
Kojto 1:99076f2d9408 5 * you may not use this file except in compliance with the License.
Kojto 1:99076f2d9408 6 * You may obtain a copy of the License at
Kojto 1:99076f2d9408 7 *
Kojto 1:99076f2d9408 8 * http://www.apache.org/licenses/LICENSE-2.0
Kojto 1:99076f2d9408 9 *
Kojto 1:99076f2d9408 10 * Unless required by applicable law or agreed to in writing, software
Kojto 1:99076f2d9408 11 * distributed under the License is distributed on an "AS IS" BASIS,
Kojto 1:99076f2d9408 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Kojto 1:99076f2d9408 13 * See the License for the specific language governing permissions and
Kojto 1:99076f2d9408 14 * limitations under the License.
Kojto 1:99076f2d9408 15 */
Kojto 1:99076f2d9408 16 #include "tcp_ip_demo.h"
Kojto 1:99076f2d9408 17 #include "cc3000.h"
Kojto 1:99076f2d9408 18 #include "main.h"
Kojto 1:99076f2d9408 19
Kojto 1:99076f2d9408 20 using namespace mbed_cc3000;
Kojto 1:99076f2d9408 21
Kojto 1:99076f2d9408 22 extern cc3000 wigo;
Kojto 1:99076f2d9408 23
Kojto 1:99076f2d9408 24 uint8_t ConnectUsingSmartConfig;
Kojto 1:99076f2d9408 25 uint8_t myMAC[8];
Kojto 1:99076f2d9408 26 tUserFS userFS;
Kojto 1:99076f2d9408 27
Kojto 1:99076f2d9408 28 uint8_t SmartConfigProfilestored = 0xff;
Kojto 1:99076f2d9408 29
Kojto 1:99076f2d9408 30 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG
Kojto 1:99076f2d9408 31 const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36};
Kojto 1:99076f2d9408 32 #else
Kojto 1:99076f2d9408 33 const uint8_t smartconfigkey = 0;
Kojto 1:99076f2d9408 34 #endif
Kojto 1:99076f2d9408 35
Kojto 1:99076f2d9408 36 /* Device name - used for Smart config in order to stop the Smart phone configuration process */
Kojto 1:99076f2d9408 37 static uint8_t DevServname[] = "CC3000";
Kojto 1:99076f2d9408 38 static int8_t requestBuffer[REQ_BUFFER_SIZE];
Kojto 1:99076f2d9408 39
Kojto 1:99076f2d9408 40 /** Start tcp server
Kojto 1:99076f2d9408 41 * \param port Port for server
Kojto 1:99076f2d9408 42 * \return none
Kojto 1:99076f2d9408 43 */
Kojto 1:99076f2d9408 44 void create_tcp_connection(int32_t port) {
Kojto 1:99076f2d9408 45 const char python_msg[] = "Hello Python\n";
Kojto 1:99076f2d9408 46 int32_t stat;
Kojto 1:99076f2d9408 47 cc3000_server server = wigo.create_tcp_server(0, 15000);
Kojto 1:99076f2d9408 48
Kojto 1:99076f2d9408 49 server.bind();
Kojto 1:99076f2d9408 50 server.listen(1); /* backlog = 1 */
Kojto 1:99076f2d9408 51 while (1) {
Kojto 1:99076f2d9408 52 stat = server.accept();
Kojto 1:99076f2d9408 53 printf("Connected\n");
Kojto 1:99076f2d9408 54 if (stat >= 0) {
Kojto 1:99076f2d9408 55 server.receive(requestBuffer,20,0);
Kojto 1:99076f2d9408 56 printf("Input = %s\n", requestBuffer);
Kojto 1:99076f2d9408 57 stat = server.send((void *)python_msg, strlen(python_msg), 0);
Kojto 1:99076f2d9408 58 printf("status= %d\n", stat);
Kojto 1:99076f2d9408 59 } else {
Kojto 3:aab73431f03b 60 printf("ERROR %d", stat);
Kojto 3:aab73431f03b 61 switch (stat) {
Kojto 3:aab73431f03b 62 case -1:
Kojto 3:aab73431f03b 63 printf(": remote socket closed.\n");
Kojto 3:aab73431f03b 64 break;
Kojto 3:aab73431f03b 65 case -2:
Kojto 3:aab73431f03b 66 printf(": no buffers available.\n");
Kojto 3:aab73431f03b 67 break;
Kojto 3:aab73431f03b 68 case -57:
Kojto 3:aab73431f03b 69 printf(": timeout - no reply from remote.\n");
Kojto 3:aab73431f03b 70 break;
Kojto 3:aab73431f03b 71 default:
Kojto 3:aab73431f03b 72 printf("\n");
Kojto 3:aab73431f03b 73 }
Kojto 1:99076f2d9408 74 }
Kojto 1:99076f2d9408 75 server.close();
Kojto 1:99076f2d9408 76 printf("Done, press any key to repeat\n");
Kojto 1:99076f2d9408 77 getchar();
Kojto 1:99076f2d9408 78 }
Kojto 1:99076f2d9408 79 }
Kojto 1:99076f2d9408 80
Kojto 1:99076f2d9408 81 /** Start smart config
Kojto 1:99076f2d9408 82 * \param none
Kojto 1:99076f2d9408 83 * \return none
Kojto 1:99076f2d9408 84 */
Kojto 1:99076f2d9408 85 void start_smart_config() {
Kojto 1:99076f2d9408 86 wigo.start_smart_config(smartconfigkey);
Kojto 1:99076f2d9408 87 }
Kojto 1:99076f2d9408 88
Kojto 1:99076f2d9408 89 /** Initialize
Kojto 1:99076f2d9408 90 * \param none
Kojto 1:99076f2d9408 91 * \return none
Kojto 1:99076f2d9408 92 */
Kojto 1:99076f2d9408 93 void init_dhcp(void)
Kojto 1:99076f2d9408 94 {
Kojto 1:99076f2d9408 95 int32_t t;
Kojto 1:99076f2d9408 96 tNetappIpconfigRetArgs ipinfo2;
Kojto 1:99076f2d9408 97
Kojto 1:99076f2d9408 98 //dynamic dhcp is set by default
Kojto 1:99076f2d9408 99 #if IP_ALLOC_METHOD == USE_STATIC_IP
Kojto 1:99076f2d9408 100 set_static_dhcp();
Kojto 1:99076f2d9408 101 #endif
Kojto 1:99076f2d9408 102 check_dhcp();
Kojto 1:99076f2d9408 103
Kojto 1:99076f2d9408 104 // If we're not blocked by accept or others, obtain the latest status
Kojto 1:99076f2d9408 105 wigo.get_ip_config(&ipinfo2); // data is returned in the ipinfo2 structure
Kojto 1:99076f2d9408 106 printf("\n*** Wi-Go board DHCP assigned IP Address = %d.%d.%d.%d\n", ipinfo2.aucIP[3], ipinfo2.aucIP[2], ipinfo2.aucIP[1], ipinfo2.aucIP[0]);
Kojto 1:99076f2d9408 107
Kojto 1:99076f2d9408 108 t = wigo._socket.mdnsAdvertiser(1, DevServname, sizeof(DevServname));
Kojto 1:99076f2d9408 109 printf("mDNS Status= %x\n", t);
Kojto 1:99076f2d9408 110 }
Kojto 1:99076f2d9408 111
Kojto 1:99076f2d9408 112 /** Waits for dhcp to be set
Kojto 1:99076f2d9408 113 * \param none
Kojto 1:99076f2d9408 114 * \return none
Kojto 1:99076f2d9408 115 */
Kojto 1:99076f2d9408 116 void check_dhcp(void)
Kojto 1:99076f2d9408 117 {
Kojto 1:99076f2d9408 118 int32_t t;
Kojto 1:99076f2d9408 119
Kojto 1:99076f2d9408 120 while (wigo.is_dhcp_configured() == false)
Kojto 1:99076f2d9408 121 {
Kojto 1:99076f2d9408 122 wait_ms(500);
Kojto 1:99076f2d9408 123 printf("Waiting for dhcp to be set.\n");
Kojto 1:99076f2d9408 124 }
Kojto 1:99076f2d9408 125
Kojto 1:99076f2d9408 126 t = wigo._socket.mdnsAdvertiser(1, DevServname, sizeof(DevServname));
Kojto 1:99076f2d9408 127 printf("mDNS Status= %x\n", t);
Kojto 1:99076f2d9408 128 }
Kojto 1:99076f2d9408 129
Kojto 1:99076f2d9408 130 /** Sets static DHCP (auto dhpc by default)
Kojto 1:99076f2d9408 131 * \param none
Kojto 1:99076f2d9408 132 * \return none
Kojto 1:99076f2d9408 133 */
Kojto 1:99076f2d9408 134 void set_static_dhcp(void)
Kojto 1:99076f2d9408 135 {
Kojto 1:99076f2d9408 136 uint8_t pucIP_Addr[4];
Kojto 1:99076f2d9408 137 uint8_t pucIP_DefaultGWAddr[4];
Kojto 1:99076f2d9408 138 uint8_t pucSubnetMask[4];
Kojto 1:99076f2d9408 139 uint8_t pucDNS[4];
Kojto 1:99076f2d9408 140
Kojto 1:99076f2d9408 141 // Subnet mask is assumed to be 255.255.255.0
Kojto 1:99076f2d9408 142 pucSubnetMask[0] = 0xFF;
Kojto 1:99076f2d9408 143 pucSubnetMask[1] = 0xFF;
Kojto 1:99076f2d9408 144 pucSubnetMask[2] = 0xFF;
Kojto 1:99076f2d9408 145 pucSubnetMask[3] = 0x0;
Kojto 1:99076f2d9408 146
Kojto 1:99076f2d9408 147 // CC3000's IP
Kojto 1:99076f2d9408 148 pucIP_Addr[0] = STATIC_IP_OCT1;
Kojto 1:99076f2d9408 149 pucIP_Addr[1] = STATIC_IP_OCT2;
Kojto 1:99076f2d9408 150 pucIP_Addr[2] = STATIC_IP_OCT3;
Kojto 1:99076f2d9408 151 pucIP_Addr[3] = STATIC_IP_OCT4;
Kojto 1:99076f2d9408 152
Kojto 1:99076f2d9408 153 // Default Gateway/Router IP
Kojto 1:99076f2d9408 154 // 192.168.1.1
Kojto 1:99076f2d9408 155 pucIP_DefaultGWAddr[0] = STATIC_IP_OCT1;
Kojto 1:99076f2d9408 156 pucIP_DefaultGWAddr[1] = STATIC_IP_OCT2;
Kojto 1:99076f2d9408 157 pucIP_DefaultGWAddr[2] = STATIC_IP_OCT3;
Kojto 1:99076f2d9408 158 pucIP_DefaultGWAddr[3] = STATIC_GW_OCT4;
Kojto 1:99076f2d9408 159
Kojto 1:99076f2d9408 160 // We assume the router is also a DNS server
Kojto 1:99076f2d9408 161 pucDNS[0] = STATIC_IP_OCT1;
Kojto 1:99076f2d9408 162 pucDNS[1] = STATIC_IP_OCT2;
Kojto 1:99076f2d9408 163 pucDNS[2] = STATIC_IP_OCT3;
Kojto 1:99076f2d9408 164 pucDNS[3] = STATIC_GW_OCT4;
Kojto 1:99076f2d9408 165
Kojto 1:99076f2d9408 166 wigo._netapp.dhcp((uint32_t *)pucIP_Addr,
Kojto 1:99076f2d9408 167 (uint32_t *)pucSubnetMask,
Kojto 1:99076f2d9408 168 (uint32_t *)pucIP_DefaultGWAddr,
Kojto 1:99076f2d9408 169 (uint32_t *)pucDNS);
Kojto 1:99076f2d9408 170
Kojto 1:99076f2d9408 171 // reset the CC3000 to apply Static Setting
Kojto 1:99076f2d9408 172 wigo._wlan.stop();
Kojto 1:99076f2d9408 173 wait(1);
Kojto 1:99076f2d9408 174 wigo._wlan.start(0);
Kojto 1:99076f2d9408 175
Kojto 1:99076f2d9408 176 // Mask out all non-required events from CC3000
Kojto 1:99076f2d9408 177 wigo._wlan.set_event_mask(HCI_EVNT_WLAN_KEEPALIVE|
Kojto 1:99076f2d9408 178 HCI_EVNT_WLAN_UNSOL_INIT|
Kojto 1:99076f2d9408 179 HCI_EVNT_WLAN_ASYNC_PING_REPORT);
Kojto 1:99076f2d9408 180 }
Kojto 1:99076f2d9408 181
Kojto 1:99076f2d9408 182 /** Executes functions for tcp/ip server
Kojto 1:99076f2d9408 183 * \param none
Kojto 1:99076f2d9408 184 * \return none
Kojto 1:99076f2d9408 185 */
Kojto 1:99076f2d9408 186 void run_tcp_server(void)
Kojto 1:99076f2d9408 187 {
Kojto 1:99076f2d9408 188 init_dhcp();
Kojto 1:99076f2d9408 189 printf("\n\nStarting TCP/IP Server\n");
Kojto 1:99076f2d9408 190 create_tcp_connection(TCPIP_PORT);
Kojto 1:99076f2d9408 191 }
Kojto 1:99076f2d9408 192
Kojto 1:99076f2d9408 193 /** First time configuration
Kojto 1:99076f2d9408 194 * \param none
Kojto 1:99076f2d9408 195 * \return none
Kojto 1:99076f2d9408 196 */
Kojto 1:99076f2d9408 197 void do_FTC(void)
Kojto 1:99076f2d9408 198 {
Kojto 1:99076f2d9408 199 printf("Running First Time Configuration\n");
Kojto 1:99076f2d9408 200 start_smart_config();
Kojto 1:99076f2d9408 201 init_dhcp();
Kojto 1:99076f2d9408 202 userFS.FTC = 1;
Kojto 1:99076f2d9408 203 wigo.set_user_file_info((uint8_t *)&userFS, sizeof(userFS));
Kojto 1:99076f2d9408 204 SmartConfigProfilestored = SMART_CONFIG_SET;
Kojto 1:99076f2d9408 205 wigo._wlan.stop();
Kojto 1:99076f2d9408 206 printf("FTC finished.\n");
Kojto 1:99076f2d9408 207 }
Kojto 1:99076f2d9408 208
Kojto 1:99076f2d9408 209
Kojto 1:99076f2d9408 210