Question for The Groovy guys & Hackineers


(Robert Rhea) #1

I am working on developing the code for my first project - A Smart Washing Machine Conroller that does the following:

  1. Has a moisture sensor
  2. Has two solenoid valves to shutoff the water to the machine should moisture be detected (these are fail safe valves so they are normally closed)
  3. has a relay to enable the box to control power to the washer (Power will be normally ON)
  4. An induction sensor to determine if power is flowing to the washer to turn on water when needed

The project is based on a Arduino Uno. The Arduino code is a snap. This is my first time doing anything in Groovy / SmartThings IDE. My Arduino sends back a string formatted like so (This is from a simple test I am running to display the voltage drop across a circuit, I measure the voltage using one of the analog inputs and then either send an OK signal or the actual voltage if it is beyond some tolerance):

name:voltage,value:%d.%02d,isStateChange:true,displayed:true

In my Smart Things device Type, I thought it would be simple to just parse that into a map, but it proved much more challenging than I believed. Here is what I came up with that finally worked:

// parse events into attributes
def parse(String description) {
    def strMsg = zigbee.parse(description)?.text
    def lstMsg = strMsg.tokenize(",")
    def mapMsg = [:] as Map
    lstMsg.each(){
        def strTxt = it
        def strSplitTxt = strTxt.tokenize(":")
        mapMsg[(strSplitTxt[0])] = strSplitTxt[1]
	}
    switch (mapMsg.name) {
	// TODO: handle 'switch' attribute
    	case "switch":
        	log.debug "Switch Event Received, Creating Event"
        	break
	// TODO: handle 'water' attribute
    	case "water":
        	log.debug "Water Event Received, Creating Event"
        	break
	// TODO: handle 'contact' attribute
    	case "contact":
        	log.debug "Contact Event Received, Creating Event"
        	break
	// handle 'voltage' attribute
		case "voltage":
    		log.debug "Voltage Event Received, Creating Event"
    		sendEvent(
				name: mapMsg.name,
				value: mapMsg.value,
				unit: "V",
				handlerName: mapMsg.name,
				isStateChange: mapMsg.isStateChange,
				displayed: mapMsg.displayed
            )
            break
    }
}

So - have at it and tell me How I could have done this better and some WHY’s with it. All feedback is much appreciated.


(Patrick Stuart [@pstuart]) #2

Hard to provide feedback. If it works, it works. Nothing really that could be optimized too much here.

Code is like art, if you want to you can pick apart every brush stroke, but the reality is, you just have to take a step back, enjoy the hard work of another and appreciate the fact that you can see the finished piece.

There are different ways one could have tackled this, but yours works just fine. Code optimization is an art, finding ways to refactor code to use less memory, execute faster, etc. are almost pointless with Smartthings, cause we have no visibility on code execution time, resources used, etc. So no benchmarks, means no ability to test change effectiveness.


(Beckwith) #3

@Robert_Rhea

I’m curious what specific hardware you are using, what moisture sensor, solenoid, power relay and induction sensor.


(Robert Rhea) #4

@beckwith

Most of it I am building myself (Unarchiving my old soldering skills, LOL). I will post some design documents as soon as I get them finalized and everything working. For the store-bought subassemblies here is what I am using:

  1. 2 ea Valves - ROB-10456 from SparkFun
  2. 1 ea Current Sensor - SEN-11005 also from SparkFun

The moisture sensor I am building myself, basically just the ends of two RCA plugs in a housing I milled out of a block of plastic with my Dremel. Current to one side, a resistor and voltage read to one of the Analog ports on the Arduino. I haven’t determined yet if I am going to trip the alarm based on an analog or a digital readout from the pin. It will depend on some field testing and calibration once I get the breadboard prototype built. It will depend on how much noise there is when doing the Analog read on the Arduino. I have already noticed that it is quite noisy - its always reading something with just the probe in air - up to 2.5-3 volts sometimes. I will have to look at that again once its connected to household current and see about setting or managing the reference voltage on those pins.

Right now I am working on building the power supply assembly. I have completed the LCD assembly (The device will have a 16x2 LCD on the front to display status messages in case ST is inaccessible, and logic to override the system) and my prototype board enough that I can start rough cutting the controller and device handler programs. The rest of my parts should be in this week.


(Beckwith) #5

@Robert_Rhea

I’ve had back luck with water shut-off solenoids. The plastic case would crack or the valve would stick over time. Hopefully, they’ve improved them since my attempts years ago.

For a whole house shut off valve I use the FortrezZ ball valve. It is real expensive but I couldn’t find a good alternative to handle my hard water well.


(Robert Rhea) #6

The same company that makes this valve also makes some pretty nice brass versions. I can always upgrade to those later if I need to. For this project since theres a small space and two valves a servo actuated project like the FortrezZ was just too clunky. I need something more compact.

When I get around to doing my whole house shutoff valve, I will look at something like that.