Small demo to purely test UDP, depends on the module already being configured to auto connect to an access point
Dependencies: USBDevice cc3000_hostdriver_mbedsocket mbed
Fork of WifiDipCortex-UDPDemo by
Wifi-DipCortex - Test application that shows you how to listen for UDP message on one port while sending messages on another
- Listens on UDP_LISTEN_PORT, upon data being recvied it can be found in UDP_RecvBuffer - Transmits UDP_TX_Buffer on UDP_TARGET_PORT to UDP_TARGET_IP
Diff: main.cpp
- Revision:
- 1:5fa4efb214e7
- Parent:
- 0:ff40ad3448c5
- Child:
- 2:6813713a64e1
--- a/main.cpp Tue Jul 08 18:56:37 2014 +0000 +++ b/main.cpp Wed Jul 09 19:27:41 2014 +0000 @@ -1,3 +1,14 @@ +/* ------------------------------------------------------------------------------------------------------------ + SolderSplash Labs - Wifi-DipCortex + Test application that shows you how to listen for UDP message on one port while sending messages on another + + - Listens on UDP_LISTEN_PORT, upon data being recvied it can be found in UDP_RecvBuffer + - Transmits UDP_TX_Buffer on UDP_TARGET_PORT to UDP_TARGET_IP + + Note : V1.28 firmware has an issue sending UDP data to 224.0.0.251 + http://forum.soldersplash.co.uk/viewtopic.php?f=15&t=97 +----------------------------------------------------------------------------------------------------------- */ + #include "mbed.h" #include "cc3000.h" #include "wifi.h" @@ -6,7 +17,8 @@ using namespace mbed_cc3000; -#define SERIAL_BAUD_RATE 115200 +#define SERIAL_BAUD_RATE 115200 +#define SOCKOPT_RECV_NONBLOCK 0 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37)); Serial uart(p19, p20); @@ -14,6 +26,23 @@ // USB CDC serial port USBSerial pc; +const char UDP_TX_Buffer[] = {0,0,0,0,0,1,0,0,0,0,0,0,6,0x61,0x61,0x61,0x61,0x61,0x61,5,0x6c,0x6f,0x63,0x61,0x6c,0,0,1,0,1}; + +// Multicast address - this causes issues with firmware version 1.28 +//const char* UDP_TARGET_IP = "224.0.0.251"; + +// This is the broadcast address for the subnet 192.168.0.x +const char* UDP_TARGET_IP = "192.168.0.255"; + +const int UDP_TARGET_PORT = 5353; +UDPSocket UDP_TransmitSocket; + +const int UDP_LISTEN_PORT = 11028; +UDPSocket UDP_RecvSocket; + +#define UDP_RECV_BUF_LEN 512 +uint8_t UDP_RecvBuffer[ UDP_RECV_BUF_LEN ]; + // ------------------------------------------------------------------------------------------------------------ /*! @brief WaitForConnection @@ -62,10 +91,18 @@ } } +// ------------------------------------------------------------------------------------------------------------ +/*! + @brief UdpTx_Init +*/ +// ------------------------------------------------------------------------------------------------------------ +void UdpTx_Init ( void ) +{ + // Creating the socket once and then re-using it is quicker than creating it everytime + // but it does tie up a 1 of the 4 sockets + UDP_TransmitSocket.init(); +} -const char* UDP_TARGET_IP = "192.168.0.10"; -const int UDP_TARGET_PORT = 1500; - // ------------------------------------------------------------------------------------------------------------ /*! @brief UdpTx @@ -73,16 +110,70 @@ // ------------------------------------------------------------------------------------------------------------ void UdpTx ( void ) { -char message[] = "Hello World\n"; -UDPSocket udpSocket; Endpoint target; - - udpSocket.init(); +int returnVal; + + target.set_address(UDP_TARGET_IP, UDP_TARGET_PORT); pc.printf("Sending UDP Message ..\r\n"); - udpSocket.sendTo(target, message, sizeof(message)); - pc.printf("UDP Message Sent..\r\n"); + + returnVal = UDP_TransmitSocket.sendTo(target, (char *)UDP_TX_Buffer, sizeof(UDP_TX_Buffer)); + + if ( returnVal < 0 ) + { + pc.printf("UdpTx : failed to send UDP message ReturnVal : %i\r\n", returnVal); + } + else + { + pc.printf("UDP Message Sent (%i)..\r\n", returnVal); + } +} + +// ------------------------------------------------------------------------------------------------------------ +/*! + @brief UdpRxTx_Init - Set up a non blocking UDP port for incoming messages +*/ +// ------------------------------------------------------------------------------------------------------------ +void UdpRx_Init ( void ) +{ +long optvalue_block = 0; + + // Note : This seems wrong but .. we are using a socket option that stops the cc3000 blocking + // this lets us call recvfrom and not wait for a message, if it has any data it returns with it, if theres no data it returns immediately + UDP_RecvSocket.set_blocking(true, 0); + + if ( 0 == UDP_RecvSocket.bind(UDP_LISTEN_PORT) ) + { + // Socket bound to the port + // Configure the module to not block when checking for UDP data + UDP_RecvSocket.set_option(SOL_SOCKET, SOCKOPT_RECV_NONBLOCK, &optvalue_block, 1); + pc.printf("UDP Socket open and listening\r\n"); + } + else + { + pc.printf("Failed to bind to UDP port\r\n"); + } +} + +// ------------------------------------------------------------------------------------------------------------ +/*! + @brief UdpRx +*/ +// ------------------------------------------------------------------------------------------------------------ +void UdpRx ( void ) +{ +Endpoint client; +int returnVal = 0; + + returnVal = UDP_RecvSocket.receiveFrom(client, (char *)UDP_RecvBuffer, UDP_RECV_BUF_LEN); + + if ( returnVal > 0 ) + { + pc.printf("UDP Message Recv'd %i bytes from : %s \r\n", returnVal, client.get_address()); + + // TODO : Process your UDP message here + } } // ------------------------------------------------------------------------------------------------------------ @@ -113,18 +204,40 @@ // ------------------------------------------------------------------------------------------------------------ int main( void ) { +int tickCnt = 0; + // Initalise the WiFi Module init(); WaitForUser(); + pc.printf("Starting CC3000 module\r\n"); + wifi.start(0); WaitForConnection(); + // set up a lisening socket + UdpRx_Init(); + UdpTx_Init(); + while (1) - { - WaitForUser(); - UdpTx(); + { + // Check for UDP coms, non blocking + UdpRx(); + + // TODO : Do other things, like service sensors etc.. + // For now lets simulate a task that lasts for 100ms + wait(0.1); + pc.printf("."); + + if ( tickCnt > 100 ) + { + tickCnt = 0; + // And then send a UDP message + UdpTx(); + } + tickCnt ++; } + } \ No newline at end of file