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:
Sun Sep 08 18:31:33 2013 +0000
Revision:
0:ce33c63eefe5
initial version of new simple socket

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 0:ce33c63eefe5 1 #include "doTCPIP.h"
Kojto 0:ce33c63eefe5 2 #include "cc3000.h"
Kojto 0:ce33c63eefe5 3 #include "main.h"
Kojto 0:ce33c63eefe5 4
Kojto 0:ce33c63eefe5 5 using namespace mbed_cc3000;
Kojto 0:ce33c63eefe5 6
Kojto 0:ce33c63eefe5 7 extern cc3000 wigo;
Kojto 0:ce33c63eefe5 8
Kojto 0:ce33c63eefe5 9 //volatile uint8_t newData;
Kojto 0:ce33c63eefe5 10 // int32_t server_running;
Kojto 0:ce33c63eefe5 11 //uint8_t ForceFixedSSID;
Kojto 0:ce33c63eefe5 12 //int8_t runSmartConfig; // Flag indicating whether user requested to perform Smart Config
Kojto 0:ce33c63eefe5 13 //volatile uint32_t ulCC3000Connected;
Kojto 0:ce33c63eefe5 14 uint8_t ConnectUsingSmartConfig;
Kojto 0:ce33c63eefe5 15 uint8_t myMAC[8];
Kojto 0:ce33c63eefe5 16 tUserFS userFS;
Kojto 0:ce33c63eefe5 17
Kojto 0:ce33c63eefe5 18 int8_t requestBuffer[REQ_BUFFER_SIZE];
Kojto 0:ce33c63eefe5 19 //int32_t LAN_Connected = 0;
Kojto 0:ce33c63eefe5 20
Kojto 0:ce33c63eefe5 21 uint8_t SmartConfigProfilestored = 0xff;
Kojto 0:ce33c63eefe5 22
Kojto 0:ce33c63eefe5 23 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG
Kojto 0:ce33c63eefe5 24 const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36};
Kojto 0:ce33c63eefe5 25 #else
Kojto 0:ce33c63eefe5 26 const uint8_t smartconfigkey = NULL;
Kojto 0:ce33c63eefe5 27 #endif
Kojto 0:ce33c63eefe5 28
Kojto 0:ce33c63eefe5 29 /** \brief Flag indicating whether to print CC3000 Connection info */
Kojto 0:ce33c63eefe5 30
Kojto 0:ce33c63eefe5 31 //Device name - used for Smart config in order to stop the Smart phone configuration process
Kojto 0:ce33c63eefe5 32 uint8_t DevServname[] = "CC3000";
Kojto 0:ce33c63eefe5 33 //volatile uint32_t SendmDNSAdvertisment;
Kojto 0:ce33c63eefe5 34
Kojto 0:ce33c63eefe5 35
Kojto 0:ce33c63eefe5 36 /* TODO */
Kojto 0:ce33c63eefe5 37 void create_tcp_connection(int32_t port)
Kojto 0:ce33c63eefe5 38 {
Kojto 0:ce33c63eefe5 39 const char python_msg[] = "Hello Python\n";
Kojto 0:ce33c63eefe5 40 int32_t stat;
Kojto 0:ce33c63eefe5 41 int32_t sock;
Kojto 0:ce33c63eefe5 42 //new TCP socket descriptor
Kojto 0:ce33c63eefe5 43 int32_t newsock;
Kojto 0:ce33c63eefe5 44 //destination address
Kojto 0:ce33c63eefe5 45 sockaddr destAddr;
Kojto 0:ce33c63eefe5 46 //local address
Kojto 0:ce33c63eefe5 47 sockaddr LocalAddr;
Kojto 0:ce33c63eefe5 48 socklen_t addrlen;
Kojto 0:ce33c63eefe5 49 memset(&LocalAddr, 0, 8);
Kojto 0:ce33c63eefe5 50 LocalAddr.family = AF_INET;
Kojto 0:ce33c63eefe5 51 LocalAddr.data[0] = (port >> 8) & 0xff;
Kojto 0:ce33c63eefe5 52 LocalAddr.data[1] = port & 0xff;
Kojto 0:ce33c63eefe5 53 memset (&LocalAddr.data[2], 0, 4);
Kojto 0:ce33c63eefe5 54 sock = wigo._socket.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Kojto 0:ce33c63eefe5 55 while(sock == -1) sock = wigo._socket.socket(AF_INET, SOCK_DGRAM, IPPROTO_TCP);
Kojto 0:ce33c63eefe5 56 wigo._socket.bind(sock,&LocalAddr,sizeof(sockaddr));
Kojto 0:ce33c63eefe5 57 wigo._socket.listen(sock, 1);
Kojto 0:ce33c63eefe5 58 addrlen = sizeof(destAddr);
Kojto 0:ce33c63eefe5 59 while(1)
Kojto 0:ce33c63eefe5 60 {
Kojto 0:ce33c63eefe5 61 newsock = -2;
Kojto 0:ce33c63eefe5 62 printf("Server waiting for connection to Python\n");
Kojto 0:ce33c63eefe5 63 LED_D2_ON;
Kojto 0:ce33c63eefe5 64 while((newsock == -1) || (newsock == -2))
Kojto 0:ce33c63eefe5 65 {
Kojto 0:ce33c63eefe5 66 newsock = wigo._socket.accept(sock,&destAddr, &addrlen);
Kojto 0:ce33c63eefe5 67 }
Kojto 0:ce33c63eefe5 68 printf("Connected\n");
Kojto 0:ce33c63eefe5 69 //receive TCP data
Kojto 0:ce33c63eefe5 70 if(newsock >= 0)
Kojto 0:ce33c63eefe5 71 {
Kojto 0:ce33c63eefe5 72 wigo._socket.recv(newsock, requestBuffer,20,0);
Kojto 0:ce33c63eefe5 73 printf("Input = %s\n", requestBuffer);
Kojto 0:ce33c63eefe5 74 stat = -2;
Kojto 0:ce33c63eefe5 75 stat = wigo._socket.send(newsock, python_msg, strlen(python_msg), 0);
Kojto 0:ce33c63eefe5 76 printf("status= %d\n", stat);
Kojto 0:ce33c63eefe5 77 LED_D2_OFF;
Kojto 0:ce33c63eefe5 78 } else printf("bad socket= %d\n", newsock);
Kojto 0:ce33c63eefe5 79 wigo._socket.closesocket(newsock);
Kojto 0:ce33c63eefe5 80 printf("Done, press any key to repeat\n");
Kojto 0:ce33c63eefe5 81 getchar();
Kojto 0:ce33c63eefe5 82 // printf("\x1B[2J"); //VT100 erase screen
Kojto 0:ce33c63eefe5 83 // printf("\x1B[H"); //VT100 home
Kojto 0:ce33c63eefe5 84 }
Kojto 0:ce33c63eefe5 85 }
Kojto 0:ce33c63eefe5 86
Kojto 0:ce33c63eefe5 87 void start_smart_config() {
Kojto 0:ce33c63eefe5 88 wigo.start_smart_config(smartconfigkey);
Kojto 0:ce33c63eefe5 89 }
Kojto 0:ce33c63eefe5 90
Kojto 0:ce33c63eefe5 91 void init_tcp(void)
Kojto 0:ce33c63eefe5 92 {
Kojto 0:ce33c63eefe5 93 int t;
Kojto 0:ce33c63eefe5 94 tNetappIpconfigRetArgs ipinfo2;
Kojto 0:ce33c63eefe5 95
Kojto 0:ce33c63eefe5 96 check_dhcp();
Kojto 0:ce33c63eefe5 97 //while(checkWiFiConnected() == false);
Kojto 0:ce33c63eefe5 98 // If connectivity is good, run the primary functionality
Kojto 0:ce33c63eefe5 99 // while(1)
Kojto 0:ce33c63eefe5 100 // {
Kojto 0:ce33c63eefe5 101 // if (checkWiFiConnected()) break;
Kojto 0:ce33c63eefe5 102 // wait(1);
Kojto 0:ce33c63eefe5 103 // }
Kojto 0:ce33c63eefe5 104 // printf("Connected\n");
Kojto 0:ce33c63eefe5 105 // If we're not blocked by accept or others, obtain the latest status
Kojto 0:ce33c63eefe5 106 wigo.get_ip_config(&ipinfo2); // data is returned in the ipinfo2 structure
Kojto 0:ce33c63eefe5 107 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 0:ce33c63eefe5 108 LED_D3_ON;
Kojto 0:ce33c63eefe5 109
Kojto 0:ce33c63eefe5 110 t = wigo._socket.mdnsAdvertiser(1, DevServname, sizeof(DevServname));
Kojto 0:ce33c63eefe5 111 printf("mDNS Status= %x\n", t);
Kojto 0:ce33c63eefe5 112 }
Kojto 0:ce33c63eefe5 113
Kojto 0:ce33c63eefe5 114 void run_tcp_server(void)
Kojto 0:ce33c63eefe5 115 {
Kojto 0:ce33c63eefe5 116 LED_D3_OFF;
Kojto 0:ce33c63eefe5 117 LED_D2_OFF;
Kojto 0:ce33c63eefe5 118 printf("\n\nStarting TCP/IP Server\n");
Kojto 0:ce33c63eefe5 119 init_tcp();
Kojto 0:ce33c63eefe5 120 create_tcp_connection(TCPIP_PORT);
Kojto 0:ce33c63eefe5 121 }
Kojto 0:ce33c63eefe5 122
Kojto 0:ce33c63eefe5 123 bool check_dhcp(void)
Kojto 0:ce33c63eefe5 124 {
Kojto 0:ce33c63eefe5 125 int32_t t;
Kojto 0:ce33c63eefe5 126 //static uint8_t obtainIpInfoFlag = FALSE;
Kojto 0:ce33c63eefe5 127
Kojto 0:ce33c63eefe5 128 // Wait until for dhcp
Kojto 0:ce33c63eefe5 129 while (wigo.is_dhcp_configured() == false)
Kojto 0:ce33c63eefe5 130 {
Kojto 0:ce33c63eefe5 131 wait_ms(500);
Kojto 0:ce33c63eefe5 132 printf("waiting\n");
Kojto 0:ce33c63eefe5 133 }
Kojto 0:ce33c63eefe5 134
Kojto 0:ce33c63eefe5 135 // Check if we are in a connected state. If so, set flags and LED
Kojto 0:ce33c63eefe5 136 //if (wigo.is_connected() == true)
Kojto 0:ce33c63eefe5 137 //{
Kojto 0:ce33c63eefe5 138 //if (obtainIpInfoFlag == FALSE)
Kojto 0:ce33c63eefe5 139 //{
Kojto 0:ce33c63eefe5 140 // obtainIpInfoFlag = TRUE; // Set flag so we don't constantly turn the LED on
Kojto 0:ce33c63eefe5 141 //LED_D3_ON;
Kojto 0:ce33c63eefe5 142 //If Smart Config was performed, we need to send complete notification to the configure (Smart Phone App)
Kojto 0:ce33c63eefe5 143 // if (ConnectUsingSmartConfig==1)
Kojto 0:ce33c63eefe5 144 // {
Kojto 0:ce33c63eefe5 145 // ConnectUsingSmartConfig = 0;
Kojto 0:ce33c63eefe5 146 // SmartConfigProfilestored = SMART_CONFIG_SET;
Kojto 0:ce33c63eefe5 147 // }
Kojto 0:ce33c63eefe5 148
Kojto 0:ce33c63eefe5 149 //}
Kojto 0:ce33c63eefe5 150 t = wigo._socket.mdnsAdvertiser(1, DevServname, sizeof(DevServname));
Kojto 0:ce33c63eefe5 151 printf("mDNS Status= %x\n", t);
Kojto 0:ce33c63eefe5 152 return true;
Kojto 0:ce33c63eefe5 153 //}
Kojto 0:ce33c63eefe5 154 //return FALSE;
Kojto 0:ce33c63eefe5 155 }
Kojto 0:ce33c63eefe5 156
Kojto 0:ce33c63eefe5 157 void do_FTC(void)
Kojto 0:ce33c63eefe5 158 {
Kojto 0:ce33c63eefe5 159 printf("Running First Time Configuration\n");
Kojto 0:ce33c63eefe5 160 // server_running = 1;
Kojto 0:ce33c63eefe5 161 //runSmartConfig = 1;
Kojto 0:ce33c63eefe5 162 start_smart_config();
Kojto 0:ce33c63eefe5 163 init_tcp();
Kojto 0:ce33c63eefe5 164 RED_OFF;
Kojto 0:ce33c63eefe5 165 GREEN_OFF;
Kojto 0:ce33c63eefe5 166 BLUE_OFF;
Kojto 0:ce33c63eefe5 167 userFS.FTC = 1;
Kojto 0:ce33c63eefe5 168 wigo._nvmem.write( NVMEM_USER_FILE_1_FILEID, sizeof(userFS), 0, (uint8_t *) &userFS);
Kojto 0:ce33c63eefe5 169 //runSmartConfig = 0;
Kojto 0:ce33c63eefe5 170 SmartConfigProfilestored = SMART_CONFIG_SET;
Kojto 0:ce33c63eefe5 171 wigo._wlan.stop();
Kojto 0:ce33c63eefe5 172 printf("FTC finished\n");
Kojto 0:ce33c63eefe5 173 }
Kojto 0:ce33c63eefe5 174
Kojto 0:ce33c63eefe5 175
Kojto 0:ce33c63eefe5 176