Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Arduino IDE demo source code for KC868-A8S]--#13-KC868-A8S_sms-relay_code
#1
[Arduino IDE demo source code for KC868-A8S]--#13-KC868-A8S_sms-relay_code
// make sure set your Serial2 pins in your "HardwareSerial.cpp" for ESP32 as follows:
//#define RX2  15
//#define TX2  13



Code:
// code use for KinCony KC868-A8S board

#include "PCF8574.h"
#include <DS18B20.h>
String incomingData;
String message;
const char phone[] = "xxxxxxxxxxx";  // replace with your mobile phone number
DS18B20 ds(14);


// Set i2c address
PCF8574 pcf8574_o(0x24,4,5);
PCF8574 pcf8574_i(0x22,4,5);

void setup() {
pinMode(2, OUTPUT);   //beep

Serial.begin(115200);
Serial2.begin(115200);

   // set gsm module to tp show the output on serial out
Serial2.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
 
pcf8574_o.pinMode(P0, OUTPUT);
pcf8574_o.pinMode(P1, OUTPUT);
pcf8574_o.pinMode(P2, OUTPUT);
pcf8574_o.pinMode(P3, OUTPUT);
pcf8574_o.pinMode(P4, OUTPUT);
pcf8574_o.pinMode(P5, OUTPUT);
pcf8574_o.pinMode(P6, OUTPUT);
pcf8574_o.pinMode(P7, OUTPUT);

pcf8574_i.pinMode(P0, INPUT);
pcf8574_i.pinMode(P1, INPUT);
pcf8574_i.pinMode(P2, INPUT);
pcf8574_i.pinMode(P3, INPUT);
pcf8574_i.pinMode(P4, INPUT);
pcf8574_i.pinMode(P5, INPUT);
pcf8574_i.pinMode(P6, INPUT);
pcf8574_i.pinMode(P7, INPUT);


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

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


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

Serial.print(ds.getTempC());

Serial.print("start test");

Serial2.print("AT+CMGF=1\r");  //Set text mode
/* digitalWrite(2, HIGH);
delay(200); 
digitalWrite(2, LOW);
delay(200);
digitalWrite(2, HIGH);
delay(200); 
digitalWrite(2, LOW);*/
}

