tcp client for cc3000, using mbed socket interface

Dependencies:   NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Committer:
Kojto
Date:
Thu Nov 07 18:39:21 2013 +0000
Revision:
4:51ad2e8bedb9
Parent:
3:f97cdf75512e
Child:
5:fbcd3cb3c81c
- updated to the host driver rev 45; - using the new EthernetInterface

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 0:2ffed535a426 1 /* mbed Microcontroller Library
Kojto 0:2ffed535a426 2 * Copyright (c) 2006-2013 ARM Limited
Kojto 0:2ffed535a426 3 *
Kojto 0:2ffed535a426 4 * Licensed under the Apache License, Version 2.0 (the "License");
Kojto 0:2ffed535a426 5 * you may not use this file except in compliance with the License.
Kojto 0:2ffed535a426 6 * You may obtain a copy of the License at
Kojto 0:2ffed535a426 7 *
Kojto 0:2ffed535a426 8 * http://www.apache.org/licenses/LICENSE-2.0
Kojto 0:2ffed535a426 9 *
Kojto 0:2ffed535a426 10 * Unless required by applicable law or agreed to in writing, software
Kojto 0:2ffed535a426 11 * distributed under the License is distributed on an "AS IS" BASIS,
Kojto 0:2ffed535a426 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Kojto 0:2ffed535a426 13 * See the License for the specific language governing permissions and
Kojto 0:2ffed535a426 14 * limitations under the License.
Kojto 0:2ffed535a426 15 */
Kojto 0:2ffed535a426 16 #include "mbed.h"
Kojto 0:2ffed535a426 17 #include "cc3000.h"
Kojto 0:2ffed535a426 18 #include "main.h"
Kojto 0:2ffed535a426 19
Kojto 0:2ffed535a426 20 #include "TCPSocketConnection.h"
Kojto 0:2ffed535a426 21 #include "TCPSocketServer.h"
Kojto 0:2ffed535a426 22
Kojto 0:2ffed535a426 23 using namespace mbed_cc3000;
Kojto 0:2ffed535a426 24
Kojto 4:51ad2e8bedb9 25 using namespace mbed_cc3000;
Kojto 0:2ffed535a426 26
Kojto 2:c8cd9df34be9 27 /* cc3000 module declaration specific for user's board. Check also init() */
Kojto 2:c8cd9df34be9 28 #if (MY_BOARD == WIGO)
Kojto 4:51ad2e8bedb9 29 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), "ssid", "key", WPA2, false);
Kojto 2:c8cd9df34be9 30 Serial pc(USBTX, USBRX);
Kojto 2:c8cd9df34be9 31 #elif (MY_BOARD == WIFI_DIPCORTEX)
Kojto 4:51ad2e8bedb9 32 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), "ssid", "key", WPA2, false);
Kojto 2:c8cd9df34be9 33 Serial pc(UART_TX, UART_RX);
Kojto 4:51ad2e8bedb9 34 #elif (MY_BOARD == MBED_BOARD_EXAMPLE)
Kojto 4:51ad2e8bedb9 35 cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7), "ssid", "key", WPA2, false);
Kojto 4:51ad2e8bedb9 36 Serial pc(USBTX, USBRX);
Kojto 2:c8cd9df34be9 37 #else
Kojto 2:c8cd9df34be9 38
Kojto 2:c8cd9df34be9 39 #endif
Kojto 0:2ffed535a426 40
Kojto 2:c8cd9df34be9 41 /**
Kojto 3:f97cdf75512e 42 * \brief TCP client demo
Kojto 3:f97cdf75512e 43 * \param none
Kojto 0:2ffed535a426 44 * \return int
Kojto 0:2ffed535a426 45 */
Kojto 0:2ffed535a426 46 int main() {
Kojto 2:c8cd9df34be9 47 init(); /* board dependent init */
Kojto 0:2ffed535a426 48 pc.baud(115200);
Kojto 0:2ffed535a426 49
Kojto 3:f97cdf75512e 50 printf("CC3000 tcp client demo. \r\n");
Kojto 4:51ad2e8bedb9 51 wifi.init();
Kojto 4:51ad2e8bedb9 52 if (wifi.connect() == -1) {
Kojto 4:51ad2e8bedb9 53 printf("Failed to connect. Please verify connection details and try again. \r\n");
Kojto 0:2ffed535a426 54 } else {
Kojto 4:51ad2e8bedb9 55 printf("IP address: %s \r\n", wifi.getIPAddress());
Kojto 0:2ffed535a426 56 }
Kojto 4:51ad2e8bedb9 57
Kojto 0:2ffed535a426 58 const char* ECHO_SERVER_ADDRESS = "192.168.1.10";
Kojto 0:2ffed535a426 59 const int ECHO_SERVER_PORT = 1895;
Kojto 0:2ffed535a426 60
Kojto 0:2ffed535a426 61 TCPSocketConnection socket;
Kojto 0:2ffed535a426 62 while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
Kojto 3:f97cdf75512e 63 printf("Unable to connect to (%s) on port (%d) \r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
Kojto 0:2ffed535a426 64 wait(1);
Kojto 0:2ffed535a426 65 }
Kojto 0:2ffed535a426 66
Kojto 0:2ffed535a426 67 char hello[] = "Hello World\n";
Kojto 0:2ffed535a426 68 socket.send_all(hello, sizeof(hello) - 1);
Kojto 0:2ffed535a426 69
Kojto 0:2ffed535a426 70 char buf[256];
Kojto 0:2ffed535a426 71 int n = socket.receive(buf, 256);
Kojto 0:2ffed535a426 72 buf[n] = '\0';
Kojto 0:2ffed535a426 73 printf("%s", buf);
Kojto 0:2ffed535a426 74
Kojto 0:2ffed535a426 75 socket.close();
Kojto 3:f97cdf75512e 76 printf("Completed. \r\n");
Kojto 4:51ad2e8bedb9 77 wifi.disconnect();
Kojto 0:2ffed535a426 78
Kojto 0:2ffed535a426 79 }