Is Z-Uno supported by Smart Things? I need a water level sensor

Hendre,

Okay, I have everything you need uploaded to my GitHub repository.

For the Arduino side of things:

  • Copy PS_Ultrasonic.h and PS_Ultrasonic.cpp from the “Arduino/libraries/ST_Anything” folder and copy them to your corresponding folder. These are updated versions of @Saif76 's files, so please overwrite any copies you may already have on your PC.

  • Copy the ST_Anything_Ultrasonic_ESP01WiFi folder and included .ino sketch from the “Arduino/Sketches” folder to your Arduino/Sketches folder. Edit this file to add your WiFI credentials, hub IP address, etc…

Note: Since you’re using an ESP01, it only has two GPIO pins on it - pin 0 and 2. I have configured this sketch to use both of those pins for the Ultrasonic sensor. It requires a pin for a trigger and another to measure the echo response. These are the pins you’ll need to figure out how to wire to your ultrasonic sensor. I do not have one of these sensors, so you’ll need to ask @Saif76 for instructions , or Google it.

On the SmartThings side of things:

-Hopefully you’re using GitHub integration in your ST IDE. If so, please be sure to update your “Parent_ST_Anything_Ethernet” Device Handler with the new version which supports the ultrasonic Child Sensor device handler. Remember to SAVE and PUBLISH the updated version!
-While you’re in the GitHub integration, be sure to install and PUBLISH the new “Child Ultrasonic Sensor” device handler.

It is probably best to delete your existing Parent Device (not the code, the actual device in the ST IDE’s “My Devices” tab - not the “My Device Handlers” tab!) Then manually create it again, and use your phone to configure it MAC Address, IP Address, Port, and number of buttons (use 0 for this). After doing so, a new child device will be created for the ultrasonic sensor. Click on it, go into its settings, and configure the dimensions or your tank.

OK - Hopefully you’ve got it from here!

1 Like

I can’t wait to try this when I get home. I really seriously appreciated your assistance.

(I do have Github integration.)

Edit: I could not wait another 6 hours when I go home, so I logged into my Home PC from work. Fortunately, I left the ESP8266 in the correct mode. Sketch compiled and successfully written to ESP8266!

Edit 2:

This is fantastic!

Thank you once again, Dan for your help. I’ll hook up the sensor tonight and will keep you updated.

1 Like

My sensor is working! I can’t thank @ogiewon enough for his patience. Also a big thank you to @Saif76 for sharing his code with us.

I wonder if someone can help me to change the DTH code so that the percentage only shows 1 or 2 decimals? I noticed @Saif76’s Device Tile shows only 1 decimal, but for some or other reason, mine shows a gazillion decimals both in respect of the % and the litres remaining:
image

I’ll at the DTH below to make it easier to take a quick look at the code - I’m sure it will be very easy to format the percentage and volume to limit the decimals.

I have one quirk in that I can’t get the sensor to show more than about 78%. (I tested by setting the height to 100cm (in the DTH configuration) and simulating the water level with a piece of cardboard). It goes up to about 78%, remains at 78% as I move the cardboard closer to the sensor and when it is about 1cm from the sensor, the reading switches to a crazy number like -256%. Below that level, the sensor is very accurate - if the piece of cardboard is 80cm away (with the height set to 100cm), the water level is reported as 20%. Anyone has any idea what could be causing that?

Lastly, I was wondering how difficult it would be to add a setting to the DTH which will allow us to set an offset (to make provision for the distance between the sensor and the maximum water level). The idea is that the sensor shows 100% when the water is at maximum level, even though there is still a few inches/cm between the sensor and the water.)

The DTH:

/**
 *  Child Ultrasonic Sensor
 *
 *  Copyright 2017 Daniel Ogorchock
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 *  Change History:
 *
 *    Date        Who            What
 *    ----        ---            ----
 *   
 *
 * 
 */
metadata {
	definition (name: "Child Ultrasonic Sensor", namespace: "ogiewon", author: "Daniel Ogorchock") {
		capability "Sensor"
		attribute "lastUpdated", "String"

		command "generateEvent", ["string", "string"]
    }

	tiles(scale: 2) {
		multiAttributeTile(name: "ultrasonic", type: "generic", width: 6, height: 4, canChangeIcon: true) {
			tileAttribute("device.ultrasonic", key: "PRIMARY_CONTROL") {
				attributeState("ultrasonic", label: '${currentValue}%', unit:"%", defaultState: true, 
						backgroundColors: [
							[value: 80, color: "#767676"],
							[value: 50, color: "#ffa81e"],
							[value: 20, color: "#d04e00"]
						])
			}
            tileAttribute ("device.liters", key: "SECONDARY_CONTROL") {
        		attributeState "power", label:'Water capacity: ${currentValue} liters', icon: "http://cdn.device-icons.smartthings.com/Bath/bath6-icn@2x.png"
            }    
        }
 		valueTile("lastUpdated", "device.lastUpdated", inactiveLabel: false, decoration: "flat", width: 6, height: 2) {
    			state "default", label:'Last Updated ${currentValue}', backgroundColor:"#ffffff"
		}
    }
    
    preferences {
        input name: "height", type: "number", title: "Height", description: "Enter height of tank in cm", required: true
        input name: "diameter", type: "number", title: "Diameter", description: "Enter radius of tank", required: true
    }
}
def generateEvent(String name, String value) {
	//log.debug("Passed values to routine generateEvent in device named $device: Name - $name  -  Value - $value")
    def sensorValue = value as float
    def volume = 3.14159 * (diameter/2) * (diameter/2) * height
    def capacityLiters = volume / 1000 * 2
    sendEvent(name: "liters", value: capacityLiters)
    def capacityValue = 100 - (sensorValue/height * 100 )
    if(capacityValue != 100)
    {
    	sendEvent(name: name, value: capacityValue)
    }
    // Update lastUpdated date and time
    def nowDay = new Date().format("MMM dd", location.timeZone)
    def nowTime = new Date().format("h:mm a", location.timeZone)
    sendEvent(name: "lastUpdated", value: nowDay + " at " + nowTime, displayed: false)
}

def installed() {

}

Dan
I was wondering if this part of the DTH is correct (the parts in bold):

preferences {
input name: “height”, type: “number”, title: “Height”, description: “Enter height of tank in cm”, required: true
input name: “diameter”, type: “number”, title: “Diameter”, description: “Enter radius of tank”, required: true
}
}

def parseValue(String namebase,String value){
//results.each { name, value ->
//Parse events and optionally create SmartThings events
//log.debug "This event happened parse1 ${value}"
def sensorValue = value as float
def volume = 3.14159 * (diameter/2) * (diameter/2) * height
def capacityLiters = volume / 1000 * 2
sendEvent(name: “liters”, value: capacityLiters)
def capacityValue = 100 - (sensorValue/height * 100 )
if(capacityValue != 100)
{
sendEvent(name: namebase, value: capacityValue)
}
return true
}

Should the description not be “Enter diameter here”? The volume calculation uses the formula to calculate volume with the diameter of the cylinder.

I was also wondering what should be added to the DTH so that the volume remaining is exposed to webCoRE? (The idea being that an alert can be set up in webCoRE which sends out a warning when the tank lever drops below e.g. 25%)

You can change the Child Ultrasonic Sensor device handler in any way that meets your needs. Since this is a custom capability, not a standard SmartThings capability, it is really up to you.

Standard ST Capabilities are listed here:

https://smartthings.developer.samsung.com/develop/api-ref/capabilities.html

Normally I try to make sure every one of my Child Device Handlers matches up with at least one, or more of these capabilities. Each capability exposes specifcally names Attributes. You’re probabyl going to want a custom attribute to the Child Device Handler called “ultrasonic” as that is what appears to have the value you desire. You’re going to want to start reading up on SmartThings Device Handler code at

http://docs.smartthings.com/en/latest/device-type-developers-guide/index.html

1 Like

Thank you Dan. My programming skills are non-existent, but maybe I’ll get lucky.
Regards and thanks once again for your help with the sensor. It works very well.
(By the way, for further reference, I had to swop the ESP01 with a Node-MCU (as you suggested all along!). Once the sensor was connected to the ESP01, it would not boot. (It goes into download mode with the sensor connected when power is restored. I suppose there is a way to circumvent that, but it seemed too complicated for my abilities. I did try to swap the pins around in the sketch, but that did not help. So in the end, I got a Node-MCU. All I had to do was to change the pin assignment in the sketch, which I managed to get right by looking at other sketches.)

1 Like

I had the same issue with decimals I can’t remmever what I did to fix it. I will find out and get back to you.

I should have mentioned this earlier that the ulatrsonic sensor doesn’t work very well I think when the distance is less than 20cm. So I placed mine higher to never go below 20cm to get True reading . You can do the same and then offset that distance in the DH code if you want.

1 Like

Good catch the label is wrong I started by wanting to use the radius but then got lazy and just used the diameter but dint change the description. Please change it to diameter, shouldn’t impact any of your calculations or working.

Continuing the discussion from Is Z-Uno supported by Smart Things? I need a water level sensor:

Hello there @Saif76 and @949BFN

I want to accomplish what you had done but I’m a newbie. I have no idea whatsoever how to get this done, what sensors to get, how to flash all programming. Basically EVERYTHING…lol Was wondering if you can help me with what hardware to get and how to get everything else done please?

Thanks,

Qasim.

It is a challenging project to attempt without some knowledge of the Arduino software. I am not a programmer and have not used the Arduino before. It took at least 4 of my vacation days to get this working and fortunately @ogiewon had a massive amount of patience with me. Be warned that without some knowledge of the Arduino, it will take lots of reading; watching Youtube videos and perseverance in general.
I would suggest you start here and work through this thread as well as this one.
Unfortunately, I know too little to be of assistance to you.
Good luck!