Issue calling PHP using Ethernet Shield

Hi,

I have the below code which basically listens for a serial command comming over RF.

When a certain serial command is received, it then calls a PHP script depending on the command received.

All works well, for the first and normally second reception for this command, but on the third reception, the PHP is not called and an error is produced. Sometimes this happens after the second reception and it hangs for a littlewhile on 'Alarm Activated (pin 11)' and then produces the predefined error 'failed to call PHP'.

I have also had to place in a RESET command at the end of the alarm_1 function as without it, the Arduino seems to crash, on the third 'activation' before even calling the PHP script.

The code is fairly modular, but don't know if there is something glaringly wrong......its driving me mad.

If I call the PHP from a web browser, it works fine, no matter how many times it is called.

Any help would be greatly appricated :slight_smile:

static String ident = "server";    // Identification for this device

#include <VirtualWire.h>  // 433Mhz Module Library
#include <SPI.h>
#include <Ethernet.h>  // Ethernet Library
#include <DHT.h>  // DHT Temp & Humidity Library

byte mac[] = {  0x90, 0xA2, 0xDA, 0x00, 0x79, 0xED };
byte ip[] = { 192,168,1,142 }; // local Arduino IP
//byte gateway[] = { 192,168,1,1 }; // IP of your gateway
//byte subnet[] = { 255, 255, 255, 0 };
byte server[] = { 192,168,1,101 }; // IP of your web server

//Server server(80);

Client client(server, 80);

const int ledPin =  13;          // The number of the LED pin, LED used to indicate we got a message
long onMillis = 0;               // Will store last time LED was updated

// Uncomment whatever type of Temp sensor being used!
#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
#define DHTPIN 8                 // Sets DHT Temp Sensor Pin 

// *** Temperature Variables ***
#define maxTemp 35               // Fan will start at this temperature
#define minTemp 32               // Sets Temp Hysteresis variation (3 degress)  

// *** RF Variables ***
#define rxPin 2                 // Sets 433Mhz Rx Pin

// *** Other Variables ***
#define startPin 11               // The first pin we connect a relay to
#define stopPin 12                // The last pin we connect a relay to
#define fanPin 5

//Set-up Temperature Sensor
DHT dht(DHTPIN, DHTTYPE);

void(* resetFunc) (void) = 0; //declare reset function @ address 0


void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  
  //Set-up the RF receiver
  vw_setup(1000);                     // Bits per sec
  vw_set_rx_pin(rxPin);               // We will be receiving on pin 2 ie the RX pin from the module connects to this pin.
  vw_rx_start();                      // Start the receiver

  dht.begin();  // Begin reading DHT sensor
  
  for (int i = startPin; i <= stopPin; i++) // We need to set the pins that control the relays to OUTPUT
  {
    Serial.print("Setting pinMode on pin: ");
    Serial.println(i);
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH); // We set the pin to HIGH because pulling it LOW will turn the relay on
  }
  
  Serial.println("Ready to receive!");
}

void loop()
{

  temperature();          //  Calls Temperature function
  
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen)) // Check to see if anything has been received
  {
    String message;

    for (int i = 0; i < buflen; i++) // Run through the stuff we just received
    {
      message += buf[i]; // Append the stuff to a String
    }

    if (message.substring(0,ident.length()) == ident) // Check if this message was for us
    {
      digitalWrite(ledPin, HIGH); // Turn led on to indicate we received something, and is going to look more at it
      onMillis = millis(); // Save when led was turned on so we remember to turn it off again later

      message = message.substring(ident.length()+1); // Remove the identifier from the message

      int startCommand = message.indexOf('-'); // Find where we split pin number from pin status

      String getPinNum = message.substring(0, startCommand); // Save a String with the pin number
      String getPinSet = message.substring(startCommand+1, message.length()); // Save a String with the value we are going to send to the pin

      char tempNum[2]; //Char to hold the message before converting it to an integer

      tempNum[getPinNum.length() + 1]; // Temp save pin number
      getPinNum.toCharArray(tempNum, getPinNum.length() + 1); // Put the temp number into a char array
      int pinNum = atoi(tempNum); // Convert char array to an integer

      tempNum[getPinSet.length() + 1]; // Temp save pin value
      getPinSet.toCharArray(tempNum, getPinSet.length() + 1); // Put the temp value into a char array
      int pinSet = atoi(tempNum); // Convert char array to an integer

      Serial.print("Pin num: ");
      Serial.print(pinNum);
      Serial.print(" - Pin status: ");
      Serial.println(pinSet);

//      digitalWrite(pinNum, pinSet); // Set the pin defined earlier
    
      if (pinNum == 11)
      {
        Serial.println("Alarm Activated (pin 11)");
        alarm_1();
      }
      if (pinNum == 12)
      {
        Serial.println("Alarm Activated (pin 12)");
        alarm_2();
      } 
          onMillis = millis();
  }
  if (millis() - onMillis > 250 && onMillis > 0) // Check if it is time to turn the led off
  {
    onMillis = 0;
    digitalWrite(ledPin, LOW);
    }
  }
}

