Arduino shield request from multiple servers

i have been trying to figure out how to get the arduino to send a file to server 1 and read the telnet console from server 2. so far i have one or the other. i have tried adding another server address but i dont know how to make the arduino connect to one at a time then repeat. heres a copy of my choppy code that works if i only try 1 server. if i try 2 servers it pulls info from server 2 then moves on to server 1. then when it goes back around the loop it freezes at "

void loop(void) {
  Serial.println("stuck");
    while(client.available() == 0) delay(500);
#include <SPI.h>
#include <Ethernet.h>
#include <SdFat.h>

SdFat sd;
#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
DHT sens1(SENS1, DHTTYPE); // create sensor object called mySensor

byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //the actual value matches my Arduino Ethernet
IPAddress ip(10,0,0,55); //the actual value matches my Arduino Ethernet

IPAddress server(10, 0, 0, 89);
IPAddress server2(10,0,0,50);  //the actual value matches my telnet server

EthernetClient client2;
EthernetClient client;
int txt = 0;
int localport =19126;
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
ArduinoOutStream cout(Serial);

#define error(s) sd.errorHalt(PSTR(s))

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;
  fsize = rdfile.fileSize() - rdfile.curPosition();
  
  Serial.println(fsize);
  // check for open error
  if (!rdfile.isOpen()) error("File could not be opened, check configuration...");

  Ethernet.begin(mac, ip);
  
  delay(500);
  
  Serial.println("Connecting to server for transfer...");
 Serial.println(Ethernet.localIP());
  // if you get a connection, report back via serial:
  if (client.connect(server, localport)) {
    Serial.println("Connected");

  while ((n = rdfile.fgets(rbuffer, sizeof(rbuffer))) > 0) {
       client.write(rbuffer, sizeof(rbuffer)-1);
  }
    
    client.stop();
  }
  else {
    // No response from server reply
    Serial.println("No server available");
  }
}

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...");

  if (client2.connect(server2, 23)) {
    Serial.println("connectedddddd");


 
  }
}
void loop(void) {
  Serial.println("stuck");
    while(client.available() == 0) delay(500);
  // get the response
  while (client.available() > 0) {
      char c = client2.read();
      Serial.print(c);
  }
  
  float tempC = sens1.readTemperature(true);
  float tempF = sens1.readHumidity(true);

  if (isnan(tempC) || isnan(tempF)) {
    NoSensor = 1;
  } else {
    NoSensor = 0;
  }

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

  }
  
   
 if (txt == 1) {
if (!sd.begin(chipSelect, SPI_FULL_SPEED));

  ftransfer();

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

    mySensorData.print(" Temp");
    mySensorData.println(""); 
    mySensorData.print( tempF );                         //Humidity
    mySensorData.print("%");  
     mySensorData.println("");  
    mySensorData.print( tempC );                         //Humidity
    mySensorData.print("F");  
   
    //Pause between readings.
    mySensorData.close();
     ftransfer();
    delay(1300);
    sd.remove("PTData.txt");
   char c = 0;
    Serial.println("okay");//close the file
    }
}

while(client.available() == 0) delay(500);

While there is no client connect, bury your head in the sand for 1/2 a second. It would make more sense, to me, to spend that time seeing if the telnet server had anything to say.

IPAddress server(10, 0, 0, 89);
IPAddress server2(10,0,0,50);  //the actual value matches my telnet server

EthernetClient client2;
EthernetClient client;

Is that the way you count? Uh-huh, two, three? Most people count one, two, three...

    Serial.println("connectedddddd");

