cc3000 tcp server demo (please check mbed socket interface for python script to test this demo) mbed.org/handbook/Socket

Dependencies:   NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Committer:
Kojto
Date:
Tue Jun 03 08:45:06 2014 +0000
Revision:
6:7b36903dbe99
Parent:
5:ba0af4c6f21e
update mbed lib to r84

Who changed what in which revision?

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