//Badkamer // 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 #include #include #include #define SLEEP_MODE false // Watt-value can only be reported when sleep mode is false. #define CHILD_ID_HUM 0 #define CHILD_ID_TEMP 1 #define HUMIDITY_SENSOR_DIGITAL_PIN 3 #define RELAY_PIN 4 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define CHILD_ID_RELAY10 2 // Total number of attached relays #define CHILD_ID_RELAY30 3 // Total number of attached relays #define CHILD_ID_ENABLED 4 // Total number of attached relays #define RELAY_ON 1 // GPIO value to write to turn on attached relay #define RELAY_OFF 0 // GPIO value to write to turn off attached relay #define CHILD_ID_SETPOINT 10 //Setpoint //#define HUM_VENTILATION_ON 50 //#define TIME_ON10 60000 //#define TIME_ON30 180000 //#define CHECK_FREQ_30s 3000 //#define CHECK_FREQ_5m 30000 //#define HUM_VENTILATION_ON 70 #define HUM_VENTILATION_ONMAX 95 #define TIME_ON10 600000 #define TIME_ON30 1800000 #define CHECK_FREQ_30s 30000 #define CHECK_FREQ_5m 300000 DHT dht; float lastTemp; float lastHum; boolean metric = true; unsigned long currentTime; unsigned long SEND_FREQUENCY = 300000; unsigned long CHECK_FREQUENCY = 30000; unsigned long lastSend; unsigned long lastCheck; unsigned long lastOn10; unsigned long lastOn30; boolean On10 = false; boolean On30 = false; boolean Check_Enabled = true; int COUNT_ON = 0; int HUM_VENTILATION_ON=70; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgRelay10(CHILD_ID_RELAY10, V_LIGHT); MyMessage msgRelay30(CHILD_ID_RELAY30, V_LIGHT); MyMessage msgEnabled(CHILD_ID_ENABLED, V_LIGHT); MyMessage msgSetpoint(CHILD_ID_SETPOINT, V_DIMMER); //Setup routine void setup() { // Initialize library and add callback for incoming messages //gw.begin(incomingMessage); dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); pinMode(RELAY_PIN, OUTPUT); digitalWrite(RELAY_PIN, RELAY_OFF); send(msgRelay10.set(RELAY_OFF)); send(msgRelay30.set(RELAY_OFF)); send(msgEnabled.set(RELAY_ON)); metric = getControllerConfig().isMetric; // metric = getConfig().isMetric; lastSend = 0; lastCheck = 0; //Request Setpoint //gw.request(CHILD_ID_SETPOINT, V_DIMMER); send(msgSetpoint.set(HUM_VENTILATION_ON)); } void presentation() { // Send the Sketch Version Information to the Gateway sendSketchInfo("Badkamer", "1.7"); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_HUM, S_HUM); present(CHILD_ID_TEMP, S_TEMP); present(CHILD_ID_RELAY10, S_LIGHT); present(CHILD_ID_RELAY30, S_LIGHT); present(CHILD_ID_ENABLED, S_LIGHT); present(CHILD_ID_SETPOINT, S_DIMMER); } //Main loop void loop() { // gw.process(); currentTime = millis(); //If milis goes to 0 if(lastCheck > currentTime) lastCheck = currentTime; if(lastSend > currentTime) lastSend = currentTime; if(lastOn30 > currentTime) lastOn30 = currentTime; if(lastOn10 > currentTime) lastOn10 = currentTime; //wait(dht.getMinimumSamplingPeriod()); //Set switch on Domoticz to off after 10 min if(On10 && (currentTime > (lastOn10 + TIME_ON10))) { Serial.println("Ventilation Off --> Domoticz"); send(msgRelay10.set(RELAY_OFF)); On10 = false; } //Set switch on Domoticz to off after 30 min if(On30 && (currentTime > (lastOn30 + TIME_ON30))) { Serial.println("Ventilation Off --> Domoticz"); send(msgRelay30.set(RELAY_OFF)); On30 = false; } //Check every 30 sec the HUMIDITY if it is above 70% then switch ventilation on and change check frequence to 5min. if(((currentTime - lastCheck) > CHECK_FREQUENCY)) { lastCheck=currentTime; float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } //Check if HUM > 70 and check enabled or HUM > 95 else if ((humidity > HUM_VENTILATION_ON && Check_Enabled) || humidity > HUM_VENTILATION_ONMAX) { if (COUNT_ON < 3) { send(msgHum.set(humidity, 1)); Serial.println("Humidity > "); Serial.print(HUM_VENTILATION_ON); Serial.println("% --> Ventilation on for 10 minuts"); send(msgRelay10.set(RELAY_ON)); lastOn10=currentTime; On10=true; digitalWrite(RELAY_PIN, RELAY_ON); wait(1000); digitalWrite(RELAY_PIN, RELAY_OFF); Serial.println("Humidity > "); Serial.print(HUM_VENTILATION_ON); Serial.println("% --> Check every 5min"); COUNT_ON = COUNT_ON + 1; } else { Serial.println("Humidity > "); Serial.print(HUM_VENTILATION_ON); Serial.println("% --> Ventilation already 3 times on !"); } CHECK_FREQUENCY = CHECK_FREQ_5m; lastHum = humidity; } else if (humidity <= HUM_VENTILATION_ON) { if (lastHum > HUM_VENTILATION_ON) { send(msgHum.set(humidity, 1)); lastHum = humidity; } Serial.print("Humidity < "); Serial.print(HUM_VENTILATION_ON); Serial.println("% --> Check every 30sec"); CHECK_FREQUENCY = CHECK_FREQ_30s; COUNT_ON = 0; } } //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); } } if(lastCheck > currentTime) lastCheck = currentTime; if(lastSend > currentTime) lastSend = currentTime; } // Check incomming messages void receive(const MyMessage &message) { //Message of type V_LIGHT ? if (message.type==V_LIGHT) { //Button 10min. if (message.sensor==CHILD_ID_RELAY10 && message.getBool()==1) { Serial.println("Domoticz --> Ventilation on for 10 minuts"); digitalWrite(RELAY_PIN, RELAY_ON); On10=true; lastOn10=currentTime; wait(1000); digitalWrite(RELAY_PIN, RELAY_OFF); } //Button 30min. if (message.sensor==CHILD_ID_RELAY30 && message.getBool()==1) { Serial.println("Domoticz --> Ventilation on for 30 minuts"); digitalWrite(RELAY_PIN, RELAY_ON); On30=true; lastOn30=currentTime; wait(3000); digitalWrite(RELAY_PIN, RELAY_OFF); } if (message.sensor==CHILD_ID_ENABLED) { if(message.getBool()==1) { Check_Enabled = true; Serial.println("Domoticz --> Ventilation Enabled, checking humidity !!"); } else { Check_Enabled = false; Serial.println("Domoticz --> Ventilation Disabled, outside humidity also high !?"); } } } //Message of type V_DIMMER ? if (message.type==V_DIMMER) { if (message.sensor==CHILD_ID_SETPOINT) { HUM_VENTILATION_ON=message.getInt(); Serial.print("Domoticz --> Setpoint received : "); Serial.print(HUM_VENTILATION_ON); Serial.println(" %"); } } }