tcp client for cc3000, using mbed socket interface

Dependencies:   NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Committer:
Kojto
Date:
Tue Jun 03 08:44:14 2014 +0000
Revision:
6:5fbda2ed5f0f
Parent:
5:fbcd3cb3c81c
update mbed lib to r84

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 2:c8cd9df34be9 25 /* cc3000 module declaration specific for user's board. Check also init() */
Kojto 2:c8cd9df34be9 26 #if (MY_BOARD == WIGO)
Kojto 4:51ad2e8bedb9 27 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), "ssid", "key", WPA2, false);
Kojto 2:c8cd9df34be9 28 Serial pc(USBTX, USBRX);
Kojto 2:c8cd9df34be9 29 #elif (MY_BOARD == WIFI_DIPCORTEX)
Kojto 4:51ad2e8bedb9 30 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), "ssid", "key", WPA2, false);
Kojto 2:c8cd9df34be9 31 Serial pc(UART_TX, UART_RX);
Kojto 4:51ad2e8bedb9 32 #elif (MY_BOARD == MBED_BOARD_EXAMPLE)
Kojto 4:51ad2e8bedb9 33 cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7), "ssid", "key", WPA2, false);
Kojto 4:51ad2e8bedb9 34 Serial pc(USBTX, USBRX);
Kojto 2:c8cd9df34be9 35 #else
Kojto 2:c8cd9df34be9 36
Kojto 2:c8cd9df34be9 37 #endif
Kojto 0:2ffed535a426 38
Kojto 2:c8cd9df34be9 39 /**
Kojto 3:f97cdf75512e 40 * \brief TCP client demo
Kojto 3:f97cdf75512e 41 * \param none
Kojto 0:2ffed535a426 42 * \return int
Kojto 0:2ffed535a426 43 */
Kojto 0:2ffed535a426 44 int main() {
Kojto 2:c8cd9df34be9 45 init(); /* board dependent init */
Kojto 0:2ffed535a426 46 pc.baud(115200);
Kojto 0:2ffed535a426 47
Kojto 5:fbcd3cb3c81c 48 printf("cc3000 tcp client demo. \r\n");
Kojto 4:51ad2e8bedb9 49 wifi.init();
Kojto 4:51ad2e8bedb9 50 if (wifi.connect() == -1) {
Kojto 4:51ad2e8bedb9 51 printf("Failed to connect. Please verify connection details and try again. \r\n");
Kojto 0:2ffed535a426 52 } else {
Kojto 4:51ad2e8bedb9 53 printf("IP address: %s \r\n", wifi.getIPAddress());
Kojto 0:2ffed535a426 54 }
Kojto 4:51ad2e8bedb9 55
Kojto 0:2ffed535a426 56 const char* ECHO_SERVER_ADDRESS = "192.168.1.10";
Kojto 0:2ffed535a426 57 const int ECHO_SERVER_PORT = 1895;
Kojto 0:2ffed535a426 58
Kojto 0:2ffed535a426 59 TCPSocketConnection socket;
Kojto 0:2ffed535a426 60 while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
Kojto 3:f97cdf75512e 61 printf("Unable to connect to (%s) on port (%d) \r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
Kojto 0:2ffed535a426 62 wait(1);
Kojto 0:2ffed535a426 63 }
Kojto 0:2ffed535a426 64
Kojto 0:2ffed535a426 65 char hello[] = "Hello World\n";
Kojto 0:2ffed535a426 66 socket.send_all(hello, sizeof(hello) - 1);
Kojto 0:2ffed535a426 67
Kojto 0:2ffed535a426 68 char buf[256];
Kojto 0:2ffed535a426 69 int n = socket.receive(buf, 256);
Kojto 0:2ffed535a426 70 buf[n] = '\0';
Kojto 0:2ffed535a426 71 printf("%s", buf);
Kojto 0:2ffed535a426 72
Kojto 0:2ffed535a426 73 socket.close();
Kojto 3:f97cdf75512e 74 printf("Completed. \r\n");
Kojto 4:51ad2e8bedb9 75 wifi.disconnect();
Kojto 0:2ffed535a426 76 }