TinyCircuits Forum

General Category => General Discussion => Topic started by: huotianqi on October 14, 2014, 09:51:57 PM

Title: WIFI tcp socket connection issue
Post by: huotianqi on October 14, 2014, 09:51:57 PM
Hi,

I have a arduino yun and running a python scripted tcp socket server and waiting for new client connections and it work perfect with xbee wifi module. I want to try out with the tinydruino board with the WIFI stack, and the problem comes:

the tinydruino can connect to my server with no problem. However, after around half to 1 mins, the client continuously sends some unknown character to my server (On my server side it tells me that it received something from this tinydruino and the message has length 0 which confuses me) and then disconnected from the server. I don't know what exactly happened here since I am new to tinydruino.

below is the code on the tinydruino part:

Code: [Select]
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"

#include<stdlib.h>

// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ   2
#define ADAFRUIT_CC3000_VBAT  A3
#define ADAFRUIT_CC3000_CS    8

// WiFi network (change with your settings !)
#define WLAN_SSID       "pp"
#define WLAN_PASS       "123456"
#define WLAN_SECURITY   WLAN_SEC_WPA2 // This can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER);
uint32_t ip = cc3000.IP2U32(192,168,240,1);
int port = 55555;
int BUFSIZ = 128;

const unsigned long
dhcpTimeout     = 60L * 1000L, // Max time to wait for address from DHCP
connectTimeout  = 15L * 1000L, // Max time to wait for server connection
responseTimeout = 15L * 1000L; // Max time to wait for data from server

uint32_t   t;
char c;
// PHP's server IP, port, and repository (change with your settings !)

Adafruit_CC3000_Client client;

void setup(void)
{
  Serial.begin(9600);

  Serial.print(F("Initializing..."));
  if(!cc3000.begin()) {
    Serial.println(F("failed. Check your wiring?"));
    return;
  }
 
  Serial.println(F("\nDeleting old connection profiles"));
  if (!cc3000.deleteProfiles()) {
    Serial.println(F("Failed!"));
    while(1);
  }

  Serial.print(F("OK.\r\nConnecting to network..."));
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println(F("connected!"));

  Serial.print(F("Requesting address from DHCP server..."));
  for(t=millis(); !cc3000.checkDHCP() && ((millis() - t) < dhcpTimeout); delay(250)) {
    //Serial.println("....waiting");
  }
  if(cc3000.checkDHCP()) {
    Serial.println(F("OK"));
  }
  else {
    Serial.println(F("failed"));
    return;
  }

  //Open Socket
  Serial.println("...Connecting to server");
  t = millis();
  do {
    client = cc3000.connectTCP(ip, port);
    delay(100);
  }
  while((!client.connected()) && ((millis() - t) < connectTimeout));
 
  // Send request
  if (client.connected()) {
    Serial.println("Connected");
    Serial.println("...Sending request...");
    client.fastrprint("hello server");
  }
  else {
    Serial.println(F("Connection failed"));   
    return;
  }
   
}

void loop(void)
{
  if (client.available()){
    c = client.read();
    Serial.print(c);
  }
  delay(5);
}

Thanks in advance...