Using TCPClient to read a file from a server

Hi all,

I’m looking to print bitmaps with my thermal printer and Spark Core. I can’t send bitmaps through the Spark API as there are strict size limits there, so my thought was I could use TCPClient to request the bitmap from the server, save it as a variable, and then print it.

Currently I’m able to connect to a server and read the contents of the file:

if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }

which is all well and good, but I’d like to write this to a variable – and then extract just the file contents.

Any help would be much appreciated!

thats a tricky one… there is a few things that will need to happen… ill try and give you a little push in the right direction till someone who knows what they are doing can chime in!

Overview:
send the request to the server, read the response and save it somehow.
then parse the response header to get the content length, find the start of the body, then take the content length, and make sure there is nothing else…

below is an example i use for saving data from a serial port, not a TCP connection but same theory applies. this looks for a line feed or carrage return and then throws data after that away. Thats NOT what you want… but i included it because it may help you find between each line in the header and then differenciate the body which from memory is after /r/n/r/n. its probably worth seeing exactly what gets returned and write the code to suit.

Just rattling away some more off the top of my head… i would read the first 100 -200 characters as a guess which should be the header, at each /r/n check the header label and value and compare it to what you expect… ie the first one should be
HTTP/1.1 200 OK
then check the rest with maybe a case statement… most of which you could throw away. i think in your case maybe content type and content length will be important. if there is a /r/n/r/n you have reached the body, save data from then until you get your content length

void readString(char *ptr, int length) {

    int pos = 0;

    while (!Serial.available()) SPARK_WLAN_Loop(); //wait for serial data to come in 
    active = millis();
    while (Serial.available()) {

        inChar = Serial.read();
        if (inChar == 0x0A || inChar == 0x0D)
            break;
        ptr[pos] = inChar;
        pos++;
        delay(10);


        if (pos >= length - 1)
            break;
    }
    ptr[pos] = '\0';

    while (Serial.available())
        (void)Serial.read(); //throw it away

    return;
}
2 Likes

Do you have the returned print data from when you did the serial print? that will help me work out a bit more of what you need…

I wonder if there is a library to deal with all that already? im sure your not the first…

Hi @hootie81, thanks for the reply. I’ll attempt to rewrite your code to get it working for TCP.

Here’s the Serial response I get from the server (currently I’m just reading my bitmap from a pastebin):

HTTP/1.1 200 OK
Date: Thu, 16 Oct 2014 10:48:44 GMT
Content-Type: text/plain; charset=utf-8
Connection: close
Set-Cookie: __cfduid=d428a2018229b075d16d28abaf2e9f4291413456524239; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.pastebin.com; HttpOnly
X-Powered-By: PHP/5.5.5
Set-Cookie: cookie_key=1; expires=Thu, 13-Nov-2014 10:48:44 GMT; Max-Age=2419200; path=/; domain=.pastebin.com
Set-Cookie: realuser=1; expires=Fri, 17-Oct-2014 10:48:44 GMT; Max-Age=86400; path=/
Vary: Accept-Encoding
Server: cloudflare-nginx
CF-RAY: 17a3af8ab53e0b7b-LHR

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0xff,0xff,0xff,0xff,0x00,0x01,0xff,0xff,0xff,0xf8,0x01,0xff,0xff,0xff,0xff,0xc0,
0x07,0xff,0xff,0xff,0xff,0x00,0x01,0xff,0xff,0xff,0xf8,0x01,0xff,0xff,0xff,0xff,0xc0,
0x07,0xff,0xff,0xff,0xff,0x00,0x01,0xff,0xff,0xff,0xf8,0x01,0xff,0xff,0xff,0xff,0xc0,
0x07,0xff,0xff,0xff,0xff,0x00,0x01,0xff,0xff,0xff,0xf8,0x01,0xff,0xff,0xff,0xff,0xc0,
0x07,0xff,0xff,0xff,0xff,0x00,0x01,0xff,0xff,0xff,0xf8,0x01,0xff,0xff,0xff,0xff,0xc0,
0x07,0xc0,0x00,0x00,0x1f,0x00,0x3f,0xff,0x80,0x00,0xf8,0x01,0xf0,0x00,0x00,0x07,0xc0,
0x07,0xc0,0x00,0x00,0x1f,0x00,0x3f,0xff,0x80,0x00,0xf8,0x01,

you get the idea.