Sesnsor Door and Relay



  • Hi I combined the sketch of a door sensor to adapt it to a photoresist and added a relay. The relay in question only works once and then freezes everything.

    // Enable debug prints to serial monitor
    #define MY_DEBUG

    #define MY_NODE_ID 2
    #define MY_PARENT_NODE_ID 0
    #define MY_RADIO_NRF24
    #define MY_RF24_CHANNEL 1
    //#define MY_RADIO_RFM69

    #include <SPI.h>
    //#define MY_REPEATER_FEATURE
    #include <MySensors.h>
    #include <Bounce2.h>

    #define RELAY_3 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // 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_2 1
    #define BUTTON_PIN_2 2 // Arduino Digital I/O pin for button/reed switch
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg1(CHILD_ID_2, V_LIGHT_LEVEL);

    void before()
    {
    for (int sensor=1, pin=RELAY_3; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
    // Then set relay pins in output mode
    pinMode(pin, OUTPUT);
    // Set relay to last known state (using eeprom storage)
    digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
    }
    }

    void setup()
    {
    pinMode(BUTTON_PIN_2, INPUT);
    digitalWrite(BUTTON_PIN_2, HIGH);

    }

    void presentation()
    {
    // Send the sketch version information to the gateway and Controller
    sendSketchInfo("Relay", "1.0");

    for (int sensor=1, pin=RELAY_3; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        present(sensor, S_BINARY);
        
        present(CHILD_ID_2, S_LIGHT_LEVEL);
    }
    

    }

    void loop()
    {
    int value = digitalRead(BUTTON_PIN_2);
    send(msg1.set(value == HIGH ? 0 : 1));

    }

    void receive(const MyMessage &message)
    {
    // We only expect one type of message from controller. But we better check anyway.
    if (message.type==V_STATUS) {
    // Change relay state
    digitalWrite(message.sensor-1+RELAY_3, message.getBool()?RELAY_ON:RELAY_OFF);
    // Store state in eeprom
    saveState(message.sensor, message.getBool());
    // Write some debug info

    }
    }

    Can someone help me?


  • Hero Member

    @arduino_89_89 can you give more details of how this node is meant to work. There seems to be some uneeded code in there and I am not sure exactly what you need it to do.



  • Not sure if this will help, but some time back I built a motion sensor setup to control a flood light for my driveway. The node has a lux/LDR sensor for light/dark, relay and a motion sensor. The idea with that node was that the light would turn on if motion was detected and if it was dark enough. You mat be able to go through the code and take out the motion sensor parts and just use the light sensor and relay code.

    Here was my writeup on the project.
    https://forum.mysensors.org/topic/5243/driveway-motion-light


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.