CC3000 won't connect to remote TCP server

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
john_salzgeber
 
Posts: 4
Joined: Sat Mar 15, 2014 12:50 pm

CC3000 won't connect to remote TCP server

Post by john_salzgeber »

Hello,

First of all thank you to Adafruit for making great products. I'm a frequent customer and have had great success in the Learning system getting the help I've needed.

I seem to have a problem with the CC3000s' TCP client capability communicating to a remote TCP server app which is running on my PC. I've created a TCP server app in Visual Studio and here is the code:

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;


namespace Server
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			try 
			{
				IPAddress ipAd = IPAddress.Parse("192.168.1.2"); //use local m/c IP address, and use the same in the client

				/* Initializes the Listener */
				TcpListener myList=new TcpListener(ipAd,8001);

				/* Start Listeneting at the specified port */		
				myList.Start();
		
				Console.WriteLine("The server is running at port 8001...");	
				Console.WriteLine("The local End point is  :" + myList.LocalEndpoint );
				Console.WriteLine("Waiting for a connection.....");
		
				//Accepts the connection
				Socket s=myList.AcceptSocket();
				Console.WriteLine("Connection accepted from "+s.RemoteEndPoint);
		
				//Get the data
				byte[] b=new byte[100];
				int k=s.Receive(b);
				Console.WriteLine("Recieved...");
				for (int i=0;i<k;i++)
					Console.Write(Convert.ToChar(b[i]));

				//Send the Reply
				ASCIIEncoding asen=new ASCIIEncoding();
				s.Send(asen.GetBytes("The string was recieved by the server."));
				Console.WriteLine("\nSent Acknowledgement");
				/* clean up */    		
				s.Close();
				myList.Stop();
			
			}
			catch (Exception e) 
			{
				Console.WriteLine("Error..... " + e.StackTrace);
			}
            Console.ReadLine();
		}
	}

}
Secondly, I've modified an example sketch I found online and attempted to create a TCP client using the CC3000:

Code: Select all

// Include required libraries
#include <Adafruit_CC3000.h>
#include <SPI.h>


// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10

// WiFi network (change with your settings !)
#define WLAN_SSID "network"  // This was added for testing
#define WLAN_PASS "password" // This was added for testing
#define WLAN_SECURITY WLAN_SEC_WPA2 // This can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2



//Create CC3000 instance

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIV2);
                                         
// Local server IP, port, (change with your settings !)
uint32_t ip = cc3000.IP2U32(192,168,1,2);
int port = 8001;

                                         
void setup(void)
{
  Serial.begin(115200);
    
  // Initialise the CC3000 module
  if (!cc3000.begin())
  {
    while(1);
  }

  // Connect to WiFi network
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println("Connected to WiFi network!");
    
  // Check DHCP
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100);
  }
  
}

void loop(void)
{
    // Send request
    String request = "test";
    send_request(request);
    
    // Update every second
    delay(1000);
}

// Function to send a TCP request and get the result as a string
void send_request (String request) {
     
    // Connect
    Serial.println("Starting connection to server...");
    Adafruit_CC3000_Client client = cc3000.connectTCP(ip, port);
    
    // Send request
    if (client.connected()) {
      client.println(request);
      client.println(F(""));
      Serial.println("Connected & Data sent");
    }
    else {
      Serial.println(F("Connection failed"));
    }

    while (client.connected()) {
      while (client.available()) {

      // Read answer
      char c = client.read();
      }
    }
    Serial.println("Closing connection");
    Serial.println("");
    client.close();
    
}
I start the server app first and then start the client sketch and I get stuck here:

Code: Select all

Started AP/SSID scan




Connecting to NETGEAR62...Waiting to connect...Connected to WiFi network!
Request DHCP
Starting connection to server...


Connect to 192.168.1.2:8001
Additionally, the command prompt just gets stuck listening for connections. I have a matching Client Visual Studio app that connects just fine and sends data to the Server. Any help/guidance is greatly appreciated. I've visited TI's site for demo apps but I'm finding it difficult working with their raw code as opposed to the Adafruit library. I am very new to Network programming and just starting out so if this is blatantly obvious my apologies.

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

Re: CC3000 won't connect to remote TCP server

Post by adafruit_support_mike »

It looks like the code is hanging in the process of trying to connect the socket.

To eliminate some unknowns, add a few print statements that list the CC3000's PI address, network mask, and gateway. Let's make sure those are set up as they should be.

john_salzgeber
 
Posts: 4
Joined: Sat Mar 15, 2014 12:50 pm

Re: CC3000 won't connect to remote TCP server

Post by john_salzgeber »

Sure. I added my code to the buildtest example sketch. I also changed the port number in both programs just for troubleshooting purposes:

Code: Select all

Hello, CC3000!

RX Buffer : 131 bytes
TX Buffer : 131 bytes
Free RAM: 1156

Initialising the CC3000 ...
Firmware V. : 1.24
MAC Address : 0x08 0x00 0x28 0x57 0x9A 0xAD
Started AP/SSID scan


Networks found: 1
================================================
SSID Name    : NETGEAR62
RSSI         : 83
Security Mode: 3

================================================

Deleting old connection profiles

Attempting to connect to NETGEAR62
Started AP/SSID scan




Connecting to NETGEAR62...Waiting to connect...Connected!
Request DHCP

IP Addr: 192.168.1.6
Netmask: 255.255.255.0
Gateway: 192.168.1.1
DHCPsrv: 192.168.1.1
DNSserv: 192.168.1.1
Starting connection to server...


Connect to 192.168.1.2:8888

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

Re: CC3000 won't connect to remote TCP server

Post by adafruit_support_mike »

Okay, that tells us all the network settings are correct.

Do you have any kind of logging on the server side? It would be helpful to know if the server is seeing the request, or if the connection is stalling before any data gets sent.

Another thing that might give us more information is to go into the file utility/debug.h in the CC3000 library and define the macro DEBUG_MODE to '(1)'. That will cause the library to print out more diagnostic information as it tries to make the connection.

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

Re: CC3000 won't connect to remote TCP server

Post by tdicola »

Also if you can, from a separate machine try to telnet to port 8001 of your server and verify it can connect and send/receive data. This can help rule out stuff like the firewall on the machine blocking connections, etc.

john_salzgeber
 
Posts: 4
Joined: Sat Mar 15, 2014 12:50 pm

Re: CC3000 won't connect to remote TCP server

Post by john_salzgeber »

Well guys I enabled debug mode but the CC3k crashes before it connects to my network. I can connect just fine with debug disabled, so I'm not sure why it's crashing. On the other hand, I decided to change gears and run the CC3k in server mode and have a remote TCP client app running on my laptop just to see what would happen. This works, and the CC3k sends and receives data packets to/from my laptop. Unfortunately the CC3k freezes after about 10 loops, and won't send the test message anymore. I'm merely sending the word "test". The connection isn't being closed on either side. Also, I'm unable to send any strings of an undefined length. For example, I would like to send float values from sensor readings. I've seen many examples of the CC3k in server mode sending random string length values to web pages, but I haven't seen it performed to any other app (such as my console app). Any suggestions?

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

Re: CC3000 won't connect to remote TCP server

Post by adafruit_support_mike »

Wifi connections aren't as reliable as wired connections, so you may be seeing drops due to interference.

Make sure your code uses:

Code: Select all

if ( ! cc3000.checkConnected() ) {
	//  shut down the current connection
	//  set up a new connection
}
//  then do the network communication

frankhsieh
 
Posts: 1
Joined: Fri Jun 20, 2014 10:12 pm

Re: CC3000 won't connect to remote TCP server

Post by frankhsieh »

Hello,

Is there any update news of this problem? I also met the same problem when I tried to write a TCP server in my PC and CC3000 was a client. Please help to clarify this problem. Thanks.

john_salzgeber
 
Posts: 4
Joined: Sat Mar 15, 2014 12:50 pm

Re: CC3000 won't connect to remote TCP server

Post by john_salzgeber »

frankhsieh wrote:Hello,

Is there any update news of this problem? I also met the same problem when I tried to write a TCP server in my PC and CC3000 was a client. Please help to clarify this problem. Thanks.
I decided to use the CC3000 as tcp server instead of my PC. I wrote a client app on my PC and all is good. Not sure what's keeping it from sending data as a client but decided that I would rather use it as a server. Maybe there's a bug that's unresolved but I've seen examples on the web using the CC3k as a client so I know it's possible.

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

Return to “Arduino Shields from Adafruit”