thanks for the work you did
i add the motion sensor and light level sensor its not working well
you can help me thank you
/*
Relay with toggle switch sketch
modified to work with no uplink
to gateway
Toggle switch connected between pin3 and ground.
*/
#define MY_DEBUG // Enable debug prints to serial monitor
#define MY_RADIO_NRF24 // Enable and select radio type attached
#define MY_TRANSPORT_WAIT_READY_MS 5000 //set how long to wait for transport ready in milliseconds
#include <MySensors.h>
#include <Bounce2.h>
#define CHILD_ID_LEVEL A0
#define RELAY_PIN 5 // Arduino Digital I/O pin number for relay
#define SWITCH_PIN 4 // Arduino Digital I/O pin number for switch
#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!)
#define CHILD_ID 1 // Id of the sensor child
#define CHILD_ID_MOT 2 //motion sensor
#define CHILD_ID_LEVEL 3
#define RELAY_ON 1
#define RELAY_OFF 0
Bounce debouncer = Bounce();
int oldswitchState = 0;
bool state = false;
bool firstStart = true;
int photocellPin = 0; // The photoresistor and resistance 10 / 12KOhms connected on the pin / analog pin A0
int photocellReading;
unsigned long interval= 6000;//dht.getMinimumSamplingPeriod(); // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
MyMessage msg(CHILD_ID, V_STATUS);
MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
MyMessage msgLight(CHILD_ID_LEVEL, V_LIGHT_LEVEL);
void setup(){
pinMode(SWITCH_PIN, INPUT_PULLUP); // Setup the button pin, Activate internal pull-up
debouncer.attach(SWITCH_PIN); // After setting up the button, setup debouncer
debouncer.interval(5);
pinMode(RELAY_PIN, OUTPUT); // set relay pin in output mode
digitalWrite(RELAY_PIN, RELAY_OFF); // Make sure relay is off when starting up
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Relay & Toggle", "1.0");
// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID, S_BINARY);
present(CHILD_ID_LEVEL, S_LIGHT_LEVEL);
present(CHILD_ID_MOT, V_TRIPPED); //me
}
void loop(){
if (firstStart) { // this code is only run once at startup
debouncer.update();
oldswitchState = debouncer.read(); // set oldswitchState to the current toggle switch state
send(msg.set(false), false); // notify controller of current state no ack
firstStart = false; // set firstStart flag false to prevent code from running again
}
debouncer.update();
int switchState = debouncer.read(); // Get the update value
if (switchState != oldswitchState) { // check for new throw of toggle switch
state = !state; // Toggle the state
digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF); // switch the relay to the new state
send(msg.set(state), false); // notify controller of current state no ack
oldswitchState = switchState;
}
// Read digital motion value
boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
// only run loop if time has passed.
unsigned long currentMillis = millis(); // grab current time
// check if "interval" time has passed
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
send(msgMot.set(tripped?"1":"0"));
photocellReading = analogRead(photocellPin)/10; // Conversion en 100% (approximatif)
// Valeur de la photorésistance avec lampe torche devant = ~1000
Serial.print("Luminosité : ");
Serial.println(photocellReading);
send(msgLight.set(photocellReading, 1));
if(tripped == 1 && photocellReading < 30) // Si la luminosité est inférieure à 30%
#ifdef MY_DEBUG
Serial.print("Motion: ");
Serial.println(tripped);
#endif
}
}
/-------------------start of functions--------------------------/
void receive(const MyMessage &message) {
if (message.type == V_STATUS) { // check to see if incoming message is for a switch
state = message.getBool(); // get the new state
digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF); // switch relay to new state
/*---- Write some debug info----*/
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}