void alarm_1(){
  unsigned long startTime = millis();

  if (client.connect()) { //connect to server
      Serial.println("connected to server");
      // Make a HTTP request:
      client.println("GET /alarm_1.php HTTP/1.0"); //location of ProwlPHP script
      client.println();
      delay(1000);
  }
  else {
    Serial.println("failed to call PHP");
  }
    delay(1000);
    Serial.print("Response from server: ");
    while ((!client.available()) && ((millis() - startTime ) < 5000));
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  
    // if the server's disconnected, stop the client:
    if (!client.connected()) {
      Serial.println("disconnecting from server");
      client.stop();
      client.flush();
      Serial.println("Disconnected...");
    }
    resetFunc();  //call reset
}

void alarm_2(){
   resetFunc();  //call reset
}


void temperature (){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float humidity = dht.readHumidity();
  float temp = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(temp) || isnan(humidity)) {
    Serial.println("Failed to read from DHT");
  } else {
    if (temp >= maxTemp){
        Serial.print("Temperature: "); 
        Serial.print(temp);
        Serial.println(" *C");  
    fan(true);}
    else if (temp <= minTemp)
    {fan(false);}
  }
  if (temp <= maxTemp && temp >= minTemp){
        Serial.print("Temperature: "); 
        Serial.print(temp);
        Serial.println(" *C");}
}
    
    
//    if (temp >= minTemp || temp >= minTemp-hysterine){
//      tone(3,1000); 
//      Serial.print("Humidity: "); 
//       Serial.print(humidity);
//       Serial.print(" %\t"); 
//       Serial.print("Temperature: "); 
//    Serial.print(temp);
//    Serial.println(" *C");
//  } else {
//       Serial.println("Ready to receive!");    
//         } 
//  }


void fan(boolean stat){
  digitalWrite(fanPin, stat);
}

This is the output from a serial debug window.

Setting pinMode on pin: 11
Setting pinMode on pin: 12
Ready to receive!
Pin num: 11 - Pin status: 1
Alarm Activated (pin 11)
connected to server
Response from server: HTTP/1.1 200 OK
Connection: close
Date: Tue, 06 Sep 2011 14:33:36 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-Powered-By: PHP/5.2.6
Content-type: text/html

Sent. 992 Messages left. (Resets at: 2011-09-06 15:13:16)
disconnecting from server
Disconnected...
Setting pinMode on pin: 11
Setting pinMode on pin: 12
Ready to receive!
Pin num: 11 - Pin status: 1
Alarm Activated (pin 11)
failed to call PHP
Response from server: disconnecting from server
Disconnected...
Setting pinMode on pin: 11
Setting pinMode on pin: 12
Ready to receive!

String message;

for (int i = 0; i < buflen; i++) // Run through the stuff we just received
{
message += buf*; // Append the stuff to a String*

  • }*
    [/quote]
    What a waste of memory. A String is allocated, with 0 bytes. Then, that object is destroyed, and replaced with one that contains 1 byte. Then, that object is destroyed, and replaced with one that contains 2 bytes. Then, that object is destroyed, and replaced with one that contains 3 bytes.
    This process is repeated, allocating little blocks that are hard to re-use, since they are so small.
    * *String message(buf);* *
    Accomplishes the same end result, without all the intermediate memory allocations.
    Since you have a fair number of constant strings, and libraries that allocate blocks of memory, I'd guess that you are running out of memory.
    Time to consider ditching the String call altogether. Everything that it does can be done using standard string functions, like strtok().

Maybe shared hosting with virtual hosts (=mutliple domains running on the same IP and machine)? You'll then have to add the domain in your HTTP request.

      client.println("GET /alarm_1.php HTTP/1.0");  // location of ProwlPHP script
      client.println("Host: www.example.com");      // domain name
      client.println("Connection: close");          // this line is not related to your problem but I recommend it ;)
      client.println();

Sui - Thank you, the calling of the PHP seems to be more 'stable' now. I'll keep testing :slight_smile:

PaulS - Unfortunatly I am only beggining my journey into Arduino and have pieced to gether various examples to get my desired functional outcome.

If I replace

String message;

for (int i = 0; i < buflen; i++) // Run through the stuff we just received
{
message += buf; // Append the stuff to a String
}

With

