EDIT: tiny duino wifi module issue

huotianqi

  • Newbie
  • *
    • Posts: 4
    • View Profile
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:

------------------------------------------------------------------------------------------------------------------
EDIT:

I have found the issue but have no idea how to solve it.
The real issue is, after a certain time, the client.available() never return true therefore it is never capable to receive anything from the server.

I have done a memory check in my code and there is no memory leak.

So I am totally lost at this point. Maybe this is a bug in the library? If so, how come I don't see anyone else have any issue about this but me?
------------------------------------------------------------------------------------------------------------------
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...
« Last Edit: November 16, 2014, 11:08:50 AM by huotianqi »



bowden100

  • Newbie
  • *
    • Posts: 4
    • View Profile
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...


I think there must be some memory leak in the code because we had a similar problem with just a chat server.
You establish the connection..then start sending data back and forth and then it would stop after 3 minutes. There is FreeMEM() function that can tell you how much memory you have.  If you get close to the end of memory and sketch code, the chance that it will stop increase.  The real problem with the Wifi code is it is just too much code for this type of processor and it is using too much of memory. We went to using a library called PString to cut down on Serial.print code and it decrease the code size a lot.

The one thing that helped was to read as many bytes as possible..so you could replace the:

client.read();

with

 int =client.readBytes(buffer,sizeof(buffer);

this will read the data and wait up to 1000ms (1sec)
until there is data available..if you want to change this

You can change it but I dont remember what it was, I think client.SetTimeout(n);
But base on what have it should work...


huotianqi

  • Newbie
  • *
    • Posts: 4
    • View Profile
Hi. Thank you for your reply!

I followed your hint and put the MemoryFree() function in my code to check my memory. The memory keeps the same number which indicates that there is no memory leak.

Another thing I tried to follow with you is the client.readBytes(buffer,sizeof(buffer) thing. Sadly this function doesn't work somehow. Below is what my code looks like:

Code: [Select]
void loop(void){
  char buffer[32];
  if (client.available()){
    client.readBytes(buffer,sizeof(buffer));
    Serial.print(buffer); //this never get printed out... I don't know why...
  }
  memset(buffer, 0, sizeof(buffer));
}
The Serial.print(buffer) never print anything out. Any idea what is wrong here?

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...


I think there must be some memory leak in the code because we had a similar problem with just a chat server.
You establish the connection..then start sending data back and forth and then it would stop after 3 minutes. There is FreeMEM() function that can tell you how much memory you have.  If you get close to the end of memory and sketch code, the chance that it will stop increase.  The real problem with the Wifi code is it is just too much code for this type of processor and it is using too much of memory. We went to using a library called PString to cut down on Serial.print code and it decrease the code size a lot.

The one thing that helped was to read as many bytes as possible..so you could replace the:

client.read();

with

 int =client.readBytes(buffer,sizeof(buffer);

this will read the data and wait up to 1000ms (1sec)
until there is data available..if you want to change this

You can change it but I dont remember what it was, I think client.SetTimeout(n);
But base on what have it should work...


 

SMF spam blocked by CleanTalk