void loop() {
// if received command is to turn on relay
  receive_message();

  if(incomingData.indexOf("relay1-on")>=0)
  {
    pcf8574_o.digitalWrite(P0, LOW);
    message = "Relay1 is turned ON";
    // Send a sms back to confirm that the relay is turned on
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);
  }
 
  // if received command is to turn off relay
  if(incomingData.indexOf("relay1-off")>=0)
  {
    pcf8574_o.digitalWrite(P0, HIGH);
    message = "Relay1 is turned OFF";
    // Send a sms back to confirm that the relay is turned off
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);
  }
 
  if(incomingData.indexOf("relay2-on")>=0)
  {
    pcf8574_o.digitalWrite(P1, LOW);
    message = "Relay2 is turned ON";
    // Send a sms back to confirm that the relay is turned on
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);
  }
 
  // if received command is to turn off relay
  if(incomingData.indexOf("relay2-off")>=0)
  {
    pcf8574_o.digitalWrite(P1, HIGH);
    message = "Relay2 is turned OFF";
    // Send a sms back to confirm that the relay is turned off
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);
  } 
 
  if(incomingData.indexOf("relay3-on")>=0)
  {
    pcf8574_o.digitalWrite(P2, LOW);
    message = "Relay3 is turned ON";
    // Send a sms back to confirm that the relay is turned on
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);
  }
 
  // if received command is to turn off relay
  if(incomingData.indexOf("relay3-off")>=0)
  {
    pcf8574_o.digitalWrite(P2, HIGH);
    message = "Relay3 is turned OFF";
    // Send a sms back to confirm that the relay is turned off
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);
  } 
 
  if(incomingData.indexOf("relay4-on")>=0)
  {
    pcf8574_o.digitalWrite(P3, LOW);
    message = "Relay4 is turned ON";
    // Send a sms back to confirm that the relay is turned on
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);     
  }
 
  // if received command is to turn off relay
  if(incomingData.indexOf("relay4-off")>=0)
  {
    pcf8574_o.digitalWrite(P3, HIGH);
    message = "Relay4 is turned OFF";
    // Send a sms back to confirm that the relay is turned off
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);     
  } 
 
  if(incomingData.indexOf("relay5-on")>=0)
  {
    pcf8574_o.digitalWrite(P4, LOW);
    message = "Relay5 is turned ON";
    // Send a sms back to confirm that the relay is turned on
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);     
  }
 
  // if received command is to turn off relay
  if(incomingData.indexOf("relay5-off")>=0)
  {
    pcf8574_o.digitalWrite(P4, HIGH);
    message = "Relay5 is turned OFF";
    // Send a sms back to confirm that the relay is turned off
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);     
  } 
 
  if(incomingData.indexOf("relay6-on")>=0)
  {
    pcf8574_o.digitalWrite(P5, LOW);
    message = "Relay6 is turned ON";
    // Send a sms back to confirm that the relay is turned on
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);     
  }
 
  // if received command is to turn off relay
  if(incomingData.indexOf("relay6-off")>=0)
  {
    pcf8574_o.digitalWrite(P5, HIGH);
    message = "Relay6 is turned OFF";
    // Send a sms back to confirm that the relay is turned off
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);     
  } 
 
  if(incomingData.indexOf("relay7-on")>=0)
  {
    pcf8574_o.digitalWrite(P6, LOW);
    message = "Relay7 is turned ON";
    // Send a sms back to confirm that the relay is turned on
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);     
  }
 
  // if received command is to turn off relay
  if(incomingData.indexOf("relay7-off")>=0)
  {
    pcf8574_o.digitalWrite(P6, HIGH);
    message = "Relay7 is turned OFF";
    // Send a sms back to confirm that the relay is turned off
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);     
  } 
 
  if(incomingData.indexOf("relay8-on")>=0)
  {
    pcf8574_o.digitalWrite(P7, LOW);
    message = "Relay8 is turned ON";
    // Send a sms back to confirm that the relay is turned on
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);     
  }
 
  // if received command is to turn off relay
  if(incomingData.indexOf("relay8-off")>=0)
  {
    pcf8574_o.digitalWrite(P7, HIGH);
    message = "Relay8 is turned OFF";
    // Send a sms back to confirm that the relay is turned off
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);     
  } 
 
  if(incomingData.indexOf("all-on")>=0)
  {
    pcf8574_o.digitalWrite(P0, LOW);
    pcf8574_o.digitalWrite(P1, LOW);
    pcf8574_o.digitalWrite(P2, LOW);
    pcf8574_o.digitalWrite(P3, LOW);
    pcf8574_o.digitalWrite(P4, LOW);
    pcf8574_o.digitalWrite(P5, LOW);
    pcf8574_o.digitalWrite(P6, LOW);
    pcf8574_o.digitalWrite(P7, LOW);
    message = "All relay are turned ON";
    // Send a sms back to confirm that the relay is turned off
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);     
  }   
 
  if(incomingData.indexOf("all-off")>=0)
  {
    pcf8574_o.digitalWrite(P0, HIGH);
    pcf8574_o.digitalWrite(P1, HIGH);
    pcf8574_o.digitalWrite(P2, HIGH);
    pcf8574_o.digitalWrite(P3, HIGH);
    pcf8574_o.digitalWrite(P4, HIGH);
    pcf8574_o.digitalWrite(P5, HIGH);
    pcf8574_o.digitalWrite(P6, HIGH);
    pcf8574_o.digitalWrite(P7, HIGH);
    message = "All relay are turned OFF";
    // Send a sms back to confirm that the relay is turned off
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);     
  }     
 
  if(incomingData.indexOf("temper")>=0)
  {
   
    message = "Temperature is "+String(ds.getTempC(),2)+" C";
    send_message(message);
    Serial2.println("AT+CMGD=1,4");  //delete all SMS on SIM card
    delay(100);     
  }   


  incomingData="";

  uint8_t val1 = pcf8574_i.digitalRead(P0);
  uint8_t val2 = pcf8574_i.digitalRead(P1);
  uint8_t val3 = pcf8574_i.digitalRead(P2);
  uint8_t val4 = pcf8574_i.digitalRead(P3);
  uint8_t val5 = pcf8574_i.digitalRead(P4);
  uint8_t val6 = pcf8574_i.digitalRead(P5);
  uint8_t val7 = pcf8574_i.digitalRead(P6);
  uint8_t val8 = pcf8574_i.digitalRead(P7);

  if (val1==LOW)
   {
     Serial.println("input1 trigger");
     pcf8574_o.digitalWrite(P0, LOW);
     delay(5000);
     pcf8574_o.digitalWrite(P0, HIGH);
     
     message = "door sensor is triggered,someone in the room!";
     send_message(message);
     delay(100);
   }

  if (val2==LOW)
   {
     Serial.println("input2 trigger");
     pcf8574_o.digitalWrite(P0, LOW);
     delay(5000);
     pcf8574_o.digitalWrite(P0, HIGH);
     
     message = "window sensor is triggered,someone in the room!";
     send_message(message);
     delay(100);
   }

  if (val3==LOW)
   {
     Serial.println("input3 trigger");
     message = "sensor3 is triggered,someone in the room!";
     send_message(message);
     delay(100);
   }

  if (val4==LOW)
   {
     Serial.println("input4 trigger");   
     message = "sensor4 is triggered,someone in the room!";
     send_message(message);
     delay(100);
   }

  if (val5==LOW)
   {
     Serial.println("input5 trigger");     
     message = "sensor5 is triggered,someone in the room!";
     send_message(message);
     delay(100);
   }

  if (val6==LOW)
   {
     Serial.println("input6 trigger");     
     message = "sensor6 is triggered,someone in the room!";
     send_message(message);
     delay(100);
   }

  if (val7==LOW)
   {
     Serial.println("input7 trigger");   
     message = "sensor7 is triggered,someone in the room!";
     send_message(message);
     delay(100);
   }

  if (val8==LOW)
   {
     Serial.println("input8 trigger");     
     message = "sensor8 is triggered,someone in the room!";
     send_message(message);
     delay(100);
   }
delay(200);
}

void receive_message()
{
  if (Serial2.available() > 0)
  {
    incomingData = Serial2.readString(); // Get the data from the serial port.
    Serial.print(incomingData);
    delay(10);
  }
}

void send_message(String message)
{
  Serial2.println("AT+CMGF=1");    //Set the GSM Module in Text Mode
  delay(100); 
  Serial2.print("AT+CMGS=\""+String(phone)+"\"\r"); //Send message
  delay(100);
  Serial2.println(message);   // The SMS text you want to send
  delay(100);
  Serial2.println((char)26);  // ASCII code of CTRL+Z
  delay(100);
  Serial2.println();
  delay(100); 
}
Reply


Messages In This Thread
[Arduino IDE demo source code for KC868-A8S]--#13-KC868-A8S_sms-relay_code - by KinCony Support - 06-30-2022, 12:54 AM

Forum Jump:


Users browsing this thread:
1 Guest(s)