String message(buf);

I just get a whole host of errors :frowning:

So i'm guessing i need to change a fair bit more.

Can you assist in what I should replace my current code with?

The 'transmitter' basically sends out "server-11-1" as an example if thats of any help.

Thank you for any assistance you are willing to give. I am on a steep learning curve !

I just get a whole host of errors

And they are?

Probably just need to cast buf to the correct type. Try

String message(char *)buf);

Since char and uint8_t are the same size, the cast should work.

Thanks Paul, that compiles fine now, albeit I think you meant to put String message((char *)buf);

Unfortunatly after the first activation and calling of the PHP, the Arduino hangs at 'Disconnected' according to the serial debug screen and doesn't seem to activated upon receiving another RF serial command.

Any suggestions ?

Thanks for your assistance.

Unfortunatly after the first activation and calling of the PHP, the Arduino hangs at 'Disconnected' according to the serial debug screen and doesn't seem to activated upon receiving another RF serial command.

Any suggestions ?

Sure. Look at where/when/why 'Disconnected' is printed. Then, look at what happens immediately afterwards.

    if (!client.connected()) {
      Serial.println("disconnecting from server");
      client.stop();
      client.flush();
      Serial.println("Disconnected...");
    }
    resetFunc();  //call reset

I'd hazard a guess that resetting the Arduino is NOT what you wanted to do, and that resetFunc is not doing what you hoped it would do.

Hi Paul,

Thanks for your help, much appriciated, I'm at a bit of a loss here.

Sorry, I should have clarified from the original posting of code.

Since your initial suggestion of changing the code to String message((char *)buf); I removed the reset function.

This was initially put in place as I was experiencing a locked state after each time the PHP was called. This was a crued temorary fix, but obvioulsy not an ideal one.

As you suggested my problems could be due to a memory overrun, I have now removed that reset function, however I am still experiencing a lock after the first activation and calling of the PHP. The Arduino hangs at 'Disconnected' according to the serial debug screen and doesn't seem to do anything upon receiving another RF serial command.

Thank you for your input :slight_smile:

Steve

Now, I think it's time to post updated code. We can't be expected to keep track of the changes you are making, when you don't even tell us about them.

Fair point, totally understand.

static String ident = "server";     // Identification for this device

#include <VirtualWire.h>            // 433Mhz Module Library
#include <SPI.h>
#include <Ethernet.h>               // Ethernet Library
#include <DHT.h>                    // DHT Temp & Humidity Library

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x79, 0xED };    // Arduino MAC Address
byte ip[] = { 192,168,1,142 };                          // local Arduino IP
//byte gateway[] = { 192,168,1,1 };                     // IP of Gateway
//byte subnet[] = { 255, 255, 255, 0 };                 // Subnet Address
byte server[] = { 192,168,1,101 };                      // IP of Web Server

Client client(server, 80);

const int ledPin =  13;            // The number of the LED pin, LED used to indicate we got a message
long onMillis = 0;                 // Will store last time LED was updated

// *** Uncomment whatever type of Temp sensor being used ***
#define DHTTYPE DHT11              // DHT 11 
//#define DHTTYPE DHT22            // DHT 22  (AM2302)
//#define DHTTYPE DHT21            // DHT 21 (AM2301)
#define DHTPIN 8                   // Sets DHT Temp Sensor Pin 

// *** Set-up Temperature Sensor ***
DHT dht(DHTPIN, DHTTYPE);

// *** Fan related Variables ***
#define fanPin 5                   // Fan is connected to PIN 5

// *** Temperature Variables ***
#define maxTemp 35                 // Fan will start at this temperature
#define minTemp 32                 // Sets Temp Hysteresis variation (3 degress)  

// *** RF Variables ***
#define rxPin 2                    // Sets 433Mhz Rx Pin

// *** Other Variables ***
#define startPin 11               // The first pin we connect a relay to
#define stopPin 12                // The last pin we connect a relay to

void(* resetFunc) (void) = 0;     //declare reset function @ address 0

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  
  //Set-up the RF receiver
  vw_setup(1000);                     // Bits per sec
  vw_set_rx_pin(rxPin);               // We will be receiving on pin 2 ie the RX pin from the module connects to this pin.
  vw_rx_start();                      // Start the receiver

  dht.begin();  // Begin reading DHT sensor
  
  for (int i = startPin; i <= stopPin; i++) // We need to set the pins that control the relays to OUTPUT
  {
    Serial.print("Setting pinMode on pin: ");
    Serial.println(i);
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH); // We set the pin to HIGH because pulling it LOW will turn the relay on
  }
  
  Serial.println("Ready to receive!");
}

