![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Demo apps : receive a string from a client and respond with a different string, TCP/IP client
Dependencies: CC3000_Hostdriver mbed
Note
Avnet Wi-Go board
For those using Avnet's Wi-Go board, there also is a full IOT demo available at
http://mbed.org/users/frankvnk/code/Wi-Go_IOT_Demo/
New cc3000 HostDriver release
For new projects, use cc3000 mbed socket compatible API driver and examples
Info
Demo application for testing the wireless CC3000 module on the Wi-Go board.
Warning
The on-board Firmware must be updated to mbed enable a Wi-Go system.
Goto the Component page to get the FirmwareUpdate tool (scroll down to the FirmwareUpdate topic).
Setup
Note
It is recommended to run initial tests WITHOUT security settings.
- Setup a wireless router with a non-secured wireless connection using the wireless settings stored in
doTCPIP.h
.
- Alternatively, these settings can be altered to match the wireless router settings (SSID, security and static IP parameters).
When the unsecure test works,AP_KEY
andAP_SECURITY
can be enabled and set to your preferred values.
Valid values forAP_SECURITY
are :NONE
,WEP
,WPA
andWPA2
// Modify the following settings as necessary for your Wi-Fi Network setup: #define IP_ALLOC_METHOD USE_DHCP // for DHCP assigned IP address //#define IP_ALLOC_METHOD USE_STATIC_IP // for static IP address // Default SSID Settings //#define AP_KEY "thisthis" //#define AP_SECURITY WPA2 // WPA2 must be enabled for use with iPhone or Android phone hotspot! #define SSID "iot" #define STATIC_IP_OCT1 192 #define STATIC_IP_OCT2 168 #define STATIC_IP_OCT3 0 #define STATIC_IP_OCT4 10 #define STATIC_GW_OCT4 1 // Static Gateway address = STATIC_IP_OCT1.STATIC_IP_OCT2.STATIC_IP_OCT3.STATIC_GW_OCT4
- Download Python 2.7 from http://www.python.org/download/
Install it on a computer able to make a wireless connection to the router we previously set up.
- Make a wireless connection between your computer and the router.
- Download this Python script to the Python2.7 folder (credit : Jim Carver from Avnet).
- Import the
CC3000_Simple_Socket
code into your compiler and save it to the Wi-Go board.
Running the application for the first time
- Open a terminal program (eg: TeraTerm) and connect to the Wi-Go module (serial speed : 115200 baud).
- Press the reset button on the Wi-Go module.
- Following startup screen will appear (the dots in the MAC address will show your CC3000's real MAC address):
CC3000 Python demo. Wi-Go MAC address ..:..:..:..:..:.. FTC 1 PP_version 3.3 SERV_PACK 1.11 DRV_VER 7.13.19 FW_VER 7.12.14 <0> Normal run. SmartConfig will start if no valid connection exists. <1> Connect using fixed SSID : iot <2> TCP/IP client: Discover public IP address. Get time and date from a daytime server in Italy. <9> SmartConfig.
- For the initial test, select option
<1>
(Connect using fixed SSID : ...). - If all goes well, the following screen is shown (the IP address and mDNS status can be different):
Starting TCP/IP Server RunSmartConfig= 0 Attempting SSID Connection waiting waiting waiting mDNS Status= 31be Connected *** Wi-Go board DHCP assigned IP Address = 192.168.0.101 mDNS Status= 3dbe Server waiting for connection to Python
- On the computer where you installed Python2.7:
- Make sure the wireless connection between your computer and the 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 Wi-Go board will send following info to the serial port:
Connected
When we press Enter
in the DOS window, the Wi-Go board will send following info to the serial port:
Input = Hello Wi-Go status= 13 Done, press any key to repeat
And the DOS window will show:
recv from : data: Hello Python
Application option <2> : TCP/IP client
This is a simple demo to discover a public IP address and get the date and time from a daytime server (port 13).
Using the application's options <0> or <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 settings stored in doTCPIP.h
.
As mentioned before, option <0>
will automatically start SmartConfig if no valid connection exists (First Time Config),
but if the CC3000 was previously configured using SmartConfig, it will automatically connect to the wireless network.
Option <9>
can be used to switch to another wireless connection.
See TI's pages on how to use the SmartConfig tool:
- Preferred method : Configuration using the SmartConfig tool
- SmartConfig download: Smart Config and Home Automation
- iOS app : available at Apple app store.
- Android app : download and install the Android SmartConfig Application on a PC.
This file contains the source code as well as the compiled APK file.
The APK file is stored inti\CC3000AndroidApp\SmartConfigCC3X\bin
.
- iOS app : available at Apple app store.
- Alternate method (no longer works since firmware upgrade 1.11 - only kept as a reference):
Configuration without the SmartConfig tool
This page also contains info on the First Time Config Probe and Beacon Definition
The Prefix can be set in cc3000.cpp. Do not change the default value for the prefix (TTT) when you want to use TI's Smartconfig application.
char aucCC3000_prefix[] = {'T', 'T', 'T'}; // Smart Config Prefix
The Device Name mentioned on the SmartConfig page is declared in doTCPIP.cpp
char DevServname[] = "CC3000";
doTCPIP.cpp@7:f13025166308, 2013-10-17 (annotated)
- Committer:
- frankvnk
- Date:
- Thu Oct 17 18:10:26 2013 +0000
- Revision:
- 7:f13025166308
- Parent:
- 6:4fb3776a9b92
HostDriver update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
frankvnk | 0:a8e46e27d041 | 1 | /**************************************************************************** |
frankvnk | 0:a8e46e27d041 | 2 | * |
frankvnk | 0:a8e46e27d041 | 3 | * doTCPIP.cpp - CC3000 TCP/IP |
frankvnk | 0:a8e46e27d041 | 4 | * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ |
frankvnk | 0:a8e46e27d041 | 5 | * |
frankvnk | 0:a8e46e27d041 | 6 | * Redistribution and use in source and binary forms, with or without |
frankvnk | 0:a8e46e27d041 | 7 | * modification, are permitted provided that the following conditions |
frankvnk | 0:a8e46e27d041 | 8 | * are met: |
frankvnk | 0:a8e46e27d041 | 9 | * |
frankvnk | 0:a8e46e27d041 | 10 | * Redistributions of source code must retain the above copyright |
frankvnk | 0:a8e46e27d041 | 11 | * notice, this list of conditions and the following disclaimer. |
frankvnk | 0:a8e46e27d041 | 12 | * |
frankvnk | 0:a8e46e27d041 | 13 | * Redistributions in binary form must reproduce the above copyright |
frankvnk | 0:a8e46e27d041 | 14 | * notice, this list of conditions and the following disclaimer in the |
frankvnk | 0:a8e46e27d041 | 15 | * documentation and/or other materials provided with the |
frankvnk | 0:a8e46e27d041 | 16 | * distribution. |
frankvnk | 0:a8e46e27d041 | 17 | * |
frankvnk | 0:a8e46e27d041 | 18 | * Neither the name of Texas Instruments Incorporated nor the names of |
frankvnk | 0:a8e46e27d041 | 19 | * its contributors may be used to endorse or promote products derived |
frankvnk | 0:a8e46e27d041 | 20 | * from this software without specific prior written permission. |
frankvnk | 0:a8e46e27d041 | 21 | * |
frankvnk | 0:a8e46e27d041 | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
frankvnk | 0:a8e46e27d041 | 23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
frankvnk | 0:a8e46e27d041 | 24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
frankvnk | 0:a8e46e27d041 | 25 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
frankvnk | 0:a8e46e27d041 | 26 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
frankvnk | 0:a8e46e27d041 | 27 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
frankvnk | 0:a8e46e27d041 | 28 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
frankvnk | 0:a8e46e27d041 | 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
frankvnk | 0:a8e46e27d041 | 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
frankvnk | 0:a8e46e27d041 | 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
frankvnk | 0:a8e46e27d041 | 32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
frankvnk | 0:a8e46e27d041 | 33 | * |
frankvnk | 0:a8e46e27d041 | 34 | *****************************************************************************/ |
frankvnk | 0:a8e46e27d041 | 35 | |
frankvnk | 0:a8e46e27d041 | 36 | #include "doTCPIP.h" |
frankvnk | 0:a8e46e27d041 | 37 | |
frankvnk | 0:a8e46e27d041 | 38 | volatile unsigned char newData; |
frankvnk | 6:4fb3776a9b92 | 39 | int server_running; |
frankvnk | 0:a8e46e27d041 | 40 | unsigned char ForceFixedSSID; |
frankvnk | 0:a8e46e27d041 | 41 | char runSmartConfig; // Flag indicating whether user requested to perform Smart Config |
frankvnk | 0:a8e46e27d041 | 42 | volatile unsigned long ulCC3000Connected; |
frankvnk | 0:a8e46e27d041 | 43 | unsigned char ConnectUsingSmartConfig; |
frankvnk | 0:a8e46e27d041 | 44 | unsigned char myMAC[8]; |
frankvnk | 0:a8e46e27d041 | 45 | userFS_t userFS; |
frankvnk | 6:4fb3776a9b92 | 46 | int do_mDNS = 0; |
frankvnk | 0:a8e46e27d041 | 47 | |
frankvnk | 0:a8e46e27d041 | 48 | // Setup the functions to handle our CGI parameters |
frankvnk | 0:a8e46e27d041 | 49 | tNetappIpconfigRetArgs ipinfo2; |
frankvnk | 0:a8e46e27d041 | 50 | char requestBuffer[REQ_BUFFER_SIZE]; |
frankvnk | 0:a8e46e27d041 | 51 | int LAN_Connected = 0; |
frankvnk | 0:a8e46e27d041 | 52 | |
frankvnk | 0:a8e46e27d041 | 53 | unsigned char SmartConfigProfilestored = 0xff; |
frankvnk | 0:a8e46e27d041 | 54 | |
frankvnk | 0:a8e46e27d041 | 55 | |
frankvnk | 0:a8e46e27d041 | 56 | /** \brief Flag indicating whether to print CC3000 Connection info */ |
frankvnk | 0:a8e46e27d041 | 57 | static unsigned char obtainIpInfoFlag = FALSE; |
frankvnk | 0:a8e46e27d041 | 58 | //Device name - used for Smart config in order to stop the Smart phone configuration process |
frankvnk | 0:a8e46e27d041 | 59 | char DevServname[] = "CC3000"; |
frankvnk | 0:a8e46e27d041 | 60 | volatile unsigned long SendmDNSAdvertisment; |
frankvnk | 0:a8e46e27d041 | 61 | |
frankvnk | 6:4fb3776a9b92 | 62 | /*int sendTCPIP(int port) |
frankvnk | 6:4fb3776a9b92 | 63 | { |
frankvnk | 6:4fb3776a9b92 | 64 | long temp, stat, skip, skipc; |
frankvnk | 6:4fb3776a9b92 | 65 | //socket descriptor |
frankvnk | 6:4fb3776a9b92 | 66 | long sock; |
frankvnk | 6:4fb3776a9b92 | 67 | //new TCP socket descriptor |
frankvnk | 6:4fb3776a9b92 | 68 | long newsock; |
frankvnk | 6:4fb3776a9b92 | 69 | //destination address |
frankvnk | 6:4fb3776a9b92 | 70 | sockaddr destAddr; |
frankvnk | 6:4fb3776a9b92 | 71 | //local address |
frankvnk | 6:4fb3776a9b92 | 72 | sockaddr LocalAddr; |
frankvnk | 0:a8e46e27d041 | 73 | |
frankvnk | 6:4fb3776a9b92 | 74 | // char buf2[40]; |
frankvnk | 6:4fb3776a9b92 | 75 | socklen_t addrlen; |
frankvnk | 6:4fb3776a9b92 | 76 | memset(&LocalAddr, 0, 8); |
frankvnk | 6:4fb3776a9b92 | 77 | LocalAddr.sa_family = AF_INET; |
frankvnk | 6:4fb3776a9b92 | 78 | LocalAddr.sa_data[0] = (port >> 8) & 0xff; |
frankvnk | 6:4fb3776a9b92 | 79 | LocalAddr.sa_data[1] = port & 0xff; |
frankvnk | 6:4fb3776a9b92 | 80 | memset (&LocalAddr.sa_data[2], 0, 4); |
frankvnk | 6:4fb3776a9b92 | 81 | sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
frankvnk | 6:4fb3776a9b92 | 82 | while(sock == -1) sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_TCP); |
frankvnk | 6:4fb3776a9b92 | 83 | temp = bind(sock,&LocalAddr,sizeof(sockaddr)); |
frankvnk | 6:4fb3776a9b92 | 84 | temp = listen(sock, 1); |
frankvnk | 6:4fb3776a9b92 | 85 | addrlen = sizeof(destAddr); |
frankvnk | 6:4fb3776a9b92 | 86 | skip = 0; |
frankvnk | 6:4fb3776a9b92 | 87 | skipc = 0; |
frankvnk | 6:4fb3776a9b92 | 88 | while(1) |
frankvnk | 6:4fb3776a9b92 | 89 | { |
frankvnk | 6:4fb3776a9b92 | 90 | newsock = -2; |
frankvnk | 6:4fb3776a9b92 | 91 | printf("Server waiting for connection\n"); |
frankvnk | 6:4fb3776a9b92 | 92 | LED_D2_ON; |
frankvnk | 6:4fb3776a9b92 | 93 | while((newsock == -1) || (newsock == -2)) |
frankvnk | 6:4fb3776a9b92 | 94 | { |
frankvnk | 6:4fb3776a9b92 | 95 | newsock = accept(sock,&destAddr, &addrlen); |
frankvnk | 6:4fb3776a9b92 | 96 | if(do_mDNS) |
frankvnk | 6:4fb3776a9b92 | 97 | { |
frankvnk | 6:4fb3776a9b92 | 98 | printf("mDNS= 0x%08x\n", mdnsAdvertiser(1, DevServname, sizeof(DevServname))); |
frankvnk | 6:4fb3776a9b92 | 99 | do_mDNS = 0; |
frankvnk | 6:4fb3776a9b92 | 100 | } |
frankvnk | 6:4fb3776a9b92 | 101 | } |
frankvnk | 6:4fb3776a9b92 | 102 | printf("Connected\n"); |
frankvnk | 6:4fb3776a9b92 | 103 | // sss = seconds; |
frankvnk | 6:4fb3776a9b92 | 104 | //receive TCP data |
frankvnk | 6:4fb3776a9b92 | 105 | temp = 0; |
frankvnk | 6:4fb3776a9b92 | 106 | if(newsock >= 0) |
frankvnk | 6:4fb3776a9b92 | 107 | { |
frankvnk | 6:4fb3776a9b92 | 108 | recv(newsock, requestBuffer,20,0); |
frankvnk | 6:4fb3776a9b92 | 109 | printf("Input = %s\n", requestBuffer); |
frankvnk | 6:4fb3776a9b92 | 110 | while(1) |
frankvnk | 6:4fb3776a9b92 | 111 | { |
frankvnk | 6:4fb3776a9b92 | 112 | while(!newData) __wfi(); |
frankvnk | 6:4fb3776a9b92 | 113 | newData = 0; |
frankvnk | 6:4fb3776a9b92 | 114 | LED_D2_ON; |
frankvnk | 6:4fb3776a9b92 | 115 | stat = -2; |
frankvnk | 6:4fb3776a9b92 | 116 | //if(tSLInformation.usNumberOfFreeBuffers <= 2) |
frankvnk | 6:4fb3776a9b92 | 117 | //{ |
frankvnk | 6:4fb3776a9b92 | 118 | // printf("LOW= %d\n", tSLInformation.usNumberOfFreeBuffers); |
frankvnk | 6:4fb3776a9b92 | 119 | //} |
frankvnk | 6:4fb3776a9b92 | 120 | //Disable sys tick interrupts for debugging |
frankvnk | 6:4fb3776a9b92 | 121 | //SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk; // *** Disable SysTick Timer Interrupt |
frankvnk | 6:4fb3776a9b92 | 122 | // stat = send(newsock,&axis6, sizeof(axis6), 0); |
frankvnk | 6:4fb3776a9b92 | 123 | // Disable sys tick interrupts for debugging |
frankvnk | 6:4fb3776a9b92 | 124 | //SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; // *** Enable SysTick Timer Interrupt |
frankvnk | 6:4fb3776a9b92 | 125 | LED_D2_OFF; |
frankvnk | 6:4fb3776a9b92 | 126 | if(stat != 96) |
frankvnk | 6:4fb3776a9b92 | 127 | { |
frankvnk | 6:4fb3776a9b92 | 128 | if(stat == -2) |
frankvnk | 6:4fb3776a9b92 | 129 | { |
frankvnk | 6:4fb3776a9b92 | 130 | skip++; |
frankvnk | 6:4fb3776a9b92 | 131 | skipc++; |
frankvnk | 6:4fb3776a9b92 | 132 | } |
frankvnk | 6:4fb3776a9b92 | 133 | //printf("status= %d\n", stat); |
frankvnk | 6:4fb3776a9b92 | 134 | if(stat == -1) break; |
frankvnk | 6:4fb3776a9b92 | 135 | } |
frankvnk | 6:4fb3776a9b92 | 136 | else |
frankvnk | 6:4fb3776a9b92 | 137 | { |
frankvnk | 6:4fb3776a9b92 | 138 | temp++; |
frankvnk | 6:4fb3776a9b92 | 139 | skipc = 0; |
frankvnk | 6:4fb3776a9b92 | 140 | } |
frankvnk | 6:4fb3776a9b92 | 141 | if(skipc > 150) |
frankvnk | 6:4fb3776a9b92 | 142 | { |
frankvnk | 6:4fb3776a9b92 | 143 | // printf("Zero Buffer Error Sent\n", temp, seconds - sss); |
frankvnk | 6:4fb3776a9b92 | 144 | return(-1); |
frankvnk | 6:4fb3776a9b92 | 145 | } |
frankvnk | 6:4fb3776a9b92 | 146 | } |
frankvnk | 6:4fb3776a9b92 | 147 | } else printf("bad socket= %d\n", newsock); |
frankvnk | 6:4fb3776a9b92 | 148 | closesocket(newsock); |
frankvnk | 6:4fb3776a9b92 | 149 | // printf("Done %d, time= %d, skipped= %d\n", temp, seconds - sss, skip); |
frankvnk | 6:4fb3776a9b92 | 150 | skip = 0; |
frankvnk | 6:4fb3776a9b92 | 151 | } |
frankvnk | 6:4fb3776a9b92 | 152 | }*/ |
frankvnk | 0:a8e46e27d041 | 153 | |
frankvnk | 0:a8e46e27d041 | 154 | void sendPython(int port) |
frankvnk | 0:a8e46e27d041 | 155 | { |
frankvnk | 0:a8e46e27d041 | 156 | char python_msg[] = "Hello Python\n"; |
frankvnk | 0:a8e46e27d041 | 157 | int stat; |
frankvnk | 0:a8e46e27d041 | 158 | long sock; |
frankvnk | 0:a8e46e27d041 | 159 | //new TCP socket descriptor |
frankvnk | 0:a8e46e27d041 | 160 | long newsock; |
frankvnk | 0:a8e46e27d041 | 161 | //destination address |
frankvnk | 0:a8e46e27d041 | 162 | sockaddr destAddr; |
frankvnk | 0:a8e46e27d041 | 163 | //local address |
frankvnk | 0:a8e46e27d041 | 164 | sockaddr LocalAddr; |
frankvnk | 0:a8e46e27d041 | 165 | socklen_t addrlen; |
frankvnk | 0:a8e46e27d041 | 166 | memset(&LocalAddr, 0, 8); |
frankvnk | 0:a8e46e27d041 | 167 | LocalAddr.sa_family = AF_INET; |
frankvnk | 0:a8e46e27d041 | 168 | LocalAddr.sa_data[0] = (port >> 8) & 0xff; |
frankvnk | 0:a8e46e27d041 | 169 | LocalAddr.sa_data[1] = port & 0xff; |
frankvnk | 0:a8e46e27d041 | 170 | memset (&LocalAddr.sa_data[2], 0, 4); |
frankvnk | 0:a8e46e27d041 | 171 | sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
frankvnk | 0:a8e46e27d041 | 172 | while(sock == -1) sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_TCP); |
frankvnk | 0:a8e46e27d041 | 173 | bind(sock,&LocalAddr,sizeof(sockaddr)); |
frankvnk | 0:a8e46e27d041 | 174 | listen(sock, 1); |
frankvnk | 0:a8e46e27d041 | 175 | addrlen = sizeof(destAddr); |
frankvnk | 0:a8e46e27d041 | 176 | while(1) |
frankvnk | 0:a8e46e27d041 | 177 | { |
frankvnk | 0:a8e46e27d041 | 178 | newsock = -2; |
frankvnk | 2:13ced2cb5933 | 179 | printf("\nServer waiting for connection to Python\n"); |
frankvnk | 0:a8e46e27d041 | 180 | LED_D2_ON; |
frankvnk | 0:a8e46e27d041 | 181 | while((newsock == -1) || (newsock == -2)) |
frankvnk | 0:a8e46e27d041 | 182 | { |
frankvnk | 0:a8e46e27d041 | 183 | newsock = accept(sock,&destAddr, &addrlen); |
frankvnk | 0:a8e46e27d041 | 184 | } |
frankvnk | 0:a8e46e27d041 | 185 | printf("Connected\n"); |
frankvnk | 0:a8e46e27d041 | 186 | //receive TCP data |
frankvnk | 0:a8e46e27d041 | 187 | if(newsock >= 0) |
frankvnk | 0:a8e46e27d041 | 188 | { |
frankvnk | 2:13ced2cb5933 | 189 | stat = recv(newsock, requestBuffer,20,0); |
frankvnk | 2:13ced2cb5933 | 190 | if(stat > 0) |
frankvnk | 2:13ced2cb5933 | 191 | { |
frankvnk | 2:13ced2cb5933 | 192 | printf("Receive Status= %d, Input = %s\n", stat, requestBuffer); |
frankvnk | 2:13ced2cb5933 | 193 | stat = -2; |
frankvnk | 2:13ced2cb5933 | 194 | stat = send(newsock, python_msg, strlen(python_msg), 0); |
frankvnk | 2:13ced2cb5933 | 195 | printf("Send status= %d\n", stat); |
frankvnk | 2:13ced2cb5933 | 196 | LED_D2_OFF; |
frankvnk | 2:13ced2cb5933 | 197 | } |
frankvnk | 2:13ced2cb5933 | 198 | else |
frankvnk | 2:13ced2cb5933 | 199 | { |
frankvnk | 2:13ced2cb5933 | 200 | printf("ERROR %d", stat); |
frankvnk | 2:13ced2cb5933 | 201 | switch (stat) |
frankvnk | 2:13ced2cb5933 | 202 | { |
frankvnk | 2:13ced2cb5933 | 203 | case -1: |
frankvnk | 2:13ced2cb5933 | 204 | { |
frankvnk | 2:13ced2cb5933 | 205 | printf(": remote socket closed.\n"); |
frankvnk | 2:13ced2cb5933 | 206 | break; |
frankvnk | 2:13ced2cb5933 | 207 | } |
frankvnk | 2:13ced2cb5933 | 208 | case -2: |
frankvnk | 2:13ced2cb5933 | 209 | { |
frankvnk | 2:13ced2cb5933 | 210 | printf(": no buffers available.\n"); |
frankvnk | 2:13ced2cb5933 | 211 | break; |
frankvnk | 2:13ced2cb5933 | 212 | } |
frankvnk | 2:13ced2cb5933 | 213 | case -57: |
frankvnk | 2:13ced2cb5933 | 214 | { |
frankvnk | 2:13ced2cb5933 | 215 | printf(": timeout - no reply from remote.\n"); |
frankvnk | 2:13ced2cb5933 | 216 | break; |
frankvnk | 2:13ced2cb5933 | 217 | } |
frankvnk | 2:13ced2cb5933 | 218 | default: |
frankvnk | 2:13ced2cb5933 | 219 | printf("\n"); |
frankvnk | 2:13ced2cb5933 | 220 | } |
frankvnk | 2:13ced2cb5933 | 221 | } |
frankvnk | 0:a8e46e27d041 | 222 | } else printf("bad socket= %d\n", newsock); |
frankvnk | 0:a8e46e27d041 | 223 | closesocket(newsock); |
frankvnk | 0:a8e46e27d041 | 224 | printf("Done, press any key to repeat\n"); |
frankvnk | 0:a8e46e27d041 | 225 | getchar(); |
frankvnk | 0:a8e46e27d041 | 226 | } |
frankvnk | 0:a8e46e27d041 | 227 | } |
frankvnk | 0:a8e46e27d041 | 228 | |
frankvnk | 6:4fb3776a9b92 | 229 | int getTCPIP( char a1, char a2, char a3, char a4, int port) |
frankvnk | 6:4fb3776a9b92 | 230 | { |
frankvnk | 6:4fb3776a9b92 | 231 | long temp; |
frankvnk | 6:4fb3776a9b92 | 232 | //socket descriptor |
frankvnk | 6:4fb3776a9b92 | 233 | long sock; |
frankvnk | 6:4fb3776a9b92 | 234 | char msg1[] = "GET / HTTP/1.1\r\n\r\n\0"; |
frankvnk | 6:4fb3776a9b92 | 235 | //destination address |
frankvnk | 6:4fb3776a9b92 | 236 | sockaddr destAddr; |
frankvnk | 6:4fb3776a9b92 | 237 | //num of bytes received |
frankvnk | 6:4fb3776a9b92 | 238 | long numofbytes = 0; |
frankvnk | 6:4fb3776a9b92 | 239 | //data buffer |
frankvnk | 6:4fb3776a9b92 | 240 | char buff[256]; |
frankvnk | 6:4fb3776a9b92 | 241 | |
frankvnk | 6:4fb3776a9b92 | 242 | memset(&destAddr, 0, 8); |
frankvnk | 6:4fb3776a9b92 | 243 | destAddr.sa_family = AF_INET; |
frankvnk | 6:4fb3776a9b92 | 244 | destAddr.sa_data[0] = (port >> 8) & 0xff; |
frankvnk | 6:4fb3776a9b92 | 245 | destAddr.sa_data[1] = port & 0xff; |
frankvnk | 6:4fb3776a9b92 | 246 | destAddr.sa_data[2] = a1; |
frankvnk | 6:4fb3776a9b92 | 247 | destAddr.sa_data[3] = a2; |
frankvnk | 6:4fb3776a9b92 | 248 | destAddr.sa_data[4] = a3; |
frankvnk | 6:4fb3776a9b92 | 249 | destAddr.sa_data[5] = a4; |
frankvnk | 6:4fb3776a9b92 | 250 | |
frankvnk | 6:4fb3776a9b92 | 251 | //open socket |
frankvnk | 6:4fb3776a9b92 | 252 | sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
frankvnk | 6:4fb3776a9b92 | 253 | while(sock == -1) sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_TCP); |
frankvnk | 6:4fb3776a9b92 | 254 | |
frankvnk | 6:4fb3776a9b92 | 255 | //hci_unsolicited_event_handler(); |
frankvnk | 6:4fb3776a9b92 | 256 | |
frankvnk | 6:4fb3776a9b92 | 257 | printf( "Client attempting connection\n"); |
frankvnk | 6:4fb3776a9b92 | 258 | if( connect(sock, &destAddr, sizeof(destAddr)) < 0) |
frankvnk | 6:4fb3776a9b92 | 259 | return(-1); |
frankvnk | 6:4fb3776a9b92 | 260 | |
frankvnk | 6:4fb3776a9b92 | 261 | printf("Connected\n"); |
frankvnk | 6:4fb3776a9b92 | 262 | |
frankvnk | 6:4fb3776a9b92 | 263 | //receive TCP data |
frankvnk | 6:4fb3776a9b92 | 264 | temp = 0; |
frankvnk | 6:4fb3776a9b92 | 265 | //hci_unsolicited_event_handler(); |
frankvnk | 6:4fb3776a9b92 | 266 | |
frankvnk | 6:4fb3776a9b92 | 267 | send(sock, &msg1, strlen(msg1), 0); |
frankvnk | 6:4fb3776a9b92 | 268 | printf("Receiving Data\n"); |
frankvnk | 6:4fb3776a9b92 | 269 | |
frankvnk | 6:4fb3776a9b92 | 270 | //printf("Input = %s\n", msg1); |
frankvnk | 6:4fb3776a9b92 | 271 | while(1) { |
frankvnk | 6:4fb3776a9b92 | 272 | numofbytes = recv(sock, &buff, 256,0); |
frankvnk | 6:4fb3776a9b92 | 273 | |
frankvnk | 6:4fb3776a9b92 | 274 | //printf("R %d\n", numofbytes); |
frankvnk | 6:4fb3776a9b92 | 275 | newData = 1; |
frankvnk | 6:4fb3776a9b92 | 276 | if(numofbytes != -1) { |
frankvnk | 6:4fb3776a9b92 | 277 | closesocket(sock); |
frankvnk | 6:4fb3776a9b92 | 278 | printf("Done [%d]\n", temp); |
frankvnk | 6:4fb3776a9b92 | 279 | printf("Data: %s\n", buff); |
frankvnk | 6:4fb3776a9b92 | 280 | |
frankvnk | 6:4fb3776a9b92 | 281 | //hci_unsolicited_event_handler(); |
frankvnk | 6:4fb3776a9b92 | 282 | |
frankvnk | 6:4fb3776a9b92 | 283 | return(1); |
frankvnk | 6:4fb3776a9b92 | 284 | } |
frankvnk | 6:4fb3776a9b92 | 285 | temp++; |
frankvnk | 6:4fb3776a9b92 | 286 | } |
frankvnk | 6:4fb3776a9b92 | 287 | |
frankvnk | 6:4fb3776a9b92 | 288 | return(0); // We'll never get here |
frankvnk | 6:4fb3776a9b92 | 289 | } |
frankvnk | 6:4fb3776a9b92 | 290 | |
frankvnk | 0:a8e46e27d041 | 291 | void initTCPIP(void) |
frankvnk | 0:a8e46e27d041 | 292 | { |
frankvnk | 0:a8e46e27d041 | 293 | int t; |
frankvnk | 0:a8e46e27d041 | 294 | LAN_Connected = 0; |
frankvnk | 0:a8e46e27d041 | 295 | // Start CC3000 State Machine |
frankvnk | 0:a8e46e27d041 | 296 | resetCC3000StateMachine(); |
frankvnk | 0:a8e46e27d041 | 297 | ulCC3000DHCP = 0; |
frankvnk | 0:a8e46e27d041 | 298 | ulCC3000Connected = 0; |
frankvnk | 0:a8e46e27d041 | 299 | // Initialize Board and CC3000 |
frankvnk | 0:a8e46e27d041 | 300 | initDriver(); |
frankvnk | 0:a8e46e27d041 | 301 | printf("RunSmartConfig= %d\n", runSmartConfig); |
frankvnk | 0:a8e46e27d041 | 302 | if(runSmartConfig == 1 ) |
frankvnk | 0:a8e46e27d041 | 303 | { |
frankvnk | 0:a8e46e27d041 | 304 | // Clear flag |
frankvnk | 0:a8e46e27d041 | 305 | //ClearFTCflag(); |
frankvnk | 0:a8e46e27d041 | 306 | unsetCC3000MachineState(CC3000_ASSOC); |
frankvnk | 0:a8e46e27d041 | 307 | // Start the Smart Config Process |
frankvnk | 0:a8e46e27d041 | 308 | StartSmartConfig(); |
frankvnk | 0:a8e46e27d041 | 309 | runSmartConfig = 0; |
frankvnk | 0:a8e46e27d041 | 310 | } |
frankvnk | 0:a8e46e27d041 | 311 | // If connectivity is good, run the primary functionality |
frankvnk | 0:a8e46e27d041 | 312 | while(1) |
frankvnk | 0:a8e46e27d041 | 313 | { |
frankvnk | 0:a8e46e27d041 | 314 | if(checkWiFiConnected()) break; |
frankvnk | 0:a8e46e27d041 | 315 | wait(1); |
frankvnk | 0:a8e46e27d041 | 316 | } |
frankvnk | 0:a8e46e27d041 | 317 | printf("Connected\n"); |
frankvnk | 0:a8e46e27d041 | 318 | if(!(currentCC3000State() & CC3000_SERVER_INIT)) |
frankvnk | 0:a8e46e27d041 | 319 | { |
frankvnk | 0:a8e46e27d041 | 320 | // If we're not blocked by accept or others, obtain the latest status |
frankvnk | 0:a8e46e27d041 | 321 | netapp_ipconfig(&ipinfo2); // data is returned in the ipinfo2 structure |
frankvnk | 0:a8e46e27d041 | 322 | } |
frankvnk | 0:a8e46e27d041 | 323 | 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]); |
frankvnk | 0:a8e46e27d041 | 324 | LED_D3_ON; |
frankvnk | 0:a8e46e27d041 | 325 | LAN_Connected = 1; |
frankvnk | 0:a8e46e27d041 | 326 | t = mdnsAdvertiser(1, DevServname, sizeof(DevServname)); |
frankvnk | 0:a8e46e27d041 | 327 | printf("mDNS Status= %x\n", t); |
frankvnk | 0:a8e46e27d041 | 328 | } |
frankvnk | 0:a8e46e27d041 | 329 | |
frankvnk | 0:a8e46e27d041 | 330 | void runTCPIPserver(void) |
frankvnk | 0:a8e46e27d041 | 331 | { |
frankvnk | 0:a8e46e27d041 | 332 | while(1) |
frankvnk | 0:a8e46e27d041 | 333 | { |
frankvnk | 0:a8e46e27d041 | 334 | LED_D3_OFF; |
frankvnk | 0:a8e46e27d041 | 335 | LAN_Connected = 0; |
frankvnk | 0:a8e46e27d041 | 336 | LED_D2_OFF; |
frankvnk | 0:a8e46e27d041 | 337 | printf("\n\nStarting TCP/IP Server\n"); |
frankvnk | 0:a8e46e27d041 | 338 | initTCPIP(); |
frankvnk | 0:a8e46e27d041 | 339 | sendPython(TCPIP_PORT); |
frankvnk | 6:4fb3776a9b92 | 340 | // sendTCPIP(TCPIP_PORT); |
frankvnk | 0:a8e46e27d041 | 341 | } |
frankvnk | 0:a8e46e27d041 | 342 | } |
frankvnk | 6:4fb3776a9b92 | 343 | |
frankvnk | 6:4fb3776a9b92 | 344 | /*void runTCPIPclient(int a1, int a2, int a3, int a4) |
frankvnk | 6:4fb3776a9b92 | 345 | { |
frankvnk | 6:4fb3776a9b92 | 346 | while(1) |
frankvnk | 6:4fb3776a9b92 | 347 | { |
frankvnk | 6:4fb3776a9b92 | 348 | LED_D3_OFF; |
frankvnk | 6:4fb3776a9b92 | 349 | LED_D2_OFF; |
frankvnk | 6:4fb3776a9b92 | 350 | printf("\n\nStarting TCP/IP Client connection\n"); |
frankvnk | 6:4fb3776a9b92 | 351 | initTCPIP(); |
frankvnk | 6:4fb3776a9b92 | 352 | getTCPIP( a1, a2, a3, a4, 80); // IP Address and Port of server |
frankvnk | 6:4fb3776a9b92 | 353 | } |
frankvnk | 6:4fb3776a9b92 | 354 | }*/ |
frankvnk | 6:4fb3776a9b92 | 355 | |
frankvnk | 6:4fb3776a9b92 | 356 | void runTCPIPclient(int a1, int a2, int a3, int a4, int port) |
frankvnk | 6:4fb3776a9b92 | 357 | { |
frankvnk | 6:4fb3776a9b92 | 358 | int res; |
frankvnk | 6:4fb3776a9b92 | 359 | int i; |
frankvnk | 6:4fb3776a9b92 | 360 | |
frankvnk | 6:4fb3776a9b92 | 361 | LED_D3_OFF; |
frankvnk | 6:4fb3776a9b92 | 362 | // LAN_Connected = 0; |
frankvnk | 6:4fb3776a9b92 | 363 | LED_D2_OFF; |
frankvnk | 6:4fb3776a9b92 | 364 | printf("\n\nStarting TCP/IP Client connection (3 attempts)\n"); |
frankvnk | 6:4fb3776a9b92 | 365 | // |
frankvnk | 6:4fb3776a9b92 | 366 | if ( LAN_Connected == 0) |
frankvnk | 6:4fb3776a9b92 | 367 | initTCPIP(); |
frankvnk | 6:4fb3776a9b92 | 368 | |
frankvnk | 6:4fb3776a9b92 | 369 | i=0; |
frankvnk | 6:4fb3776a9b92 | 370 | while( i<3) { |
frankvnk | 6:4fb3776a9b92 | 371 | res=getTCPIP( a1, a2, a3, a4, port); // IP Address and Port of server |
frankvnk | 6:4fb3776a9b92 | 372 | if ( res != 1) { |
frankvnk | 6:4fb3776a9b92 | 373 | printf("[%d] Error connecting to: %d.%d.%d.%d:%d\n", ++i, a1, a2, a3, a4, port); |
frankvnk | 6:4fb3776a9b92 | 374 | } else { |
frankvnk | 6:4fb3776a9b92 | 375 | printf("Receiving Done. \n"); |
frankvnk | 6:4fb3776a9b92 | 376 | wait(1); |
frankvnk | 6:4fb3776a9b92 | 377 | return; |
frankvnk | 6:4fb3776a9b92 | 378 | } |
frankvnk | 6:4fb3776a9b92 | 379 | wait(3); |
frankvnk | 6:4fb3776a9b92 | 380 | } |
frankvnk | 6:4fb3776a9b92 | 381 | } |
frankvnk | 0:a8e46e27d041 | 382 | |
frankvnk | 0:a8e46e27d041 | 383 | unsigned char checkWiFiConnected(void) |
frankvnk | 0:a8e46e27d041 | 384 | { |
frankvnk | 0:a8e46e27d041 | 385 | int t; |
frankvnk | 0:a8e46e27d041 | 386 | if(!(currentCC3000State() & CC3000_ASSOC)) //try to associate with an Access Point |
frankvnk | 0:a8e46e27d041 | 387 | { |
frankvnk | 0:a8e46e27d041 | 388 | // Check whether Smart Config was run previously. If it was, we |
frankvnk | 0:a8e46e27d041 | 389 | // use it to connect to an access point. Otherwise, we connect to the |
frankvnk | 0:a8e46e27d041 | 390 | // default. |
frankvnk | 0:a8e46e27d041 | 391 | if(((ConnectUsingSmartConfig==0)&&(SmartConfigProfilestored != SMART_CONFIG_SET)) || ForceFixedSSID) |
frankvnk | 0:a8e46e27d041 | 392 | { |
frankvnk | 0:a8e46e27d041 | 393 | // Smart Config not set, check whether we have an SSID |
frankvnk | 0:a8e46e27d041 | 394 | // from the assoc terminal command. If not, use fixed SSID. |
frankvnk | 0:a8e46e27d041 | 395 | printf("Attempting SSID Connection\n"); |
frankvnk | 0:a8e46e27d041 | 396 | ConnectUsingSSID(SSID); |
frankvnk | 0:a8e46e27d041 | 397 | } |
frankvnk | 0:a8e46e27d041 | 398 | //unsolicicted_events_timer_init(); |
frankvnk | 0:a8e46e27d041 | 399 | // Wait until connection is finished |
frankvnk | 0:a8e46e27d041 | 400 | while ((ulCC3000DHCP == 0) || (ulCC3000Connected == 0)) |
frankvnk | 0:a8e46e27d041 | 401 | { |
frankvnk | 0:a8e46e27d041 | 402 | wait_ms(500); |
frankvnk | 0:a8e46e27d041 | 403 | printf("waiting\n"); |
frankvnk | 0:a8e46e27d041 | 404 | } |
frankvnk | 0:a8e46e27d041 | 405 | } |
frankvnk | 0:a8e46e27d041 | 406 | // Check if we are in a connected state. If so, set flags and LED |
frankvnk | 0:a8e46e27d041 | 407 | if(ulCC3000Connected == 1) |
frankvnk | 0:a8e46e27d041 | 408 | { |
frankvnk | 0:a8e46e27d041 | 409 | if (obtainIpInfoFlag == FALSE) |
frankvnk | 0:a8e46e27d041 | 410 | { |
frankvnk | 0:a8e46e27d041 | 411 | obtainIpInfoFlag = TRUE; // Set flag so we don't constantly turn the LED on |
frankvnk | 0:a8e46e27d041 | 412 | LED_D3_ON; |
frankvnk | 0:a8e46e27d041 | 413 | } |
frankvnk | 0:a8e46e27d041 | 414 | if (obtainIpInfoFlag == TRUE) |
frankvnk | 0:a8e46e27d041 | 415 | { |
frankvnk | 0:a8e46e27d041 | 416 | //If Smart Config was performed, we need to send complete notification to the configure (Smart Phone App) |
frankvnk | 0:a8e46e27d041 | 417 | if (ConnectUsingSmartConfig==1) |
frankvnk | 0:a8e46e27d041 | 418 | { |
frankvnk | 0:a8e46e27d041 | 419 | ConnectUsingSmartConfig = 0; |
frankvnk | 0:a8e46e27d041 | 420 | SmartConfigProfilestored = SMART_CONFIG_SET; |
frankvnk | 0:a8e46e27d041 | 421 | } |
frankvnk | 0:a8e46e27d041 | 422 | |
frankvnk | 0:a8e46e27d041 | 423 | } |
frankvnk | 0:a8e46e27d041 | 424 | t = mdnsAdvertiser(1, DevServname, sizeof(DevServname)); |
frankvnk | 0:a8e46e27d041 | 425 | printf("mDNS Status= %x\n", t); |
frankvnk | 0:a8e46e27d041 | 426 | return TRUE; |
frankvnk | 0:a8e46e27d041 | 427 | } |
frankvnk | 0:a8e46e27d041 | 428 | return FALSE; |
frankvnk | 0:a8e46e27d041 | 429 | } |
frankvnk | 0:a8e46e27d041 | 430 | |
frankvnk | 0:a8e46e27d041 | 431 | void print_mac(void) |
frankvnk | 0:a8e46e27d041 | 432 | { |
frankvnk | 0:a8e46e27d041 | 433 | printf("\n\nWi-Go MAC address %02x:%02x:%02x:%02x:%02x:%02x\n\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]); |
frankvnk | 0:a8e46e27d041 | 434 | } |
frankvnk | 0:a8e46e27d041 | 435 | |
frankvnk | 0:a8e46e27d041 | 436 | void do_FTC(void) |
frankvnk | 0:a8e46e27d041 | 437 | { |
frankvnk | 0:a8e46e27d041 | 438 | printf("Running First Time Configuration\n"); |
frankvnk | 6:4fb3776a9b92 | 439 | server_running = 1; |
frankvnk | 0:a8e46e27d041 | 440 | runSmartConfig = 1; |
frankvnk | 0:a8e46e27d041 | 441 | initTCPIP(); |
frankvnk | 0:a8e46e27d041 | 442 | RED_OFF; |
frankvnk | 0:a8e46e27d041 | 443 | GREEN_OFF; |
frankvnk | 0:a8e46e27d041 | 444 | BLUE_OFF; |
frankvnk | 0:a8e46e27d041 | 445 | userFS.FTC = 1; |
frankvnk | 0:a8e46e27d041 | 446 | nvmem_write( NVMEM_USER_FILE_1_FILEID, sizeof(userFS), 0, (unsigned char *) &userFS); |
frankvnk | 0:a8e46e27d041 | 447 | runSmartConfig = 0; |
frankvnk | 0:a8e46e27d041 | 448 | SmartConfigProfilestored = SMART_CONFIG_SET; |
frankvnk | 0:a8e46e27d041 | 449 | wlan_stop(); |
frankvnk | 0:a8e46e27d041 | 450 | printf("FTC finished\n"); |
frankvnk | 0:a8e46e27d041 | 451 | } |
frankvnk | 0:a8e46e27d041 | 452 | |
frankvnk | 0:a8e46e27d041 | 453 | |
frankvnk | 1:32d1ef95eceb | 454 | |
frankvnk | 6:4fb3776a9b92 | 455 | |
frankvnk | 6:4fb3776a9b92 | 456 |