Create Bedlampje.ino

This commit is contained in:
Frank 2021-12-30 13:32:32 +01:00
commit 55ced64e37

250
Bedlampje.ino Normal file
View File

@ -0,0 +1,250 @@
//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 19
// Set channel to 13
//#define MY_RF24_CHANNEL 13
#include <SPI.h>
#include <MySensors.h>
#include <Bounce2.h>
#include <DHT.h>
#define SLEEP_MODE false // Watt-value can only be reported when sleep mode is false.
//DHT22
#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define HUMIDITY_SENSOR_DIGITAL_PIN 8
//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
DHT dht;
float lastTemp;
float lastHum;
boolean metric = true;
unsigned long currentTime;
unsigned long SEND_FREQUENCY = 300000;
//unsigned long SEND_FREQUENCY = 30000;
unsigned long lastSend;
//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 msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
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()
{
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
// 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);
metric = getControllerConfig().isMetric;
// metric = getConfig().isMetric;
lastSend = 0;
}
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Bedlampje Maud", "1.1");
present(CHILD_ID_HUM, S_HUM);
present(CHILD_ID_TEMP, S_TEMP);
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;
}
//Send data only every 5 minuts
bool sendTime = (currentTime - lastSend) > SEND_FREQUENCY;
if ((currentTime - lastSend) > SEND_FREQUENCY)
{
lastSend=currentTime;
float temperature = dht.getTemperature();
if (isnan(temperature))
{
Serial.println("Failed reading temperature from DHT");
}
else if (temperature != lastTemp)
{
lastTemp = temperature;
if (!metric)
{
temperature = dht.toFahrenheit(temperature);
}
send(msgTemp.set(temperature, 1));
Serial.print("T: ");
Serial.println(temperature);
}
float humidity = dht.getHumidity();
if (isnan(humidity))
{
Serial.println("Failed reading humidity from DHT");
}
else if (humidity != lastHum)
{
lastHum = humidity;
send(msgHum.set(humidity, 1));
Serial.print("H: ");
Serial.println(humidity);
}
}
//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;
}
}
}
}