Description

writeFromClient() this function receives data from an internet client (e.g.WiFi client) and writes it into the file. This function does not block. It returns 0 if there is currently no further data available from the server. This function could be used to download data from a webserver or from a tcp client in the network.

Syntax

writeFromClient( opt_Client ); – reads the currently available data from the client pointed at by opt_Client and write it into the file.

Parameters

Client* opt_Client – pointer to an object of the Arduino core class ‘Client’ or a derived class e.g. CANIP_Client, WiFiClient, EthernetClient etc. For http requests (file upload / downloads) there are more convenient HTTP_Client classes. The client needs to have an established connection to a tcp-peer (e.g. webserver) before invoking this function and the file needs to be opened first.

Returns

int:
-2: if there was an error while writing into the file (e.g. file is full).
-1 : if the client connection to the server is closed.
0: if no data is currently available to be read from the server (Client-Object)
>0: number of bytes that could successfully be read from the ‘Client’ and written into the file.

Example Code

This example downloads a file from a webserver to the SerialFlash using a GET request via a CANIP connection to the internet.

#include "CANIP.h"
#include <SerialFlash.h>

CANIP_Client myCANIP_client(&MCAN0, &BOOTLOADER_SETTING);
SerialFlashFile tempFile;
int i_Response;      

void setup()
{  
  String s_server = "www.di-ce.de";
  String s_path = "/downloads/";
  String s_fileName = "dice-ascii-logo.txt";
  
  SerialFlash.begin();  
  while(!Serial);
  Serial.println("STARTUP: "+BOOTLOADER_SETTING.s_get_app_name()+" (V" + BOOTLOADER_SETTING.u32_get_app_version()+")");
  // Check if the file exists already in the Serial Flash
  tempFile = SerialFlash.open(s_fileName.c_str());
  if(tempFile)
  {
    Serial.println("The file "+s_fileName+" exists already. Please remove it first");
    while(1){ yield(); };
  }  
  // Connect to the internet and the server
  Serial.print("Trying to get an internet connection...");
  myCANIP_client.b_async_op = false;
  i_Response = myCANIP_client.httpGET(s_server, s_path + s_fileName);
  Serial.print("Server response: ");
  if(i_Response <= 0)
  {
    Serial.println(String(i_Response)+ " (error)");
    myCANIP_client.stop();
    while(1){ yield(); };
  }  
  // Create and open the file in serial flash
  SerialFlash.create(s_fileName.c_str(), i_Response, 1);
  tempFile = SerialFlash.open(s_fileName.c_str());  
  // Download the file into the serial flash
  Serial.println(" 200 OK\nDownloading "+s_fileName+" - "+ String(i_Response) + " bytes...");
}

void loop()
{
  static int t = 0;  
  int i = tempFile.writeFromClient(&myCANIP_client);
  if(i == -1)
  {
    Serial.println(" - connection closed -");
    myCANIP_client.stop();
    while(1){ yield(); };   
  }
  if(i == -2)
  {
    Serial.println(" - File full / Write error -");
    myCANIP_client.stop();
    while(1){ yield(); };   
  }
  if(i > 0)
  {
    i_Response -= i;
    t++;
    if(t > 20)
    {
      Serial.println("Bytes remaining: "+String(i_Response));
      t = 0;
    }
  }
}

see example ‘File -> Examples -> SerialFlash -> examples -> RecordsetFiles’

Notes and Warnings

It is a non blocking function so that an application that should download data from an server can do so without stalling the main task.

Note that in order to use this function, the ‘file’ has to be opened first and the ‘Client’ has to have an established connection to its peers.

See also

GitHub – PaulStoffregen/SerialFlash – org. code this library is based on
c_str() – function to create a 0-terminated char array from a String
open() – open a file for read/write operations