void loop()
{

  temperature();          //  Calls Temperature function
  
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen)) // Check to see if anything has been received
  {
    String message((char *)buf);

    if (message.substring(0,ident.length()) == ident) // Check if this message was for us
    {
      digitalWrite(ledPin, HIGH); // Turn led on to indicate we received something, and is going to look more at it
      onMillis = millis(); // Save when led was turned on so we remember to turn it off again later

      message = message.substring(ident.length()+1); // Remove the identifier from the message

      int startCommand = message.indexOf('-'); // Find where we split pin number from pin status

      String getPinNum = message.substring(0, startCommand); // Save a String with the pin number
      String getPinSet = message.substring(startCommand+1, message.length()); // Save a String with the value we are going to send to the pin

      char tempNum[2]; //Char to hold the message before converting it to an integer

      tempNum[getPinNum.length() + 1]; // Temp save pin number
      getPinNum.toCharArray(tempNum, getPinNum.length() + 1); // Put the temp number into a char array
      int pinNum = atoi(tempNum); // Convert char array to an integer

      tempNum[getPinSet.length() + 1]; // Temp save pin value
      getPinSet.toCharArray(tempNum, getPinSet.length() + 1); // Put the temp value into a char array
      int pinSet = atoi(tempNum); // Convert char array to an integer

      Serial.print("Pin num: ");
      Serial.print(pinNum);
      Serial.print(" - Pin status: ");
      Serial.println(pinSet);

//      digitalWrite(pinNum, pinSet); // Set the pin defined earlier
    
      if (pinNum == 11)
      {
        Serial.println("Alarm Activated (pin 11)");
        alarm_1();
      }
      if (pinNum == 12)
      {
        Serial.println("Alarm Activated (pin 12)");
        alarm_2();
      } 
          onMillis = millis();
  }
  if (millis() - onMillis > 250 && onMillis > 0) // Check if it is time to turn the led off
  {
   onMillis = 0;
    digitalWrite(ledPin, LOW);
    }
  }
}

void alarm_1(){
  unsigned long startTime = millis();

  if (client.connect()) { //connect to server
      Serial.println("connected to server");
      // Make a HTTP request:
      client.println("GET /alarm_1.php HTTP/1.0");               // location of ProwlPHP script
      client.println("Host: tisseyre.homeserver.com");           // domain name
//      client.println("Connection: close");                       // this line is not related to your problem but I recommend it ;)
      client.println();

      delay(1000);
  }
  else {
    Serial.println("failed to call PHP");
  }
    delay(1000);
    Serial.print("Response from server: ");
    while ((!client.available()) && ((millis() - startTime ) < 5000));
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  
    // if the server's disconnected, stop the client:
    if (!client.connected()) {
      Serial.println("disconnecting from server");
//      client.println("Connection: close");                       // this line is not related to your problem but I recommend it ;)
      client.stop();
      client.flush();
      Serial.println("Disconnected...");
    }
//    resetFunc();  //call reset
}

void alarm_2(){
   resetFunc();  //call reset
}


void temperature (){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float humidity = dht.readHumidity();
  float temp = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(temp) || isnan(humidity)) {
    Serial.println("Failed to read from DHT");
  } else {
    if (temp >= maxTemp){
        Serial.print("Temperature: "); 
        Serial.print(temp);
        Serial.println(" *C");  
    fan(true);}
    else if (temp <= minTemp)
    {fan(false);}
  }
  if (temp <= maxTemp && temp >= minTemp){
        Serial.print("Temperature: "); 
        Serial.print(temp);
        Serial.println(" *C");}
}
    
    
//    if (temp >= minTemp || temp >= minTemp-hysterine){
//      tone(3,1000); 
//      Serial.print("Humidity: "); 
//       Serial.print(humidity);
//       Serial.print(" %\t"); 
//       Serial.print("Temperature: "); 
//    Serial.print(temp);
//    Serial.println(" *C");
//  } else {
//       Serial.println("Ready to receive!");    
//         } 
//  }


void fan(boolean stat){
  digitalWrite(fanPin, stat);
}

I have now removed that reset function

void(* resetFunc) (void) = 0;     //declare reset function @ address 0
void alarm_2(){
   resetFunc();  //call reset
}

I see.

Some code:
char tempNum[2]; //Char to hold the message before converting it to an integer

tempNum[getPinNum.length() + 1]; // Temp save pin number
What is this line supposed to be doing? It references the 3rd element of a 3 element array, when the pin number is 11 or 12. But, it does nothing with the value returned by the [] operator. So, it accomplishes nothing.

getPinNum.toCharArray(tempNum, getPinNum.length() + 1); // Put the temp number into a char array
The 2nd argument is supposed to be the size of the destination array, not anything based on the input string. This is so that the toCharArray knows how big the destination array is, so that it doesn't overwrite anything.

