CC3000 - sending UDP packets

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
gg066
 
Posts: 16
Joined: Wed Mar 06, 2013 4:18 pm

CC3000 - sending UDP packets

Post by gg066 »

I have a requirement to send UDP packets to a server running a Java application. The guaranteed delivery aspects of TCP are not required or desired, so UDP is a good choice for me.

When I implemented this on the CC3000, it did not work.

Based on a hint in one of the other threads (https://www.adafruit.com/forums/viewtop ... dp#p245315), I started reading the CC3000 code closely. It appears that the write method does not handle UDP, so I ended up writing a writeUDP method for this purpose. I have included the code for this class, which I added to Adafruit_CC3000.cpp.

This method seems to work, although I did have to add some 1 second delays around the connectUDP call, but the UDP packets are now being sent.

Code: Select all

int16_t Adafruit_CC3000_Client::writeUDP(const void *buf, uint16_t len, uint32_t destIP, uint16_t destPort, uint32_t flags)
{
  	sockaddr			socketAddress;
	socklen_t			sockLen;
	int8_t				byteCount;
	
// set the udp dest address and port
//	memset(&socketAddr, 0, sizeof(sockaddr_in));
    memset(&socketAddress, 0x00, sizeof(socketAddress));
    socketAddress.sa_family = AF_INET;
    socketAddress.sa_data[0] = (destPort & 0xFF00) >> 8;  // Set the Port Number
    socketAddress.sa_data[1] = (destPort & 0x00FF);
    socketAddress.sa_data[2] = destIP >> 24;
    socketAddress.sa_data[3] = destIP >> 16;
    socketAddress.sa_data[4] = destIP >> 8;
    socketAddress.sa_data[5] = destIP;
    
    sockLen = sizeof(socketAddress);
    byteCount = sendto(_socket, buf, len, 0, (sockaddr*)&socketAddress, sockLen);

    return byteCount;
}


User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: CC3000 - sending UDP packets

Post by adafruit_support_mike »

Nice! Thanks for posting it. ;-)

seanguzz
 
Posts: 1
Joined: Tue Apr 08, 2014 2:19 am

Re: CC3000 - sending UDP packets

Post by seanguzz »

Is it possible to post how you are using this code within your sketch? I am attempting to send udp packets and am currious how you are achieving it

User avatar
Anthrum
 
Posts: 26
Joined: Fri Aug 15, 2014 8:45 am

Re: CC3000 - sending UDP packets

Post by Anthrum »

Can anyone assist in adding the code to the .h file of the CC3000 library? I thought i could just add this line;

size_t write(const void *buf, uint16_t len, uint32_t flags = 0);
-->
int16_t writeUDP(const void *buf, uint16_t len, uint32_t destIP, uint16_t destPort, uint32_t flags);
<--
int read(void *buf, uint16_t len, uint32_t flags = 0);
int read(void);
int32_t close(void);
int available(void);


But I still get verify errors. pretty sure I added code to .cpp correctly.

Thanks

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: CC3000 - sending UDP packets

Post by adafruit_support_mike »

If you're trying to add UDP support to the CC3000 library, that already exists in the versions we've released since this thread was originally posted.

The InternetTime and SendTweet sketches that come with the current library have examples of working with UDP connections.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”