Relay and Light Node



  • 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());

    }
    }



  • 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!



  • 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());

    }
    }


Log in to reply
 

Suggested Topics

  • 1
  • 5
  • 3
  • 2
  • 2
  • 1

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts