TCP client dies

I did notice the result from the unicorn example looked truncated but when I did the same get request in one of those online things I got the same result. It was a not authorized response. So I started getting data from my own server, I got the favicon for my Web page dozens of times. I run the serial at 115200 otherwise there is no way it will keep up. I think a good place to start looking would be the 15 seconds it takes to do the client prints. Something must be going on there.

Ok Guys,
A new week brings new chances, I was a bit grumpy last week about the unpredictable behavior of HTTP requests, one day perfect, the next nada.
For those offended by the undertone in my posts, sorry, human emotions, yes I do have them still, Iā€™m not yet assimilated by the Borg!

About the delays and slow responses, I know for sure it will work. All other stuff works, the cloud client runs like a breeze so weā€™ll work it out this week.
I wish you all a lot of sparkling moments with this little wonder beast!

Hereā€™s a photo of the android app that I made and that chatā€™s flawlessly with Spark:

In this project the spark measures a bunch of thermometers, it decides what to do and as a result of the algorithm it switches pumps and chimney ventilator on and off. In version 2.00 it makes coffee too!

Look at it this project as a NEST thermostat for houses with wood burners like this:

and trust me, this is an average house! we all have a setup like this!.

The goal of this project is to provide precise control of the temperature of a house and to save energy. The energy is saved in the two big tanks on the left, in the form of heated water. And the heat is released when needed. Thatā€™s new: over here they just heat a house 24/7, what a waist!
Further more the system senses whether your home or not automatically.

Thanks for the feedback, Iā€™ll get back to work, itā€™s getting cold :wink:
Marcus

2 Likes

@marcus can you post your latest code of your project? Iā€™ll try TCPClient part from my own. Thanks.

Hi,
Thanks for your offer to help, hereā€™s the code:

TCPClient client;

void setup()
{
  Serial1.begin(57600);
  Serial1.println("http test");
  // Now open your Serial Terminal, and hit any key to continue!
// while(!Serial1.available()) SPARK_WLAN_Loop(); 

 
}

int cnt = 0;
void doit(){
     Serial1.println("connecting ...");

  if (client.connect("api.openweathermap.org", 80))
  {
    Serial1.println("connected");
   client.println("GET /data/2.5/weather?q=Broakulla,Sweden");
   client.println("Host: api.openweathermap.org");
   client.println("Content-Length: 0");
   client.println();
    cnt =0;
    while(client.available()==0)  //<-- dangerous, just for the test
        SPARK_WLAN_Loop();
    Serial1.println("something available"); //<-- never come here....
    while (client.available()) {  //nothing comes in????
            char c = client.read(); 
            if (cnt++ < 80)    
               Serial1.print(c);
     }
    Serial1.println("no more available");
    client.stop();
  }
  else
  {
    Serial1.println("connection failed");
    
  }
}


void loop(){
    static int update_cnt = millis();
    if (millis() - update_cnt > 10000 ) {
        update_cnt = millis();
        doit();
    }
}

Please see the forum tricks and tips for how to format code! :slight_smile:

@marcus, I did a quick edit on your post to format your code. @mdma is correct in that members need to learn the simple syntax when posting code in a topic. :smile:

thanks, Tried the [code] tag earlier, this looks indeed better

1 Like

Hmmā€¦ you only send GET requests. Can you or someone else try to send POST requests? Iā€™ll get sometimes such corrupted data and then Spark resets. Can someone tell why TCP Client corrupts data?

Examples:

  • General 408 response
  • Missing parts of requests
  • 7^M
  • =<88><9E>^A^H^C

How are you handling the bytes returned from the server after the POST command? You canā€™t just ignore them or depend on client.flush() to get rid of them. I would read and check the result if you arenā€™t already doing that.

Ok fellow sparkers,

To get all doubts off the line I propose to use an old Arduino sketch, the basic web client example as a standard. I adopted it to the Spark. It works, but thereā€™s a lag between the posting of the headers and the reponse from Google.

letā€™s see @mdma and @peekay123 if I format the code right this timeā€¦

/*
  Web client

 
 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen
 Adapted By Marcus Hund for Spark
 */



char server[] = "www.google.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(144, 76, 102, 166);
TCPClient client;
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):

void setup() {
  // Open serial communications and wait for port to open:
  Serial1.begin(115200);
  
  Serial1.println("connecting...");
 // if (client.connect("api.openweathermap.org", 80))
  if (client.connect(ip, 80))
  {
    Serial1.println("connected");
  // if you get a connection, report back via serial:
    // Make a HTTP request:
    client.println("GET /search?q=spark core HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial1.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial1.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial1.println();
    Serial1.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true)
    SPARK_WLAN_Loop();;
  }
}

So the challenge is to make it work FAST!.

All contributions are welcome!

@marcus