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);
}
}