Hum temp rain sensor help



  • Hi,

    i merged 2 sketches and hoped that it would work. but not. the node sends only the temp when there is a change at the rain-sensor. and i want the temp/hum be send each couple of minutes. can anyone help? here is my sketch;.

    [code]
    #include <SPI.h>
    #include <MySensor.h>  
    #include <DHT.h>

    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define CHILD_ID_RAIN 3

    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
    #define DIGITAL_INPUT_SOIL_SENSOR_PIN 4
    #define INTERRUPT DIGITAL_INPUT_SOIL_SENSOR_PIN-2 // Usually the interrupt = pin -2 (on uno/nano anyway)

    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)

    MySensor gw;
    DHT dht;
    float lastTemp;
    float lastHum;
    boolean metric = true;
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msg(CHILD_ID_RAIN, V_TRIPPED);
    int lastSoilValue = -1;

    void setup()

      gw.begin();
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);

    // Send the Sketch Version Information to the Gateway
      gw.sendSketchInfo("Floris Multi Rain+Hum", "1.0");

    // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID_HUM, S_HUM);
      gw.present(CHILD_ID_TEMP, S_TEMP);

    pinMode(DIGITAL_INPUT_SOIL_SENSOR_PIN, INPUT); // sets the motion sensor digital pin as input
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID_RAIN, S_MOTION);
      
     metric = gw.getConfig().isMetric; 
    }

    void loop()
    {

    // Read digital soil value
      int soilValue = digitalRead(DIGITAL_INPUT_SOIL_SENSOR_PIN); // 1 = Not triggered, 0 = In soil with water
      if (soilValue != lastSoilValue) {
        Serial.println(soilValue);
        gw.send(msg.set(soilValue==0?1:0)); // Send the inverse to gw as tripped should be when no water in soil
        lastSoilValue = soilValue;

    delay(dht.getMinimumSamplingPeriod());

    float temperature = dht.getTemperature();
      if (isnan(temperature)) {
          Serial.println("Failed reading temperature from DHT");
      } else if (temperature != lastTemp) {
        lastTemp = temperature;
        if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        }
        gw.send(msgTemp.set(temperature, 1));
        Serial.print("T: ");
        Serial.println(temperature);
      }
      
      float humidity = dht.getHumidity();
      if (isnan(humidity)) {
          Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum) {
          lastHum = humidity;
          gw.send(msgHum.set(humidity, 1));
          Serial.print("H: ");
          Serial.println(humidity);
      }
      
      gw.sleep(SLEEP_TIME);
    }
    }

    [/code]



  • @floris said:

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

    Hello, have you a message on the serial print ? Failed reading humidity from DHT or the value ?

    If you want to send every 2 mns, you have to "sleep" 2601000 milllseconds - the processing time for your code (near 0 compared to 2 mns)



  • Hi Totche, thanks for reaction.

    this is the serial print;
    sensor started, id 11
    send: 11-11-20-0 s=255,c=0,t=17,pt=0,l=5,st=ok:1.4.1
    send: 11-11-20-0 s=255,c=3,t=6,pt=1,l=1,st=ok:20
    send: 11-11-20-0 s=255,c=3,t=11,pt=0,l=21,st=ok:Floris Multi Rain+Hum
    send: 11-11-20-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
    send: 11-11-20-0 s=0,c=0,t=7,pt=0,l=5,st=ok:1.4.1
    send: 11-11-20-0 s=1,c=0,t=6,pt=0,l=5,st=ok:1.4.1
    send: 11-11-20-0 s=3,c=0,t=1,pt=0,l=5,st=ok:1.4.1
    1
    send: 11-11-20-0 s=3,c=1,t=16,pt=2,l=2,st=ok:0
    send: 11-11-20-0 s=1,c=1,t=0,pt=7,l=5,st=ok:22.3
    T: 22.30
    send: 11-11-20-0 s=0,c=1,t=1,pt=7,l=5,st=ok:59.0
    H: 59.00

    and after using the rain sensor:

    sensor started, id 11
    send: 11-11-20-0 s=255,c=0,t=17,pt=0,l=5,st=ok:1.4.1
    send: 11-11-20-0 s=255,c=3,t=6,pt=1,l=1,st=ok:20
    send: 11-11-20-0 s=255,c=3,t=11,pt=0,l=21,st=ok:Floris Multi Rain+Hum
    send: 11-11-20-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
    send: 11-11-20-0 s=0,c=0,t=7,pt=0,l=5,st=ok:1.4.1
    send: 11-11-20-0 s=1,c=0,t=6,pt=0,l=5,st=ok:1.4.1
    send: 11-11-20-0 s=3,c=0,t=1,pt=0,l=5,st=ok:1.4.1
    1
    send: 11-11-20-0 s=3,c=1,t=16,pt=2,l=2,st=ok:0
    send: 11-11-20-0 s=1,c=1,t=0,pt=7,l=5,st=ok:22.3
    T: 22.30
    send: 11-11-20-0 s=0,c=1,t=1,pt=7,l=5,st=ok:59.0
    H: 59.00
    0
    send: 11-11-20-0 s=3,c=1,t=16,pt=2,l=2,st=ok:1
    send: 11-11-20-0 s=0,c=1,t=1,pt=7,l=5,st=ok:59.1
    H: 59.10
    1
    send: 11-11-20-0 s=3,c=1,t=16,pt=2,l=2,st=ok:0
    send: 11-11-20-0 s=0,c=1,t=1,pt=7,l=5,st=ok:59.4
    H: 59.40

    Then i get a update from my hum/sensor....(so after activity from the rainsensor, there is an update for hum/rain, and not automagicly). For the moment i want to keep it to 30 seconds, for testing.
    i changed the gw.sleep;
    gw.sleep(INTERRUPT, CHANGE, SLEEP_TIME);
    but no luck so far.



  • @floris I think it may have to do with the brackets { } in your loop. You have a open bracket { for your first " if" statement to check the rain and it does not appear to close } until after your "sleep" function at the bottom of the entire loop. This would cause the hum and temp readings (and sleep) to be skipped until the "if" statement is satisfied.



  • @Dwalt thanks, that did the trick. now it's working! thanks!

    Floris


Log in to reply
 

Suggested Topics

24
Online

11.2k
Users

11.1k
Topics

112.5k
Posts