Cannot join Ethernet shield with IRremote library

I have some code here

#include <SPI.h>
#include <Ethernet.h>
#include <Time.h> 
#include <Wire.h> 
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t


// Enter a MAC address and IP address for your controller below.
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x48, 0xD3 };
int lamp[] = {4, 6, 7, 8}; //pin buat relay
int lampAutoOnH[4];
int lampAutoOnM[4];
int lampAutoOffH[4];
int lampAutoOffM[4];
int j;
boolean lampAuto[4];

// The IP address will be dependent on your local network:
// assign an IP address for the controller:

byte ip[] = {192,168,0,20};
byte gateway[] = {192,168,0,1};
byte subnet[] = {255, 255, 255, 0};

// Initialize the Ethernet server library with the port you want to use.
EthernetServer server(80);
String readString;

// Declare Pin 8 as an LED because thats what we will be connecting the LED to.You could use any other pin and would then have to change the pin number.

void setup()
{
  for(int i=0; i<4; i++){
    pinMode(lamp[i], OUTPUT);
    digitalWrite(lamp[i], LOW);
    lampAuto[i] = false;
  }
  
  //trial with led, plug in to D1

  //enable serial data print
  Serial.begin(9600);
  
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  //Ethernet.begin(mac); //for DHCP
  server.begin();
  Serial.print("Server is at ");
  Serial.println(Ethernet.localIP());
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else{
     Serial.println("RTC has set the system time");
     printTime();
  }
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client){
    while (client.connected()){
      if (client.available()){
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100){
          //store characters to string
          readString += c;
          //Serial.print(c);
        }
        //Serial.write(c);
        if (c == '\n') {
          Serial.println(readString); //print to serial monitor for debuging

          // Needed to Display Site:
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();
          client.println("<HTML>");
          client.println("<HEAD>");

          // what is being Displayed :     
          client.println("<TITLE>Home Automation</TITLE>");
          client.println("<center>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>Home Automation</H1>");
          client.println("<hr />");
          client.println("<center>");            
          client.println("<a href=\"/?lighton1\"\">Turn On Light1 </a>");
          client.println("<a href=\"/?lightoff1\"\"> Turn Off Light1</a>

");
          client.println("<a href=\"/?lighton2\"\">Turn On Light2 </a>");
          client.println("<a href=\"/?lightoff2\"\"> Turn Off Light2</a>

");
          client.println("<a href=\"/?lighton3\"\">Turn On Light3 </a>");
          client.println("<a href=\"/?lightoff3\"\"> Turn Off Light3</a>

");
          client.println("<a href=\"/?lighton4\"\">Turn On Light4 </a>");
          client.println("<a href=\"/?lightoff4\"\"> Turn Off Light4</a>

");            
          client.println("<a href=\"/?rekamIR\"\">Rekam IR</a>

");            
          client.println("Jika ingin mencoba auto, ketikkan : 

<b>");
          client.println("http://192.168.1.20/?lightauto[angka][HHOn][MMOn][HHOff][MMOff]");
          client.println("<b/></BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();
          cekLink();
          printTime();
          //clearing string for next read
          readString="";
        }
      }
    }
  }
  cekAuto();
}

void cekLink(){ 
  // Code which needs to be Implemented:
  if(readString.indexOf("?lighton") >0)//checks for on
  {
    String i = readString.substring(13, 14);
    j = i.toInt()-1;
    Serial.print("Lamp number '");
    Serial.print(j+1);
    Serial.print("' is on\nreadString = " + readString);
    Serial.print(readString);
    digitalWrite(lamp[j], HIGH);
    lampAuto[j] = false;
  }
  else if(readString.indexOf("?lightoff") >0)//checks for off
  {
    String i = readString.substring(14, 15);
    j = i.toInt()-1;
    Serial.print("Lamp number '");
    Serial.print(j+1);
    Serial.print("' is off\nreadString = " + readString);
    digitalWrite(lamp[j], LOW);
    lampAuto[j] = false;
  }else if(readString.indexOf("?lightauto") >0)//checks for on
  {
    //GET /?lightauto119171918
    //0123456789012345678
    String ll = readString.substring(15, 16);
    String hhOn = readString.substring(16, 18);
    String mmOn = readString.substring(18, 20);
    String hhOff = readString.substring(20, 22);
    String mmOff = readString.substring(22, 24);
    j = ll.toInt()-1;
    lampAuto[j] = true;
    lampAutoOnH[j] = hhOn.toInt();
    lampAutoOnM[j] = mmOn.toInt();
    lampAutoOffH[j] = hhOff.toInt();
    lampAutoOffM[j] = mmOff.toInt();
    Serial.print("Lamp number '");
    Serial.print(j+1);
    Serial.print("' is auto. \n");
    Serial.println("On at " + hhOn + ":" + mmOn);
    Serial.println("Off at " + hhOff + ":" + mmOff);
    Serial.print("readString = " + readString);
    if(hour()>=lampAutoOnH[j])
      digitalWrite(lamp[j], HIGH);
    else if(hour()<=lampAutoOffH[j]&&minute()<=lampAutoOffM[j])
      digitalWrite(lamp[j], HIGH);
    else
      digitalWrite(lamp[j], LOW);
  }else if(readString.indexOf("?rekamIR") >0){
    Serial.println("Rekam IR");
  }
}

void printTime() {
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
 //konversi waktu normal
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void cekAuto(){
  for(int i=0; i<4; i++){
    if(lampAuto[j]==true){
      if(lampAutoOnH[i]==hour()&&lampAutoOnM[i]==minute())
        digitalWrite(lamp[j], HIGH);
      else if(lampAutoOffH[i]==hour()&&lampAutoOffM[i]==minute())
        digitalWrite(lamp[j], LOW); 
    }
  }
}

but, when i add this library

#include <IRremote.h>

the ethernet not working anymore.

the serial get an output like this :

GET /?lighton1 HTTP/1.1

but the relay stuck and i can't do anything with the web server. Anyone can help?

Don't know about the rest, but this will cause problems. You omitted a parameter.

// change this
  Ethernet.begin(mac, ip, gateway, subnet);
// to this
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

I avoid using the String data type. It has proven to be unreliable.

edit: Which IRRemote library are you using? There could be a pin conflict.

I already tried your solution, but it still doesn't work.

This is my serial monitor output without IRremote library Old output

And this is when i add the IRremote library :
New output

I tried all of the library. IRLib (I forgot the source), IRremote (from github), and still doesn't work,

A link to the IR library please.

Which pins are the IR device connected to?

edit: Looks like a SRAM shortage. I would guess it is the readString variable that is failing to store your requests.

GitHub - Arduino-IRremote/Arduino-IRremote: Infrared remote library for Arduino: send and receive infrared signals with multiple protocols --> IRremote.h

I have not installed the IR receiver and transmitter. how can I install it if when I add the library I've been getting an error? :confused:

And what should i do with the readString variable?

That library attempts to use D13. That is reserved for the SPI bus.

However, I don't think that is the problem. I think you are running out of SRAM. How much SRAM is the compiler reporting used by global variables?

Add this function to your sketch and call it at the end of the setup function.

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval); 
}

the output is 176 sir.

And this is output from my IDE :

Sketch uses 20,636 bytes (63%) of program storage space. Maximum is 32,256 bytes.
Global variables use 1,826 bytes (89%) of dynamic memory, leaving 222 bytes for local variables. Maximum is 2,048 bytes.

You are out of SRAM.

Global variables use 1,826 bytes (89%) of dynamic memory, leaving 222 bytes for local variables. Maximum is 2,048 bytes.

whether the possibility that I can do to optimize my sketch?

I don't know if you can. It appears all the global SRAM allocation is happening in the libraries.

You could try using the F() function with your client.print and client.println calls. It has a downside though. Each character is sent in a separate packet, making the code a bit of a network hog.

I prefer using a small character array and strcpy_P() and strcat_P() with PSTR() static strings.

Below is an example of putting individual lines in an F() macro and bottom is bundling a number of lines in an F() macro

 client.println(F("<TITLE>Home Automation</TITLE>"));
          client.println(F("<center>"));
          client.println(F("</HEAD>"));
          client.println(F("<BODY>"));
          client.println(F("<H1>Home Automation</H1>"));
         //now output HTML data header

client.print(F(  //start F() macro
"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"
"<HTML>"
"<HEAD>"
"<meta name='apple-mobile-web-app-capable' content='yes' />"
"<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />"
"<TITLE>JAVA Page</TITLE>"
"</HEAD>"
"<BODY>"
"<H1>JAVA</H1>"
"<hr />"
"
"
"<FORM ACTION='/' method=get >"
"Enter Code: <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
"
"
"
"<input type=submit value='5 ON' style=width:100px;height:45px onClick=location.href='/?on8;'><input type=submit value='5 OFF' style=width:100px;height:45px onClick=location.href='/?off9;'>
"
"<input type=submit value='6 ON' style=width:100px;height:45px onClick=location.href='/?on8;'><input type=submit value='6 OFF' style=width:100px;height:45px onClick=location.href='/?off9;'>
"
"<input type=submit value='7 ON' style=width:100px;height:45px onClick=location.href='/?on8;'><input type=submit value='7 OFF' style=width:100px;height:45px onClick=location.href='/?off9;'>
"
"<input type=submit value='8 ON' style=width:100px;height:45px onClick=location.href='/?on8;'><input type=submit value='8 OFF' style=width:100px;height:45px onClick=location.href='/?off9;'>
"
"</FORM>"
"</BODY>"
"</HTML>"
));   //end F() macro

FYI: Using the F() macro with the client.print or client.println functions sends one character per packet. It works most of the time, but even if it works, it is slow and network unfriendly.

Thank you for all of your helps sir. My code now working. I solved the problem by deleting all of the html code. :slight_smile:

That's good, but I didn't notice one thing about your code. You are using D7 in your code. The wifi shield uses D7 as the handshake pin.

// in global declarations
int lamp[] = {4, 6, 7, 8}; //pin buat relay

// then in setup
  for(int i=0; i<4; i++){
    pinMode(lamp[i], OUTPUT);
    digitalWrite(lamp[i], LOW);
    lampAuto[i] = false;