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();
}
}
}
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();
}
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