以太网与IRremote库冲突

时间:2015-06-01 12:45:42

标签: arduino arduino-uno ethernet infrared

我有一个带以太网屏蔽的arduino uno板。我用它来控制我的继电器灯。这是我的代码

#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, 7, 8, 9}; //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><br /><br />");
          client.println("<a href=\"/?lighton2\"\">Turn On Light2 </a>");
          client.println("<a href=\"/?lightoff2\"\"> Turn Off Light2</a><br /><br />");
          client.println("<a href=\"/?lighton3\"\">Turn On Light3 </a>");
          client.println("<a href=\"/?lightoff3\"\"> Turn Off Light3</a><br /><br />");
          client.println("<a href=\"/?lighton4\"\">Turn On Light4 </a>");
          client.println("<a href=\"/?lightoff4\"\"> Turn Off Light4</a><br /><br />");            
          client.println("<a href=\"/?rekamIR\"\">Rekam IR</a><br /><br />");            
          client.println("Jika ingin mencoba auto, ketikkan : <br/><br/><b>");
          client.println("http://192.168.1.20/?lightauto[angka][HHOn][MMOn][HHOff][MMOff]");
          client.println("<b/></BODY>");
          client.println("</HTML>");

          delay(1);
          cekLink();
          printTime();
          //stopping client
          client.stop();
          //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); 
    }
  }
}

但是当我添加IRremote库来接收IR频率时,

#include <IRremote.h>

它不再起作用了。有人可以帮忙吗?

0 个答案:

没有答案