cc3000 udp client demo

Dependencies:   NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include "cc3000.h"
00018 #include "main.h"
00019 
00020 #include "UDPSocket.h"
00021 
00022 using namespace mbed_cc3000;
00023 
00024 /* cc3000 module declaration specific for user's board. Check also init() */
00025 #if (MY_BOARD == WIGO)
00026 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), "ssid", "key", WPA2, false);
00027 Serial pc(USBTX, USBRX);
00028 #elif (MY_BOARD == WIFI_DIPCORTEX)
00029 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), "ssid", "key", WPA2, false);
00030 Serial pc(UART_TX, UART_RX);
00031 #elif (MY_BOARD == MBED_BOARD_EXAMPLE)
00032 cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7), "ssid", "key", WPA2, false);
00033 Serial pc(USBTX, USBRX);
00034 #else
00035 
00036 #endif
00037 
00038 /**
00039  *  \brief Upd client demo
00040  *  \param  none
00041  *  \return int
00042  */
00043 int main() {
00044     init(); /* board dependent init */
00045     pc.baud(115200);
00046 
00047     printf("cc3000 udp client demo.\r\n");
00048     wifi.init();
00049     if (wifi.connect() == -1) {
00050         printf("Failed to connect. Please verify connection details and try again. \r\n");
00051     } else {
00052         printf("IP address: %s \r\n", wifi.getIPAddress());
00053     }
00054     const char* ECHO_SERVER_ADDRESS = "192.168.1.5";
00055     const int ECHO_SERVER_PORT = 1500;
00056 
00057     UDPSocket sock;
00058     sock.init();
00059 
00060     Endpoint echo_server;
00061     echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
00062 
00063     char out_buffer[] = "Hello World\n";
00064     sock.sendTo(echo_server, out_buffer, sizeof(out_buffer));
00065 
00066     char in_buffer[256];
00067     int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer));
00068 
00069     in_buffer[n] = '\0';
00070     printf("%s \r\n", in_buffer);
00071 
00072     sock.close();
00073     printf("Completed. \r\n");
00074     wifi.disconnect();
00075 }