Eric, I appreciate all of your work with SONOFF (esp8266).
I have a SonoffTH running your BIN and I modified another to use a SR04 sonic range finder, running espeasy R120
I have been trying for that couple of weeks to get the JSON results into ST device, to display the distance measurement. The http://192.168.x.xxx.)/json gets this.
I have made several versions of the device handler many sampling yours and other code.
import groovy.json.JsonSlurper
preferences {
input("ip", "text", title: "IP Address", description: "ip", required: true)
input("port", "text", title: "Port", description: "port", required: true)
input("mac", "text", title: "MAC Addr", description: "mac")
}
metadata {
definition (name: "ESP8266", namespace: "phealt", author: "Peter Healt") {
capability "Refresh"
capability "Sensor"
capability "Polling"
attribute "Distance", "string"
}
// tile definitions
tiles {
valueTile("distance", "device.distance", width: 2, height: 2) {
state("distance", label:'${currentValue}', unit:"cm",
backgroundColors:[
[value: 31, color: "#153591"],
[value: 44, color: "#1e9cbb"],
[value: 59, color: "#90d2a7"],
[value: 74, color: "#44b621"],
[value: 84, color: "#f1d801"],
[value: 95, color: "#d04e00"],
[value: 96, color: "#bc2323"]
]
)
}
standardTile("refresh", "device.distance", inactiveLabel: false, decoration: "flat") {
state "default", label:'6', action:"refresh.refresh", icon:"st.secondary.refresh"
}
main "distance"
details("distance","refresh")
}
}
def installed() {
log.debug "installed()"
configure()
}
def updated() {
log.debug "updated()"
}
def parse(description) {
def events = []
def descMap = parseDescriptionAsMap(description)
def body = new String(descMap["body"].decodeBase64())
def slurper = new JsonSlurper()
def result = slurper.parseText(body)
log.debug result
if (result.containsKey("distance")) {
events << createEvent(name:"distance", value:result.message)
}
return events
}
def configureInstant(ip, port, pos){
return getAction("/config?haip=${ip}&haport=${port}&pos=${pos}")
}
def parseDescriptionAsMap(description) {
description.split(",").inject([:]) { map, param ->
def nameAndValue = param.split(":")
if (nameAndValue.length == 2) map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
else map += [(nameAndValue[0].trim()):""]
}
log.debug "Map"
}
def refresh() {
log.debug "refreshing"
def cmds = []
cmds << getAction("/json")
return cmds
}
private getAction(uri){
updateDNI()
def userpass
if(password != null && password != "")
userpass = encodeCredentials("admin", password)
def headers = getHeader(userpass)
def hubAction = new physicalgraph.device.HubAction(
method: "GET",
path: "/json",
headers: headers
)
return hubAction
}
private postAction(uri, data){
updateDNI()
def userpass
if(password != null && password != "")
userpass = encodeCredentials("admin", password)
def headers = getHeader(userpass)
def hubAction = new physicalgraph.device.HubAction(
method: "POST",
path: uri,
headers: headers,
body: data
)
return hubAction
}
private setDeviceNetworkId(ip, port = null){
def myDNI
if (port == null) {
myDNI = ip
} else {
def iphex = convertIPtoHex(ip)
def porthex = convertPortToHex(port)
myDNI = "$iphex:$porthex"
}
log.debug "Device Network Id set to ${myDNI}"
return myDNI
}
private updateDNI() {
if (state.dni != null && state.dni != "" && device.deviceNetworkId != state.dni) {
device.deviceNetworkId = state.dni
}
}
private getHostAddress() {
if (override == "true" && ip != null && ip != ""){
return "${ip}:80"
}
else if(getDeviceDataByName("ip") && getDeviceDataByName("port")){
return "${getDeviceDataByName("ip")}:${getDeviceDataByName("port")}"
}else{
return "${ip}:80"
}
}
private String convertIPtoHex(ipAddress) {
String hex = ipAddress.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
return hex
}
private String convertPortToHex(port) {
String hexport = port.toString().format( '%04x', port.toInteger() )
return hexport
}
private encodeCredentials(username, password){
def userpassascii = "${username}:${password}"
def userpass = "Basic " + userpassascii.encodeAsBase64().toString()
return userpass
}
private getHeader(userpass = null){
def headers = [:]
headers.put("Host", getHostAddress())
headers.put("Content-Type", "application/x-www-form-urlencoded")
if (userpass != null)
headers.put("Authorization", userpass)
return headers
}
def reboot() {
log.debug "reboot()"
def uri = "/cmd?=reboot"
getAction(uri)
}
def sync(ip, port) {
def existingIp = getDataValue("ip")
def existingPort = getDataValue("port")
if (ip && ip != existingIp) {
updateDataValue("ip", ip)
}
if (port && port != existingPort) {
updateDataValue("port", port)
}
}
When I press the refresh in the ST app on the device I get this in the log…
but I can’t even get the value or even the JSON body in the log, any help would be great.
Thanks