Hello all,
I have a sketch you can see below on a arduino nano with an dht sensor and a one channel relay.
The sketch is working and i can see it in pimatic. But when the relay is off in pimatic the red and green LED that
is on the circuit board of the relay are on when i turn on the relay only the red LED is on.
This means that when i plug out the power of the arduino the relay is also closed and that is not what i want.
So i would like to know if there is a way to invert it in pimatic or maybe you guys have an other solution.
#define MY_RADIO_NRF24
#define MY_REPEATER_FEATURE
#define MY_NODE_ID 1
#include <SPI.h>
#include <MySensors.h>
#include <DHT.h>
// Set this to the pin you connected the DHT's data pin to
#define DHT_DATA_PIN 6
// Set this offset if the sensor has a permanent small offset to the real temperatures
#define SENSOR_TEMP_OFFSET 0
// Sleep time between sensor updates (in milliseconds)
// Must be >1000ms for DHT22 and >2000ms for DHT11
static const uint64_t UPDATE_INTERVAL = 60000;
// Force sending an update of the temperature after n sensor reads, so a controller showing the
// timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
// the value didn't change since;
// i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
static const uint8_t FORCE_UPDATE_N_READS = 10;
#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define CHILD_ID_RLAY 2
#define RELAY_PIN 3
#define RELAY_ON 1
#define RELAY_OFF 0
#define TEMP 0
#define HUMI 1
byte state = 0;
bool metric = true;
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgRlay(CHILD_ID_RLAY, V_STATUS);
typedef struct {
float last;
uint8_t nNoUpdates;
} sensorState_t;
sensorState_t dhtState[2];
DHT dht;
void presentation()
{
// Send the sketch version information to the gateway
sendSketchInfo("TempHumidRelay", "1.1");
// 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_RLAY, S_BINARY);
// Ask the gateway for the units of measure to use
metric = getConfig().isMetric;
}
void setup()
{
// Make sure relays are off when starting up
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, RELAY_OFF);
// Initialize the Temperature & Humidity sensor, first reading will be sent from loop()
if (UPDATE_INTERVAL >= dht.getMinimumSamplingPeriod()) {
dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor and model (e.g. DHT_22, DHT_11)
} else {
Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
Serial.println("Increase UPDATE_INTERVAL and re-upload sketch...");
}
// Request the latest relay status from the gateway
Serial.println("Requesting initial value from controller");
request(CHILD_ID_RLAY, V_STATUS);
}
void loop()
{
// Sleep until next valid interal to read temperature and humidity
wait(UPDATE_INTERVAL);
// Read current temperature & humidity from DHT22
dht.readSensor(true);
float temperature = dht.getTemperature();
// Only send temperature if it has changed since the last measurement or if we didn't send an update for n times
if (temperature != dhtState[TEMP].last || dhtState[TEMP].nNoUpdates >= FORCE_UPDATE_N_READS) {
dhtState[TEMP].last = temperature;
temperature = metric ? temperature : dht.toFahrenheit(temperature);
temperature += SENSOR_TEMP_OFFSET;
send(msgTemp.set(temperature, 1));
#ifdef MY_DEBUG
Serial.print("T: ");
Serial.println(temperature);
#endif
dhtState[TEMP].nNoUpdates = 0;
}
else if (isnan(temperature)) {
Serial.println("Failed reading temperature from DHT!");
} else {
// Increase no update counter if the temperature stayed the same
dhtState[TEMP].nNoUpdates++;
}
// Retrieve the current humidity
float humidity = dht.getHumidity();
// Only send humidity if it changed since the last measurement or if we didn't send an update for n times
if (humidity != dhtState[HUMI].last || dhtState[HUMI].nNoUpdates >= FORCE_UPDATE_N_READS) {
dhtState[HUMI].last = humidity;
send(msgHum.set(humidity, 1));
#ifdef MY_DEBUG
Serial.print("H: ");
Serial.println(humidity);
#endif
dhtState[HUMI].nNoUpdates = 0;
} else if (isnan(humidity)) {
Serial.println("Failed reading humidity from DHT");
} else {
// Increase no update counter if the humidity stayed the same
dhtState[HUMI].nNoUpdates++;
}
}
void receive(const MyMessage &message) {
Serial.println("Received message...");
if (message.type == V_STATUS) {
// Change relay state
state = message.getByte();
digitalWrite(RELAY_PIN, state);
send(msgRlay.set(state));
}
}