Monday, November 28, 2016

NRF24L01 for remote relay

NRF24L01 for remote relay


//receiver

#include <SPI.h>                       // SPI Arayüzünü dahil ettik.
//#include "nRF24L01.h"
#include "RF24.h"                      // RF24 kütüpanelerini dahil ettik.
#include <Wire.h>
#include <DS3231.h>


DS3231  rtc(SDA, SCL);
// Init a Time-data structure
Time  t;
uint32_t targetTime = 0;
uint8_t hh = 0, mm = 0, ss = 0, dd = 0, bb = 0; //variable Unsignet int 0-255
int yy = 0; //variable integer
byte data[1];                          // Göndereceğimiz data değişkenini oluşturduk.
boolean var;                           // Bit şeklinde var değişkeninini oluşturduk.
const uint64_t pipe = 0xF0F0F0F0E1LL;
RF24 radio(9, 10);                     // Modülün CE pinini 9.pin CS pinini de 10.pine bağlayacağımızı belirttik.

int r1 = 0, r2 = 0, r3 = 0;

void setup() {
  Serial.begin(57600);
  pinMode(3, OUTPUT); digitalWrite(3, HIGH);//kolam renang
  pinMode(5, OUTPUT); digitalWrite(5, HIGH);//kolam naga
  pinMode(7, OUTPUT); digitalWrite(7, HIGH); //lampu naga
  radio.begin();                        
  radio.openReadingPipe(1, pipe);      
  radio.startListening();
  rtc.begin();
}

void loop() {

  // NRF remote

  if (radio.available()) {
    bool var = false;
    while (!var) {
      var = radio.read(&data, 1);
    }//while

    if (data[0] == 1 && r1 == 0) {
      digitalWrite(3, LOW);
     // Serial.println("RELAY 3 LOW");
      r1 = 1; data[0] = 0;
    }
    if (data[0] == 1 && r1 == 1 ) {
      digitalWrite(3, HIGH);
      //Serial.println("RELAY 3 HIGH ");
      r1 = 0; data[0] = 0;
    }

    if (data[0] == 2 && r2 == 0) {
      digitalWrite(5, LOW);
      //Serial.println("RELAY 5 LOW");
      r2 = 1; data[0] = 0;
    }
    if (data[0] == 2 && r2 == 1 ) {
      digitalWrite(5, HIGH);
      //Serial.println("RELAY 5 HIGH ");
      r2 = 0; data[0] = 0;
    }

    if (data[0] == 3 && r3 == 0) {
      digitalWrite(7, LOW);
      //Serial.println("RELAY 7 LOW");
      r3 = 1; data[0] = 0;
    }
    if (data[0] == 3 && r3 == 1 ) {
      digitalWrite(7, HIGH);
      //Serial.println("RELAY 7 HIGH ");
      r3 = 0 ; data[0] = 0;
    }
  }//if

  t = rtc.getTime();  // gate all data time & date from RTC
  hh = t.hour, DEC; //pengambilan data jam
  mm = t.min, DEC; //pengambilan data menit
  ss = t.sec, DEC; //pengambilan data detik
  //Serial.print (hh); Serial.print (F(" : "));
  //Serial.print (mm); Serial.print (F(" : "));
  //Serial.println (ss);

  //jam 9 pagi
  if (hh == 10 && mm == 0 && ss == 0) {
    digitalWrite(3, LOW);//kolam renang
    digitalWrite(5, LOW);//kolam naga
    digitalWrite(7, HIGH);//lampu naga //relay 3
  }
  if (hh == 15 &&  mm == 0 && ss == 0) {
    digitalWrite(3, HIGH);//kolam renang
    digitalWrite(5, HIGH);//kolam naga
    digitalWrite(7, HIGH);//lampu naga
  }
delay(1000);
}



// transmitter
#include  <SPI.h>
//#include "nRF24L01.h"
#include  "RF24.h"
#include <PinChangeInterrupt.h>
#include <avr/sleep.h>


volatile byte flag = 0;
void handler() {
    flag = 1;
    //Serial.println("wake up");
    //delay(100);
    detachPinChangeInterrupt(3);
    detachPinChangeInterrupt(5);
    detachPinChangeInterrupt(7);
}

byte data[1];
const uint64_t pipe = 0xF0F0F0F0E1LL;
RF24 radio(9, 10);                    // Modülün CE pinini 9.pin CS pinini de 10.pine bağlayacağımızı belirttik.
unsigned long nowTime;
unsigned long start1;

unsigned long previousMillis = 0;
unsigned long startSleep;

int kirim = 0,btn3,btn5,btn7;
int sleepStatus = 0;             // variable to store a request for sleep
int count = 0;                   // counter
void wakeUpNow()   {}

void setup() {
  Serial.begin(57600);
  pinMode(3, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(13, INPUT);
  digitalWrite(13, LOW);
  attachPCINT(digitalPinToPCINT(3), handler, CHANGE);
  attachPCINT(digitalPinToPCINT(5), handler, CHANGE);
  attachPCINT(digitalPinToPCINT(7), handler, CHANGE);
  radio.begin();
  radio.setRetries(15,15);
  radio.openWritingPipe(pipe);
  radio.startListening();
}
void sleepNow(){
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    sleep_enable();
      attachPCINT(digitalPinToPCINT(3), handler, CHANGE);
  attachPCINT(digitalPinToPCINT(5), handler, CHANGE);
  attachPCINT(digitalPinToPCINT(7), handler, CHANGE);
    sleep_mode();
    sleep_disable();
    detachPinChangeInterrupt(3);
    detachPinChangeInterrupt(5);
    detachPinChangeInterrupt(7);
}

void loop() {
//Serial.println("bgn");
  btn3 = digitalRead(3);
  btn5 = digitalRead(5);
  btn7 = digitalRead(7);

  if(flag == 1){
      if (btn3==0) {
        data[0] = 1;
        Serial.println("RELAY 3 ");
      }
      else if (btn5==0) {
        data[0] = 2;
        Serial.println("RELAY 5 ");
      }
      else if (btn7==0) {
        data[0] = 3;
        Serial.println("RELAY 7 ");
      }
 
    radio.stopListening();
    radio.write( &data, sizeof(unsigned long) );
    radio.startListening();
    delay(1000);
    data[0]=9;
    flag = 0;
  }//flag
  startSleep = millis();
  if(startSleep - previousMillis >= 3000) {
    previousMillis = startSleep;
    Serial.println("sleep");
    sleepNow();
  }
}//end loop



No comments:

Post a Comment