More TCPClient strangeness, including a RED LED!

This is an offshoot of my other post, where the TCPClient was doing weird things.
This is weirder.

First, I just did a git pull on all three parts: core-common-lib, core-communication-lib and core-firmware.

Second, here’s the simple code:

#include "application.h"

const char server[] = "www.random.org";
const char url[] = "/integers/?num=1&min=1&max=100&col=6&base=10&format=plain";
TCPClient TCP;

void setup (void)
{
   pinMode (D7, OUTPUT);
   Serial.begin(38400);
   delay (2000);
   Serial.println ("Welcome");
   digitalWrite (D7, HIGH);
}


void sendGetRequest (const char *server, const char *url)
{
   if (TCP.connect (server, 80))
   {
      TCP.print ("GET ");
      TCP.print (url);
      TCP.println ("HTTP/1.0");
      TCP.println ("Connection: close");
      TCP.print ("Host: ");
      TCP.println (server);
      TCP.println ("Accept: text/html, text/plain");
      TCP.println ();
      TCP.flush();
   }
}

void loop (void)
{
   char buf[32];

   // unsigned long response;
   unsigned long count = 0;
   unsigned long s = millis();
   digitalWrite (D7, HIGH);
   sendGetRequest (server, url);
   while (!TCP.available()) {}
   TCP.stop();
   digitalWrite (D7, LOW);
   unsigned long e = millis();
   sprintf (buf, "et = %lu, count = %lu\n", e-s, count);
   count = TCP.available();
   while (count--) TCP.read();
   Serial.println (buf);
   delay (5000);
}

I usually get three reports of successfull communication (the LED toggles 3 times) but on the fourth (sometimes 3rd) the LED stays lit a LONG time, then the RED LED starts doing the SOS thing, and then the core resets, but it’s not this app that’s running.

Ideas? Suggestions?

Thanks.

Gee, that code looks familiar :smiley:!

The red LED SOS means that there was a panic. the number of flashes between SOS n-flashes SOS tells you the panic code.

I don't think you should be reading after you have called TCP.stop() since I think the buffers get flushed and reused. Try moving the stop() call down to after the while(count--) TCP.read(); loop.

Also one thing @Dave has been saying is to try putting a small delay after sendGetRequest() but before the while(!TCP.available()) loop. I have not found that to be needed but maybe it depends on your internet router etc. I call TCPClient.flush() at the end of the GET request on the assumption that I won't be interested in any bytes left in the buffer.

Finally I am assuming this part is just some kind of mistake and the bash junk is not really in your file since it wouldn't compile:

1 Like

I found a bug. There is no space before “HTTP/1.0”. That would do it.

All of this is hampered by not being able to see the wifi traffic.

Thanks @bko. I have made my corrected code work by using Springfield into a local buffer and sending that instead of the broken up strings.

I’ll post my final results.

I am having a similar issue. The code compiles but when I run it, red led appears. I don’t know what happens. What could it be?

#include "application.h"
 
/* ================ HTTP ================ */
TCPClient client;
char buffer[1024];
char currentLine[1024];
int ptr=0;

char * http_get(char const * hostname, String path) {
 
  if (client.connect(hostname, 80)) {
    client.print("GET ");
    client.print(path);
    client.print(" HTTP/1.1\r\n");
    client.print("HOST: ");
    client.println(hostname);
    client.println("Accept-Encoding: ");
    client.println("Connection: close");
    client.print("\r\n\r\n\r\n");
  } else {
    Serial.println("\r\n\r\nConnection Failed!");
    client.stop();
    return NULL;
  }
 
  uint32_t lastRead = millis();
  while ((millis() - lastRead) < 1000) {
    while (client.available()) {
      currentLine[ptr++] = client.read();
      //Serial.print(c);
      lastRead = millis();
    }
  }
  client.flush();
  client.stop();
  //String response(buffer);
  //return buffer;
  return currentLine;
}
 
/* ================ APPLICATION.cpp ================ */

void setup() {
  pinMode(D7, OUTPUT);
  digitalWrite(D7, HIGH);
  Serial.begin(115200);
  while (!Serial.available()); // After Core D7 LED turns on, open serial monitor and press enter!
}
 
uint32_t nextTime = millis(); // next time to contact the server
int count = 19;
bool state = 1;
void loop() {
  if (millis() > nextTime) {
    nextTime = millis() + 500UL;
    if (++count == 20) {
      count = 0;
      char * response = http_get("subdomain.domain.com", "/subir?id=core2");
      Serial.println(response);
     
    } else { // toggle the led while we wait for 10 second mark
      state = !state;
      digitalWrite(D7, state);
    }
  }
}

Is your LED flashing red with SOS (... --- ...) followed by 8 flashes in a row? I suspect you could be running out of heap memory. Have you thought about reducing the two 1k byte buffers? One of them appears to not be used. There is lots of buffering already going on in the TCP stream so you should try to minimize what you need to buffer.

Also I would not try to return a string from your GET request code. The string is already global, just null terminate it and you are done. You should null terminate it either way since you are likely running off the end of it.

bko is correct, you have some wasted RAM there. That by itself may fix your problem, but I would think the Spark Core should be able to allow 2KB of user RAM usage.

Looks like you may be using some old code that I worked on… try taking a look at this latest code of mine that works fine today:
https://community.spark.io/t/spark-maker-kit-facebook-likes-alert/3418

1 Like