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:
Fri Sep 13 18:02:03 2013 +0000
Revision:
1:99076f2d9408
Child:
3:aab73431f03b
tcp server addition

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 1:99076f2d9408 60 printf("bad socket= %d\n", stat);
Kojto 1:99076f2d9408 61 }
Kojto 1:99076f2d9408 62 server.close();
Kojto 1:99076f2d9408 63 printf("Done, press any key to repeat\n");
Kojto 1:99076f2d9408 64 getchar();
Kojto 1:99076f2d9408 65 }
Kojto 1:99076f2d9408 66 }
Kojto 1:99076f2d9408 67
Kojto 1:99076f2d9408 68 /** Start smart config
Kojto 1:99076f2d9408 69 * \param none
Kojto 1:99076f2d9408 70 * \return none
Kojto 1:99076f2d9408 71 */
Kojto 1:99076f2d9408 72 void start_smart_config() {
Kojto 1:99076f2d9408 73 wigo.start_smart_config(smartconfigkey);
Kojto 1:99076f2d9408 74 }
Kojto 1:99076f2d9408 75
Kojto 1:99076f2d9408 76 /** Initialize
Kojto 1:99076f2d9408 77 * \param none
Kojto 1:99076f2d9408 78 * \return none
Kojto 1:99076f2d9408 79 */
Kojto 1:99076f2d9408 80 void init_dhcp(void)
Kojto 1:99076f2d9408 81 {
Kojto 1:99076f2d9408 82 int32_t t;
Kojto 1:99076f2d9408 83 tNetappIpconfigRetArgs ipinfo2;
Kojto 1:99076f2d9408 84
Kojto 1:99076f2d9408 85 //dynamic dhcp is set by default
Kojto 1:99076f2d9408 86 #if IP_ALLOC_METHOD == USE_STATIC_IP
Kojto 1:99076f2d9408 87 set_static_dhcp();
Kojto 1:99076f2d9408 88 #endif
Kojto 1:99076f2d9408 89 check_dhcp();
Kojto 1:99076f2d9408 90
Kojto 1:99076f2d9408 91 // If we're not blocked by accept or others, obtain the latest status
Kojto 1:99076f2d9408 92 wigo.get_ip_config(&ipinfo2); // data is returned in the ipinfo2 structure
Kojto 1:99076f2d9408 93 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 94
Kojto 1:99076f2d9408 95 t = wigo._socket.mdnsAdvertiser(1, DevServname, sizeof(DevServname));
Kojto 1:99076f2d9408 96 printf("mDNS Status= %x\n", t);
Kojto 1:99076f2d9408 97 }
Kojto 1:99076f2d9408 98
Kojto 1:99076f2d9408 99 /** Waits for dhcp to be set
Kojto 1:99076f2d9408 100 * \param none
Kojto 1:99076f2d9408 101 * \return none
Kojto 1:99076f2d9408 102 */
Kojto 1:99076f2d9408 103 void check_dhcp(void)
Kojto 1:99076f2d9408 104 {
Kojto 1:99076f2d9408 105 int32_t t;
Kojto 1:99076f2d9408 106
Kojto 1:99076f2d9408 107 while (wigo.is_dhcp_configured() == false)
Kojto 1:99076f2d9408 108 {
Kojto 1:99076f2d9408 109 wait_ms(500);
Kojto 1:99076f2d9408 110 printf("Waiting for dhcp to be set.\n");
Kojto 1:99076f2d9408 111 }
Kojto 1:99076f2d9408 112
Kojto 1:99076f2d9408 113 t = wigo._socket.mdnsAdvertiser(1, DevServname, sizeof(DevServname));
Kojto 1:99076f2d9408 114 printf("mDNS Status= %x\n", t);
Kojto 1:99076f2d9408 115 }
Kojto 1:99076f2d9408 116
Kojto 1:99076f2d9408 117 /** Sets static DHCP (auto dhpc by default)
Kojto 1:99076f2d9408 118 * \param none
Kojto 1:99076f2d9408 119 * \return none
Kojto 1:99076f2d9408 120 */
Kojto 1:99076f2d9408 121 void set_static_dhcp(void)
Kojto 1:99076f2d9408 122 {
Kojto 1:99076f2d9408 123 uint8_t pucIP_Addr[4];
Kojto 1:99076f2d9408 124 uint8_t pucIP_DefaultGWAddr[4];
Kojto 1:99076f2d9408 125 uint8_t pucSubnetMask[4];
Kojto 1:99076f2d9408 126 uint8_t pucDNS[4];
Kojto 1:99076f2d9408 127
Kojto 1:99076f2d9408 128 // Subnet mask is assumed to be 255.255.255.0
Kojto 1:99076f2d9408 129 pucSubnetMask[0] = 0xFF;
Kojto 1:99076f2d9408 130 pucSubnetMask[1] = 0xFF;
Kojto 1:99076f2d9408 131 pucSubnetMask[2] = 0xFF;
Kojto 1:99076f2d9408 132 pucSubnetMask[3] = 0x0;
Kojto 1:99076f2d9408 133
Kojto 1:99076f2d9408 134 // CC3000's IP
Kojto 1:99076f2d9408 135 pucIP_Addr[0] = STATIC_IP_OCT1;
Kojto 1:99076f2d9408 136 pucIP_Addr[1] = STATIC_IP_OCT2;
Kojto 1:99076f2d9408 137 pucIP_Addr[2] = STATIC_IP_OCT3;
Kojto 1:99076f2d9408 138 pucIP_Addr[3] = STATIC_IP_OCT4;
Kojto 1:99076f2d9408 139
Kojto 1:99076f2d9408 140 // Default Gateway/Router IP
Kojto 1:99076f2d9408 141 // 192.168.1.1
Kojto 1:99076f2d9408 142 pucIP_DefaultGWAddr[0] = STATIC_IP_OCT1;
Kojto 1:99076f2d9408 143 pucIP_DefaultGWAddr[1] = STATIC_IP_OCT2;
Kojto 1:99076f2d9408 144 pucIP_DefaultGWAddr[2] = STATIC_IP_OCT3;
Kojto 1:99076f2d9408 145 pucIP_DefaultGWAddr[3] = STATIC_GW_OCT4;
Kojto 1:99076f2d9408 146
Kojto 1:99076f2d9408 147 // We assume the router is also a DNS server
Kojto 1:99076f2d9408 148 pucDNS[0] = STATIC_IP_OCT1;
Kojto 1:99076f2d9408 149 pucDNS[1] = STATIC_IP_OCT2;
Kojto 1:99076f2d9408 150 pucDNS[2] = STATIC_IP_OCT3;
Kojto 1:99076f2d9408 151 pucDNS[3] = STATIC_GW_OCT4;
Kojto 1:99076f2d9408 152
Kojto 1:99076f2d9408 153 wigo._netapp.dhcp((uint32_t *)pucIP_Addr,
Kojto 1:99076f2d9408 154 (uint32_t *)pucSubnetMask,
Kojto 1:99076f2d9408 155 (uint32_t *)pucIP_DefaultGWAddr,
Kojto 1:99076f2d9408 156 (uint32_t *)pucDNS);
Kojto 1:99076f2d9408 157
Kojto 1:99076f2d9408 158 // reset the CC3000 to apply Static Setting
Kojto 1:99076f2d9408 159 wigo._wlan.stop();
Kojto 1:99076f2d9408 160 wait(1);
Kojto 1:99076f2d9408 161 wigo._wlan.start(0);
Kojto 1:99076f2d9408 162
Kojto 1:99076f2d9408 163 // Mask out all non-required events from CC3000
Kojto 1:99076f2d9408 164 wigo._wlan.set_event_mask(HCI_EVNT_WLAN_KEEPALIVE|
Kojto 1:99076f2d9408 165 HCI_EVNT_WLAN_UNSOL_INIT|
Kojto 1:99076f2d9408 166 HCI_EVNT_WLAN_ASYNC_PING_REPORT);
Kojto 1:99076f2d9408 167 }
Kojto 1:99076f2d9408 168
Kojto 1:99076f2d9408 169 /** Executes functions for tcp/ip server
Kojto 1:99076f2d9408 170 * \param none
Kojto 1:99076f2d9408 171 * \return none
Kojto 1:99076f2d9408 172 */
Kojto 1:99076f2d9408 173 void run_tcp_server(void)
Kojto 1:99076f2d9408 174 {
Kojto 1:99076f2d9408 175 init_dhcp();
Kojto 1:99076f2d9408 176 printf("\n\nStarting TCP/IP Server\n");
Kojto 1:99076f2d9408 177 create_tcp_connection(TCPIP_PORT);
Kojto 1:99076f2d9408 178 }
Kojto 1:99076f2d9408 179
Kojto 1:99076f2d9408 180 /** First time configuration
Kojto 1:99076f2d9408 181 * \param none
Kojto 1:99076f2d9408 182 * \return none
Kojto 1:99076f2d9408 183 */
Kojto 1:99076f2d9408 184 void do_FTC(void)
Kojto 1:99076f2d9408 185 {
Kojto 1:99076f2d9408 186 printf("Running First Time Configuration\n");
Kojto 1:99076f2d9408 187 start_smart_config();
Kojto 1:99076f2d9408 188 init_dhcp();
Kojto 1:99076f2d9408 189 userFS.FTC = 1;
Kojto 1:99076f2d9408 190 wigo.set_user_file_info((uint8_t *)&userFS, sizeof(userFS));
Kojto 1:99076f2d9408 191 SmartConfigProfilestored = SMART_CONFIG_SET;
Kojto 1:99076f2d9408 192 wigo._wlan.stop();
Kojto 1:99076f2d9408 193 printf("FTC finished.\n");
Kojto 1:99076f2d9408 194 }
Kojto 1:99076f2d9408 195
Kojto 1:99076f2d9408 196
Kojto 1:99076f2d9408 197