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. Development
  3. Relay and Light Node

Relay and Light Node

Scheduled Pinned Locked Moved Development
3 Posts 2 Posters 1.9k Views 2 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.
  • Marek BělušaM Offline
    Marek BělušaM Offline
    Marek Běluša
    wrote on last edited by Marek Běluša
    #1

    Hi, i try to combine Relay and Light node sketch. It works ok but i need put interupt or delay for light sensor reading only 1 read per minute. If i put sleep time to loop then it block comunication for realy and node repeater :-(

    I found some examples only for 1.5 v nodes .........

    Thanks for help!

    My sketch:

    /**

    • The MySensors Arduino library handles the wireless radio link and protocol
    • between your home built sensors/actuators and HA controller of choice.
    • The sensors forms a self healing radio network with optional repeaters. Each
    • repeater and gateway builds a routing tables in EEPROM which keeps track of the
    • network topology allowing messages to be routed to nodes.
    • Created by Henrik Ekblad henrik.ekblad@mysensors.org
    • Copyright (C) 2013-2015 Sensnology AB
    • Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
    • Documentation: http://www.mysensors.org
    • Support Forum: http://forum.mysensors.org
    • This program is free software; you can redistribute it and/or
    • modify it under the terms of the GNU General Public License
    • version 2 as published by the Free Software Foundation.

    • REVISION HISTORY
    • Version 1.0 - idefix
    • DESCRIPTION
    • Arduino BH1750FVI Light sensor
    • communicate using I2C Protocol
    • this library enable 2 slave device addresses
    • Main address 0x23
    • secondary address 0x5C
    • connect the sensor as follows :
    • VCC >>> 5V
    • Gnd >>> Gnd
    • ADDR >>> NC or GND
    • SCL >>> A5
    • SDA >>> A4
    • http://www.mysensors.org/build/light
      */

    #define MY_NODE_ID 9

    // Enable debug prints to serial monitor
    #define MY_DEBUG

    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69

    #define MY_RF24_PA_LEVEL RF24_PA_MAX

    #include <SPI.h>
    #include <MySensors.h>
    #include <BH1750.h>
    #include <Wire.h>

    #define CHILD_ID_LIGHT 10
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)

    #define RELAY_1 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 0 // GPIO value to write to turn on attached relay
    #define RELAY_OFF 1 // GPIO value to write to turn off attached relay

    BH1750 lightSensor;

    // transmitting LUX light level.
    MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    // MyMessage msg(CHILD_ID_LIGHT, V_LEVEL);
    uint16_t lastlux;

    void before() {
    for (int sensor=1, pin=RELAY_1; 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()
    {
    lightSensor.begin();
    for (int sensor=1, pin=RELAY_1; 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 presentation() {
    // Send the sketch version information to the gateway and Controller
    sendSketchInfo("Relays and Light Lux Sensor node", "1.0");

    // Register all sensors to gateway (they will be created as child devices)
    present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);

    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)
    present(sensor, S_LIGHT);
    }
    }

    void loop()
    {
    uint16_t lux = lightSensor.readLightLevel();// Get Lux value
    Serial.println(lux);
    if (lux != lastlux) {
    send(msg.set(lux));
    lastlux = lux;

    }
    }
    void receive(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
    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());

    }
    }

    1 Reply Last reply
    0
    • mrwombleM Offline
      mrwombleM Offline
      mrwomble
      wrote on last edited by
      #2

      Hi Marek,
      What I've done in this situation is to introduce a time check instead of sleeping the node, since you need to keep it awake to listen for relay commands. I got my inspiration from Hek's RGB dimmer sketch.

      My code was for a gas sensor:

      unsigned long currentTime  = millis();
      if (currentTime > lastTime + READ_DELAY_TIME) {
          lastTime = currentTime;
          readgassensor();
        }
      

      READ_DELAY_TIME is set to 30000 so that it waits 30s between reads (in your case, increase that to get 1min intervals).

      This is not a super-precise way of doing it, but it's simple enough for regular sensor reads and seems to work for me.

      Hope that helps!

      1 Reply Last reply
      0
      • Marek BělušaM Offline
        Marek BělušaM Offline
        Marek Běluša
        wrote on last edited by
        #3

        Hi!

        Thanks for help, now it works perfect :-)

        Posting updated code:

        /**

        • The MySensors Arduino library handles the wireless radio link and protocol
        • between your home built sensors/actuators and HA controller of choice.
        • The sensors forms a self healing radio network with optional repeaters. Each
        • repeater and gateway builds a routing tables in EEPROM which keeps track of the
        • network topology allowing messages to be routed to nodes.
        • Created by Henrik Ekblad henrik.ekblad@mysensors.org
        • Copyright (C) 2013-2015 Sensnology AB
        • Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
        • Documentation: http://www.mysensors.org
        • Support Forum: http://forum.mysensors.org
        • This program is free software; you can redistribute it and/or
        • modify it under the terms of the GNU General Public License
        • version 2 as published by the Free Software Foundation.

        • REVISION HISTORY
        • Version 1.0 - idefix
        • DESCRIPTION
        • Arduino BH1750FVI Light sensor
        • communicate using I2C Protocol
        • this library enable 2 slave device addresses
        • Main address 0x23
        • secondary address 0x5C
        • connect the sensor as follows :
        • VCC >>> 5V
        • Gnd >>> Gnd
        • ADDR >>> NC or GND
        • SCL >>> A5
        • SDA >>> A4
        • http://www.mysensors.org/build/light
          */

        #define MY_NODE_ID 9

        // Enable debug prints to serial monitor
        #define MY_DEBUG

        // Enable and select radio type attached
        #define MY_RADIO_NRF24
        //#define MY_RADIO_RFM69

        #define MY_RF24_PA_LEVEL RF24_PA_MAX

        #include <SPI.h>
        #include <MySensors.h>
        #include <BH1750.h>
        #include <Wire.h>

        #define CHILD_ID_LIGHT 10

        unsigned long lastTime = millis();
        #define RELAY_1 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 0 // GPIO value to write to turn on attached relay
        #define RELAY_OFF 1 // GPIO value to write to turn off attached relay

        BH1750 lightSensor;

        // transmitting LUX light level.
        MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
        // MyMessage msg(CHILD_ID_LIGHT, V_LEVEL);
        uint16_t lastlux;

        void before() {
        for (int sensor=1, pin=RELAY_1; 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()

        {
        lightSensor.begin();
        for (int sensor=1, pin=RELAY_1; 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 presentation() {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Relays and Light Lux Sensor node", "1.0");

        // Register all sensors to gateway (they will be created as child devices)
        present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);

        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)
        present(sensor, S_LIGHT);
        }
        }

        void loop()
        {
        unsigned long currentTime = millis();
        if (currentTime > lastTime + 60000) {
        lastTime = currentTime;

        uint16_t lux = lightSensor.readLightLevel();
        Serial.println(lux);
        if (lux != lastlux)
        send(msg.set(lux));
        lastlux = lux; }

        }

        void receive(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
        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());

        }
        }

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


        10

        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