cc3000 udp client demo

Dependencies:   NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Committer:
Kojto
Date:
Tue Jun 03 09:21:32 2014 +0000
Revision:
4:7bab74efae95
Parent:
3:2941c1b5d79e
update mbed lib to r84

Who changed what in which revision?

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