Moisture Sensor Polarity

Discussion in 'General Discussion' started by ColinCamSmith, Jul 17, 2021.

  1. ColinCamSmith

    ColinCamSmith

    Joined:
    Nov 25, 2004
    Messages:
    70
    Likes Received:
    0
    I have a Watermark Moisture Sensor in my very small Vineyard connected to an Irrometer 200SS-VA Voltage Adapter.
    This Voltage Adapter is connected to the Gen Input Unit in CBus using 0-5 volts. It is set to transmit on the Measurement Application (Manager). BTW, the distance from CBus to the Vineyard has precluded using amps or ohms on advice from the manufacturer.

    Now, using Code in Homegate I load the System I/O with the result which comes in a percentage (%). The problem I have is that when the Sensor is fully Dry it shows 100% and fully Wet 0%. The polarity can not be changed due to the wiring setup on the Adapter.

    I have tried writing code to reverse the reading but all to no avail. Putting negative values into System I/O is not possible nor will negatives work in the Gen Input unit as I can reduce the 100% to 0% but then the 0% goes to minus 100%.

    So, any ideas on this issue would be appreciated.

    Regards,

    Colin
     

    Attached Files:

    ColinCamSmith, Jul 17, 2021
    #1
    1. Advertisements

  2. ColinCamSmith

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,254
    Likes Received:
    130
    Location:
    Adelaide, Australia
    Just create a new user system variable and set it to the correct value when the measurement value changes.
    e.g.
    Assuming the measurement value is on unit 1 channel 1.
    Create a user system variable called say "New Value"

    Then:

    Code:
    if HasChanged(GetRealIBSystemIO("Measurement App Real Value", "Local", 1, 1)) then
      SetRealSystemIO("New Value", 100 - GetRealIBSystemIO("Measurement App Real Value", "Local", 1, 1));
     
    Ashley, Jul 17, 2021
    #2
    1. Advertisements

  3. ColinCamSmith

    ColinCamSmith

    Joined:
    Nov 25, 2004
    Messages:
    70
    Likes Received:
    0
    Ashley,

    Many thanks
    I have tried your suggestion but have not achieved the result I was after.
    First I need to clarify the percentage I quoted in my initial post.
    For simplicity, I use 100% whereas in fact the reading I get when Fully Dry
    is 60%.
    This is based on the 0.05900028 volts returned from the adapter and is in
    line with the Manufacturers advice.
    Naturally the percentage reads correctly when used through a Data Logger but
    is reversed in CBus / Homegate.

    To allow you to more fully understand how I have set it up I have attached a
    file with a number of Screenshots.

    Fig 1 Shows the Measurement Application Manager set to Channel 0 for the
    Moisture Sensor (Vineyard) and lined up with the Gen Input Unit.
    Fig 2 Shows the Gen Input unit with the Moisture Sensor (Vineyard) on
    Channel 1 and reading 60%.
    Fig 3 Shows the CBus Log reflecting the voltage readings from the Gen
    Input Unit.
    Fig 4 Shows the System I/O from Homegate with the Moisture Sensor
    (Vineyard) reading 0.05900028 and set to a Maximum of 0.15.
    Fig 5 Shows the Homegate Text Box reading 59% and the Properties showing
    it is connected to the Status of the System I/O for the Moisture Sensor
    (Vineyard).
    Fig 6 Shows the Properties of the Moisture Sensor (Vineyard) Text Box in
    Homegate.
    Fig 7 Shows the Homegate Logic with your code added but using 0.08 as the
    number to be subtracted from rather than the 100 you used in your example.

    So, the reading I am getting is still incorrect so somewhere I have made a
    mistake but where?
    And finally, if we get it to work and read 0% when Dry wont we end up with a
    negative % when Wet?

    Regards,

    Colin
     

    Attached Files:

    ColinCamSmith, Jul 18, 2021
    #3
  4. ColinCamSmith

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,254
    Likes Received:
    130
    Location:
    Adelaide, Australia
    You just need to do the maths :)

    Define 2 constants that represent the value from the measurement manager when the desired result is 0% and 100%

    e.g.
    M0 is value read you want to display 0%
    M100 is value read you want to display 100%

    Assume MX is the currently read value.
    You just have to subtract M0 to get the offset to 0, then scale it by the range (M100-M0)

    so you get

    result = (MX - M0) / (M100 - M0) * 100

    in code:

    Code:
    [Const section]
    M0 = ? ;            // Value from measurement app when sensor shows 0%
    M100 = ? ;      // Value from measurement app when sensors shows 100%
    
    [Global Variables]
    MX: real ;     // Temp value to hold value read from measurement so we don't have to read it twice.
    
    [Module]
    
    MX := getRealIBSystemIO("Measurement App Real Value", "Local", 4, 0);
    if HasChanged(MX) then
      SetRealSystemIO("New Value", (MX - M0) / (M100 - M0) * 100);
     
     
    Ashley, Jul 18, 2021
    #4
  5. ColinCamSmith

    ColinCamSmith

    Joined:
    Nov 25, 2004
    Messages:
    70
    Likes Received:
    0
    Ashley,
    I am afraid you are losing me. I first played with CBus in the 90's and other than write the odd code for Homegate have not played with it fore some time.
    It is also difficult when you pass 80.

    I will fill in the variables and see how I go.

    Regards,

    Colin

    PS - have added the current Homegate file but without the add-ons other wise too large
     

    Attached Files:

    ColinCamSmith, Jul 18, 2021
    #5
  6. ColinCamSmith

    ColinCamSmith

    Joined:
    Nov 25, 2004
    Messages:
    70
    Likes Received:
    0
    Ashley,

    See attached Constants and Variables. I think it is getting above my skill set. The code I enter will obviously not run. And these days there is no one in the Illawarra that knows anything about CBus and Homegate. Sad as it appears that Amazon crap is taking over.
     

    Attached Files:

    ColinCamSmith, Jul 18, 2021
    #6
  7. ColinCamSmith

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,254
    Likes Received:
    130
    Location:
    Adelaide, Australia
    The constants have to be variable names you make up (i.e. not tags which are in quotes). They can only contain letters (a-z), digits (0-9) and underscores (_) and can't start with a digit. They aren't enclosed in quotes. Uppercase and lower case letters are the same.

    So try:
    //Constants
    Moisture_Sensor_Dry = 0.059000028;
    Moisture_Sensor_Wet= 0.001;

    //Global Vars
    Moisture_Sensor: real; // This just define the variable. You have to assign it's value in the module
     
    Ashley, Jul 18, 2021
    #7
  8. ColinCamSmith

    ColinCamSmith

    Joined:
    Nov 25, 2004
    Messages:
    70
    Likes Received:
    0
    Ashley,

    Yes I had the Constants & the Glob al variables sorted (it must be 25 years since I wrote the original variables for daylight). I have also managed to write the code for the the inclusion of the Constants but am having trouble "assigning" the Global Variable. See attached.

    Regards,

    Colin Screenshot 2021-07-18 12.59.25.png
     
    ColinCamSmith, Jul 18, 2021
    #8
  9. ColinCamSmith

    ColinCamSmith

    Joined:
    Nov 25, 2004
    Messages:
    70
    Likes Received:
    0
     
    ColinCamSmith, Jul 18, 2021
    #9
  10. ColinCamSmith

    ColinCamSmith

    Joined:
    Nov 25, 2004
    Messages:
    70
    Likes Received:
    0
    Ashley,

    Got all the code in without errors so now will see what the results are.
    Will keep you posted.
     
    ColinCamSmith, Jul 18, 2021
    #10
  11. ColinCamSmith

    ColinCamSmith

    Joined:
    Nov 25, 2004
    Messages:
    70
    Likes Received:
    0
    Ashley,
    Sorry to bother you again but there has been no change in the readings. Have attached a shot of the Welcome page and the Homegate File.

    What have I missed this time

    Regards,
    Colin
     
    ColinCamSmith, Jul 18, 2021
    #11
  12. ColinCamSmith

    ColinCamSmith

    Joined:
    Nov 25, 2004
    Messages:
    70
    Likes Received:
    0
    Sorry, forgot files
     

    Attached Files:

    ColinCamSmith, Jul 18, 2021
    #12
  13. ColinCamSmith

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,254
    Likes Received:
    130
    Location:
    Adelaide, Australia
    Here's your "Moisture Reading Adjustment" module:

    Code:
    { Adjustment of Moisture reading to allow for  polarity change }
     
    if (GetCBusState("Local Network", "Inputs", "Vineyard Rain Sensor") = OFF) then
    begin
          SetRealSystemIO("Moisture Sensor", GetRealIBSystemIO("Measurement App Real Value", "Local Network", 4, 0));
    end;
    
    if (GetCBusState("Local Network", "Inputs", "Vineyard Rain Sensor") = ON) then
    begin
          SetRealSystemIO("Moisture Sensor", GetRealIBSystemIO("Measurement App Real Value", "Local Network", 4, 0));
    end;
    
    
    MoistureSensor := GetRealIBSystemIO("Measurement App Real Value", "Local Network", 4, 0);
    
    if HasChanged(GetRealIBSystemIO("Measurement App Real Value", "Local Network", 4, 0)) then
    begin
      SetRealSystemIO("Moisture Sensor",(MoistureSensor-MoistureDry) / (MoistureWet-MoistureDry) * 100);
    end;
    Why are the 2 IF statements there (and the ones in the Vineyard Moisture module)? They are setting the "Moisture Sensor" user variable to the value read directly from the measurement every scan of the module. Then when the value changes, the hasChanged sets it to the correct percentage, only to be clobbered again next scan. The IF statements also perform the same action whether or not the rain sensors are on or off which doesn't actually accomplish anything.

    I'm not really sure what you are trying to accomplish here, but the only time you should be setting the "Moisture Sensor" user variable is in the hasChanged statement. Then you can read it anytime to do other stuff.

    If fact, anything you want to do as a result of the moisture changing should be inside the hasChanged function, as you only need to do stuff when the variable changes.

    I think you need to delete all the IF statements, then move the code that sets the "Moisture Sensor (Vineyard)" cbus variable into the hasChanged functions (then delete the Vineyard Moisture module.

    You should end up with:

    Code:
    MoistureSensor := GetRealIBSystemIO("Measurement App Real Value", "Local Network", 4, 0);
    
    if HasChanged(MoistureSensor) then
    begin
        SetRealSystemIO("Moisture Sensor",(MoistureSensor-MoistureDry) / (MoistureWet-MoistureDry) * 100);
        if MoistureSensor") > 0.0500 then // Can use our variable here rather than reading it back
          SetCBusState("Local Network", "Inputs","Moisture Sensor (Vineyard)", ON)
       else
         SetCBusState("Local Network", "Inputs","Moisture Sensor (Vineyard)", OFF);
    end;
    
    end;
     
    Ashley, Jul 18, 2021
    #13
  14. ColinCamSmith

    ColinCamSmith

    Joined:
    Nov 25, 2004
    Messages:
    70
    Likes Received:
    0
    Ashley,
    You are correct in that they are now no longer required. They were a carry over from when I first started trying to get code that would work and need to stop the irrigation from time to time. I will delete them.
    First thing tomorrow I will put your code into the Logic and see what results we get.

    Thanks again,
    Regards,

    Colin
     
    ColinCamSmith, Jul 18, 2021
    #14
  15. ColinCamSmith

    ColinCamSmith

    Joined:
    Nov 25, 2004
    Messages:
    70
    Likes Received:
    0
    Ashley,

    Unfortunately after some 24 hours the System I/O remained at 0.059000028 and naturally the Text Box read 59%.
    In short it appears that the code is not working to reflect the Dry Status.

    As a test I flooded the Sensor and the readings immediately reflected the change.
    The voltage dropped to 0.002 and the System I/O 0.002 and the Text Box reads 2%.

    Attached is file which reflects these readings:
    Figure 1 shows the Gen Input Unit with 2%
    Figure 2 shows the System I/O with a reading of 0.002
    Figure 3 shows the CBus log with a voltage of 0.002
    Figure 4 shows the Text Box reading 2%
    Figure 5 shows the code we have tried.

    I am not sure where we can go from here as the problem may be insurmountable.

    Regards,

    Colin
     

    Attached Files:

    ColinCamSmith, Jul 19, 2021
    #15
  16. ColinCamSmith

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,254
    Likes Received:
    130
    Location:
    Adelaide, Australia
    Have you set up the moisture sensor in the measurement manager? There is no print out that shows this.
     
    Ashley, Jul 20, 2021
    #16
  17. ColinCamSmith

    ColinCamSmith

    Joined:
    Nov 25, 2004
    Messages:
    70
    Likes Received:
    0
    Ashley,

    See attached., Also note voltage has gone up to 0.003 as Senor dries out.

    Regards,
    Colin
     

    Attached Files:

    ColinCamSmith, Jul 20, 2021
    #17
  18. ColinCamSmith

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,254
    Likes Received:
    130
    Location:
    Adelaide, Australia
    So does the value in the measurement manager change correctly if you watch it?
     
    Ashley, Jul 20, 2021
    #18
  19. ColinCamSmith

    ColinCamSmith

    Joined:
    Nov 25, 2004
    Messages:
    70
    Likes Received:
    0
    The value in the Measurement Manager is following the voltage received from The Gen Input unit. Currently 0.004 and the C Bus Log is reading 0.00400000018998981
    The System I/O is reading 0.00400000018998981

    Regards,

    CCS
     
    ColinCamSmith, Jul 20, 2021
    #19
  20. ColinCamSmith

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,254
    Likes Received:
    130
    Location:
    Adelaide, Australia
    Change the code as follows. It just logs if it is executing. (Don't forget the delay!)
    In the logic engine menu under TOOLS select LOGIC OPTIONS then tick Send WriteLn to Log.

    See what the log does. You should get the raw value printed every 2 seconds. When the value changes, it should print 'Value changed'


    Code:
    MoistureSensor := GetRealIBSystemIO("Measurement App Real Value", "Local Network", 4, 0);
    
    writeLn('Moisture Sensor raw=', moistureSensor);
    if HasChanged(MoistureSensor) then
    begin
        SetRealSystemIO("Moisture Sensor",(MoistureSensor-MoistureDry) / (MoistureWet-MoistureDry) * 100);
        writeLn('Value changed');
    end;
    
    delay(2);
    
    end;
     
    Ashley, Jul 20, 2021
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.