Page 1 of 1

How to send HTTP POST to remote device?

Posted: Tuesday 02 January 2018 8:24
by irongarment
Hi all,

I have searched for an answer, but nothing came up (and 'post' is a very common word...).

I have a device that can be switched on or off by sending an HTTP POST to a URL. I want a switch in Domoticz that I can click on and off which will send the two different POST payloads for 'on' and 'off'. How can this be done?

I created a dummy device with a virtual switch, but it only offers me a simple HTTP URL for on and off actions. Apologies if the answer is simple, but to me it's not obvious.

Thanks.

Re: How to send HTTP POST to remote device?

Posted: Tuesday 02 January 2018 9:45
by ben53252642
Use CURL in a Linux bash script and have Domoticz run the script when you turn the device on or off.

The answer is definitely not simple, if you cannot find a guide for controlling these devices via CURL, depending on your skill it could take you hours to figure out.

You haven't mentioned what sort of device, the way it is controlled (via browser or phone app) and how you know it can be controlled by HTTP post (is there a guide)?

Re: How to send HTTP POST to remote device?

Posted: Tuesday 02 January 2018 10:02
by irongarment
Hi there,

Thanks for that. Now that I know it's not a built-in function in Domoticz and I have to do it via curl in a script then that actually makes it easier.

It's a coffee machine with an ESP8266 grafted onto it:
https://github.com/hn/jura-coffee-machine

I'll write a quick script and do it that way.

Thanks.

Re: How to send HTTP POST to remote device?

Posted: Tuesday 02 January 2018 10:06
by ben53252642
Depending on the complexity you may be able to do it from within a LUA event:

Example:

Code: Select all

commandArray = {}
if (devicechanged['Smooth Jazz'] == 'On')  then
    os.execute ('curl -s "http://192.168.0.5:5005/preset/smoothjazz"')
end
return commandArray
Be sure to post the script afterwards to add to our collection of devices that can be integrated with Domoticz.

Cheers

Re: How to send HTTP POST to remote device?

Posted: Wednesday 03 January 2018 4:52
by irongarment
Ok, thanks to your hints I have something that should work. The tricky thing is that to turn on the machine I want to send it a command to turn on, then a minute later send it another command to rinse itself. Then it's ready.

1) Created a dummy device called 'Jura interface'
2) Created a virtual sensor of type 'switch'
3) Set up the switch, name 'Jura', switch type 'On/Off', switch icon 'Generic', protected - yes.
4) Log in to the Pi running Domoticz and change to the ~/domoticz/scripts/lua directory.
5) Create a script for turning the machine on, followed by a flush, using the Unix at command:

jura_on_flush.sh

Code: Select all

curl -d "AN:01" "http://192.168.1.74/api";
sleep 60;
curl -d "FA:0B" "http://192.168.1.74/api"
6) Create Lua script to take the correct action when the machine is turned on or off:

script_device_jura.lua

Code: Select all

commandArray={}
if (devicechanged['Jura'] == 'On') then
    os.execute ('at now -M -f /home/pi/domoticz/scripts/lua/jura_on_flush.sh')
end
if (devicechanged['Jura'] == 'Off') then
    os.execute ('curl -d "AN:02" "http://192.168.1.74/api"')
end
return commandArray
7) Finally, configure the timer for the switch to come on at 06:00 and off at 22:00. The switch is protected, so it cannot be pressed manually, but the timer 'presses' it. The icon state will be out of step if someone turns the machine on or off manually, but I'll deal with that later.

It's a bit ugly, but it should work. I haven't plugged the Wemos D1 Mini into the coffee maker yet, however I can see the activity LED on the module light up when commands are received.

Re: How to send HTTP POST to remote device?

Posted: Wednesday 03 January 2018 5:04
by ben53252642
This might be of help, I use it to operate my blinds for 19 seconds to get them to the desired location such that they are raised just enough that my cat can't reach them.

if (devicechanged['Blinds Cat Mode'] == 'On') then
commandArray['Living Room Left Blind'] = 'On FOR 19 SECONDS'
commandArray['Living Room Right Blind'] = 'On FOR 19 SECONDS'
end

Re: How to send HTTP POST to remote device?

Posted: Wednesday 03 January 2018 5:53
by irongarment
Yes, but I'd need something that sends a command, waits 60 seconds, then sends another command. As far as I know Domoticz is blocking, so control would not return to Domoticz until the second command has completed.

Anyway, it's all good for now until I find something better.

Thanks.

Re: How to send HTTP POST to remote device?

Posted: Saturday 20 January 2018 9:57
by irongarment
Just to remind my future self, for the Jura Impressa E75 the rinse command is "FA:02". I have amended my script and wired up the Wemos D1 Mini Pro to the coffee machine. Tomorrow morning I'll know if it worked.

Edit: It worked!

Re: How to send HTTP POST to remote device?

Posted: Wednesday 11 July 2018 19:25
by mzy2240
why not build a python plugin for this coffer maker? This is how the python plugin be designed for, right?

Re: How to send HTTP POST to remote device?

Posted: Thursday 04 October 2018 23:14
by h143
i have try the sama as you to control my jura in domoticz but i get a bad request response, do i somthing wrong?

Re: How to send HTTP POST to remote device?

Posted: Friday 05 October 2018 7:28
by irongarment
Is it the same model of Jura? I have a Jura Impressa E75.

If you look at the source code you can see what causes a "Bad request" error. It happens around line 96 when the command is less than three characters. Perhaps you are sending a very short command (or no command)?