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

Sounds like @Saif76 does indeed have a solution that you could use without having to reinvent the water wheel! :wink:

I have ordered the sensor and ESP8266. (I have never used a device like this, so I suppose a big learning curve awaits me.
Is my understanding correct that once I have these devices set up with my wifi, I’ll be using your ST_Anything DTH. Am I correct that there is a child-device for the Ultrasonic Distance Measuring Transducer? I suppose once I have all this connect and talking to each other, I will be able to figure out how to calibrate the sensor. I’m concerned that I’ll not be able to read the water level in webCoRE.


I believe you’re going to need some code developed by @Saif76 to get things going. My library does not currently have support for an Ultrasonic Distance Sensor (at least not that I can recall! :slight_smile: )

You are correct that you’ve got a big learning curve ahead of you if you’ve never done any Arduino programming before. There are plenty of “getting started tutorials” that you should familiarize yourself with before even attempting to use ST_Anything. ST_Anything is a hobbyist tool-kit, with some ready-to-run device support. For custom things, like this ultrasonic sensor with tight timing characteristics, some knowledge of C/C++ programming on the Arduino/ESP8266 platform is required. You will also need to learn how to write some Groovy language code on the SmartThings IDE side of things. There is no standard ST Capability for a Level Sensor, so you’re looking at some custom code there as well.

Hopefully @Saif76 can provide some of his work to get you kick-started.

Thanks Dan. I will first do some reading so that I at least have some understanding of what I’m dealing with, before I start bugging @Saif76.

Hi @Saif76.
Would you mind sharing the code you used to get the ultrasonic sensor working with ST_Anything?
There are a few members looking for a solution to check the water level of a reservoir/tank.
Thank you for considering.


Yes, Let me post it here and if @ogiewon is interested in including it in his library i’m fine with that.

There is one important change you have to make in the “parent-st-anything-ethernet.groovy”, I had to add a condition to intercept the value of the sensor so i could calculate and show me capacity in percentage and liters. The water tank i am using is cylindrical in shape so my settings accept a height and diameter in cm to calculate volume.

This is the sensor i used -

Here is the code to the Arduino class and the DH -

Here is what the it looks like in the app

In the parent device handler i had to change this

childDevice.sendEvent(name: namebase, value: value) 


            def functionExist = childDevice.parseValue(namebase,value) // check if the child has a parse function
            	 //log.debug "Function Exists"
            	childDevice.sendEvent(name: namebase, value: value)
                //log.debug "function does not exist"

I know @ogiewon has made a few changes to the library since then, I have not updated my code to accommodate the changes, So please make modifications to fit your needs.

1 Like

Thanks very much - much appreciated. I can’t wait to try this.

Did you install the sensor at the top of the tank, looking down towards the bottom thereof?

Hi again @saif76 and @ogiewon
I’ve now been busy for a few hours and have made some headway. I’ve learned how and managed to program the ESP8266 (ESP-01 Standalone version) and I think I know how to compile the sketch.

I got so far as to get the On/Off LED ST device working. I messed this up however by writing @saif76’s sketch to the ESP8266, which made me realize that one can only write one sketch at a time. I suppose I have to add the libraries together and write them all at the same time as one sketch to the chip?

I noticed the following in the heading of your sketch:

- String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name
//				- long interval - REQUIRED - the polling interval in seconds
//				- long offset - REQUIRED - the polling interval offset in seconds - used to prevent all polling sensors from executing at the same time
//				- byte digitalTriggerPin - REQUIRED - the Arduino Pin to be used as a digital output to trigger ultrasonic
//				- byte digitalEchoPin - REQUIRED - the Arduino Pin to be used as a digital input to read the echo

Am I correct that I have to add this info here:

PS_Ultrasonic(const __FlashStringHelper *name, unsigned int interval, int offset, byte digitalTriggerPin, byte digitalEchoPin);

If I’m correct, could you help me with the Groovy ST_Anything name? Not sure which DH info I should add there. Should it be Child Ultrasonic Sensor?

Which brings me to another question - once the ESP8266 has been programmed, how did you hook-up the chip to power and the Ultrasonic Sensor? The Sensor came with its own board, but so far I’ve been unable to determine how that should be connected.

Sorry for the newbie questions and thanks again for your assistance.

Edit: I don’t know if I’ve been going at this for too long now, but I don’t think I have any idea of how to combine the sketches and which ones I have to use. My goal is (at this stage) to only use the Ultrasonic Sensor, connected to an ESP-01, to measure the water level in a tank. I managed to do the LED On/Off thing in @ogiewon’s example, but now I stuck. I don’t know which of his sketches I should use and how @Saif76’s sketch for the Ultrasonic sensor, should be added to that.

I made the changes to the parent-st-anything-ethernet DTH.

Hi @Saif76/@ogiewon
I hate to be annoying, but if you perhaps have a few minutes in the next few days, I’ll appreciate your guidance immensely.
Thank you.

Start by following the standard ST_Anything ReadMe linked below. Use the ST_Anything_Multiples_ESP01.ino sketch to get started. Once you have this example working, you can start incorporating @Saif76’s new additions for the ultrasonic level sensor.

Is there a reason you didn’t go with a NodeMCU ESP8266 based board? They are much easier to use as they have onboard USB to ease connection and programming from your computer. The ESP01’s are tiny and inexpensive, but a bit of a pain to hook up and program.

Thank you, Dan, your assistance is much appreciated.

Why the ESP01? Simply because of a lack of experience. As I indicated earlier, I, however, managed to figure the programming out and got so far as to get your LED On/Off example working just fine.
It is the next step where I run into trouble, regardless of how many times I’ve read the README. (I really try to work stuff out, because that is the only way of getting to understand these things.)

My issues which I’ve not been able to figure out:
• Which of your libraries should I use with @Saif76’s sketch? I understand I should use the SmartThingsESP8266WiFi library?
• My understanding is that your library should be combined with @Saif76’s code for the ultrasonic sensor. My issue is that I don’t know how to combine the two. I have tried by loading your SmartThingsESP8266WiFi library and @Saif76’s code in 2 tabs of the Arduino IDE and issued the compile command. That did not work. I tried to “integrate” @Saif76’s code to the bottom of your ESP8266 library within the same tab in the Arduino IDE, but that would also not compile the sketch. My issue is that I know so little, it is almost impossible for me to tell why it fails because I could be making mistakes on numerous levels. I don’t know if I’m adding @Saif76’s code correctly, neither which of your many libraries I should use.

The question therefore here is: If I only want to use the Ultrasonic Sensor, which of your libraries should I use and how do I add @Saif76’s code to your library in the Arduino IDE to create the sketch?

@Saif76’s code contains the following remark at the top: “- String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name” What is the object here? Is it your generic ST_Anything Tile in the ST App, or the Tile name of the new DTH being created for the Ultrasonic Sensor.

• Also, not using an Arduino but the standalone ESP8266, how should I change this part of his code, taking into account his remarks at the top of his code: _PS_Ultrasonic(const __FlashStringHelper *name, unsigned int interval, int offset, byte digitalTriggerPin, byte digitalEchoPin). Do you know what the Serial Pin (for the ESP8266) settings should be and how it should be defined here? The examples I could find on the internet contains what seems to be complicated coding and not simply a single digit representing the Pin. Perhaps if I could see what @SAI76’s working sketch looks like, I’ll be able to figure this one out?

• Lastly, as far as the Hardware setup is concerned, once I’ve written the Sketch to the ESP8266, do I connect the Trig and Echo pins of the ultrasonic sensor to the TX/RX pins respectively, or the other way around? Or should the Sensor be connected to the GPO pins of the ESP8266 (I’ve seen other projects on the www connecting sensors to the GPO pins, but I can’t figure out why/when that should rather be used.)

I’m sorry for being such a bother, but I really need to get this working to determine the level of my backup water reservoir. My problem is that I often attempt (electronic/programming) projects far beyond my capabilities.


Before we can learn to fly, we all need to learn to crawl, eh? :slight_smile:

For now, I need you to follow my instructions in the ST_Anything ReadME that I linked above. When you say you got my “On/Off” example working, I need for you to realize that example is not really part of ST_Anything. That is part of the SmartThings… set of communication libraries. Ignore the “SmartThings” examples. I need you to follow the exact step by step instructions in the ST_Anything v2.9 ReadMe file.

In those instructions, it tells you to try compiling and running one of the sample ST_Anything sketches. I specifically told you which sketch you should try to get ST_Anything running on your ESP01 module. Once you get that sketch up and running on the ESP01, and properly showing up in SmartThings on your phone’s ST app, then we can start looking at @Saif76’s additions. Forget about ultrasonic sensors, wiring, combining code, etc… for now. Just focus on following the ReadMe I linked above and getting a solid ST_Anything foundation up and running.

After you get that working, we can start thinking about @Saif76’s code.

1 Like

Hi Dan
I have gone through the Readme again, step by step, but I must be missing something, because I got to the same result:
I compiled the sketch you referred me to, changing the device IP; gateway; subnet; DNS server and ST Hub ID. The sketch compiled without an error and was successfully written to the ESP8266.
I again checked the settings of Parent_ST_Anything_Ethernet in the ST IDE. All seem to be correct:

ST Anything Device ST Anything
Name ST Anything
Label ST Anything
Type Parent_ST_Anything_Ethernet
Version Published
Device Network Id 50B7C3F43143
Hub Home Hub
Group System
Last Activity At 2018-02-10 3:26 PM SAST
Date Created 2018-02-10 3:26 PM SAST
Last Updated 2018-02-14 6:18 PM SAST
Data No data found for device
Current States
numberOfButtons: 5
Preferences (edit)
Name Type Value
ip text
mac text 50B7************
numButtons number 5
port text 8090
Execution Location Cloud

I checked the settings of the Device in the ST App on my phone. I did not know what I should set the “buttons” to and entered “5”.
My understanding is that child devices should be created when I save the settings in the ST App. No child devices are however created.

I see no error messages in the ST IDE Log (for ST Anything):

728117ed-a723-41fd-8396-060c0954332e  6:28:06 PM: debug Using ip: and port: 8090 for device: 728117ed-a723-41fd-8396-060c0954332e
728117ed-a723-41fd-8396-060c0954332e  6:28:06 PM: debug Executing 'sendEthernet' refresh
728117ed-a723-41fd-8396-060c0954332e  6:28:06 PM: debug Executing 'refresh()'
728117ed-a723-41fd-8396-060c0954332e  6:27:46 PM: debug Using ip: and port: 8090 for device: 728117ed-a723-41fd-8396-060c0954332e
728117ed-a723-41fd-8396-060c0954332e  6:27:46 PM: debug Executing 'sendEthernet' refresh
728117ed-a723-41fd-8396-060c0954332e  6:27:46 PM: debug Executing 'refresh()'
728117ed-a723-41fd-8396-060c0954332e  6:27:46 PM: debug Executing 'updateDeviceNetworkID'
728117ed-a723-41fd-8396-060c0954332e  6:27:46 PM: debug Executing 'configure()'
728117ed-a723-41fd-8396-060c0954332e  6:27:18 PM: debug Using ip: and port: 8090 for device: 728117ed-a723-41fd-8396-060c0954332e
728117ed-a723-41fd-8396-060c0954332e  6:27:18 PM: debug Executing 'sendEthernet' refresh
728117ed-a723-41fd-8396-060c0954332e  6:27:18 PM: debug Executing 'refresh()'
728117ed-a723-41fd-8396-060c0954332e  6:27:18 PM: debug Executing 'updateDeviceNetworkID'
728117ed-a723-41fd-8396-060c0954332e  6:27:15 PM: debug Hub Port = 39500
728117ed-a723-41fd-8396-060c0954332e  6:27:15 PM: debug Hub IP Address =
728117ed-a723-41fd-8396-060c0954332e  6:27:15 PM: debug Executing 'updated()'

This is where I get stuck every time.

What should I see in the ST app?

Not sure which other info will help you to help me:

  • I noticed that the Network ID of the Device changed to the MAC Address of the ESP2866.
  • I can ping the ESP8066’s IP.
  • Github integration is working fine and there are no updates.
  • The Events Log of the ST Anything Device shows nothing unusual:

ST Anything (Device) Events — displayed | all | from device
Date Source Type Name Value User Displayed Text
2018-02-14 6:16:03.700 PM SAST
37 minutes ago DEVICE numberOfButtons 5 ST Anything number of buttons is 5
2018-02-10 3:28:12.044 PM SAST
4 days ago DEVICE numberOfButtons 0 ST Anything number of buttons is 0
2018-02-10 3:26:51.431 PM SAST
4 days ago DEVICE ALERT childDeviceCreation failed ST_Anything Parent Device has not yet been fully configured. Click the ‘Gear’ icon, enter data for all fields, and click 'Done’
2018-02-10 3:26:08.494 PM SAST
4 days ago DEVICE ALERT childDeviceCreation failed ST_Anything Parent Device has not yet been fully configured.

Sure hope you can spot where I’m going wrong and thanks again for your patience. (Hope I won’t have to say that for much longer)

The number of buttons should be set to 0 (zero) since the sketch I told you to use should only have 1 contact sensor and 1 motion sensor defined in the setup() portion of the sketch.

What you should see in SmartThings is a Parent Device with 2 Child Devices (one for Motion and another for Contact).

If you don’t have those created, then you should

  1. check to make sure the MAC address you entered into the Parent’s configuration is correct. This is the number 1 problem people have.
  2. verify the IP address of your ST Hub is correct in the Arduino sketch.
  3. make sure the Parent Device is assigned to your Location and Hub in the ST IDE (Devices tab)
  4. Verify that you _publish_ed each of the Child Device Handlers in the ST IDE (Device Handlers tab)

By pressing “Refresh” in the Parent Device (inside the ST app on your phone), you should see some Live Logging data in the ST IDE. You should also see the corresponding ‘refresh’ command received by the ESP01 in the Arduino IDE Serial Monitor window.

Check all of those things and let us know what you find…

Thank you Dan, you’ve done it! Now I’m even more excited about the possibility of getting the sensor to work.


Patiently awaiting the next lesson.

(I suppose I owe you an explanation, even though I’m too embarrassed to tell what I had done wrong. I had the MAC address wrong in the app. When I could not get this to work a few days ago, I tried a different ESP8266 to eliminate the possibility of a hardware issue and in the process, got the MAC addresses of the devices mixed up. :anguished:)

1 Like

Hi @ogiewon
I have been playing with the Arduino IDE to see if I can figure out how to add the Sensor to the ST_A Multiples sketch, but without success. I though I’l first try to compile the sketch without doing the configuration part as explained in @Saif76’s library and if I could get that to work, I’ll bother you guys to find out how to configure the sketch for the sensor, but as I say, I did not have any luck. I found a few videos on Youtube dealing with ST_Anything, but they are more about the result than how to set up the sketch.

So, I’ll appreciate some guidance when you have a few minutes to spare.

  • I don’t know how to include the Ultrasonic sensor to the ST_A Multiples sketch. I have a sub-folder (called ST_WaterLevel) under the Libraries folder containing the .h and .cpp files and I though all I had to do, was to add #include <PS_Ultrasonic.h> under // ST_Anything Library, but it fails to compile.

  • Where do I add this: Create an instance of this class in your sketch’s global variable section
    __ /For Example: st::PS_Ultrasonic sensor1(“ultrasonic”, 120, 0, PIN_ULTRASONIC_T, PIN_ULTRASONIC_T); and_

  • And what should these be set to:
    st::PS_Ultrasonic() constructor requires the following arguments
    // - String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name
    // - long interval - REQUIRED - the polling interval in seconds
    // - long offset - REQUIRED - the polling interval offset in seconds - used to prevent all polling sensors from executing at the same time

I hope this will be the last time I have to ask about this.

I’ll take a quick look ar @Saif76’s code to see what it will take to add it to an ST_Anything sketch…


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:

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:
 *  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: ""
 		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() {