Coding help to display image data over ESP8266

Hi,

What i try to accomplish:

I have an Arduino Mega with a CTE TFT shield connected to a SSD1963 (CTE70)
Display. Also connected to this Arduino is my ESP8266 programmed with AT firmware.

I try to fetch over the ESP8266 an image what i have on my webserver send this over
serial to my Mega and display it on my display.

What i have at the moment is that it is connecting to my webserver and is fetching the image but
what i receive are pieces of the Image containing multiple times SEND OK, then some data etc.

Perhaps somebody can point me in the right direction what i am doing wrong or if it is even possible what
i try to do.

#include <ArduinoJson.h>
#include <UTFT.h>
#include <Time.h>
#include <Timer.h>
#include <tinyFAT.h>
//#include <UTFT_tinyFAT.h>
#include "OneWire.h"
#include "DallasTemperature.h"
#include <UTFT_SdRaw.h>
#include <SdFat.h>



#define SD_CHIP_SELECT  53  // SD chip select pin

const int buffer=400;//JSON Buffer size


// file system object
SdFat sd;
SdFile myFile;


#define LOCATIONID "AT/Salzburg" // location id
#define ONE_WIRE_BUS 16 

// WLAN
#define SSID "WLAN" // insert your SSID
#define PASS "PASS" // insert your password

// Camera
#define DST_IP "192.168.20.200"

//Display
UTFT  myGLCD(CTE70, 38, 39, 40, 41);
UTFT_SdRaw myFiles(&myGLCD);


char fileName[] = "2468.txt";


byte response;
byte incomingbyte;
boolean EndFlag=0;
int j=0, k=0, count=0;


//Check Module and connect to WLAN
void espLogin()
{
  
  Serial1.println("AT+RST"); // reset and test if module is redy
  delay(5000);
  if (Serial1.find("OK")) {
    Serial.println("WiFi - Module is ready");   
  } else {
    Serial.println("Module doesn't respond.");    
    delay(1000);
    espLogin();
  }
  delay(2000);
  // try to connect to wifi
  boolean connected = false;
  for (int i = 0; i < 5; i++) {
    if (connectWiFi()) {
      connected = true;     
      Serial.println("Connected to WiFi..."); 
      delay(2000);
      getimage();    
     }
  }
  if (!connected) {   
    Serial.println("Coudn't connect to WiFi.");
    delay(6000);
    espLogin();
     }
    delay(5000);
}

boolean connectWiFi()
{
 String cmd = "AT+CWJAP=\"";
  cmd += SSID;
  cmd += "\",\"";
  cmd += PASS;
  cmd += "\"";
  Serial.println(cmd);
  Serial1.println(cmd);
  delay(2000);
  if (Serial1.find("OK")) {
    Serial.println("OK, Connected to WiFi.");
    return true;
    } else {
    Serial.println("Can not connect to the WiFi.");
    delay(5000);
    return false;
  } 
  getimage();
}

void getimage() 
{       
        myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END);

  
        String out = "";        
        String cmd = "AT+CIPSTART=\"TCP\",\"";
        cmd += DST_IP;
        cmd += "\",80";
        Serial1.println(cmd);
        Serial.println(cmd);
        if(Serial1.find("ERROR")) return;

        cmd = "GET /test.jpg";
        cmd += " HTTP/1.1\r\nHost: 192.168.20.200\r\n\r\n";
        
        Serial1.print("AT+CIPSEND=");
        Serial1.println(cmd.length());
        Serial.println(cmd.length());


        if(Serial1.find(">")){
        Serial.println(">");
        }else{   
        Serial1.println(F("AT+CIPCLOSE"));
        Serial.println("Connection get Forecast timeout");
        delay(1000);
        getimage();
        } 
        
        Serial1.print(cmd);
        Serial.print(cmd);
        delay(1000);
        while(Serial1.available()>0) {
        incomingbyte=Serial1.read();
        out += (char)incomingbyte;
        //Serial.print(out); 
      myFile.print((char)incomingbyte);
      
    }   
  myFile.close();  

  delay(3000);
  myGLCD.print("out",100,150,150);

// Done with processing for now - close connection
Serial1.println("AT+CIPCLOSE");
}


void parseJSON(char json[400])
{
  Serial.println(json);
  StaticJsonBuffer<buffer> jsonBuffer;
 JsonObject& root = jsonBuffer.parseObject(json);
 
 if (!root.success())
 {
  Serial.print("?fparseObject() failed");
  return;
 }
}

void setup()
{  

  Serial.println(F("Initialising SD card..."));
  bool mysd = 0;
  // see if the card is present and can be initialized:
  while (!mysd)
  {
    if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED)) {
      Serial.println(F("Card failed, or not present"));
      Serial.println(F("Retrying...."));
    }
    else
    {
      mysd = 1;
      Serial.println(F("Card initialised."));
    }
  }


  
   Serial1.begin(115200);
   Serial.begin(115200); // Debug

   myGLCD.InitLCD(LANDSCAPE);
   myGLCD.clrScr();
   myGLCD.setColor(0,255,0);
 
   espLogin();
   getimage(),
   delay(10000);  
}

void loop()
{

}
const int buffer=400;//JSON Buffer size

So, it isn't a buffer. Why is it called that, then?

        Serial.println("Connection get Forecast timeout");

In getImage()?

        getimage();

Why are you recursively calling getimage()?

And, why do you call that function if the connection timed out?

        out += (char)incomingbyte;

Why?

You seem to assume that the entire image will arrive all at once, at least while you are f**king around adding binary data to a String instance. That is most unlikely to be a valid assumption.

  myGLCD.print("out",100,150,150);

How is printing the string "out" useful?

Hi,

Thanks for criticizing my code. I know it is far from perfect and
there are a lot of code not necessary and wrong. I played a lot
with the code to see at some points what it does.
getimage(); is called for example because the ESP8266 did had
wirless connection and it said could't connect to Wifi.

Main question is when i have data available on the Serial1 input and the
ESP8266 is sending the response from the AT+CIPSEND command
it is receiving the data in blocks.

You are correct. I expect i receive +IPD,1430:HTTP/1.1 200 OK and then a
data stream of the response. and not HTTP/1.1 200 OK some data and then
again HTTP/1.1 200 OK some data. According other posts it sends blocks of 2048Bytes.

So how do i put those pieces of data together to receive a valid image data stream what
i can write out to for examle an SD card or display.

I expect i receive +IPD,1430:HTTP/1.1 200 OK and then

The first thing you need to know is what +IPD means. Then, what does 1430 mean? The rest of the statement means that the server recognized the request and was able to process it.

The packet may contain up to 2048 bytes (not 2048Bytes). It may contain less. It will never contain more. You need to know how many it does contain.

You need to read each of the bytes in the response, and do something with them, and ONLY with them.

If the image is going to come back in 2048 byte chunks, and the first packet is full, you need to read all of it, and then request another one. You do that over and over until you get a packet that contains less than 2048 bytes. That is the last packet, so you simply read and handle all of the bytes.

What you do with them depends on the ultimate goal. Most libraries for displaying an image on the screen expect the data to be in PROGMEM, since there is not, usually, enough SRAM to hold an image. PROGMEM is not, typically, writable at run time.

The library MIGHT provide a method to read the image data from a file, though that is unlikely, but wouldn't be that hard to add.