Multisensor with Relay control



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


  • Admin


Log in to reply
 

Suggested Topics

  • 2
  • 1
  • 10
  • 1
  • 3

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts