How to send HTTP POST to remote device?

Others (MiLight, Hue, Toon etc...)
Post Reply
irongarment
Posts: 18
Joined: Friday 24 November 2017 22:42
Target OS: Raspberry Pi
Domoticz version:
Location: New Zealand
Contact:

How to send HTTP POST to remote device?

Post by irongarment » Tuesday 02 January 2018 9:24

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.

ben53252642
Posts: 524
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: How to send HTTP POST to remote device?

Post by ben53252642 » Tuesday 02 January 2018 10:45

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)?

irongarment
Posts: 18
Joined: Friday 24 November 2017 22:42
Target OS: Raspberry Pi
Domoticz version:
Location: New Zealand
Contact:

Re: How to send HTTP POST to remote device?

Post by irongarment » Tuesday 02 January 2018 11:02

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.

ben53252642
Posts: 524
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: How to send HTTP POST to remote device?

Post by ben53252642 » Tuesday 02 January 2018 11:06

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

irongarment
Posts: 18
Joined: Friday 24 November 2017 22:42
Target OS: Raspberry Pi
Domoticz version:
Location: New Zealand
Contact:

Re: How to send HTTP POST to remote device?

Post by irongarment » Wednesday 03 January 2018 5:52

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.

ben53252642
Posts: 524
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: How to send HTTP POST to remote device?

Post by ben53252642 » Wednesday 03 January 2018 6:04

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

irongarment
Posts: 18
Joined: Friday 24 November 2017 22:42
Target OS: Raspberry Pi
Domoticz version:
Location: New Zealand
Contact:

Re: How to send HTTP POST to remote device?

Post by irongarment » Wednesday 03 January 2018 6:53

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.

irongarment
Posts: 18
Joined: Friday 24 November 2017 22:42
Target OS: Raspberry Pi
Domoticz version:
Location: New Zealand
Contact:

Re: How to send HTTP POST to remote device?

Post by irongarment » Saturday 20 January 2018 10:57

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!

mzy2240
Posts: 3
Joined: Wednesday 27 June 2018 19:11
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: How to send HTTP POST to remote device?

Post by mzy2240 » Wednesday 11 July 2018 19:25

why not build a python plugin for this coffer maker? This is how the python plugin be designed for, right?

h143
Posts: 38
Joined: Tuesday 28 October 2014 23:26
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: How to send HTTP POST to remote device?

Post by h143 » Thursday 04 October 2018 23:14

i have try the sama as you to control my jura in domoticz but i get a bad request response, do i somthing wrong?

irongarment
Posts: 18
Joined: Friday 24 November 2017 22:42
Target OS: Raspberry Pi
Domoticz version:
Location: New Zealand
Contact:

Re: How to send HTTP POST to remote device?

Post by irongarment » Friday 05 October 2018 7:28

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)?

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest