410 lines
8.3 KiB
C++
Executable File
410 lines
8.3 KiB
C++
Executable File
//Bedverlichting
|
|
|
|
// Enable debug prints to serial monitor
|
|
#define MY_DEBUG
|
|
|
|
// Enable and select radio type attached
|
|
#define MY_RADIO_NRF24
|
|
//#define MY_RADIO_RFM69
|
|
|
|
// Enable repeater functionality for this node
|
|
//#define MY_REPEATER_FEATURE
|
|
|
|
// Define a static node address, remove if you want auto address assignment
|
|
#define MY_NODE_ID 51
|
|
#define KAST_NODE_ID 50
|
|
|
|
#include <SPI.h>
|
|
#include <MySensors.h>
|
|
#include <Bounce2.h>
|
|
|
|
#define SLEEP_MODE false // Watt-value can only be reported when sleep mode is false.
|
|
|
|
//Drukknoppen
|
|
#define s1_PIN 4 // I/O pin switch 1 frank
|
|
#define s2_PIN 5 // I/O pin switch 2 hilde
|
|
#define s3_PIN 6 // I/O pin switch 3 midden
|
|
#define l1_PIN 7 // I/O pin lamp 1 frank
|
|
#define l2_PIN 8 // I/O pin lamp 2 hilde
|
|
|
|
#define l1_CHILD_ID 11 // Id lamp 1 frank
|
|
#define l2_CHILD_ID 12 // Id lamp 2 hilde
|
|
#define kast_CHILD_ID 13 // ID kast verlichting
|
|
|
|
#define BUZZER_PIN 8 // Output buzzer
|
|
#define BUZZER_CHILD_ID 99 // ID Buzzer
|
|
|
|
//Declare switch
|
|
Bounce s1_debouncer = Bounce();
|
|
int s1_oldValue=HIGH;
|
|
Bounce s2_debouncer = Bounce();
|
|
int s2_oldValue=HIGH;
|
|
Bounce s3_debouncer = Bounce();
|
|
int s3_oldValue=HIGH;
|
|
|
|
int s1_value;
|
|
int s2_value;
|
|
int s3_value;
|
|
|
|
MyMessage l1_msg(l1_CHILD_ID,V_LIGHT);
|
|
MyMessage l2_msg(l2_CHILD_ID,V_LIGHT);
|
|
MyMessage kast_msg(kast_CHILD_ID,V_LIGHT);
|
|
|
|
MyMessage BUZZER_msg(BUZZER_CHILD_ID, V_LIGHT);
|
|
|
|
bool lamp1 = false;
|
|
bool lamp2 = false;
|
|
bool kast = false;
|
|
|
|
unsigned long s1_up;
|
|
unsigned long s1_down;
|
|
unsigned long s1_delta;
|
|
|
|
unsigned long s2_up;
|
|
unsigned long s2_down;
|
|
unsigned long s2_delta;
|
|
|
|
unsigned long s3_up;
|
|
unsigned long s3_down;
|
|
unsigned long s3_delta;
|
|
|
|
unsigned long s_delta;
|
|
|
|
unsigned long s_all = 1000;
|
|
unsigned long s_kast = 3000;
|
|
|
|
bool val_init = false;
|
|
|
|
void setup()
|
|
{
|
|
// gw.begin(incomingMessage, AUTO, true);
|
|
// gw.begin(incomingMessage);
|
|
|
|
// Send the sketch version information to the gateway and Controller
|
|
// gw.sendSketchInfo("Bedverlichting", "1.0");
|
|
|
|
// Setup switch 1
|
|
pinMode(s1_PIN,INPUT);
|
|
// Activate internal pull-up
|
|
digitalWrite(s1_PIN,HIGH);
|
|
// After setting up the button, setup debouncer
|
|
s1_debouncer.attach(s1_PIN);
|
|
s1_debouncer.interval(30);
|
|
|
|
// Setup switch 2
|
|
pinMode(s2_PIN,INPUT);
|
|
// Activate internal pull-up
|
|
digitalWrite(s2_PIN,HIGH);
|
|
// After setting up the button, setup debouncer
|
|
s2_debouncer.attach(s2_PIN);
|
|
s2_debouncer.interval(30);
|
|
|
|
// Setup switch 3
|
|
pinMode(s3_PIN,INPUT);
|
|
// Activate internal pull-up
|
|
digitalWrite(s3_PIN,HIGH);
|
|
// After setting up the button, setup debouncer
|
|
s3_debouncer.attach(s3_PIN);
|
|
s3_debouncer.interval(30);
|
|
|
|
// Setup lamp 1
|
|
pinMode(l1_PIN,OUTPUT);
|
|
digitalWrite(l1_PIN,LOW);
|
|
|
|
// Setup lamp 2
|
|
pinMode(l2_PIN,OUTPUT);
|
|
digitalWrite(l2_PIN,LOW);
|
|
|
|
// Setup the buzzer
|
|
pinMode(BUZZER_PIN,OUTPUT);
|
|
}
|
|
|
|
|
|
void presentation()
|
|
{
|
|
// Send the sketch version information to the gateway and Controller
|
|
sendSketchInfo("Bedverlichting", "2.0");
|
|
|
|
present(l1_CHILD_ID, S_LIGHT);
|
|
present(l2_CHILD_ID, S_LIGHT);
|
|
//present(kast_CHILD_ID, S_LIGHT);
|
|
present(BUZZER_CHILD_ID, S_LIGHT);
|
|
}
|
|
|
|
|
|
void loop()
|
|
{
|
|
unsigned long now = millis();
|
|
//unsigned long currentTime = millis();
|
|
|
|
if(!val_init)
|
|
{
|
|
wait(1000);
|
|
request(l1_CHILD_ID, V_LIGHT);
|
|
wait(1000);
|
|
request(l2_CHILD_ID, V_LIGHT);
|
|
wait(1000);
|
|
// request(kast_CHILD_ID, V_LIGHT);
|
|
// wait(1000);
|
|
val_init = true;
|
|
}
|
|
|
|
//Switch 1
|
|
s1_debouncer.update();
|
|
s1_value = s1_debouncer.read();
|
|
//If switch status changed
|
|
if (s1_value != s1_oldValue)
|
|
{
|
|
if (s1_value == LOW)
|
|
{
|
|
s1_down = millis();
|
|
}
|
|
else
|
|
{
|
|
s1_up = millis();
|
|
s1_delta = s1_up - s1_down;
|
|
switch_up(1);
|
|
}
|
|
s1_oldValue = s1_value;
|
|
}
|
|
|
|
//Switch 2
|
|
s2_debouncer.update();
|
|
s2_value = s2_debouncer.read();
|
|
//If door status changed
|
|
if (s2_value != s2_oldValue)
|
|
{
|
|
if (s2_value == LOW)
|
|
{
|
|
s2_down = millis();
|
|
}
|
|
else
|
|
{
|
|
s2_up = millis();
|
|
s2_delta = s2_up - s2_down;
|
|
switch_up(2);
|
|
}
|
|
s2_oldValue = s2_value;
|
|
}
|
|
|
|
//Switch 1
|
|
s3_debouncer.update();
|
|
s3_value = s3_debouncer.read();
|
|
//If switch status changed
|
|
if (s3_value != s3_oldValue)
|
|
{
|
|
if (s3_value == LOW)
|
|
{
|
|
s3_down = millis();
|
|
}
|
|
else
|
|
{
|
|
s3_up = millis();
|
|
s3_delta = s3_up - s3_down;
|
|
switch_up(3);
|
|
}
|
|
s3_oldValue = s3_value;
|
|
}
|
|
}
|
|
|
|
//Incomming message from Domoticz
|
|
//void incomingMessage(const MyMessage &message)
|
|
void receive(const MyMessage &message)
|
|
{
|
|
//Message of type V_LIGHT ?
|
|
if (message.type==V_LIGHT)
|
|
{
|
|
//Lamp 1
|
|
if (message.sensor==l1_CHILD_ID)
|
|
{
|
|
if(message.getBool()==1)
|
|
{
|
|
Serial.println("Domoticz --> Lamp 1 AAN !!");
|
|
digitalWrite(l1_PIN,HIGH);
|
|
lamp1 = true;
|
|
}
|
|
else
|
|
{
|
|
Serial.println("Domoticz --> Lamp 1 UIT !!");
|
|
digitalWrite(l1_PIN,LOW);
|
|
lamp1 = false;
|
|
}
|
|
}
|
|
|
|
//Lamp 2
|
|
if (message.sensor==l2_CHILD_ID)
|
|
{
|
|
if(message.getBool()==1)
|
|
{
|
|
Serial.println("Domoticz --> Lamp 2 AAN !!");
|
|
digitalWrite(l2_PIN,HIGH);
|
|
lamp2 = true;
|
|
}
|
|
else
|
|
{
|
|
Serial.println("Domoticz --> Lamp 2 UIT !!");
|
|
digitalWrite(l2_PIN,LOW);
|
|
lamp2 = false;
|
|
}
|
|
}
|
|
//Kast verlichting
|
|
if (message.sensor==kast_CHILD_ID)
|
|
{
|
|
if(message.getInt()==1)
|
|
{
|
|
Serial.println("Node Kast --> Kast verlichting AAN !!");
|
|
kast = true;
|
|
}
|
|
else
|
|
{
|
|
Serial.println("Node Kast --> Kast verlichting UIT !!");
|
|
kast = false;
|
|
}
|
|
}
|
|
if(message.sensor == BUZZER_CHILD_ID)
|
|
{
|
|
Serial.println("Received from Domoticz - Buzzer");
|
|
for (int i=0; i<10; i++)
|
|
{
|
|
tone(BUZZER_PIN,1000,200);
|
|
wait(500);
|
|
}
|
|
send(BUZZER_msg.set(0));
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
void switch_up(int switch_id)
|
|
{
|
|
Serial.print("Switch : ");
|
|
Serial.println(switch_id);
|
|
|
|
if (switch_id == 1)
|
|
s_delta = s1_up - s1_down;
|
|
|
|
if (switch_id == 2)
|
|
s_delta = s2_up - s2_down;
|
|
|
|
if (switch_id == 3)
|
|
s_delta = s3_up - s3_down;
|
|
|
|
if (s_delta < s_all)
|
|
{
|
|
//Switch 1
|
|
if(switch_id == 1)
|
|
{
|
|
if(lamp1 == true) lamp(1,false); else lamp(1,true);
|
|
}
|
|
//Switch 2
|
|
if(switch_id == 2)
|
|
{
|
|
if(lamp2 == true) lamp(2,false); else lamp(2,true);
|
|
}
|
|
//Switch 3
|
|
if(switch_id == 3)
|
|
{
|
|
if((lamp1 == true) or (lamp2 == true) or (kast == true))
|
|
{
|
|
lamp(1,false);
|
|
lamp(2,false);
|
|
if(kast==true)
|
|
{
|
|
lamp(3,false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lamp(1,true);
|
|
lamp(2,true);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
else
|
|
{
|
|
if(s_delta < s_kast)
|
|
{
|
|
if((lamp1==true) or (lamp2==true) or (kast==true))
|
|
{
|
|
lamp(1,false);
|
|
lamp(2,false);
|
|
if(kast==true)
|
|
{
|
|
lamp(3,false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lamp(1,true);
|
|
lamp(2,true);
|
|
lamp(3,true);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Serial.println("Kast verlichting");
|
|
if(kast == false)
|
|
{
|
|
// lamp(1,true);
|
|
// lamp(2,true);
|
|
lamp(3,true);
|
|
}
|
|
else
|
|
{
|
|
// lamp(1,false);
|
|
// lamp(2,false);
|
|
lamp(3,false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//Switch lampen
|
|
void lamp(int lamp_id, bool stat)
|
|
{
|
|
|
|
Serial.print("Lamp : ");
|
|
Serial.print(lamp_id);
|
|
Serial.print(" --> ");
|
|
Serial.println(stat==false ? "UIT" : "AAN");
|
|
|
|
//Lamp 1
|
|
if(lamp_id == 1)
|
|
{
|
|
digitalWrite(l1_PIN,stat==HIGH ? 1 : 0);
|
|
lamp1 = stat;
|
|
send(l1_msg.set(stat==HIGH ? 1 : 0));
|
|
wait(20);
|
|
|
|
}
|
|
|
|
//Lamp 2
|
|
if(lamp_id == 2)
|
|
{
|
|
digitalWrite(l2_PIN,stat==HIGH ? 1 : 0);
|
|
lamp2 = stat;
|
|
send(l2_msg.set(stat==HIGH ? 1 : 0));
|
|
wait(20);
|
|
}
|
|
|
|
//Kast verlichting
|
|
if(lamp_id == 3)
|
|
{
|
|
kast = stat;
|
|
if(stat==true)
|
|
{
|
|
send(kast_msg.setDestination(KAST_NODE_ID).set(1));
|
|
}
|
|
else
|
|
{
|
|
send(kast_msg.setDestination(KAST_NODE_ID).set(0));
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|