Now, about the size of the array. The toCharArray is going to copy 2 characters AND a NULL into the output array, if there is room. You've told the function that there is room in the output array for 3 characters, when the getPinNum String contains "11" or "12". Is that true? No, of course not. But, the toCharArray function doesn't know that, so it happily writes to all 3 of the 2 elements of your array.

Oops.

Paul,

I have obvioulsy got things a bit wrong !!

As previously said, I have got this code from another application and thought it would suit my purpose, unfortunatly it appears the code is wrong in the first place.

The 'transmitter' sends "server-11-1" (for example), 'server' being the identifier, '11' being the pin and '1' being the status of that pin.

Although its commented out at the moment, with:

// digitalWrite(pinNum, pinSet); // Set the pin defined earlier

I can alter the state of a pin when a transmission is received.

So i'm looking at being able to also set a pin high or low depending on whats being transmitted and also call the PHP script.

Unfortunatly what you have suggested is over my head :~

Would you be able to alter my code to what it should look like as that way i will see the difference and perhaps understand where i went wrong and learn. If i try, i could be here for weeks !

Obvioulsy if things are way off and you can think of a better way of doing it, i'm all ears.

Thanks again,

Steve

You clearly know how to use Serial.print() to see what the code is doing. If there are memory issues, adding more Serial.print() statements will make the problem worse.

So, the first thing to do is to search for FreeMemory, in the forum. There is some code you can add, and call at appropriate points, to determine if, indeed, you are running out of SRAM.

If that is the case, perfecting the code you have now would be pointless. If it is not, then, perfecting the code you have now is meaningful.

To fix the code I was talking about in my last reply, you have this:

      char tempNum[2]; //Char to hold the message before converting it to an integer

      tempNum[getPinNum.length() + 1]; // Temp save pin number
      getPinNum.toCharArray(tempNum, getPinNum.length() + 1); // Put the temp number into a char array
      int pinNum = atoi(tempNum); // Convert char array to an integer

      tempNum[getPinSet.length() + 1]; // Temp save pin value
      getPinSet.toCharArray(tempNum, getPinSet.length() + 1); // Put the temp value into a char array
      int pinSet = atoi(tempNum); // Convert char array to an integer

These statements:
tempNum[getPinNum.length() + 1]; // Temp save pin number
are doing nothing, so get rid of them.

These statements:
getPinNum.toCharArray(tempNum, getPinNum.length() + 1);
getPinSet.toCharArray(tempNum, getPinSet.length() + 1);
are passed the wrong value for the second argument.

The array being written to is too small.

So, the code from above should be:

      char tempNum[10]; // Make it plenty big

      getPinNum.toCharArray(tempNum, 10); 
      int pinNum = atoi(tempNum);

      getPinSet.toCharArray(tempNum, 10);
      int pinSet = atoi(tempNum);

A lot of your comments simply state the obvious, and get in the way. Typically, I never append comments to the end of a line of code. If a comment is required (and I use way too few, most of the time), I put a comment (or block of comments) before the code.

// A comment to explain what this code is doing
if(something != thingOfInterest)
{
// Some code to actually do something not obvious would go here
}

Sir, you are a legend 8)

Thank you very much for your help, this code is working well now with no lock-ups.

Its people like you that help newbies like me learn and build on the Arduino community, so i do appriciate it.

Kind regards,

Steve

Sorry me again and still a little confused :~

Because I am using an Ethernet Shield, pins 10-13 cannot be used for standard outputs, so i decided to use pins 8 & 9 as my outputs instead.

Nothing was changed other than specifying the new startPin and stopPin variables:

#define startPin 8 // The first pin we connect a relay to
#define stopPin 9 // The last pin we connect a relay to

and changing the 'if (pinNum == 8 )' statements.

Unfortunatly the PHP is now not being called and just states 'failed to call PHP' in the serial debug window.

I'm guessing its something to do with the array causing this memory error ?

If I select it for Pins 10 & 11 for example, program runs fine. The error is produced for any pin < 10.

Having read the tutorial on arrays, i'm still not fully understanding them, but i guess I need the array to be more flexible to allow for flexibility in the length of the ident string and also for a 1 or 2 digit pin number. This is what was attempted to be achieved in the initial code variation.

This is the latest code:

static String ident = "server";     // Identification for this device

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

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x79, 0xED };    // Arduino MAC Address
byte ip[] = { 192,168,1,142 };                          // local Arduino IP
byte server[] = { 192,168,1,101 };                      // IP of Web Server

Client client(server, 80);

