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



Monday, November 7, 2016

nodemcu cloudmqtt servo

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Servo.h>
#include<EEPROM.h>

long lastReconnectAttempt = 0;
int servoPin = D4;
bool statusBall;
const char *statusBall2;
bool statusBall3;
int addressEpp = 0;
Servo servo;
const char *statusServo;
const char *ssid =  "ssid";   // cannot be longer than 32 characters!
const char *pass =  "pass";   //

//Prepare Mqtt connection data
const char *MQTTBroker = "cloudmqtt.com";
const char *MQTTuser = "";
const char *MQTTpassword = "";
//const char *Topic = "/kranstatus";
//const char *Topicid = "/kran";
uint16_t port = 12728;


void cekMem(){
    statusBall=EEPROM.read(1);
 // Serial.println(" memory ");
 // Serial.println(statusBall);
  if(statusBall == 1){
    statusBall2 = "on";
    servoOn();
  }
  if(statusBall == 0){
    statusBall2 ="off";
    servoOff();
  }
 // Serial.println("memory part");
   //Serial.println(statusBall2);
}

void servoOn(){
    servo.attach(servoPin);
    servo.write(86);
    delay(800);
    servo.detach();
   // Serial.println("servo on");
    statusBall2 = "on";
    EEPROM.write(1, 1);
}
void servoOff(){
    servo.attach(servoPin);
    servo.write(250);
    delay(800);
    servo.detach();
    //Serial.println("servo off");
    statusBall2 = "off";
    EEPROM.write(1, 0);
}

//PubSubClient mqtt(MQTTBroker,port);
WiFiClient wclient;
PubSubClient client(wclient, MQTTBroker, port);

void callback(const MQTT::Publish& pub){
  //Serial.println(pub.payload_string());
    if(pub.payload_string()=="on" && statusBall2 == "off" ){
    servoOn();
    //Serial.println("call back on");
  }
  if(pub.payload_string()=="off" && statusBall2 == "on"){
    servoOff();
    //Serial.println("call back off");
  }
}
void WIFI_Connect()
{
  WiFi.disconnect(1);
  Serial.println("Booting Sketch...");
  delay(2000);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
    int Attempt = 0;
    while (WiFi.status() == 6) {
    delay(100);
    Attempt++;
      if (Attempt == 300) {
        ESP.restart();
      }
    }
 
  client.set_callback(callback);  //Set Callback function

  if (client.connect(MQTT::Connect("kran").set_auth(MQTTuser,MQTTpassword))){
    client.publish("kranstatus", statusBall2);  //publish
    client.subscribe("kranstatus");    //subscribe
  }
   // Serial.print("Wifi connect");
    //Serial.println(WiFi.status());
}
void setup()
{
  statusBall=EEPROM.read(1);
  //Serial.print(" memory --");
  //Serial.println(statusBall);
  if(statusBall == 1){
    statusBall2 = "on";
    servoOn();
  }
  if(statusBall == 0){
    statusBall2 ="off";
    servoOff();
  }
  //Serial.print("memory status");
   //Serial.println(statusBall2);
  //Serial.begin(9600);

  WIFI_Connect();
  //client.set_callback(callback);  //Set Callback function

  //if (client.connect(MQTT::Connect("kran").set_auth(MQTTuser,MQTTpassword))){
  //  client.publish("kranstatus", statusBall2);  //publish
  //  client.subscribe("kranstatus");    //subscribe
  //}

}


void loop()
{

      //delay(1000);
      //client.subscribe("kranstatus");
      //client.publish("kranstatus", statusBall2);  //publish
    //Serial.print("status loop ");
    //Serial.println(WiFi.status());
    delay(2000);
    if (WiFi.status() == 6)
    {
      //Serial.println("putus koneksi");
      WIFI_Connect();
    }
    else
    {
      if(!client.subscribe("kranstatus")){
        WIFI_Connect();
      }
      if(!client.publish("kranstatus", statusBall2)){
        WIFI_Connect();
      }

      //delay(1000);
      //client.subscribe("kranstatus");
      //client.publish("kranstatus", statusBall2);  //publish
    }

client.loop();

 
}