Webclient stops connecting when trigger is changed

I have been having trouble getting the webclient to work in a separate function until I found the code posted here http://forum.arduino.cc/index.php?topic=120216.msg907301#msg907301
which works perfectly, although I slightly modified it to connect to my webserver.

My end game, however is to merge it with some other code so that rather than it being triggered by entering ‘e’, it will be triggered by receiving a signal from an InfraRed sensor. I have this code working already.

I figured that I could replace the code in the loop function with my code and call the sendGET function. The sendGet function gets called but it no longer connects.

Here is the code I am trying to get to work. It only stops working once I changed the loop function

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

int receiver = 11; // pin 1 of IR receiver to Arduino digital pin 11
IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 20 }; // ip in lan assigned to arduino
//byte gateway[] = { 192, 168, 0, 20 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 192, 168, 0, 4 }; // web page server IP address
EthernetClient client;
//////////////////////

void setup(){

  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, gateway, gateway, subnet);
  Serial.begin(9600); 
  Serial.println("Better client test 4/04/12"); // so I can keep track of what is loaded
  Serial.println("Send an ir signal to test"); // what to do to test
  irrecv.enableIRIn(); // Start the receiver
}

void loop(){
  // check for ir input
  if (irrecv.decode(&results)) 
    {
      Serial.println(results.value);  // UN Comment to see raw values
      sendGET(); // call sendGET function below when IR is recieved
      irrecv.resume(); // receive the next value
    }
 }  
 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /index.html HTTP/1.0"); //download text
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

In the serial monitor I see:

Better client test 4/04/12
Send an ir signal to test
1
connection failed


disconnecting.
==================

1
connection failed


disconnecting.
==================

1
connection failed


disconnecting.
==================

Could anyone explain why this is happening or have any suggestions on how to fix it?

Cheers
jcaira

int receiver = 11; // pin 1 of IR receiver to Arduino digital pin 11

Pins 11, 12, and 13 are the SPI pins. The Arduino and Ethernet shield communicate using SPI. You can't use the SPI pins for anything else.

Ah ok, I didnt know that. So if I move the IR to a different pin it should work?

So if I move the IR to a different pin it should work?

I'll go out on a limb and say that if you don't, it won't. I'm not willing to assume that you will move the IR to a usable pin.

:smiley:

Switched it to Pin8 and it now works perfectly. I never would have worked that out.
Thanks so much

jcaira