Smart Home Automation Forum
[Arduino IDE demo source code for KC868-A8]--#08-Serial control relays - Printable Version

+- Smart Home Automation Forum (https://www.kincony.com/forum)
+-- Forum: Technical Support (https://www.kincony.com/forum/forumdisplay.php?fid=20)
+--- Forum: KC868-A8 (https://www.kincony.com/forum/forumdisplay.php?fid=23)
+--- Thread: [Arduino IDE demo source code for KC868-A8]--#08-Serial control relays (/showthread.php?tid=1611)



[Arduino IDE demo source code for KC868-A8]--#08-Serial control relays - KinCony Support - 01-05-2022

Code 7: //The demo code is SERIAL-CONTROL-RELAY    You can copy the code to your Arduino IDE

Code:
#include "Arduino.h"
#include "PCF8574.h"
PCF8574 pcf8574(0x24);
char cmd='0';
void setup(){
  pcf8574.pinMode(P0, OUTPUT);
  pcf8574.pinMode(P1, OUTPUT);
  pcf8574.pinMode(P2, OUTPUT);
  pcf8574.pinMode(P3, OUTPUT);
  pcf8574.pinMode(P4, OUTPUT);
  pcf8574.pinMode(P5, OUTPUT);
  pcf8574.pinMode(P6, OUTPUT);
  pcf8574.pinMode(P7, OUTPUT);

  Serial.begin(9600);
    Serial.print("Init pcf8574...");
  if (pcf8574.begin()){
    Serial.println("OK");
  }else{
    Serial.println("KO");

  pcf8574.digitalWrite(P0, HIGH);
  pcf8574.digitalWrite(P1, HIGH);
  pcf8574.digitalWrite(P2, HIGH);
  pcf8574.digitalWrite(P3, HIGH);
  pcf8574.digitalWrite(P4, HIGH);
  pcf8574.digitalWrite(P5, HIGH);
  pcf8574.digitalWrite(P6, HIGH);
  pcf8574.digitalWrite(P7, HIGH);
  }
}

void loop(){
  while(Serial.available())  // To Get up ALL Serial data
  {
    cmd=Serial.read();
   
  }

    if (cmd=='1')
    {
      pcf8574.digitalWrite(P0, LOW);
      Serial.println("You've turned on the Relay");
      cmd = '0';
    }
    else if(cmd == '2')
    {
      pcf8574.digitalWrite(P0, HIGH);
      Serial.println("You've turned off the Relay");
      cmd = '0';
    }
}