Of 2 sensors on a node only one reports values


  • Hardware Contributor

    I am testing a soil moisture sensor that has both a digital and an analog output. So I took the example sketch and added a V_LIGHT_LEVEL sensor for the analog value (is there a better choice somewhere?). The V_TRIPPED sensor for the digital part is unchanged.
    Both sensors appear in Domoticz, but only the analog updates.
    Is there a stupid error in the code I overlooked?

    #include <SPI.h>
    #include <MySensor.h>  
    
    #define DIGITAL_INPUT_SOIL_SENSOR 3   // Digital input did you attach your soil sensor.
    #define ANALOG_INPUT_SOIL_SENSOR A0
    #define CHILD_ID 0   // Id of the sensor child
    #define CHILD_ID_2 1
    #define sleepTime 30000
    
    MySensor gw;
    MyMessage msg(CHILD_ID, V_TRIPPED);
    MyMessage msgLevel(CHILD_ID_2, V_LIGHT_LEVEL);
    
    void setup()  
    { 
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Soil Moisture Sensor", "1.0");
      // sets the soil sensor digital pin as input
      pinMode(DIGITAL_INPUT_SOIL_SENSOR, INPUT); 
      pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);   
      // Register all sensors to gw (they will be created as child devices)  
      gw.present(CHILD_ID, S_MOTION);
      gw.present(CHILD_ID_2, S_LIGHT_LEVEL);
    }
     
    void loop()     
    {     
      // Read digital soil value
      int soilValue = digitalRead(DIGITAL_INPUT_SOIL_SENSOR); // 1 = Not triggered, 0 = In soil with water 
      int soilAnalog = analogRead(ANALOG_INPUT_SOIL_SENSOR);
      
        Serial.println(soilValue);
        Serial.println(soilAnalog);
        gw.send(msg.set(soilValue==0?1:0));  // Send the inverse to gw as tripped should be when no water in soil
        gw.send(msgLevel.set(soilAnalog));
    
      gw.sleep(sleepTime);
    }
    

    Thanks for the help 🙂


  • Hero Member

    @LastSamurai What about S_MOISTURE. (is in the API description). There is a soil moisture sensor in the Domoticz beta. i don't see strange things in your sketch other than that you are not "mapping" the analog value to something sensible (if needed at all)


  • Hardware Contributor

    Thanks! I did overlook the S_MOISTURE. I made a new sketch using that one. I also changed the sensor child IDs and flashed the new sketch. Now everything seems to be working.

    If someone needs something like this:

    #include <SPI.h>
    #include <MySensor.h>  
    
    #define DIGITAL_INPUT_SOIL_SENSOR 3   // Digital input did you attach your soil sensor.
    #define ANALOG_INPUT_SOIL_SENSOR A0
    #define CHILD_ID_1 3   // Id of the sensor child
    #define CHILD_ID_2 2
    #define sleepTime 30000
    
    MySensor gw;
    MyMessage msgDigital(CHILD_ID_1, V_TRIPPED); // digital = on/off
    MyMessage msgAnalog(CHILD_ID_2, V_LEVEL); // analog = int value
    
    void setup()  
    { 
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Soil Moisture Sensor", "1.0");
      
      // sets the soil sensor pins as input
      pinMode(DIGITAL_INPUT_SOIL_SENSOR, INPUT); 
      pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);  
      
      // Register all sensors to gw (they will be created as child devices)  
      gw.present(CHILD_ID_1, S_MOISTURE);
      gw.present(CHILD_ID_2, S_MOISTURE);
    }
     
    void loop()     
    {     
      // Read soil values
      int soilValue = digitalRead(DIGITAL_INPUT_SOIL_SENSOR); // 1 = Not triggered, 0 = In soil with water 
      int soilAnalog = analogRead(ANALOG_INPUT_SOIL_SENSOR);
      
      // debugging
      Serial.println(soilValue);
      Serial.println(soilAnalog);
      
      // send values
      gw.send(msgAnalog.set(soilAnalog));
      gw.send(msgDigital.set(soilValue==0?1:0));  // Send the inverse to gw as tripped should be when no water in soil  
    
      // sleep until the next cycle starts
      gw.sleep(sleepTime);
    }
    

  • Hardware Contributor

    Update: Somehow the updates from the digital sensor stopped again after about 5 updates this time (while the analog one still updates perfectly...).
    Does anyone have an idea why? So strange...
    Is it possible that the sending isn't 100% before the node goes to sleep?


  • Hardware Contributor

    It's getting even stranger. Today it worked again but only for about 5 readings...


  • Hero Member

    @LastSamurai I would add a small delay between sending using the gw.wait method and see if that improves the reliability of the transmissions.

    Cheers
    Al


  • Hero Member

    @LastSamurai Did you check the debug information on the sensor node and checked for "fail" messages? It could be a power issue. As you are sending two values directly after each other (first analog, second digital) the last one could be suffering. Have you followed the instructions for connecting the radio ?


  • Hardware Contributor

    I have connected the radio like in the instructions (otherwise how could any message get through anyways?).
    I also added pauses. It still doesn't work though. Only the analog values get updated.

    New Code:

    #include <SPI.h>
    #include <MySensor.h>  
    
    #define DIGITAL_INPUT_SOIL_SENSOR 3   // Digital input did you attach your soil sensor.
    #define ANALOG_INPUT_SOIL_SENSOR A0
    #define CHILD_ID_1 3   // Id of the sensor child
    #define CHILD_ID_2 2
    #define sleepTime 30000
    
    MySensor gw;
    MyMessage msgDigital(CHILD_ID_1, V_TRIPPED); // digital = on/off
    MyMessage msgAnalog(CHILD_ID_2, V_LEVEL); // analog = int value
    
    void setup()  
    { 
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Soil Moisture Sensor", "1.0");
      
      // sets the soil sensor pins as input
      pinMode(DIGITAL_INPUT_SOIL_SENSOR, INPUT); 
      pinMode(ANALOG_INPUT_SOIL_SENSOR, INPUT);  
      
      // Register all sensors to gw (they will be created as child devices)  
      gw.present(CHILD_ID_1, S_MOISTURE);
      gw.present(CHILD_ID_2, S_MOISTURE);
    }
     
    void loop()     
    {     
      // Read soil values
      int soilValue = digitalRead(DIGITAL_INPUT_SOIL_SENSOR); // 1 = Not triggered, 0 = In soil with water 
      int soilAnalog = analogRead(ANALOG_INPUT_SOIL_SENSOR);
      
      // debugging
      Serial.println(soilValue);
      Serial.println(soilAnalog);
      
      // send values
      gw.send(msgAnalog.set(soilAnalog));
      gw.sleep(200);
      gw.send(msgDigital.set(soilValue));  // Send the inverse to gw as tripped should be when no water in soil - no inverse atm => high == needs water
      gw.sleep(200);
    
      // sleep until the next cycle starts
      gw.sleep(sleepTime);
    }
    
    

    The node with the sensor has output like this:

    sensor started, id=2, parent=0, distance=1
    send: 2-2-0-0 s=255,c=3,t=11,pt=0,l=20,sg=0,st=ok:Soil Moisture Sensor
    send: 2-2-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.0
    send: 2-2-0-0 s=3,c=0,t=35,pt=0,l=0,sg=0,st=ok:
    send: 2-2-0-0 s=2,c=0,t=35,pt=0,l=0,sg=0,st=ok:
    1
    1023
    send: 2-2-0-0 s=2,c=1,t=37,pt=2,l=2,sg=0,st=ok:1023
    send: 2-2-0-0 s=3,c=1,t=16,pt=2,l=2,sg=0,st=ok:1
    1
    1023
    send: 2-2-0-0 s=2,c=1,t=37,pt=2,l=2,sg=0,st=ok:1023
    send: 2-2-0-0 s=3,c=1,t=16,pt=2,l=2,sg=0,st=ok:1
    

    To me everything looks fine. Is it possible that this is a bug in Domoticz? I am using the newest beta version already.

    PS Now it did just send one value again, after restarting the node... but only one, now there are only analog values again.


  • Admin

    Any difference if you try gw.wait(50) instead of gw.sleep(200)?


  • Hardware Contributor

    Thanks hek, thats what I really wanted to do... typo... I will try that one too later.
    I might have found the "error" though (need to confirm). The logs are showing lots of changes yesterday and today, but always only if the value changed (off => on or on => off). So I think it might be that domoticz ignores updates (even in the log) that don't change the value.
    Can anyone confirm this behaviour? Else I need to check in their forum.



  • I can confirm that the mysensors "adapter" for domoticz does not update anything for a sensor if the value has not changed - including last seen time. There has been a bit of a discussion about it and I have a bug and feature request in for it to be a configurable choice for the user - but as it stands - if no change in value then update is ignored.

    I modified the mysensors adapter code to always update - even if values haven't changed - but that isn't really a long term solution since it gets overwritten on every update.

    https://www.domoticz.com/forum/viewtopic.php?f=42&t=9272


Log in to reply
 

Suggested Topics

  • 5
  • 4
  • 1
  • 5
  • 2
  • 2

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts