Mixing 2 programs

Hi I am trying to get my (badly written) temperature monitor program and a wifi server program to mix.
but as usual I am now lost. the idea was to get the serial.print to be changed to client.print and so output the reading to a webpage, and then maybe later use the switch in there to turn on the fan power. but the wifi program seems to have stopped my loop. I have tried to remove the blockage and it just breaks everything and I get error after error.
any SIMPLE solutions to how to get the two to play nice so I can see the results on the webpage and maybe turn on or off the fan via webpage would help seems everything I do with the new wifi program creates an out of scope error.

#include <Wire.h>
#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(9, 8); // RX, TX
#endif

char ssid[] = "e";            
char pass[] = "Nt";        
int status = WL_IDLE_STATUS;
int ledStatus = LOW;
WiFiEspServer server(80);
// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);
//wifi end


// SHT31 I2C address is 0x44(68)
#define Addr 0x44
#define Addr2 0x45
#define RelayControl1 4
#define RelayControl2 5
#define RelayControl3 6
#define RelayControl4 7



void setup()
{
  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);

// wifi START

 pinMode(LED_BUILTIN, OUTPUT);  // initialize digital pin LED_BUILTIN as an output.
  Serial.begin(115200);   // initialize serial for debugging
  Serial1.begin(9600);    // initialize serial for ESP module
  WiFi.init(&Serial1);    // initialize ESP module

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");
  printWifiStatus();
  
  // start the web server on port 80
  server.begin();


//wifi end
  
 
  pinMode(RelayControl1, OUTPUT);
  pinMode(RelayControl2, OUTPUT);
  pinMode(RelayControl3, OUTPUT);
  pinMode(RelayControl4, OUTPUT);


digitalWrite(RelayControl1,HIGH);
digitalWrite(RelayControl2,HIGH);
digitalWrite(RelayControl3,HIGH); 
digitalWrite(RelayControl4,HIGH);

  }