const int ledPin =  13;            // The number of the LED pin, LED used to indicate we got a message
long onMillis = 0;                 // Will store last time LED was updated

// *** Uncomment whatever type of Temp sensor being used ***
#define DHTTYPE DHT11              // DHT 11 
#define DHTPIN 5                   // Sets DHT Temp Sensor Pin 

// *** Set-up Temperature Sensor ***
DHT dht(DHTPIN, DHTTYPE);

// *** Fan related Variables ***
#define fanPin 7                   // Fan is connected to PIN 5

// *** Temperature Variables ***
#define maxTemp 35                 // Fan will start at this temperature
#define minTemp 32                 // Sets Temp Hysteresis variation (3 degress)  

// *** RF Variables ***
#define rxPin 2                    // Sets 433Mhz Rx Pin

// *** Other Variables ***
#define startPin 8               // The first pin we connect a relay to
#define stopPin 9                // The last pin we connect a relay to

void(* resetFunc) (void) = 0;     //declare reset function @ address 0

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  
  //Set-up the RF receiver
  vw_setup(1000);                     // Bits per sec
  vw_set_rx_pin(rxPin);               // We will be receiving on pin 2 ie the RX pin from the module connects to this pin.
  vw_rx_start();                      // Start the receiver

  dht.begin();  // Begin reading DHT sensor
  
  // Set necessary Pins for OUTPUT and set their initial state
  for (int i = startPin; i <= stopPin; i++)
  {
    Serial.print("Setting pinMode on pin: ");
    Serial.println(i);
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }
  Serial.println("Ready to receive!");
}

void loop()
{

  temperature();          //  Calls Temperature function
  
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen)) // Check to see if anything has been received
  {
    String message((char *)buf);

    if (message.substring(0,ident.length()) == ident) // Check if this message was for us
    {
      digitalWrite(ledPin, HIGH); // Turn led on to indicate we received something, and is going to look more at it
      onMillis = millis(); // Save when led was turned on so we remember to turn it off again later

      message = message.substring(ident.length()+1); // Remove the identifier from the message

      int startCommand = message.indexOf('-'); // Find where we split pin number from pin status

      String getPinNum = message.substring(0, startCommand); // Save a String with the pin number
      String getPinSet = message.substring(startCommand+1, message.length()); // Save a String with the value we are going to send to the pin

      char tempNum[10]; //Char to hold the message before converting it to an integer

//      tempNum[getPinNum.length() + 1]; // Temp save pin number
      getPinNum.toCharArray(tempNum, 10);
//      getPinNum.toCharArray(tempNum, getPinNum.length() + 1); // Put the temp number into a char array
      int pinNum = atoi(tempNum); // Convert char array to an integer

//      tempNum[getPinSet.length() + 1]; // Temp save pin value
      getPinSet.toCharArray(tempNum, 10);
//      getPinSet.toCharArray(tempNum, getPinSet.length() + 1); // Put the temp value into a char array
      int pinSet = atoi(tempNum); // Convert char array to an integer

      Serial.print("Pin num: ");
      Serial.print(pinNum);
      Serial.print(" - Pin status: ");
      Serial.println(pinSet);


//      digitalWrite(pinNum, pinSet); // Set the pin defined earlier
    
      if (pinNum == 8)
      {
        Serial.print("Alarm Activated (Pin 8)");
        alarm_1();
      }
      if (pinNum == 9)
      {
        Serial.println("Alarm Activated (pin 9)");
        alarm_2();
      } 
          onMillis = millis();
  }
  if (millis() - onMillis > 250 && onMillis > 0) // Check if it is time to turn the led off
  {
   onMillis = 0;
    digitalWrite(ledPin, LOW);
    }
  }
}

void alarm_1(){
  unsigned long startTime = millis();

  if (client.connect()) { //connect to server
      Serial.println("connected to server");
      // Make a HTTP request:
      client.println("GET /alarm_1.php HTTP/1.0");               // location of ProwlPHP script
      client.println("Host: xxxxxxxx.xxxxxxxxxx.com");           // domain name
      client.println("Connection: close");                       // this line is not related to your problem but I recommend it ;)
      client.println();
      delay(1000);
  }
  else {
    Serial.println("failed to call PHP");
  }
    delay(1000);
    Serial.print("Response from server: ");
    while ((!client.available()) && ((millis() - startTime ) < 5000));
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  
    // if the server's disconnected, stop the client:
    if (!client.connected()) {
      Serial.println("disconnecting from server");
//      client.println("Connection: close");                       // this line is not related to your problem but I recommend it ;)
      client.stop();
      client.flush();
      Serial.println("Disconnected...");
    }
}

