Best way to control devices

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
cherowley
Posts: 65
Joined: Tuesday 24 February 2015 15:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Best way to control devices

Post by cherowley »

Hi!

What's the best way to send http control command to devices in lua scripts?

I'm currently using "openurl"

My devices often are unreachable (bad wifi, chip crash etc)

I think this eventually is making domoticz unstable.

There must be a way for domoticz to control devices that doesn't lead it to eventually crash when those devices are unreachable surely?

(By crash I mean the web ui stops responding, the error log then starts showing domoticz folders as missing etc)

Example of scripts:

commandArray = {};
IP = '192.168.2.170';
Port = '80';
GPIO = 3; -- r=0,g=1,b=2,w=3
DomDevice = 'Bathroom Ceiling Lamp'

if devicechanged[DomDevice] then
if(devicechanged[DomDevice]=='Off') then
print("Turning OFF with openurl " .. DomDevice)
commandArray['OpenURL']="http://" .. IP .. ":" .. Port .. "/control?cmd=PWMFADE," .. tostring(GPIO) .. ",0,1"
elseif (devicechanged[DomDevice]=='On') then
print("Turning On with openurl value : " .. tostring(uservariables[DomDevice]))
otherdevices_svalues[DomDevice] = tostring(uservariables[DomDevice])
commandArray['OpenURL']="http://" .. IP .. ":" .. Port .. "/control?cmd=PWMFADE," .. tostring(GPIO) .. "," .. tostring(tostring(uservariables[DomDevice]*10+0)) .. ",1" -- new dimmers now do 0-100!
else
print("Value openurl sent from domoticz is : " .. otherdevices_svalues[DomDevice])
uservariables[DomDevice] = otherdevices_svalues[DomDevice]
commandArray['OpenURL']="http://" .. IP .. ":" .. Port .. "/control?cmd=PWMFADE," .. tostring(GPIO) .. "," .. tostring(tostring(uservariables[DomDevice]*10+0)) .. ",1" -- new dimmers now do 0-100!
commandArray['Variable:'..DomDevice] = uservariables[DomDevice]
end
end;
return commandArray


commandArray = {};
DomDevice = 'House Thermostat';
IP = '192.168.2.180'
Port = '80'

function send(msg)
runcommand = "echo "..string.char(34).."POST "..msg.. " HTTP/1.1"..string.char(13)..string.char(10)..string.char(13)..string.char(10)..string.char(34).."| telnet "..IP.." "..Port.." "
print (runcommand)
os.execute(runcommand)
end

if devicechanged[DomDevice] then
commandArray['OpenURL']="http://" .. IP .. ":" .. Port .. "/control?cmd=thermoset," .. tostring(otherdevices_svalues[DomDevice])
print("Thermoset Rads Set :" .. tostring(otherdevices_svalues[DomDevice]))


end
return commandArray


Cheers for any ideas!
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Best way to control devices

Post by Egregius »

I would think you need to set a timeout for the requests and act upon that.
Don't know about lua, in PHP you can set that for either file_get_contents and curl:

Code: Select all

function curl($url){
  $headers=array('Content-Type: application/json');
  $ch=curl_init();
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE);
  curl_setopt($ch,CURLOPT_TIMEOUT,5);
  $data=curl_exec($ch);
  curl_close($ch);
  return $data;
}
$ctx=stream_context_create(array('http'=>array('timeout' => 7)));

//usage:
curl('urltoget');
file_get_contents('urltoget',false,$ctx);
But complaining that folders are missing shouldn't have anything to do with it.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest