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. Controllers
  3. Vera
  4. Multisensor with Relay control

Multisensor with Relay control

Scheduled Pinned Locked Moved Vera
2 Posts 2 Posters 2.5k 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.
  • A Offline
    A Offline
    antcas2000
    wrote on last edited by
    #1

    I had build a multi sensor with temp,humidity, Door sensor and Relay for my garage door. I am using a Nano board. Connecting the Vera creates all sensors, but when i am using the button swich to swich the Relay, from the serial terminal i can see that this command is not received from the sensors node. I try several things without any success. Please check my code:
    Thanks in Advance.

    #include <SPI.h>
    #include <MySensor.h>
    #include <NewPing.h>
    #include <Bounce2.h>
    #include <DHT.h>

    //Relay

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

    //define temp/humidity sensor
    #define CHILD_ID_HUM 2
    #define CHILD_ID_TEMP 3
    #define HUMIDITY_SENSOR_DIGITAL_PIN 4

    //define button/magnet sensor
    #define CHILD_ID_BUT 4
    #define BUTTON_PIN 7 // Arduino Digital I/O pin for button/reed switch

    //define Distace Sensor
    #define CHILD_ID_DIS 5
    #define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
    unsigned long SLEEP_TIME = 1000; // Sleep time between reads (in milliseconds)

    MySensor gw;

    //Distance sensor initiliazation
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    int lastDist;
    boolean metric = true;

    //Temp sensor initiliazation
    DHT dht;
    float lastTemp;
    float lastHum;

    //Magnet sensor initiliazation
    Bounce debouncer = Bounce();
    int oldValue=0;
    bool state;

    //Distace Sensor
    MyMessage msgDis(CHILD_ID_DIS, V_DISTANCE);

    //Temp message
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);

    //Magnet door sensor message
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msgBut(CHILD_ID_BUT,V_TRIPPED);

    void setup()
    {
    gw.begin(incomingMessage, AUTO, false);

    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("Distance & Temp Sensor & Rele & Magnet,Relay", "1.0");
    //gw.begin(incomingMessage, AUTO, true);

    // Setup the button
    pinMode(BUTTON_PIN,INPUT);
    // Activate internal pull-up
    digitalWrite(BUTTON_PIN,HIGH);

    // After setting up the button, setup debouncer
    debouncer.attach(BUTTON_PIN);
    debouncer.interval(5);

    //setup dht sensor
    dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);

    // Fetch relay status
    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)
    gw.present(sensor, S_LIGHT);
    // Then set relay pins in output mode
    pinMode(pin, OUTPUT);
    // Set relay to last known state (using eeprom storage)
    digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
    }

    // Register all sensors to gw (they will be created as child devices)
    gw.present(CHILD_ID_BUT, S_DOOR);
    gw.present(CHILD_ID_DIS, S_DISTANCE);
    gw.present(CHILD_ID_HUM, S_HUM);
    gw.present(CHILD_ID_TEMP, S_TEMP);
    //gw.present(CHILD_ID_REL, S_LIGHT);

    }

    void loop()
    {
    // Alway process incoming messages whenever possible = for Relay

    gw.process();

    debouncer.update();

    // Distance process

    int dist = metric?sonar.ping_cm():sonar.ping_in();
    Serial.print(F("Ping: "));
    Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
    Serial.println(metric?" cm":" in");

    if (dist != lastDist) {
    gw.send(msgDis.set(dist));
    lastDist = dist;
    }

    //Temp
    delay(dht.getMinimumSamplingPeriod());
    float temperature = dht.getTemperature();
    if (isnan(temperature)) {
    Serial.println(F("Failed reading temperature from DHT"));
    } else if (temperature != lastTemp) {
    lastTemp = temperature;
    if (!metric) {
    temperature = dht.toCelsius(temperature);
    }
    gw.send(msgTemp.set(temperature, 1));
    Serial.print("T: ");
    Serial.println(temperature);
    }

    float humidity = dht.getHumidity();
    if (isnan(humidity)) {
    Serial.println(F("Failed reading humidity from DHT"));
    } else if (humidity != lastHum) {
    lastHum = humidity;
    gw.send(msgHum.set(humidity, 1));
    Serial.print("H: ");
    Serial.println(humidity);
    }

    //Button - Magnet update

    debouncer.update();
    // Get the update value
    int value = debouncer.read();

    if (value != oldValue) {
    // Send in the new value
    gw.send(msgBut.set(value==HIGH ? 1 : 0));
    oldValue = value;
    }

    //gw.sleep(SLEEP_TIME);
    }

    void incomingMessage(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
    gw.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());
    }
    }

    H 1 Reply Last reply
    0
    • A antcas2000

      I had build a multi sensor with temp,humidity, Door sensor and Relay for my garage door. I am using a Nano board. Connecting the Vera creates all sensors, but when i am using the button swich to swich the Relay, from the serial terminal i can see that this command is not received from the sensors node. I try several things without any success. Please check my code:
      Thanks in Advance.

      #include <SPI.h>
      #include <MySensor.h>
      #include <NewPing.h>
      #include <Bounce2.h>
      #include <DHT.h>

      //Relay

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

      //define temp/humidity sensor
      #define CHILD_ID_HUM 2
      #define CHILD_ID_TEMP 3
      #define HUMIDITY_SENSOR_DIGITAL_PIN 4

      //define button/magnet sensor
      #define CHILD_ID_BUT 4
      #define BUTTON_PIN 7 // Arduino Digital I/O pin for button/reed switch

      //define Distace Sensor
      #define CHILD_ID_DIS 5
      #define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor.
      #define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor.
      #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
      unsigned long SLEEP_TIME = 1000; // Sleep time between reads (in milliseconds)

      MySensor gw;

      //Distance sensor initiliazation
      NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
      int lastDist;
      boolean metric = true;

      //Temp sensor initiliazation
      DHT dht;
      float lastTemp;
      float lastHum;

      //Magnet sensor initiliazation
      Bounce debouncer = Bounce();
      int oldValue=0;
      bool state;

      //Distace Sensor
      MyMessage msgDis(CHILD_ID_DIS, V_DISTANCE);

      //Temp message
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);

      //Magnet door sensor message
      // Change to V_LIGHT if you use S_LIGHT in presentation below
      MyMessage msgBut(CHILD_ID_BUT,V_TRIPPED);

      void setup()
      {
      gw.begin(incomingMessage, AUTO, false);

      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Distance & Temp Sensor & Rele & Magnet,Relay", "1.0");
      //gw.begin(incomingMessage, AUTO, true);

      // Setup the button
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);

      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);

      //setup dht sensor
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);

      // Fetch relay status
      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)
      gw.present(sensor, S_LIGHT);
      // Then set relay pins in output mode
      pinMode(pin, OUTPUT);
      // Set relay to last known state (using eeprom storage)
      digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
      }

      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID_BUT, S_DOOR);
      gw.present(CHILD_ID_DIS, S_DISTANCE);
      gw.present(CHILD_ID_HUM, S_HUM);
      gw.present(CHILD_ID_TEMP, S_TEMP);
      //gw.present(CHILD_ID_REL, S_LIGHT);

      }

      void loop()
      {
      // Alway process incoming messages whenever possible = for Relay

      gw.process();

      debouncer.update();

      // Distance process

      int dist = metric?sonar.ping_cm():sonar.ping_in();
      Serial.print(F("Ping: "));
      Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
      Serial.println(metric?" cm":" in");

      if (dist != lastDist) {
      gw.send(msgDis.set(dist));
      lastDist = dist;
      }

      //Temp
      delay(dht.getMinimumSamplingPeriod());
      float temperature = dht.getTemperature();
      if (isnan(temperature)) {
      Serial.println(F("Failed reading temperature from DHT"));
      } else if (temperature != lastTemp) {
      lastTemp = temperature;
      if (!metric) {
      temperature = dht.toCelsius(temperature);
      }
      gw.send(msgTemp.set(temperature, 1));
      Serial.print("T: ");
      Serial.println(temperature);
      }

      float humidity = dht.getHumidity();
      if (isnan(humidity)) {
      Serial.println(F("Failed reading humidity from DHT"));
      } else if (humidity != lastHum) {
      lastHum = humidity;
      gw.send(msgHum.set(humidity, 1));
      Serial.print("H: ");
      Serial.println(humidity);
      }

      //Button - Magnet update

      debouncer.update();
      // Get the update value
      int value = debouncer.read();

      if (value != oldValue) {
      // Send in the new value
      gw.send(msgBut.set(value==HIGH ? 1 : 0));
      oldValue = value;
      }

      //gw.sleep(SLEEP_TIME);
      }

      void incomingMessage(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
      gw.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());
      }
      }

      H Offline
      H Offline
      hek
      Admin
      wrote on last edited by
      #2

      @antcas2000

      http://forum.mysensors.org/topic/666/how-to-ask-for-help

      1 Reply Last reply
      0

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      31

      Online

      12.1k

      Users

      11.2k

      Topics

      113.4k

      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