Datalogging telnet session to sdcard

Hi, im using an arduino with an ethernet shield and sdcard. i have managed to connect to the server and print it to the serial screen but i cant get it to print to the sdard. this part of the code im working with is not working correctly. it wont save the data from client to to the sdcard instead it just saves random letters. please help!

  EthernetClient client2;
  if (client2.connect(server2, 23)) {
    if (client2.available() == 0) delay(2000);
    mySensorData2 = sd.open("PTData.txt", FILE_WRITE);
    while (client2.available() > 0) {   //read data from server2 telnet
      char c = client2.read();
       mySensorData2.print(c);
      mySensorData2.close();
      ftransfer2();
   
  
    }
  }

the data transfer works

Post you whole code. Check also if closing the sdcard file after sending each character is a good idea.

If you put the { on a line BY ITSELF, and properly indented your code, you'd have:

   mySensorData2 = sd.open("PTData.txt", FILE_WRITE);
    while (client2.available() > 0)
    {
       char c = client2.read();
       mySensorData2.print(c);
       mySensorData2.close();
       ftransfer2();
    }

So, if you connect to the telnet server, and there is no data immediately available, you wait two seconds, in the hope that data will arrive.

After two seconds, you open the file. Then, if there is data to read, you read ONE character, print it to the file, close the file, and call some function whose name means nothing.

Then, you read the rest of the characters, and try to write them to the file you closed, which of course won't work worth a damned.

Give some SERIOUS thought about when to open the file and when to close it.

heres the whoel code. sorry im sure its choppy. everything except the process of reading and saving the telnet information works,

#include <SPI.h>
#include <Ethernet.h>
#include <SdFat.h>
#include "Wire.h"
#include "DHT.h"
#define SENS1 2
#define DHTTYPE DHT22 // import the Pressure Sensor Library We are using Version one of Adafruit API for this sensor
#define error(s) sd.errorHalt(PSTR(s))
DHT sens1(SENS1, DHTTYPE); // create sensor object called mySensor
SdFat sd;

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //the actual value matches my Arduino Ethernet
IPAddress ip(10, 0, 0, 55); //ipaddr of arduino
IPAddress server1(10, 0, 0, 89);
IPAddress server2(10, 0, 0, 50); //telnet server of esp8266 esplink
EthernetClient client2;
EthernetClient client;

int localport = 19126;
int localport2 = 19127;
int NoSensor = 0;
int NoSensorAlert = 0;
const uint8_t chipSelect = 4; //chipSelect pin for the SD card Reader
File mySensorData; //Data object you will write your sesnor data to
File mySensorData2;
ArduinoOutStream cout(Serial);


void ftransfer() {
  char rbuffer[2]; //Chunk can be increased to bigger sizes, be careful about memory!!
  char c;
  int n; // open test file
  SdFile rdfile("PTData.txt", O_READ); // <--- Name of file to be transfered
  uint32_t fsize; //Obtain file size
  fsize = rdfile.fileSize() - rdfile.curPosition();
  Serial.println(fsize);
  if (!rdfile.isOpen()) error("File could not be opened, check configuration...");  // check for open error
  Ethernet.begin(mac, ip);
  Serial.println("Connecting to server for sensor data transfer...");
  Serial.println(Ethernet.localIP());
  if (client.connect(server1, localport)) {
  Serial.println("Connected");   // if you get a connection, report back via serial:
    while ((n = rdfile.fgets(rbuffer, sizeof(rbuffer))) > 0) {
      client.write(rbuffer, sizeof(rbuffer) - 1);
     }
    client.stop();
  }
  else {
    Serial.println("No upload server available");     // No response from server1
  }
}

void ftransfer2() {
  char rbuffer2[2]; //Chunk can be increased to bigger sizes, be careful about memory!!
  char d;
  int o; // open test file
  SdFile rdfile2("PTData2.txt", O_READ);
  uint32_t fsize2; //Obtain file size
  fsize2 = rdfile2.fileSize() - rdfile2.curPosition();
  Serial.println(fsize2);
  if (!rdfile2.isOpen()) error("File could not be opened, check configuration...");  // check for open error
  Ethernet.begin(mac, ip);
  Serial.println("Connecting to server for sensor data transfer2...");
  Serial.println(Ethernet.localIP());
  if (client.connect(server1, localport2)) {
  Serial.println("Connected");   // if you get a connection, report back via serial:
    while ((o = rdfile2.fgets(rbuffer2, sizeof(rbuffer2))) > 0) {
      client.write(rbuffer2, sizeof(rbuffer2) - 1);
     }
    client.stop();
  }
  else {
    Serial.println("No upload server available");     // No response from server1
  }
}


void setup() {
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  sens1.begin();
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  sd.begin(4); //Initialize the SD card reader
  delay(400);  // catch Due reset problem
  Serial.println("connecting to server2...");
  if (client2.connect(server2, 23)) {
    Serial.println("connected to server2");
  }
}
void loop(void) {
float tempF = sens1.readTemperature(true);
  float Hum = sens1.readHumidity(true);
  
  EthernetClient client2;
  if (client2.connect(server2, 23)) {
    if (client2.available() == 0) delay(2000);
    mySensorData2 = sd.open("PTData.txt", FILE_WRITE);
    while (client2.available() > 0)
    {   
      char c = client2.read();
      delay(3000);
       mySensorData2.print(c);
      mySensorData2.close();
      ftransfer2();
    }
  
    }
  
  

  if (isnan(tempF) || isnan(Hum)) {  //sens1 connection check
    NoSensor = 1;
  } else {
    NoSensor = 0;
  }

  if (NoSensor == 1 && millis() - NoSensorAlert >= 10000) {
    NoSensorAlert = millis();
    Serial.println("Check The DHT Sensor '1' pin 2");
  }

  mySensorData = sd.open("PTData.txt", FILE_WRITE);
  if (mySensorData) {
    Serial.print("The Humidity is: "); //Print Your results
    Serial.print(Hum);
    Serial.println("%");
    Serial.println("");
    Serial.print(tempF);  //TEMP
    Serial.println("Degrees F");
    Serial.println("");
    delay(200); //Pause between readings.

    mySensorData.print(" Temp");
    mySensorData.println("");
    mySensorData.print( Hum );                         //Humidity
    mySensorData.print("%");
    mySensorData.println("");
    mySensorData.print( tempF );                         //Humidity
    mySensorData.print("F");
    mySensorData.close();
    ftransfer();
    sd.remove("PTData.txt");
    Serial.println("okay");//close the file
}
  }

i guess the question is how do i get it to print more than just a character to the sdcard?

PaulS:
If you put the { on a line BY ITSELF, and properly indented your code, you'd have:

   mySensorData2 = sd.open("PTData.txt", FILE_WRITE);

while (client2.available() > 0)
    {
      char c = client2.read();
      mySensorData2.print(c);
      mySensorData2.close();
      ftransfer2();
    }



So, if you connect to the telnet server, and there is no data immediately available, you wait two seconds, in the hope that data will arrive.

After two seconds, you open the file. Then, if there is data to read, you read ONE character, print it to the file, close the file, and call some function whose name means nothing.

Then, you read the rest of the characters, and try to write them to the file you closed, which of course won't work worth a damned.

Give some SERIOUS thought about when to open the file and when to close it.

ftransfer2(); isnt just nothing it calls the file transfer

and i dont understand what you mean about when to close and open the file. will you please tell me what to do here

will you please tell me what to do here

No, but I’ll make you think. If you are to put all the mail that arrives for your neighbor in a box for the time he is on vacation, when do you get the box (open the file)? When do you tape the box up (close the file)?

BEFORE you write ANY code, you should write some comments that show that you have thought about the correct order of events.

// Is there a client with data?

// Open the file

// Read each character from the client, and write it to the file

// Close the file

Now, figure out where to put these comments in your code, and you’ll see what the problem is, and what the solution is.

PaulS:
No, but I'll make you think. If you are to put all the mail that arrives for your neighbor in a box for the time he is on vacation, when do you get the box (open the file)? When do you tape the box up (close the file)?

BEFORE you write ANY code, you should write some comments that show that you have thought about the correct order of events.

// Is there a client with data?

// Open the file

// Read each character from the client, and write it to the file

// Close the file




Now, figure out where to put these comments in your code, and you'll see what the problem is, and what the solution is.

i dont quite understand. what your saying looks exactly like this

if (client2.connect(server2, 23)) {
    if (client2.available() == 0) delay(2000);
    while (client2.available() > 0)  //theres data to be read
    {   
    mySensorData2 = sd.open("PTData2.txt", FILE_WRITE); // open the file
    char c = client2.read();  //read "a" character?
    mySensorData2.print(c); //write it to the file
    mySensorData2.close(); //close the file 
      ftransfer2(); //init transfer
    }

is the problem that it closes the file before it reads more than 1 character?

okay i get it but what is the best way to go about opening and closing the file. obviously not in the loop like that. i set a serial menu to manually open and close the file and i got the data perfectly but i cant decide the easiest way to go about it yet

obviously not in the loop like that.

Obviously. It is also obvious that you did NOT read, understand, or follow, or all of the above, my previous post.

Open the file. Loop to read all the data. Close the file.