How do I add dimmer switch to code


(Bob5731) #1

How do I add dimmer switch to my code that I got from https://github.com/vietquocnguyen/NodeMCU-ESP8266-Servo-Smart-Blinds


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #2

Capability “Switch Level”

http://docs.smartthings.com/en/latest/capabilities-reference.html#switch-level


(Bob5731) #3
/**
 *  NodeMCU Blinds Switch Device Handler
 *
 *  Copyright 2018 Viet Nguyen
 */

import java.security.MessageDigest

preferences {
    input("serverIP", "text", title: "NodeMCU Server IP Address")
}

metadata {
    definition (name: "NodeMCU Blinds", namespace: "smartthings-users", author: "Viet Nguyen") {
        capability "Switch"
        capability "Refresh"
        capability "Polling"
        command "subscribe"
        command "setOffline"
    }

    simulator {}

    tiles(scale: 2) {
        multiAttributeTile(name:"switch", type: "lighting", width: 6, height: 4, canChangeIcon: true){
            tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
                attributeState "on", label:'${name}', action:"switch.off", icon:"st.switches.switch.on", backgroundColor:"#79b821", nextState:"off"
                attributeState "off", label:'${name}', action:"switch.on", icon:"st.switches.switch.off", backgroundColor:"#ffffff", nextState:"On"
                attributeState "offline", label:'${name}', icon:"st.switches.switch.off", backgroundColor:"#cccccc"
            }
        }
        standardTile("refresh", "device.switch", inactiveLabel: false, height: 2, width: 2, decoration: "flat") {
            state "default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh"
        }
		
        main "switch"
        details(["switch","refresh"])
    }

}

def setLevel(value) {
	log.trace "setLevel($value)"
    sendEvent(name: "level", value: value)
    
}

// handle commands
def on() {
    sendEvent(name: "switch", value: 'on')
    apiGet("open")
}

def off() {
    sendEvent(name: "switch", value: 'off')
    apiGet("close")
}

private apiGet(action) {


    log.debug "Posting Body: NODEMCU"
	def result = new physicalgraph.device.HubAction(
        method:     'GET',
        path:       '/'+action,
        headers:    [
            HOST:       settings.serverIP + ':80',
            'Content-Type': 'application/json'
        ],
        body: []
    )

    return sendHubCommand(result)

}

def refresh() {
 	log.debug "Executing NodeMCU Blinds refresh'"
	poll()
}

def poll() {
 	log.debug "Executing NodeMCU Blinds POLL'"
    
    def cmd = new physicalgraph.device.HubAction([
        method:     'GET',
        path:       '/status',
        headers:    [
            HOST:       settings.serverIP + ':80',
            Accept:     "*/*"
        ]
    ], '', [callback: calledBackHandler])
	
    def result = sendHubCommand(cmd)
    
    log.debug "cmd: $cmd"
    log.debug "result: $result"
}

void calledBackHandler(physicalgraph.device.HubResponse hubResponse) {
	def body = hubResponse.json;
    	log.debug "body $body"
        if(body.isOpen == 1){
        	sendEvent(name: "switch", value: 'on')
        }else if(body.isOpen == 0){
        	sendEvent(name: "switch", value: 'off')
        }else{
        	log.debug "OFFFFFFFLINE"
        	setOffline()
        }
}

def updated() {
 	log.debug "Executing NodeMCU Blinds UPDATED'"
    unschedule()
    runEvery1Minute(poll)
}

def setOffline() {
    sendEvent(name: "switch", value: "offline", descriptionText: "The device is offline")
}

(Bob5731) #4

dimmer switch capabilities. The latter is used to set a level (ex. 50%) just like a dimmer.

can some one add give me the code so. I can add the dimmer switch?


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #5

Why not contact the original Author/Developer?

You might get lucky and find someone in the Community who just happens to be familiar with that exact device and/or code, but … the original author is more likely to be able to help.

And/Or study the code and the documentation link provided above.


(Bob5731) #6

I tried contacting the author on GitHub and no response.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #7

It is non-trivial code… because at a glance, it looks like the current code only handles on/off (fully open and fully closed?).

So the MCU needs to be programmed to accept “level” as an additional command and control the servo accordingly.

The SmartThings side is relatively easy. But what happens if the blinds are manually tilted? Does the MCU get feedback from the servo and then send a status event update back to SmartThings so that it can reflect the correct level.


In other words, this is a fun modification idea … but impossible to do without the existing software and hardware already installed and working. Look for who has forked the repo?


(Bob5731) #8

I am adapting this to a Smart vent for air conditioning. I’m not a programmer by any stretch of the imagination


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #9

Oh…

Well: PM me if you want to pay for a custom development contract. Thanks!


#11

This guy will not give up. I have posted to him over a dozen times now. He’s trying to add blinds control to ST_Anything and doesn’t understand how smartthings or Arduino code works. Ridiculous.


(Bob5731) #12

I’m learning Arduino code. But my learning disabilities for making it hard.


#13

I’ve answered your question a dozen times though and you just keep posting the same question over and over and over and over and over.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #14

I can’t fault him for trying, but 1:1 tutoring costs $$. Perhaps someone will volunteer.


(Bob5731) #15

I’m asking for much just a little help.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #16

It doesn’t hurt to ask.

But in my opinion, this is unfortunately more complicated than you think.

If you’re inclined to learn, you’ll find out it isn’t quite trivial - but really satisfying with patience.

I just fear you might not have the patience to absorb that this can’t be done with just a paste of a code snip…


(Bob5731) #17

I’m just trying to get a job done since my damn folks won’t fix my air conditioning. I really need it done. Servo to control air vent.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #18

(Bob5731) #19

I do not have $3,000 to spend. They are my landlord.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #20

Each Keen vent costs $150 or less.

You have 20 vents in your apartment?


(Bob5731) #21

that’s what Keen told me.
EcoVince told me the same thing.

That was for all the hardware needed.

I have 7 Vince

But I got my quote a year ago.
My budget is $200 to complete the project that includes all hardware needed.