void loop()
{
 
  //wifi start

WiFiEspClient client = server.available();  // listen for incoming clients

  if (client) {                               // if you get a client,
    Serial.println("New client");             // print a message out the serial port
    buf.init();                               // initialize the circular buffer
    while (client.connected()) {              // loop while the client's connected
      if (client.available()) {               // if there's bytes to read from the client,
        char c = client.read();               // read a byte, then
        buf.push(c);                          // push it to the ring buffer

        // printing the stream to the serial monitor will slow down
        // the receiving of data from the ESP filling the serial buffer
        //Serial.write(c);
        
        // you got two newline characters in a row
        // that's the end of the HTTP request, so send a response
        if (buf.endsWith("rnrn")) {
          sendHttpResponse(client);
          break;
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (buf.endsWith("GET /H")) {
          Serial.println("Turn led ON");
          ledStatus = HIGH;
          digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
        }
        else if (buf.endsWith("GET /L")) {
          Serial.println("Turn led OFF");
          ledStatus = LOW;
          digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
        }
      }
    }
    
    // close the connection
    client.stop();
    Serial.println("Client disconnected");
  }
}


void sendHttpResponse(WiFiEspClient client)
{
  // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  // and a content-type so the client knows what's coming, then a blank line:
  client.println("HTTP/1.1 200 OK");
  client.println("Content-type:text/html");
  client.println();
  
  // the content of the HTTP response follows the header:
  client.print("The LED is ");
  client.print(ledStatus);

 
  client.println("
");
  client.println("
");
  
  client.println("Click <a href="/H">here</a> turn the LED on
");
  client.println("Click <a href="/L">here</a> turn the LED off
");
  
  // The HTTP response ends with another blank line:
  client.println();
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print where to go in the browser
  Serial.println();
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
  Serial.println();
  //wifi end
  
  
  unsigned int data[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);



  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);

  // Read 6 bytes of data
  // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
  if (Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }

//second write and read
  
{
  unsigned int data2[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 6 bytes of data
  Wire.requestFrom(Addr2, 6);

  // Read 6 bytes of data
  // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
  if (Wire.available() == 6)
  {
    data2[0] = Wire.read();
    data2[1] = Wire.read();
    data2[2] = Wire.read();
    data2[3] = Wire.read();
    data2[4] = Wire.read();
    data2[5] = Wire.read();
 
   }

  
  // Convert the data
  int temp = (data[0] * 256) + data[1];
  float cTemp = -45.0 + (175.0 * temp / 65535.0);
  float fTemp = (cTemp * 1.8) + 32.0;
  float humidity = (100.0 * ((data[3] * 256.0) + data[4])) / 65535.0;

 int temp2 = (data2[0] * 256) + data2[1];
  float cTemp2 = -45.0 + (175.0 * temp2 / 65535.0);
  float fTemp2 = (cTemp2 * 1.8) + 32.0;
  float humidity2 = (100.0 * ((data2[3] * 256.0) + data2[4])) / 65535.0;
  
  
  // Output formatted  data to serial monitor
  
Serial.print("Temp     :");
Serial.print(cTemp);
Serial.print(" c     ");

Serial.print("Temp2      :");
Serial.print(cTemp2);
Serial.println(" c  (loft)");
  
Serial.print("Humidity :"); 
Serial.print(humidity);
Serial.print(" %     ");
Serial.print("Humidity2  :"); 
Serial.print(humidity2);  
Serial.println(" %  (loft)");

   

if (((cTemp-2) < cTemp2 ) && (humidity > humidity2 ))
{
  Serial.println("**** RELAYS ON Temp Higher (withing 2 deg) in loft humidity in loft lower ****");
  digitalWrite(RelayControl1,LOW);
  digitalWrite(RelayControl2,LOW);
  digitalWrite(RelayControl3,LOW);
  digitalWrite(RelayControl4,LOW);
  delay(1000);
 }


  if (((cTemp-2) < cTemp2 ) && (humidity < humidity2 )) 
 { 
  Serial.println("Relays off temp ok but humid in loft ");
  digitalWrite(RelayControl1,HIGH);
  digitalWrite(RelayControl2,HIGH);
  digitalWrite(RelayControl3,HIGH);
  digitalWrite(RelayControl4,HIGH);
 }


 if (((cTemp-2) > cTemp2 ) && (humidity < humidity2 )) 
 { 
  Serial.println("Relays Off too cold and too humid in loft");
  digitalWrite(RelayControl1,HIGH);
  digitalWrite(RelayControl2,HIGH);
  digitalWrite(RelayControl3,HIGH);
  digitalWrite(RelayControl4,HIGH);
 }
 
 
  if (((cTemp-2) > cTemp2 ) && (humidity > humidity2 ))
 {
  Serial.println("Relays off too cold in loft humidity ok  Change");
  digitalWrite(RelayControl1,HIGH);
  digitalWrite(RelayControl2,HIGH);
  digitalWrite(RelayControl3,HIGH);
  digitalWrite(RelayControl4,HIGH);
}

Serial.println(" "); 
 
  delay(2000);
}}

I have fixed the fan switching on and off, but i still cant get the temp into the output for the webpage.

any ideas how to get it to display on the page?

You need to emit the HTML that makes up the page. Here's an example that should help.

I tried getting the data before the wifi html print and then to client.println the temp data for the html. but when i do this the whole wifi starts making errors saying that it is out of scope even in setup?
the program doesn't loop anymore like it used to so the temperature just gets read once.

Start by posting the two working separate programs.

...R

#include <Wire.h>

// SHT31 I2C address is 0x44(68)
#define Addr 0x44
#define Addr2 0x45
#define RelayControl1 4
#define RelayControl2 5
#define RelayControl3 6
#define RelayControl4 7

void setup()
{
  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);

  
 
//new code******************************************
 
  pinMode(RelayControl1, OUTPUT);
  pinMode(RelayControl2, OUTPUT);
  pinMode(RelayControl3, OUTPUT);
  pinMode(RelayControl4, OUTPUT);


digitalWrite(RelayControl1,HIGH);
digitalWrite(RelayControl2,HIGH);
digitalWrite(RelayControl3,HIGH); 
digitalWrite(RelayControl4,HIGH);
  delay(300);
  }

void loop()
{
  unsigned int data[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);



  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);

  // Read 6 bytes of data
  // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
  if (Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }

//second write and read
  
{
  unsigned int data2[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 6 bytes of data
  Wire.requestFrom(Addr2, 6);

  // Read 6 bytes of data
  // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
  if (Wire.available() == 6)
  {
    data2[0] = Wire.read();
    data2[1] = Wire.read();
    data2[2] = Wire.read();
    data2[3] = Wire.read();
    data2[4] = Wire.read();
    data2[5] = Wire.read();
 
   }




  
  // Convert the data
  int temp = (data[0] * 256) + data[1];
  float cTemp = -45.0 + (175.0 * temp / 65535.0);
  float fTemp = (cTemp * 1.8) + 32.0;
  float humidity = (100.0 * ((data[3] * 256.0) + data[4])) / 65535.0;

 int temp2 = (data2[0] * 256) + data2[1];
  float cTemp2 = -45.0 + (175.0 * temp2 / 65535.0);
  float fTemp2 = (cTemp2 * 1.8) + 32.0;
  float humidity2 = (100.0 * ((data2[3] * 256.0) + data2[4])) / 65535.0;


  
  
  
  
  
  // Output formatted  data to serial monitor
  
Serial.print("Temp     :");
Serial.print(cTemp);
Serial.print(" c     ");

Serial.print("Temp2      :");
Serial.print(cTemp2);
Serial.println(" c  (loft)");
  
Serial.print("Humidity :"); 
Serial.print(humidity);
Serial.print(" %     ");
Serial.print("Humidity2  :"); 
Serial.print(humidity2);  
Serial.println(" %  (loft)");

  
  // Output data to serial graph
 
//  Serial.print(cTemp);
 // Serial.print(" ");
//  Serial.println(cTemp2);
 // Serial.print(humidity);
  // Serial.print(" ");
//  Serial.println(humidity2); 



if (((cTemp-2) < cTemp2 ) && (humidity > humidity2 ))
{
  Serial.println("**** RELAYS ON Temp Higher (withing 2 deg) in loft humidity in loft lower ****");
  digitalWrite(RelayControl1,LOW);
  digitalWrite(RelayControl2,LOW);
  digitalWrite(RelayControl3,LOW);
  digitalWrite(RelayControl4,LOW);
  delay(1000);
 }


  if (((cTemp-2) < cTemp2 ) && (humidity < humidity2 )) 
 { 
  Serial.println("Relays off temp ok but humid in loft ");
  digitalWrite(RelayControl1,HIGH);
  digitalWrite(RelayControl2,HIGH);
  digitalWrite(RelayControl3,HIGH);
  digitalWrite(RelayControl4,HIGH);
 }


 if (((cTemp-2) > cTemp2 ) && (humidity < humidity2 )) 
 { 
  Serial.println("Relays Off too cold and too humid in loft");
  digitalWrite(RelayControl1,HIGH);
  digitalWrite(RelayControl2,HIGH);
  digitalWrite(RelayControl3,HIGH);
  digitalWrite(RelayControl4,HIGH);
 }
 
 
  if (((cTemp-2) > cTemp2 ) && (humidity > humidity2 ))
 {
  Serial.println("Relays off too cold in loft humidity ok  Change");
  digitalWrite(RelayControl1,HIGH);
  digitalWrite(RelayControl2,HIGH);
  digitalWrite(RelayControl3,HIGH);
  digitalWrite(RelayControl4,HIGH);
}

Serial.println(" ");
  
  // delay(1000);
  // digitalWrite(RelayControl2,HIGH);
  // delay(1000);

  // delay(1000);
  // digitalWrite(RelayControl3,HIGH);
  // delay(1000);
  //
  // delay(1000);     
  // digitalWrite(RelayControl4,HIGH);  
  // delay(1000);
  //  






 




 
 
  delay(500);
}}

and this program.

I couldn't fit them all on one page as it is over 9000 and then it said I had posted too many time when I tried to upload this, so i am sorry for the delay.

/*
 WiFiEsp example: WebServerLed
 
 A simple web server that lets you turn on and of an LED via a web page.
 This sketch will print the IP address of your ESP8266 module (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to turn on and off the LED on pin 13.

 For more details see: http://yaab-arduino.blogspot.com/p/wifiesp.html
*/

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

char ssid[] = "Twim";            // your network SSID (name)
char pass[] = "12345678";        // your network password
int status = WL_IDLE_STATUS;

int ledStatus = LOW;

WiFiEspServer server(80);

// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output.
  Serial.begin(115200);   // initialize serial for debugging
  Serial1.begin(9600);    // initialize serial for ESP module
  WiFi.init(&Serial1);    // initialize ESP module

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");
  printWifiStatus();
  
  // start the web server on port 80
  server.begin();
}


void loop()
{
  WiFiEspClient client = server.available();  // listen for incoming clients

  if (client) {                               // if you get a client,
    Serial.println("New client");             // print a message out the serial port
    buf.init();                               // initialize the circular buffer
    while (client.connected()) {              // loop while the client's connected
      if (client.available()) {               // if there's bytes to read from the client,
        char c = client.read();               // read a byte, then
        buf.push(c);                          // push it to the ring buffer

        // printing the stream to the serial monitor will slow down
        // the receiving of data from the ESP filling the serial buffer
        //Serial.write(c);
        
        // you got two newline characters in a row
        // that's the end of the HTTP request, so send a response
        if (buf.endsWith("\r\n\r\n")) {
          sendHttpResponse(client);
          break;
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (buf.endsWith("GET /H")) {
          Serial.println("Turn led ON");
          ledStatus = HIGH;
          digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
        }
        else if (buf.endsWith("GET /L")) {
          Serial.println("Turn led OFF");
          ledStatus = LOW;
          digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
        }
      }
    }
    
    // close the connection
    client.stop();
    Serial.println("Client disconnected");
  }
}


void sendHttpResponse(WiFiEspClient client)
{
  // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  // and a content-type so the client knows what's coming, then a blank line:
  client.println("HTTP/1.1 200 OK");
  client.println("Content-type:text/html");
  client.println();
  
  // the content of the HTTP response follows the header:
  client.print("The LED is ");
  client.print(ledStatus);
  client.println("
");
  client.println("
");
  
  client.println("Click <a href=\"/H\">here</a> turn the LED on
");
  client.println("Click <a href=\"/L\">here</a> turn the LED off
");
  
  // The HTTP response ends with another blank line:
  client.println();
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print where to go in the browser
  Serial.println();
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
  Serial.println();
}

as separating the files didn't seem to help, I have messed about some more and as i see the problem, i need to call the i2c chip up and store the data so it can be used later in the html creation, the problem is when i do this it causes an error in setup saying that

Arduino: 1.8.5 (Windows 8.1), Board: "Arduino/Genuino Uno"

C:\Users\Admin\Documents\Arduino\working on\Working_onNewWith_WIFI\Working_onNewWith_WIFI.ino: In function 'void setup()':

Working_onNewWith_WIFI:55: error: 'printWifiStatus' was not declared in this scope

printWifiStatus();

^

C:\Users\Admin\Documents\Arduino\working on\Working_onNewWith_WIFI\Working_onNewWith_WIFI.ino: In function 'void loop()':

Working_onNewWith_WIFI:155: error: 'sendHttpResponse' was not declared in this scope

sendHttpResponse(client);

^

Working_onNewWith_WIFI:182: error: a function-definition is not allowed here before '{' token

{

^

Working_onNewWith_WIFI:204: error: a function-definition is not allowed here before '{' token

{

^

Working_onNewWith_WIFI:318: error: expected '}' at end of input

}}

^

exit status 1
'printWifiStatus' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

so it is either my formatting has suddenly gone wrong. or i am using memory space for the i2c chip where it is storing the wifi stuff or I have no idea and im lost.

Post the code that got you that. Most likely you'll have to attach it - you'll see "Attachments and other options" when you reply.

the error i get is 'cTemp' was not declared in this scope... but i am now so confused i am not sure where i am going. the ctemp needs to be declared an int but I need the values so im just lost.

#include <Wire.h>

//wifi start
#include "WiFiEsp.h"
// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(9, 8); // RX, TX
#endif

char ssid[] = "Home";            // your network SSID (name)
char pass[] = "NonEmmet";        // your network password
int status = WL_IDLE_STATUS;
int ledStatus = LOW;

WiFiEspServer server(80);
// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);

// SHT31 I2C address is 0x44(68)
#define Addr 0x44
#define Addr2 0x45
#define RelayControl1 4
#define RelayControl2 5
#define RelayControl3 6
#define RelayControl4 7

void setup()
{
 
  Wire.begin();
  Serial.begin(9600);     //SH13 
  Serial1.begin(9600);    // wifi
   // wifi START
  pinMode(LED_BUILTIN, OUTPUT);  // initialize digital pin LED_BUILTIN as an output.
  Serial1.begin(9600);    // initialize serial for ESP module
  WiFi.init(&Serial1);    // initialize ESP module

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }
  Serial.println("You're connected to the network");
  printWifiStatus();
  // start the web server on port 80
  server.begin();
//wifi end
  pinMode(RelayControl1, OUTPUT);
  pinMode(RelayControl2, OUTPUT);
  pinMode(RelayControl3, OUTPUT);
  pinMode(RelayControl4, OUTPUT);
  digitalWrite(RelayControl1,HIGH);
  digitalWrite(RelayControl2,HIGH);
  digitalWrite(RelayControl3,HIGH); 
  digitalWrite(RelayControl4,HIGH);

  }

void loop()
{
WiFiEspClient client = server.available();  // listen for incoming clients
if (client) {                               // if you get a client,
    Serial.println("New client");             // print a message out the serial port
    buf.init();                               // initialize the circular buffer
    while (client.connected()) {              // loop while the client's connected
      if (client.available()) {               // if there's bytes to read from the client,
        char c = client.read();               // read a byte, then
        buf.push(c);                          // push it to the ring buffer

        // printing the stream to the serial monitor will slow down
        // the receiving of data from the ESP filling the serial buffer
        //Serial.write(c);
        // you got two newline characters in a row
        // that's the end of the HTTP request, so send a response
        if (buf.endsWith("\r\n\r\n")) {
          sendHttpResponse(client);
          break;
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (buf.endsWith("GET /H")) {
          Serial.println("Turn Fan ON");
          ledStatus = HIGH;
          digitalWrite(RelayControl3,LOW);
         // digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
        }
        else if (buf.endsWith("GET /L")) {
          Serial.println("Turn Fan OFF");
          ledStatus = LOW;
          digitalWrite(RelayControl3,HIGH);    // turn the LED off by making the voltage LOW
        }
      }
    }
    
    // close the connection
    client.stop();
    Serial.println("Client disconnected");
  }
}


void sendHttpResponse(WiFiEspClient client)
{
  // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  // and a content-type so the client knows what's coming, then a blank line:
  client.println("HTTP/1.1 200 OK");
  client.println("Content-type:text/html");
  client.println();
  
  // the content of the HTTP response follows the header:
  client.print("The Fan is ");
  client.print(ledStatus);
client.print(cTemp);
 
  client.println("
");
  client.println("
");
  
  client.println("Click <a href=\"/H\">here</a> turn the Fan on
");
  client.println("Click <a href=\"/L\">here</a> turn the Fan off
");

  // The HTTP response ends with another blank line:
  client.println();
}

void printWifiStatus()
{



 
  unsigned int data[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);



  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);

  // Read 6 bytes of data
  // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
  if (Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }

//second write and read
  
{
  unsigned int data2[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 6 bytes of data
  Wire.requestFrom(Addr2, 6);

  // Read 6 bytes of data
  // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
  if (Wire.available() == 6)
  {
    data2[0] = Wire.read();
    data2[1] = Wire.read();
    data2[2] = Wire.read();
    data2[3] = Wire.read();
    data2[4] = Wire.read();
    data2[5] = Wire.read();
 
   }




  
  // Convert the data
  int temp = (data[0] * 256) + data[1];
  float cTemp = -45.0 + (175.0 * temp / 65535.0);
  float fTemp = (cTemp * 1.8) + 32.0;
  float humidity = (100.0 * ((data[3] * 256.0) + data[4])) / 65535.0;

 int temp2 = (data2[0] * 256) + data2[1];
  float cTemp2 = -45.0 + (175.0 * temp2 / 65535.0);
  float fTemp2 = (cTemp2 * 1.8) + 32.0;
  float humidity2 = (100.0 * ((data2[3] * 256.0) + data2[4])) / 65535.0;


  
  
  
  
  
  // Output formatted  data to serial monitor
  
Serial.print("Temp     :");
Serial.print(cTemp);
Serial.print(" c     ");

Serial.print("Temp2      :");
Serial.print(cTemp2);
Serial.println(" c  (loft)");
  
Serial.print("Humidity :"); 
Serial.print(humidity);
Serial.print(" %     ");
Serial.print("Humidity2  :"); 
Serial.print(humidity2);  
Serial.println(" %  (loft)");

if (((cTemp-2) < cTemp2 ) && (humidity > humidity2 ))
{
  Serial.println("**** RELAYS ON Temp Higher (withing 2 deg) in loft humidity in loft lower ****");
  digitalWrite(RelayControl1,LOW);
  digitalWrite(RelayControl2,LOW);
  digitalWrite(RelayControl3,LOW);
  digitalWrite(RelayControl4,LOW);
  delay(1000);
 }


  if (((cTemp-2) < cTemp2 ) && (humidity < humidity2 )) 
 { 
  Serial.println("Relays off temp ok but humid in loft ");
  digitalWrite(RelayControl1,HIGH);
  digitalWrite(RelayControl2,HIGH);
  digitalWrite(RelayControl3,HIGH);
  digitalWrite(RelayControl4,HIGH);
 }


 if (((cTemp-2) > cTemp2 ) && (humidity < humidity2 )) 
 { 
  Serial.println("Relays Off too cold and too humid in loft");
  digitalWrite(RelayControl1,HIGH);
  digitalWrite(RelayControl2,HIGH);
  digitalWrite(RelayControl3,HIGH);
  digitalWrite(RelayControl4,HIGH);
 }
 
 
  if (((cTemp-2) > cTemp2 ) && (humidity > humidity2 ))
 {
  Serial.println("Relays off too cold in loft humidity ok  Change");
  digitalWrite(RelayControl1,HIGH);
  digitalWrite(RelayControl2,HIGH);
  digitalWrite(RelayControl3,HIGH);
  digitalWrite(RelayControl4,HIGH);
}

Serial.println(" ");

//wifi end

 




 




 
 
  delay(2000);




  
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print where to go in the browser
  Serial.println();
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
  Serial.println();
  //wifi end
  
 
}}

At the moment the variable ctemp is defined on line 212 and again on line 217 within the function printWifiStatus() so it is not visible in any other function.

Just define it as a global variable at the top of the program and remove "float" from lines 212 and 217.

Also remove it from lines 218 and 219. You should only define a variable in one place.

...R

Super. im getting the idea but messing it up in practice :slight_smile:

so I remove the line

int temp = (data[0] * 256) + data[1];

and put that before void setup to make it global, but if i do this it wont know what data[0] is because I haven't called the data? so do I set it as a char ?

next question is that if I remove float how will it work out the temperature equations?

and finally i think it is just a typo line 218 and 219 are not repeats they are for the second ctemp data from the second i2c so that will need to be a global int temp2 as well.

thank you for your help. i feel im close but my brain has gone to putty trying to just get it sorted.

Robin2:
At the moment the variable ctemp is defined on line 212 and again on line 217 within the function printWifiStatus() so it is not visible in any other function.

Just define it as a global variable at the top of the program and remove "float" from lines 212 and 217.

Also remove it from lines 218 and 219. You should only define a variable in one place.

...R

Ok I have it displaying data. and all seems within my grasp (yes i know the format is horrendous etc) so in the end all I needed to do was to type

 int = cTemp; 
int = cTemp2;

before the setup all would have worked, so simple.

but It still isnt looping and refreshing the data like it was. any ideas on how to get it back looping?

Call printWifiStatus somewhere in the loop function. Right at the top perhaps.

wildbill:
Call printWifiStatus somewhere in the loop function. Right at the top perhaps.

so close!

if i put wifistatus at the end it loops round lovely but it breaks the web server and i cant connect. anywhere else it doesn't loop.

any ideas?

The calls to delay are likely causing problems. Any time you're trying to get a web page from the arduino, it's most probably executing a call to delay.

This is bad news in the short term. It means you will need to embrace the use of millis which means mastering the blink without delay example sketch that comes with the IDE.

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

...R

if i put wifistatus at the end it loops round lovely

WTF is that supposed to mean? At the end of what? What loops?

but it breaks the web server and i cant connect. anywhere else it doesn't loop.

Printing that status of the WiFi connection, which is ALL that that function should do has NO impact on your ability to connect or to the rest of the code's ability to loop.

POST YOUR CODE!

Dear Paul, please don't put WTF it is unnecessary and makes you look rude.

there is enough code on here to sink a battle ship. everyone else understands what I am trying to do, so please try to keep you help positive. the file size is now over 9000 characters and so is a pain to post and more importantly unnecessary, I appreciate you have joined the conversation half way though and haven't spent as much time as others to read but please dont take you days frustrations out on me and my lack of knowledge in this one subject area,

to answer your question.
the program stops when it does one check of the temperature then does not refresh. One kind person suggested that I put a printwifistatus line in at the top to get it to re check, this didnt work so I added it at the end (trying all options, infinite monkey cage style) and this got the temperature to be re checked well but the timing for the web page stopped and so my webserver broke. and being the greedy person I am I wanted both working at once.
so somehow printing status seems to make alot of difference. I dont know why, ask me a question on a non programming topic and I will probably be able to answer you.

I would gladly like to have your positive input and learn, but aggressive and rude I would rather not.

PaulS:
WTF is that supposed to mean? At the end of what? What loops?
Printing that status of the WiFi connection, which is ALL that that function should do has NO impact on your ability to connect or to the rest of the code's ability to loop.

POST YOUR CODE!

thank you wildbill and robin,

I have read about that delay system ( was looking into getting a long term delays before for the fan)
I also noticed there are alot of support questions about it too!

so further into the rabbit hole I will go. first I will try to tidy my code up so it is smaller and remove all delays that are from the first program as they were only put in to slow up polling the thermistor and so the fan didnt keep cutting in every 1 second when the temperature was varying.

Thank you again for your help, it is very much appreciated