Wednesday, February 13, 2019

Arduino HC12 with DFplayer

i want to make bell using HC-12 long range 433mhz with dfplayer


here is the code

////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// RECEIVER //////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// HC-12 TRANSCEIVER
// RECEIVER (RX) part

#include <SoftwareSerial.h> // import the necessary library
#include <DFPlayer_Mini_Mp3.h>
SoftwareSerial hc12(10, 9); // TX - RX-- HC-12
SoftwareSerial myMP3(12, 11); // bener 12/11

char incomingByte;
String readBuffer = "";
const int led     = 5;
const int setPin = 7; // pin used to set up the HC-12 wireless module


void setup() {
  pinMode(setPin, OUTPUT); //  setPin on hc12
  digitalWrite(led, LOW);

  Serial.begin(9600);
  hc12.begin(9600); // Start the software serial port
  Serial.println("start");

  digitalWrite(setPin, LOW);
  //hc12.print("AT+DEFAULT");
  hc12.print("AT+B19200");
  hc12.print("AT+C001");
  digitalWrite(setPin, HIGH);


  myMP3.begin(9600);
  mp3_set_serial (myMP3);
  delay(400);
  mp3_reset();
  delay(400);
  mp3_set_volume(15);
  delay(1000);
}

void loop() {
  hc12.listen();
  while (hc12.available()) {
    Serial.println("GET data");
    incomingByte = hc12.read();
    readBuffer += char(incomingByte);
    Serial.println(readBuffer);
    if (readBuffer == "bel") {
      hc12.print("bel1");
      //Serial.println("GET DATA 2");
      mp3_play (1); //PLAY SONG 0001.mp3
      delay(14000L);
      readBuffer = "";
    } // bel

    // PLAYING ANOTHER SOUND
    if (readBuffer == "ob" ) {
      hc12.print("ob1");
      mp3_play (2);
      delay(14000L);
      readBuffer = "";
    } //calling my office boy
  }

}



/////////////////////////////////////////////////////////////////////////////////
//TRANSMITTER
//////////////////////////////////////////////////////////////////////////////
// HC-12 TRANSCEIVER
// TRANSMITTER (TX) part


#include <SoftwareSerial.h>
SoftwareSerial hc12(11, 9); //  TX-RX pins on Arduino

const int pcnPin = 3; // pin connected to the output of the client alarm system
const int setPin = 7; // pin used to set up the HC-12 wireless module
const int led     = 5; //confirmation led blink
int var = 0;
int flag = 0;
unsigned long start, skrg;
unsigned long interval = 1000UL * 60 * 0.5;
char incomingByte;
String readBuffer = "";
void setup() {
  pinMode(setPin, OUTPUT);
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
  pinMode(pcnPin, INPUT_PULLUP); // my alarm system will ground the dry contact upon alarm

  hc12.begin(9600); // start the software serial port
  Serial.begin(9600);

  digitalWrite(setPin, LOW);
  //hc12.print("AT+DEFAULT");
  hc12.print("AT+B19200");
  hc12.print("AT+C001");
  digitalWrite(setPin, HIGH);

}
void loop() {

  if (digitalRead(pcnPin) == LOW && flag == 0) {
    start = millis();
    digitalWrite(led, HIGH);
    delay(200);
    digitalWrite(led, LOW);
    delay(200);
    flag = 1;
  }
  skrg = millis();
  if ( skrg - start > interval ) {
    flag = 0;
    //Serial.println("reset push");
  }
  if (digitalRead(pcnPin) == LOW && flag == 1) {
    hc12.print("bel");
    digitalWrite(led, HIGH);
    delay(200);
    digitalWrite(led, LOW);
    delay(200);
    digitalWrite(led, HIGH);
    delay(200);
    digitalWrite(led, LOW);
    delay(200);

    while (hc12.available()) {
      incomingByte = hc12.read();
      readBuffer += char(incomingByte);
    }
  }
  if (readBuffer == "") {
    flag = 1;
    hc12.print("bel");
  }
  if (readBuffer == "bel1") {
    flag = 0;
  }

  delay(100);

}



No comments:

Post a Comment