cc3000 connect to other computer on my network by name

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: cc3000 connect to other computer on my network by name

Post by adafruit_support_mike »

Rereading the previous posts in the thread, I noticed some confusion, some of which was mine.

Let me step back and as a fundamental question: are you trying to make the CC3000 emit UDP packets to a specific machine, or listen for broadcast packets emitted by some other machine?

User avatar
pmcquay
 
Posts: 39
Joined: Fri Jan 16, 2015 8:54 am

Re: cc3000 connect to other computer on my network by name

Post by pmcquay »

I am trying to make it listen for packets broadcast from another machine. Specifically the packet from the other machine will contain its IP address (and some checksum data). The other machine packet will be sent once every second.

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

Re: cc3000 connect to other computer on my network by name

Post by adafruit_support_mike »

Thank you.

Doing that requires a different set of tools. The connectUDP() method emits UDP packets.

To read UDP packets you need a server. This post has code for a working UDP server:

http://forums.adafruit.com/viewtopic.ph ... 52#p223227

but let me check with out CC3000 experts to see if the same capabilities have been rolled into the Adafruit CC3000 library.

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: cc3000 connect to other computer on my network by name

Post by tdicola »

There isn't a nice UDP server wrapper in the Adafruit CC3000 library, but you can actually access the lower level socket API that TI exposes with the CC3000 driver. Check out TI's documentation here: http://software-dl.ti.com/ecs/simplelin ... __api.html You can actually see a small example of using them to listen for UDP packets in this MDNS responder code too: https://github.com/adafruit/CC3000_MDNS That might be handy to look at to see a simple DNS-related listener too (although the code doesn't really parse out all the info from the DNS queries, there really isn't enough memory on an Arduino to understand everything possible in a DNS packet).

User avatar
pmcquay
 
Posts: 39
Joined: Fri Jan 16, 2015 8:54 am

Re: cc3000 connect to other computer on my network by name

Post by pmcquay »

Thank you very much for that information, it made all of the difference. I got it to work (on my first try, which is kind of amazing!)

Anyway, for posterity, here is the code for listening to a UDP broadcast:

Code: Select all

int soc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (soc < 0) {
    Serial.println(F("unable to create a listening socket"));
    while(true){}; //stop here if we cant create a socket
  }
  
  sockaddr_in address;
  memset(&address, 0, sizeof(address));
  
  address.sin_family = AF_INET;
  address.sin_port = htons(port);
  address.sin_addr.s_addr = htonl(cc3000.IP2U32(0, 0, 0, 0));
  
  socklen_t len = sizeof(address);
  
  if (bind(soc, (sockaddr*) &address, sizeof(address)) < 0) {
    Serial.println(F("unable to bind a listening socket"));
    while(true){}; //stop here if we cant bind a socket
  }
  
  Serial.println(F("UDP listener opened"));
  
  while(true)
  {
    uint8_t buffer[8];
    int n = recv(soc, &buffer, sizeof(buffer), 0);
    
    for(int i = 0; i < 8; i++)
      Serial.print(buffer[i]);
      
    Serial.println("");
  }

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

Re: cc3000 connect to other computer on my network by name

Post by adafruit_support_mike »

Glad to hear it's working for you, and thank you for posting the code. We do get UDP questions occasionally, and every bit of new reference material helps!

User avatar
pmcquay
 
Posts: 39
Joined: Fri Jan 16, 2015 8:54 am

Re: cc3000 connect to other computer on my network by name

Post by pmcquay »

So, it seems as if not all is roses, yet :).

There appears to be a problem with the code that I posted above.

When I use that code to get the IP address (I modified it slightly to return when it got an IP address), my other code will connect fine (over TCP, which is what its supposed to do), and then after about 8 seconds of transmitting, it will hang at the write call for sending data over the TCP connection.

This only happens when I use the UDP function to get the IP address. If I hardcode it, it works fine.

I couldn't find anywhere in the library where a socket was disconnected (unbound? I don't know the proper term for this), and that is the only thing that I'm not doing here, so I figure it might be a good place to start. Maybe the UDP socket is timing out and taking everything else with it. Could you point me toward an example of some code that disconnects a socket and cleans up after itself when its finished? (I will also have to stay connected to the wireless network, so just disconnecting that is not an option here)

User avatar
pmcquay
 
Posts: 39
Joined: Fri Jan 16, 2015 8:54 am

Re: cc3000 connect to other computer on my network by name

Post by pmcquay »

from looking at the TI documentation it appears that closesocket is the function I want here. I will try this when I get home and post results.

User avatar
pmcquay
 
Posts: 39
Joined: Fri Jan 16, 2015 8:54 am

Re: cc3000 connect to other computer on my network by name

Post by pmcquay »

Yup, that appears to work, Here is the adjusted code:

I'm using a really simple way of making sure the IP address in the heartbeat is correct (just repeat the bytes of the address backwards), so that could probably change, but it works for me.

Code: Select all

void GetServerIpAddressFromHeartBeat(uint16_t port)
{
  int soc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  
  if (soc < 0) {
    Serial.println(F("unable to create a listening socket"));
    while(true){}; //stop here if we cant create a socket
  }
  
  sockaddr_in address;
  memset(&address, 0, sizeof(address));
  
  address.sin_family = AF_INET;
  address.sin_port = htons(port);
  address.sin_addr.s_addr = htonl(cc3000.IP2U32(0, 0, 0, 0));
  
  socklen_t len = sizeof(address);
  
  if (bind(soc, (sockaddr*) &address, sizeof(address)) < 0) {
    Serial.println(F("unable to bind a listening socket"));
    while(true){}; //stop here if we cant bind a socket
  }
  
  Serial.println(F("UDP listener opened"));
  
  while(true)
  {
    uint8_t buffer[8];
    int n = recv(soc, &buffer, sizeof(buffer), 0);
    
    if(buffer[0] == buffer[7] &&
       buffer[1] == buffer[6] &&
       buffer[2] == buffer[5] &&
       buffer[3] == buffer[4])
    {
      Serial.println(F("Got IP address of server"));
      serverIpAddress = cc3000.IP2U32(buffer[0], buffer[1], buffer[2], buffer[3]);
      
      if(closesocket(soc) < 0) {
        Serial.println(F("unable to close the listening socket"));
        while(true){}; //stop here if we cant close the socket
      }
        
      return;
    }
  }
}

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

Re: cc3000 connect to other computer on my network by name

Post by adafruit_support_mike »

Looks good.

You might want to modify the loop that listens for UDP packets with a status variable instead of a fixed value:

Code: Select all

  boolean scan = true;  
  while( scan )
  {
    uint8_t buffer[8];
    int n = recv(soc, &buffer, sizeof(buffer), 0);
    
    if(buffer[0] == buffer[7] &&
       buffer[1] == buffer[6] &&
       buffer[2] == buffer[5] &&
       buffer[3] == buffer[4])
    {
      Serial.println(F("Got IP address of server"));
      serverIpAddress = cc3000.IP2U32(buffer[0], buffer[1], buffer[2], buffer[3]);
      
      if(closesocket(soc) < 0) {
        Serial.println(F("unable to close the listening socket"));
        while(true){}; //stop here if we cant close the socket
      }
      scan = false;
    }
  }
  return;
That avoids the need to short-circuit the loop with a 'return'. There isn't much difference functionally, but makes the code a little more flexible.

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

Return to “Other Arduino products from Adafruit”