Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. Sesnsor Door and Relay

Sesnsor Door and Relay

Scheduled Pinned Locked Moved Troubleshooting
3 Posts 3 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arduino_89_89
    wrote on last edited by
    #1

    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?

    Boots33B 1 Reply Last reply
    0
    • A arduino_89_89

      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?

      Boots33B Offline
      Boots33B Offline
      Boots33
      Hero Member
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      1
      • dbemowskD Offline
        dbemowskD Offline
        dbemowsk
        wrote on last edited by
        #3

        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

        Vera Plus running UI7 with MySensors, Sonoffs and 1-Wire devices
        Visit my website for more Bits, Bytes and Ramblings from me: http://dan.bemowski.info/

        1 Reply Last reply
        1
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        13

        Online

        11.7k

        Users

        11.2k

        Topics

        113.1k

        Posts


        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • MySensors
        • OpenHardware.io
        • Categories
        • Recent
        • Tags
        • Popular