cc3000 UDP server demo

Dependencies:   NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Committer:
Kojto
Date:
Tue Jun 03 09:20:16 2014 +0000
Revision:
3:62fc96bb91b5
Parent:
2:6079591c6683
update mbed lib to r84

Who changed what in which revision?

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