void alarm_2(){
  unsigned long startTime = millis();

  if (client.connect()) { //connect to server
      Serial.println("connected to server");
      // Make a HTTP request:
      client.println("GET /alarm_2.php HTTP/1.0");               // location of ProwlPHP script
      client.println("Host: xxxxxxxx.xxxxxxxxxx.com");           // domain name
      client.println("Connection: close");                       // this line is not related to your problem but I recommend it ;)
      client.println();
      delay(1000);
  }
  else {
    Serial.println("failed to call PHP");
  }
    delay(1000);
    Serial.print("Response from server: ");
    while ((!client.available()) && ((millis() - startTime ) < 5000));
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  
    // if the server's disconnected, stop the client:
    if (!client.connected()) {
      Serial.println("disconnecting from server");
//      client.println("Connection: close");                       // this line is not related to your problem but I recommend it ;)
      client.stop();
      client.flush();
      Serial.println("Disconnected...");
    }
}


void temperature (){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float humidity = dht.readHumidity();
  float temp = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(temp) || isnan(humidity)) {
    Serial.println("Failed to read from DHT");
  } else {
    if (temp >= maxTemp){
        Serial.print("Temperature: "); 
        Serial.print(temp);
        Serial.println(" *C");  
    fan(true);}
    else if (temp <= minTemp)
    {fan(false);}
  }
  if (temp <= maxTemp && temp >= minTemp){
        Serial.print("Temperature: "); 
        Serial.print(temp);
        Serial.println(" *C");}
}
    
void fan(boolean stat){
  digitalWrite(fanPin, stat);
}

Because I am using an Ethernet Shield, pins 10-13 cannot be used for standard outputs, so i decided to use pins 8 & 9 as my outputs instead.

You won't be able to use the led on pin 13, either.

Nothing was changed other than specifying the new startPin and stopPin variables:

#define startPin 8 // The first pin we connect a relay to
#define stopPin 9 // The last pin we connect a relay to

and changing the 'if (pinNum == 8 )' statements.

Why isn't the if statement

if (pinNum == startPin)

?

Unfortunatly the PHP is now not being called and just states 'failed to call PHP' in the serial debug window.

Pin 10 is not now being set as an OUTPUT pin, in setup(). Even if you do not want to deselect the ethernet shield, you must define pin 10 as OUTPUT for the ethernet shield to work.

Having read the tutorial on arrays, i'm still not fully understanding them, but i guess I need the array to be more flexible to allow for flexibility in the length of the ident string and also for a 1 or 2 digit pin number. This is what was attempted to be achieved in the initial code variation.

Your failure to connect to the internet has nothing to do with parsing the data that you get from virtual wire.

Thanks for your reply and help Paul.

You won't be able to use the led on pin 13, either.

This has now been moved to a different pin

Why isn't the if statement
if (pinNum == startPin)

Because ultimately I will have 4 alarm conditions, so for now I thought it easier to define them that way opposed to using variables. However, I have renamed the 2 if statements using the startPin & stopPin variables for ease whilst testing.

Pin 10 is not now being set as an OUTPUT pin, in setup(). Even if you do not want to deselect the ethernet shield, you must define pin 10 as OUTPUT for the ethernet shield to work.

I did think this may be an issue, however setting these pins (10-12) to OUTPUT in setup() has made no difference. In all examples I've seen using the Ethernet shield I have not seen the requirement to set these pins manually.

It seems to ouput the "failed to call PHP" instantly as if its not really even attempting to call it.

Any other suggestions ?

Only pin 10 needs to be set as an OUTPUT. The SPI code that the ethernet shield uses to communicate with the Arduino knows how to deal with the other pins. It may be necessary to actually set the pin (10) HIGH, too.

This is driving me nuts and seems to defy all logic :0

The main code below doesn't work and prevents the PHP from being called.

However, if i comment out this section, it works fine ??

I've stripped out as much as i can for testing only leaving a simple loop.

Could something in the RF receiver section be causing problems ? Seems weird if it is, but commenting it out resolves the issue.

  //Set-up the RF receiver
//  vw_setup(1000);                     // Bits per sec
//  vw_set_rx_pin(rxPin);               // We will be receiving on pin 2 ie the RX pin from the module connects to this pin.
//  vw_rx_start();                      // Start the receiver
static String ident = "server";     // Identification for this device

#include <SPI.h>
#include <Ethernet.h>               // Ethernet Library
#include <DHT.h>                    // DHT Temp & Humidity Library
#include <VirtualWire.h>            // 433Mhz Module Library

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x79, 0xED };    // Arduino MAC Address
byte ip[] = { 192,168,1,142 };                          // local Arduino IP
//byte gateway[] = { 192,168,1,1 };                     // IP of Gateway
//byte subnet[] = { 255, 255, 255, 0 };                 // Subnet Address
byte server[] = { 192,168,1,101 };                      // IP of Web Server

