Hi everyone,
I'm trying to combine 3 relays, PIR, gas and distance sensors together for my kitchen but i have no luck already spend 3 days on but nothing works.
Can someone help me please? Gas sensor is not necessary
Using: mysensors 1.4
Controller: Domoticz
Everything separately working fine but not together.
Here is my code with 3 relays, pir and distance sensors
#include <SPI.h>
#include <MySensor.h>
#include <NewPing.h>
#define CHILD_ID 8
#define TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)
#define MOTOIN_INPUT_SENSOR 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!)
#define RELAY_1 4 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 3 // Total number of attached relays
#define RELAY_ON 0 // GPIO value to write to turn on attached relay
#define RELAY_OFF 1 // GPIO value to write to turn off attached relay
#define MOTION_CHILD_ID 5 // Id of the sensor child
#define RELAY_CHILD_ID 1 // Id of the sensor child
boolean lastMotion = false;
MySensor gw;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
MyMessage msgdist(MOTION_CHILD_ID, V_TRIPPED);
MyMessage msg(CHILD_ID, V_DISTANCE);
int lastDist;
boolean metric = true;
void setup()
{
gw.begin(incomingMessage, AUTO, true);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Zet", "1.0");
// Register all sensors to gw (they will be created as child devices)
gw.present(CHILD_ID, S_DISTANCE);
boolean metric = gw.getConfig().isMetric;
pinMode(MOTOIN_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input
// Register all sensors to gw (they will be created as child devices)
gw.present(MOTION_CHILD_ID, S_MOTION);
// Fetch relay status
for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
gw.present(sensor, S_LIGHT);
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
}
}
void loop()
{
int dist = metric?sonar.ping_cm():sonar.ping_in();
Serial.print("Ping: ");
Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println(metric?" cm":" in");
if (dist != lastDist) {
gw.send(msg.set(dist));
lastDist = dist;
// Alway process incoming messages whenever possible
gw.process();
// Read digital motion value
boolean motion = digitalRead(MOTOIN_INPUT_SENSOR) == HIGH;
if (lastMotion != motion) {
lastMotion = motion;
gw.send(msg.set(motion ? "1" : "0" )); // Send motion value to gw
}
gw.sleep(SLEEP_TIME);
}
}
void incomingMessage(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_LIGHT) {
// Change relay state
digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
// Store state in eeprom
gw.saveState(message.sensor, message.getBool());
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}
LOG from serial monitor:
repeater started, id 1
send: 1-1-0-0 s=255,c=0,t=18,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
read: 0-0-1 s=255,c=3,t=6,pt=0,l=1:M
send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=3,st=ok:Zet
send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
send: 1-1-0-0 s=8,c=0,t=15,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=5,c=0,t=1,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=1,c=0,t=3,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=2,c=0,t=3,pt=0,l=5,st=ok:1.4.1
send: 1-1-0-0 s=3,c=0,t=3,pt=0,l=5,st=ok:1.4.1
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Ping: 0 cm
Any help?