@BartE Adding the extra code done the job, now its working great. Thank you for your help!
The final working code if someone wonders is:
#include <SPI.h>
#include <MySensor.h>
#include <DHT.h>
#include <Bounce2.h>
#include <MySigningNone.h>
#include <MyTransportNRF24.h>
#include <MyTransportRFM69.h>
#include <MyHwATMega328.h>
#define NODE_ID 1
#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define CHILD_ID_RELAY 2
#define RELAY_PIN 3
#define BUTTON_PIN 4
#define HUMIDITY_SENSOR_DIGITAL_PIN 2
//unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
#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
MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);
MyHwATMega328 hw;
MySensor gw(radio, hw);
Bounce debouncer = Bounce();
int oldValue=0;
bool state;
MyMessage msg(CHILD_ID_RELAY,V_LIGHT);
DHT dht;
float lastTemp;
float lastHum;
boolean metric = true;
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
void setup()
{
gw.begin(incomingMessage, AUTO, true);
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
gw.sendSketchInfo("Combo", "1.0");
gw.present(CHILD_ID_HUM, S_HUM);
gw.present(CHILD_ID_TEMP, S_TEMP);
pinMode(BUTTON_PIN,INPUT);
digitalWrite(BUTTON_PIN,HIGH);
debouncer.attach(BUTTON_PIN);
debouncer.interval(5);
gw.present(CHILD_ID_RELAY, S_LIGHT);
metric = gw.getConfig().isMetric;
digitalWrite(RELAY_PIN, RELAY_OFF);
pinMode(RELAY_PIN, OUTPUT);
state = gw.loadState(CHILD_ID_RELAY);
digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
}
unsigned long lastCheckTimestamp = 0;
// setup stuff
void incomingMessage(const MyMessage &message)
{
if (message.type == V_LIGHT)
{
digitalWrite(RELAY_PIN, message.getBool() ? RELAY_ON : RELAY_OFF);
gw.saveState(1, message.getBool());
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}
void loop()
{
gw.process();
if (debouncer.update() && debouncer.read() == 0) {
gw.send(msg.set(gw.loadState(1)?false:true), true); // Send new state and request ack back
}
// Check if the minimal sample period has been passed
if ((lastCheckTimestamp < (millis() - dht.getMinimumSamplingPeriod()) ) || lastCheckTimestamp > millis())
{
lastCheckTimestamp = millis();
// do the normal stuff after the delay
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);
}
gw.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;
gw.send(msgHum.set(humidity, 1));
Serial.print("H: ");
Serial.println(humidity);
}
}
}