From fa381edb38f2419000673c20556558286d73bfa7 Mon Sep 17 00:00:00 2001 From: Frank Date: Thu, 30 Dec 2021 13:29:50 +0100 Subject: [PATCH] Create Bedlampje_Zolder.ino --- Bedlampje_Zolder.ino | 189 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 Bedlampje_Zolder.ino diff --git a/Bedlampje_Zolder.ino b/Bedlampje_Zolder.ino new file mode 100644 index 0000000..8bb531e --- /dev/null +++ b/Bedlampje_Zolder.ino @@ -0,0 +1,189 @@ +//Bedlampje Maud + +// 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 18 + +#define MY_RF24_CE_PIN 10 +#define MY_RF24_CS_PIN 9 + +// Set channel to 13 +//#define MY_RF24_CHANNEL 13 + +#include +#include +#include + +#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 + +//Uitgangen +#define l1_PIN 6 // I/O pin lamp Bedlampje + +#define l1_CHILD_ID 11 // Id Bedlampje +#define l1_timed_CHILD_ID 19 // ID timed Bedlampje + +unsigned long currentTime; + +//unsigned long l1_timed_ON = 120000; +unsigned long l1_timed_ON = 2700000; //45 minuten ON +//unsigned long l1_timed_ON = 180000; //3 minuten ON +unsigned long l1_start_ON; + +//Declare switch +Bounce s1_debouncer = Bounce(); +int s1_oldValue=HIGH; +int s1_value; + +MyMessage l1_msg(l1_CHILD_ID,V_LIGHT); +MyMessage timed_l1_msg(l1_timed_CHILD_ID, V_LIGHT); +//MyMessage bed_msg(kast_CHILD_ID,V_LIGHT); + +bool lamp1 = false; +bool l1_timed = true; + +unsigned long s_delta; + +bool val_init = false; + +void setup() +{ + + // 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 lamp 1 + pinMode(l1_PIN,OUTPUT); + digitalWrite(l1_PIN,LOW); +} + +void presentation() +{ + // Send the sketch version information to the gateway and Controller + sendSketchInfo("Bedlampje Zolder", "1.0"); + + present(l1_CHILD_ID, S_LIGHT); + present(l1_timed_CHILD_ID, S_LIGHT); +} + + +void loop() +{ + unsigned long now = millis(); + //unsigned long currentTime = millis(); + + currentTime = millis(); + + if(!val_init) + { + wait(1000); + request(l1_CHILD_ID, V_LIGHT); + wait(1000); + request(l1_timed_CHILD_ID, V_LIGHT); + val_init = true; + } + + //Switch 1 + s1_debouncer.update(); + s1_value = s1_debouncer.read(); + //If switch status changed + if (s1_value != s1_oldValue) + { + if (lamp1==false) + { + //Switch lamp on + digitalWrite(l1_PIN,1); + lamp1 = true; + l1_start_ON = millis(); + send(l1_msg.set(1)); + Serial.println("Switch --> Bedlampje AAN !!"); + } + else + { + //Switch lamp off + digitalWrite(l1_PIN,0); + lamp1 = false; + send(l1_msg.set(0)); + Serial.println("Switch --> Bedlampje UIT !!"); + } + s1_oldValue = s1_value; + } + + //When clock has made a complete round ;-) + if (l1_start_ON > currentTime) + { + l1_start_ON = currentTime; + } + + //If lamp1 = timed + if ((lamp1==true) && (l1_timed==true)) + { + if ((currentTime - l1_start_ON) > l1_timed_ON) + { + //Switch lamp off + digitalWrite(l1_PIN,0); + lamp1 = false; + send(l1_msg.set(0)); + Serial.println("Timer --> Bedlampje UIT Timer !!"); + } + } + } + +//Incomming message from Domoticz +//void incomingMessage(const MyMessage &message) +void receive(const MyMessage &message) +{ + //Message of type V_LIGHT ? + if (message.type==V_LIGHT) + { + //Bedlampje + if (message.sensor==l1_CHILD_ID) + { + + if(message.getBool()==1) + { + Serial.println("Domoticz --> Bedlampje AAN !!"); + digitalWrite(l1_PIN,HIGH); + lamp1 = true; + l1_start_ON = millis(); + } + else + { + Serial.println("Domoticz --> Bedlampje UIT !!"); + digitalWrite(l1_PIN,LOW); + lamp1 = false; + } + } + + if (message.sensor==l1_timed_CHILD_ID) + { + if(message.getBool()==1) + { + Serial.println("Domoticz --> Timer enabled !!"); + l1_timed = true; + } + else + { + Serial.println("Domoticz --> Timer disabled !!"); + l1_timed = false; + } + } + } +}