Two ethernet client connections in one loop

I've got a couple of LDR's and a TMP36 happily sending data to a MySQL server running on my network using the ethernet shield. I have some PHP scripts which control my LightwaveRF WiFi module, in order to turn lights on and off.

I want to add an action to turn the lights on when an LDR is below a certain value, i.e. it's dark. Controlling the lights is also done via a client connection to the same server the SQL DB is running on. Testing a sketch with this on its own worked very well and created an impromptu disco mode which I may keep in.

Anyway, when I combine the MySQL sketch with the lighting control sketch, I get MySQL database updates, but I don't get any lighting control.

My question is, do I need to do anything special in order to 'visit' another web page in order to send another GET? Do I close the ethernet client connection from the mysql writing part, and then open another connection for the lighting control part? I've tried both and no joy. I've also tried it with the lighting control bit just at the end of the rest of the code, so without any conditions to trigger it - and still nothing. Try the lighting control sketch on its own again and it works fine. What am I missing? I haven't been able to find any examples of code where there are multiple connections to pages.

I can post the code, but I'll warn you - it's a mess.

Thanks for any help!

I can post the code, but I'll warn you - it's a mess.

Either post it or pay to get my crystal ball repaired. Your call.

While it would have been interesting to see the results you get with the repaired crystal ball, here it is - in all its glory.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xAA, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 39 };
byte serverName[] = { 192, 168, 0, 105 }; 

EthernetClient client; 

///////////////////////////
// THINGS TO ADD
// 
// Trigger EventGhost via HTTP get request
// Lights on if dim, heating on if cold, fan on if hot?
// 



//tmp36 storage
float temperatureC; 

//ldr storage
int photocellPin = 1;     // the cell and 10K pulldown are connected to a0
int photocellReadingA;     // the analog reading from the analog resistor divider
int photocellReadingB;


void setup()
{
  
   pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  
    Ethernet.begin(mac, ip);
    
     // If you want to set the aref to something other than 5v
  analogReference(EXTERNAL);
    
    delay(1000);
    Serial.begin(9600);
}

void loop()
{
       //ethernet connect
        if(client.connect(serverName, 8033))
        {
        
        
         
         digitalWrite(5, HIGH);
 
         //LDR SENSOR
         
          photocellReadingA = analogRead(photocellPin);  
  Serial.print("LightA: ");
  Serial.println(photocellReadingA);     // the raw analog reading
        
        //let the sensor recover
        delay(1000);
        
         
         //TEMP SENSOR
         //getting the voltage reading from the temperature sensor
 int reading = analogRead(0);  
 
 // converting that reading to voltage, for 3.3v arduino use 3.3
 float voltage = reading * 3.3;
 voltage /= 1024.0; 
 // now print out the temperature
 float temperatureC = (voltage - 0.5) * 100 ;
 Serial.print("Temp1: "); Serial.print(temperatureC); Serial.println(" C");
 
 if (temperatureC >= 22) {
   Serial.println("It's warm!");
 }
 else
 {
   Serial.println("It's cold!");
 }
 
   delay(1000);
   digitalWrite(5, LOW);
     
         //ethernet stuff
            Serial.println("Connected to server.");
            digitalWrite(6, HIGH);
            Serial.println("Writing to MySQL DB");
            client.print("GET /envmon/tempwrite.php?temp1=");
            client.print(temperatureC);
            client.print("&light1=");
            client.print(photocellReadingA);
            client.println(" HTTP/1.1");
            client.println("Host: localhost");
            client.println();
            
                      client.flush();         
                      client.stop();
   
            Serial.println("Server connection closed.");
   delay(2000);
   
          photocellReadingB = analogRead(photocellPin);  
          Serial.print("LightB: ");
          Serial.println(photocellReadingB);     // the raw analog reading
            
          Serial.print("Difference: ");  
          Serial.println(photocellReadingA - photocellReadingB);
            
         
          if ((photocellReadingA - photocellReadingB) <= 10) {
            Serial.println("Lighting indifferent.");
          }
  
          else if ((photocellReadingA - photocellReadingB) <= 100) {
            Serial.println("Sudden lighting change!");
        
         boolean suddendark = true;
          }
  

        else
        {
            Serial.println("connection failure");
        }
         client.flush(); 
         
           if(client.connect(serverName, 8033))
             
        {
            delay(1000);
            digitalWrite(6, LOW);
            delay(3000);
            
            //light switching test
            Serial.println("Doing lighting stuff!");
            //ethernet stuff
            Serial.println("connected for lights...");
            digitalWrite(6, HIGH);
            Serial.println("ARDUINO: forming HTTP request message");
            client.print("GET /envmon/EGAP.php?turnon=books1");
            //finish off the ethernet code 
            client.println(" HTTP/1.1");
            client.println("Host: localhost");
            client.println();
            
                      client.flush();     
                   client.stop();    
           
        }
        
}}
         boolean suddendark = true;

A local variable that immediately goes out of scope. How useful is that?

Why does your code jerk around all over the place? Haven't you learned about Tools + Auto Format yet?

        //let the sensor recover
        delay(1000);

Useless. A photocell doesn't need time to recover. Besides, you are doing plenty of other stuff before you get back to reading the thing again.

   delay(1000);

More uselessness. You have wasted more than 2 seconds already...

   delay(2000);

We're up to 4 seconds of wasted time so far...

         client.flush();

You've already stopped and flushed once. Why are you flushing again?

                      client.flush();     
                   client.stop();

Is it stop() then flush() or flush() then stop()? You have it both ways. I'd think that only one way was correct.

}}

Never!

Since you are connecting to the same server to run both scripts. why not just have one script that takes more arguments?

I think you can setup for multiple clients like below.

Client client1(ip, port);
Client client2(ip2, port2);