Sonoff DIY non complicated
Posted: Friday 31 December 2021 14:03
Hi, Just wanted to share my experience in activating a Sonoff BasicR3 in domoticz. Looking on the internet I found many complex solutions (Tasmota, MQTT, eWelink ...) all working but complicated. Since 2019 sonoff offers the DIY where you can simply post http requests to control the device.
references : http://developers.sonoff.tech/sonoff-di ... tocol.html and https://sonoff.tech/product-review/prod ... evice-yes/
In fact it comes to 5 actions :
1. put the DIY bridge in the Sonoff device and restart pairing
2. connect to 'http://10.10.7.1/' (password 12345678) and enter your WIFI network and password.
3. restart (link is blinking twice) and find the ipaddress of the Sonoff ESP device (eg. 192.168.178.193)
4. create a dummy switch in domoticz (eg. Plug1)
5. create a script to post request (dzvents example below)
Hope this helps.
dzvents script (devices part is normally sufficient, extended with error check) :
return {
on = {
devices = {'Plug1'},
httpResponses = {'Resp_on', 'Resp_off'}, -- must match with the callback passed to the openURL command
customEvents ={'Check_on','Check_off'},
},
execute = function(domoticz, item)
if (item.isDevice) then
if item.state == "On" then comm = "on" else comm = "off" end
domoticz.openURL({
url = 'http://192.168.178.193:8081/zeroconf/switch',
method = 'POST',
callback = 'None',
postData = {['deviceid']='', ['data']={['switch']= comm }}
})
domoticz.emitEvent('Check_'..comm, domoticz.time.rawTime ).afterSec(1) --check if OK after one second
print("Plug1 command send = "..comm)
end
if (item.isCustomEvent) then
if item.trigger == "Check_on" then comm = "on" else comm = "off" end
domoticz.openURL({
url = 'http://192.168.178.193:8081/zeroconf/info',
method = 'POST',
callback = 'Resp_'..comm,
postData = {['deviceid']='', ['data']={}}
})
print("Plug1 status requested info for status = "..comm)
end
if (item.isHTTPResponse) and (item.isJSON) then -- check after 1 second if error or status switch is different than requested
print("HTTPResponse is "..item.trigger.." , error is "..item.json.error.." status is "..item.json.data['switch'])
if item.trigger == "Resp_on" then comm = "on" else comm = "off" end
if (not item.ok or (item.json.error ~= 0) or item.json.data['switch']~= comm) then
domoticz.openURL({
url = 'http://192.168.178.193:8081/zeroconf/switch',
method = 'POST',
callback = 'None', -- only one retry
postData = {['deviceid']='', ['data']={['switch']= comm }}
})
print("ERROR on Plug1, Retry once : Plug1 command send = "..comm)
end
end
end
}
references : http://developers.sonoff.tech/sonoff-di ... tocol.html and https://sonoff.tech/product-review/prod ... evice-yes/
In fact it comes to 5 actions :
1. put the DIY bridge in the Sonoff device and restart pairing
2. connect to 'http://10.10.7.1/' (password 12345678) and enter your WIFI network and password.
3. restart (link is blinking twice) and find the ipaddress of the Sonoff ESP device (eg. 192.168.178.193)
4. create a dummy switch in domoticz (eg. Plug1)
5. create a script to post request (dzvents example below)
Hope this helps.
dzvents script (devices part is normally sufficient, extended with error check) :
return {
on = {
devices = {'Plug1'},
httpResponses = {'Resp_on', 'Resp_off'}, -- must match with the callback passed to the openURL command
customEvents ={'Check_on','Check_off'},
},
execute = function(domoticz, item)
if (item.isDevice) then
if item.state == "On" then comm = "on" else comm = "off" end
domoticz.openURL({
url = 'http://192.168.178.193:8081/zeroconf/switch',
method = 'POST',
callback = 'None',
postData = {['deviceid']='', ['data']={['switch']= comm }}
})
domoticz.emitEvent('Check_'..comm, domoticz.time.rawTime ).afterSec(1) --check if OK after one second
print("Plug1 command send = "..comm)
end
if (item.isCustomEvent) then
if item.trigger == "Check_on" then comm = "on" else comm = "off" end
domoticz.openURL({
url = 'http://192.168.178.193:8081/zeroconf/info',
method = 'POST',
callback = 'Resp_'..comm,
postData = {['deviceid']='', ['data']={}}
})
print("Plug1 status requested info for status = "..comm)
end
if (item.isHTTPResponse) and (item.isJSON) then -- check after 1 second if error or status switch is different than requested
print("HTTPResponse is "..item.trigger.." , error is "..item.json.error.." status is "..item.json.data['switch'])
if item.trigger == "Resp_on" then comm = "on" else comm = "off" end
if (not item.ok or (item.json.error ~= 0) or item.json.data['switch']~= comm) then
domoticz.openURL({
url = 'http://192.168.178.193:8081/zeroconf/switch',
method = 'POST',
callback = 'None', -- only one retry
postData = {['deviceid']='', ['data']={['switch']= comm }}
})
print("ERROR on Plug1, Retry once : Plug1 command send = "..comm)
end
end
end
}