Client client(server, 80);

const int ledPin =  6;            // The number of the LED pin, LED used to indicate we got a message
long onMillis = 0;                 // Will store last time LED was updated

// *** Uncomment whatever type of Temp sensor being used ***
#define DHTTYPE DHT11              // DHT 11 
#define DHTPIN 5                   // Sets DHT Temp Sensor Pin 

// *** Set-up Temperature Sensor ***
DHT dht(DHTPIN, DHTTYPE);

// *** Fan related Variables ***
#define fanPin 7                   // Fan is connected to PIN 5

// *** Temperature Variables ***
#define maxTemp 35                 // Fan will start at this temperature
#define minTemp 32                 // Sets Temp Hysteresis variation (3 degress)  

// *** RF Variables ***
#define rxPin 3                    // Sets 433Mhz Rx Pin

// *** Other Variables ***
#define startPin 8               // The first pin we connect a relay to
#define stopPin 9                // The last pin we connect a relay to

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  
    pinMode(10, OUTPUT);
    digitalWrite(10, HIGH);    
    
//    pinMode(11, OUTPUT);
//    digitalWrite(11, HIGH);    
    
    pinMode(12, OUTPUT);
    digitalWrite(12, HIGH);  
  
  //Set-up the RF receiver
  vw_setup(1000);                     // Bits per sec
  vw_set_rx_pin(rxPin);               // We will be receiving on pin 2 ie the RX pin from the module connects to this pin.
  vw_rx_start();                      // Start the receiver

  dht.begin();  // Begin reading DHT sensor
  
  // Set necessary Pins for OUTPUT and set their initial state
  for (int i = startPin; i <= stopPin; i++)
  {
    Serial.print("Setting pinMode on pin: ");
    Serial.println(i);
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);    
  }
  Serial.println("Ready to receive!");
}

void loop()
{
  alarm_1();
  delay(5000);
  alarm_2();
  delay(5000);
}

void alarm_1(){
  unsigned long startTime = millis();

  if (client.connect()) { //connect to server
      Serial.println("connected to server");
      // Make a HTTP request:
      client.println("GET /alarm_1.php");               // location of ProwlPHP script
 //     client.println("Host: tisseyre.homeserver.com");           // domain name
 //     client.println("Connection: close");                       // this line is not related to your problem but I recommend it ;)
      client.println();
      delay(1000);
  }
  else {
    Serial.println("failed to call PHP");
  }
    delay(1000);
    Serial.print("Response from server: ");
    while ((!client.available()) && ((millis() - startTime ) < 5000));
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  
    // if the server's disconnected, stop the client:
    if (!client.connected()) {
      Serial.println("disconnecting from server");
//      client.println("Connection: close");                       // this line is not related to your problem but I recommend it ;)
      client.stop();
      client.flush();
      Serial.println("Disconnected...");
    }
}

void alarm_2(){
  unsigned long startTime = millis();

  if (client.connect()) { //connect to server
      Serial.println("connected to server");
      // Make a HTTP request:
      client.println("GET /alarm_2.php");               // location of ProwlPHP script
      client.println("Host: tisseyre.homeserver.com");           // domain name
      client.println("Connection: close");                       // this line is not related to your problem but I recommend it ;)
      client.println();
      delay(1000);
  }
  else {
    Serial.println("failed to call PHP");
  }
    delay(1000);
    Serial.print("Response from server: ");
    while ((!client.available()) && ((millis() - startTime ) < 5000));
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  
    // if the server's disconnected, stop the client:
    if (!client.connected()) {
      Serial.println("disconnecting from server");
//      client.println("Connection: close");                       // this line is not related to your problem but I recommend it ;)
      client.stop();
      client.flush();
      Serial.println("Disconnected...");
    }
}

void temperature (){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float humidity = dht.readHumidity();
  float temp = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(temp) || isnan(humidity)) {
    Serial.println("Failed to read from DHT");
  } else {
    if (temp >= maxTemp){
        Serial.print("Temperature: "); 
        Serial.print(temp);
        Serial.println(" *C");  
    fan(true);}
    else if (temp <= minTemp)
    {fan(false);}
  }
  if (temp <= maxTemp && temp >= minTemp){
        Serial.print("Temperature: "); 
        Serial.print(temp);
        Serial.println(" *C");}
}
    
void fan(boolean stat){
  digitalWrite(fanPin, stat);
}
    pinMode(12, OUTPUT);
    digitalWrite(12, HIGH);

I don't think you want to do this.