Doessssss thissssss reallyyyyyyy conveyyyyyy moreeeeee informationnnnnn?

 if (txt == 1) {

Why would anyone expect a variable named txt to contain numeric data?

PaulS:
While there is no client connect, bury your head in the sand for 1/2 a second. It would make more sense, to me, to spend that time seeing if the telnet server had anything to say.

    Serial.println("connectedddddd");

Doessssss thissssss reallyyyyyyy conveyyyyyy moreeeeee informationnnnnn?

 if (txt == 1) {

Why would anyone expect a variable named txt to contain numeric data?

thats just debug info for me so i can tell what servers it connected to. and a couple commands i threw in there to delete some files.

im not sure how to setup the connection for 2 servers. i already tried with an "if(client.available() == 0) delay(500);" but i assume the servers not connected and i dont know how to initialize the connection

i already tried with an "if(client.available() == 0) delay(500);" but i assume the servers not connected and i dont know how to initialize the connection

That approach is ass-backwards. Instead of waiting for a client connection, like a blind date, to show up, do something when the client (blind date) does show up. Meanwhile, you can do other things.

im trying to connect to server2 with the arduino shield. its connecting to another arduino running a telnet server and it passed information from the console of server2 to the arduino shield. after i can get it to work for both, connection to server2 and server then ill have it datalog the server information. server2 is a esp8266/arduino running esp-link. so now im stuck i dont know what to do now. if i delete the server2 and related daata then server1 does exactly what i want it to. i was trying to merge another sketch with this one and im not sure how to setup the variables and parameters for 2 connection request. server 2's telnet console constantly updates so if it was connected and listening for 2 seconds that would be plenty of time to get what i needed and be time to move on to server1's job

How would YOU manage the problem? You have two servers, server1 and server2, NOT server and server2, that you want data from.

What I would do is connect to server1, get the data, and do something with it. Then, I’d connect to server2, get the data, and do something with it.

But, your code has some real problems, aside from the poor names and blocking nature.

  while (client.available() > 0) {
      char c = client2.read();
      Serial.print(c);
  }

While client(1) has data, it doesn’t make sense to read from client2.

PaulS:
How would YOU manage the problem? You have two servers, server1 and server2, NOT server and server2, that you want data from.

What I would do is connect to server1, get the data, and do something with it. Then, I'd connect to server2, get the data, and do something with it.

But, your code has some real problems, aside from the poor names and blocking nature.

  while (client.available() > 0) {

char c = client2.read();
     Serial.print(c);
 }



While client(1) has data, it doesn't make sense to read from client2.

i got it to reconnect to server2 by changing that to this

void loop(void) {
  EthernetClient client2;
  delay(1000);
   if (client2.connect(server2, 23)) {
    if(client2.available() == 0) delay(5000);
  // get the response
  while (client2.available() > 0) {
      char c = client2.read();
      Serial.print(c);

however now i have to figure out how to log server2's data to a text file on the sdcard. its a job for tomorrow. so far i have it working with blueiris to setup my "half ass" text overlays. it works great for updating the temp and humidity overlay on the camera screen. but i would still like some additional data for my liking. hopefully when/if i get server 2 running correctly i can figure out how to organize the variables in the text file

    if(client2.available() == 0) delay(5000);

You seem completely fixated on doing nothing as much as possible. Someday, I hope you wake up and smell the coffee. When there is something to do, such as read data from the client2 instance, do it. When there is nothing to do, look for something else to do.

void loop(void) {
  EthernetClient client2;
  delay(1000);

Here's another example. On EVERY pass through loop(), you waste a full second. Why?

PaulS:

    if(client2.available() == 0) delay(5000);

You seem completely fixated on doing nothing as much as possible. Someday, I hope you wake up and smell the coffee. When there is something to do, such as read data from the client2 instance, do it. When there is nothing to do, look for something else to do.

void loop(void) {

EthernetClient client2;
  delay(1000);



Here's another example. On EVERY pass through loop(), you waste a full second. Why?

because otherwise it seems to take the reading too fast and alot of the times not get any data..
since this might be a good project for people with the blueiris camera software maybe if you feel like it you could rewrite it to be more professional and clean so other people can use it if they want. im sure im not the only person that wants arduino temp sensor overlays on their screens. on the pc side i have a loopign batch program run a python script in a hidden background to constantly update the text files containing the sensor information

because otherwise it seems to take the reading too fast and alot of the times not get any data..

Why is that a problem? If there is no (new) data, there is nothing (new) to do. Don't bury your head in the sand, though.

PaulS:
Why is that a problem? If there is no (new) data, there is nothing (new) to do. Don’t bury your head in the sand, though.

i have gone through and cleaned it up a little bit. the 4 second delay at the very end of the loop is there so my computer is not updating the text files too fast and it takes some load off the cpu. the 2 second delay is the only way i can get data from the telnet console of server2 because it updates about once a second.

#include <SPI.h>
#include <Ethernet.h>
#include <SdFat.h>

SdFat sd;
#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
DHT sens1(SENS1, DHTTYPE); // create sensor object called mySensor

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; //socket for python script 
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
ArduinoOutStream cout(Serial);

#define error(s) sd.errorHalt(PSTR(s))

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

  //Obtain file size
  uint32_t fsize;
  fsize = rdfile.fileSize() - rdfile.curPosition();

  Serial.println(fsize);
  // check for open error
  if (!rdfile.isOpen()) error("File could not be opened, check configuration...");

  Ethernet.begin(mac, ip);

  Serial.println("Connecting to server for sensor data transfer...");
  Serial.println(Ethernet.localIP());
  // if you get a connection, report back via serial:
  if (client.connect(server1, localport)) {
    Serial.println("Connected");

    while ((n = rdfile.fgets(rbuffer, sizeof(rbuffer))) > 0) {
      client.write(rbuffer, sizeof(rbuffer) - 1);
    }
    client.stop();
  }
  else {
    // No response from server1 reply
    Serial.println("No server available");
  }
}

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) {
  EthernetClient client2;
  if (client2.connect(server2, 23)) {
    if (client2.available() == 0) delay(2000);
    while (client2.available() > 0) {   //read data from server2 telnet
      char c = client2.read();
      Serial.print(c); //print telnet data in serial console

    }
  }
  float tempF = sens1.readTemperature(true);
  float Hum = sens1.readHumidity(true);

  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("Grow Temp");
    mySensorData.println("");
    mySensorData.print( tempF );                         //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
    delay(4000);
  }
}