Page 1 of 1

iKettle / Smarter wifikettle in Domoticz

Posted: Saturday 09 January 2016 18:44
by rogierNL
Received my Raspberry Pi yesterday so I haven't really figured out the way to properly program for Domoticz but I used this script on my Mac to start my wifi water kettle since the iPhone app is a pain in the **s. It is for iKettle 1.0. No idea whether the second generation uses the same script.

Hope you like it, if so, let me know and I'll make sure it's better adapted to the Domoticz environment. Also, I'll clean the code in that case.

You have to reserve the IP address of your iKettle on your router, it's hostname will probably be 'unknown'. Save it as tea.py, fill in the kettle's IP address behind the TCP_IP, make sure it's executable (chmod +x tea.py), add a dummy switch and add this code as script!

For temperatures check: http://www.awe.com/mark/blog/20140223.html Make sure to include the "\n"

Code: Select all

#!/usr/bin/env python
import time
import socket

TCP_IP = 'xxx.xxx.xxx.xxx'
TCP_PORT = 2000
BUFFER_SIZE = 10
INITIATE = "HELLOKETTLE\n"
START = "set sys output 0x4\n" 
TEMP = "set sys output 0x2\n" #Fill in your desired temperature, this is 95 degrees
WARM = "set sys output 0x8\n" #including this one means it will keep it warm for 30 minutes. If you want to get rid of this, also remove it below.

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(INITIATE)
data = s.recv(BUFFER_SIZE)
print "Initiate", data
s.send(START)
data = s.recv(BUFFER_SIZE)
print "Start data", data
time.sleep(1)
s.send(TEMP)
data = s.recv(BUFFER_SIZE)
print "Temp", data
#If you don't want to keep your tea warm, remove until the next comment.
time.sleep(1)
s.send(WARM)
data = s.recv(BUFFER_SIZE)
print "Warm:", data
#Leave the s.close() intact :)
s.close()



Re: iKettle / Smarter wifikettle in Domoticz

Posted: Monday 11 January 2016 17:10
by bink
Nice. Implemented it and works great! Do you know if it's possible to create a virtual switch with multiple input so you can maybe make the temperature setting a variable like for instance the one used for Evohome (It has some sort of "action" button which can set the status to normal/economy/away/...)?
Also tried to sniffer what message gets sent to the app when the kettle has finished boiling so I could maybe get a prowl notification or something but so far I did not see anything.

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Monday 11 January 2016 17:23
by rogierNL
I have no idea whether that's possible for a dummy. If so I will implement it but for now I'm just getting used to the system. The tcp connection gets terminated after it sent the commands. If you would keep it open till you receive the finished command "sys status 0x3" you can send it back to your switch. But if you implement it with a time script I think you will have to ping the kettle every minute. So for now use it as a fixed temperature and maybe even two switches and I'll see what I can as soon as I implemented my routines in my own system:)

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Monday 11 January 2016 20:55
by bink
Thanks for looking into this. Already glad that I can control it from Domoticz. I did find a virtual switch type "selector" where you can define multiple presets. Just haven't found out how that translates to a parameter that you can then supply to the script (maybe this sets a system variable somewhere?)

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Monday 11 January 2016 21:06
by rogierNL
I will look into it!

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Wednesday 27 January 2016 11:32
by karl8754
Nice work Rogier!
I put together a similar Lua script a while back for the same kettle and it was working well (using on/off scripts) until I started playing around with the new Selector Switches so it looks like this:
ket.PNG
ket.PNG (9.64 KiB) Viewed 4614 times
However, I'm having issues getting it working because Domoticz' internal Lua doesn't have sockets included (the on/off scripts use the external (Ubuntu in my case) Lua which does have sockets). Using Python should solve that though. I've never coded in Python before so didn't know enough to build it myself but using your script I think I can probably merge it with mine and come up with something that works (mayyyyybe!)

I'll give it a try and let you know.

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Wednesday 27 January 2016 11:47
by rogierNL
Looks great! I was looking for the selector in the beta but I couldn't even get to an array of buttons. Can you maybe guide me through that, not too elaborate just the core? I did some Python programming in the past so I should get that skill back fairly quick. Since the script currently doesn't loop or call back. It's literally typed in <5minutes haha.
EDIT: Got it! I'll start fooling around with it somewhere during the weekend.

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Wednesday 03 February 2016 16:45
by karl8754
I have this working now - managed to merge my old script and your python code into something that works. It's probably really bad coding (forgive me, I don't know any python!) but it works! If you haven't had chance to look at it yet, let me know and I'll send a copy of my scripts.

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Wednesday 03 February 2016 17:24
by bbqkees
Can someone explain to me why someone would need a remote controlled water cooker? :D

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Wednesday 03 February 2016 17:47
by karl8754
I didn't actually buy it myself (honest!! :D) but it's good in the morning to switch on the kettle from bed / bathroom so the kettle is ready to pour when you get downstairs. Or perhaps when heading home from work you can make sure the kettle is ready for your recovery tea! (Maybe it's just a British thing...? haha)

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Thursday 04 February 2016 9:24
by bbqkees
Perhaps it is.
I have a boiling water tap at home so no need for a WiFi kettle here.

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Saturday 06 February 2016 21:48
by bink
@karl8754. Could you post your script for the multifunction button? Also interested to be able to completely control my iKettle from Domoticz.

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Sunday 07 February 2016 23:04
by karl8754
Sure, here's the code I'm currently using. It's split into two files (one lua and one python); the lua file for handling the selector switch in Domoticz and that calls the python script to connect to the Kettle.

As I've already mentioned this is the first python script I've ever written and I'm pretty new to lua too - so if anyone can improve them, I'm happy to receive a better version! :D

script_device_kettle.lua (domoticz/scripts/lua/script_device_kettle.lua)
  • You'll need to edit the script variable to the location of the python script on your system
  • My virtual device is called 'Kettle' - if yours is not, you'll need to change that
  • Based on a selector switch with buttons saying.. Off | 65 C | 80 C | 95 C | 100 C
  • I've included the code for a 'Warm' button, but haven't used or tested it

Code: Select all

------------------------------
--      iKettle Control     --
-- script_device_kettle.lua --
------------------------------

-- Replace with location of your kettle python script
script = "/home/USER/domoticz/scripts/python/kettle.py"


commandArray = {}

if  (devicechanged['Kettle'] == '65 C') then
	print("Kettle switched on / Set to 65")
	os.execute(string.format("%s 65", script))
	commandArray['Kettle'] = "Off AFTER 300"

elseif (devicechanged['Kettle'] == '80 C') then
	print("Kettle switched on / Set to 80")
	os.execute(string.format("%s 90", script))
	commandArray['Kettle'] = "Off AFTER 300"

elseif (devicechanged['Kettle'] == '95 C') then
	print("Kettle switched on / Set to 95")
	os.execute(string.format("%s 95", script))
	commandArray['Kettle'] = "Off AFTER 300"

elseif (devicechanged['Kettle'] == '100 C') then
	print("Kettle switched on / Set to 100")
	os.execute(string.format("%s 100", script))	
	commandArray['Kettle'] = "Off AFTER 300"

elseif (devicechanged['Kettle'] == 'Off') then
	print("Kettle switched off")
	os.execute(string.format("%s off", script))

elseif (devicechanged['Kettle'] == 'Warm') then
	print("Kettle set to warm")
	os.execute(string.format("%s warm", script))
end

return commandArray
I've also coded into the lua script a 'turn-off' the (virtual) kettle after 300 seconds. This is so the device is switched off in Domoticz after it's finished boiling as these scripts don't do any checking to see if it's actually boiled.


kettle.py (domoticz/scripts/python/kettle.py)
  • You'll need to add the IP address of your kettle

Code: Select all

#!/usr/bin/env python
import time
import socket
import sys #For Command line args

TCP_IP = '192.168.1.XXX' #Change to your Kettle's (fixed) IP.

TCP_PORT = 2000
BUFFER_SIZE = 10
INITIATE = "HELLOKETTLE\n"
START = "set sys output 0x4\n" #Switch on kettle
T_65 = "set sys output 0x200\n" #Set to 65 degrees
T_80 = "set sys output 0x4000\n" #Set to 80 degrees
T_95 = "set sys output 0x2\n" #Set to 95 degrees
T_100 = "set sys output 0x80\n"  #Set to 100 degrees
WARM = "set sys output 0x8\n" #Keep warm for 30 minutes.
OFF = "set sys output 0x0\n" #Turn off kettle

#Grab switch from commmand line
try:
	action = str(sys.argv[1])
except IndexError:
	print "No option provided, unable to continue"
	sys.exit(2);
else: 
	#has one argument, initiate connection
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.connect((TCP_IP, TCP_PORT))
	s.send(INITIATE)
	data = s.recv(BUFFER_SIZE)
	print "Initiate: ", data

	if action == "off":
		s.send(OFF)
	else:
		#Switch on kettle
		s.send(START)
		data = s.recv(BUFFER_SIZE)
		print "Start data", data
		time.sleep(1)

		#Send chosen action to kettle
		if action == "65":
			s.send(T_65)
		elif action == "80":
			s.send(T_80)
		elif action == "95":
			s.send(T_95)
		elif action == "100":
			s.send(T_100)
		elif action == "warm":
			s.send(WARM)
		else:
			print "Unknown argument: (%s). Sending OFF command" % sys.argv[1]
			s.send(OFF)

		data = s.recv(BUFFER_SIZE)
		print "Func:", data
	
	#Close connection
	print "Closing connection"
	s.close()
Hope that helps, happy to try and help out with it, if I can.

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Thursday 11 February 2016 9:24
by bink
Thanks for including the script. It works great! Your scripting skills are way better than mine.. :D Initialy had some trouble to trigger the LUA script until I noticed the "space" between the degrees and the "C". After also putting this into the selector button it triggers perfectly! Now just need to find the nice icon you have in your button... :D

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Thursday 11 February 2016 13:51
by karl8754
Thanks! Glad it worked for you
The icon is one I made myself, well.. edited from another icon into this one, but you can find the zip attached, ready to add to the custom icons bit.

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Sunday 22 May 2016 16:25
by rogierNL
Waited until this weekend to install everything in the selector way, works like a charm. Now I only have to figure out a way in which 'on' functions as well since it isn't supported by my default app, Pilot on iOS.

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Sunday 26 June 2016 0:23
by bxlouis
Hi, I tried it with iKettle 2.0 but it does not seem to work...
Anyone have an idea how I can adjuste the script in order to make it work ?
I can work on it by myself but I would need some help !

Re: iKettle / Smarter wifikettle in Domoticz

Posted: Sunday 26 June 2016 10:17
by rogierNL
You first have to figure out how they communicate. This isn't secured at all so if it's done through telnet you should be able to figure out the commands easily. Read about ARP spoofing through Cain & Able and Wireshark. Use the first to have the communication with your kettle go through your laptop and the second to read the traffic (tip: use IP filters).
Then you will be able to see all the traffic and when sending commands from the smartphone app you should be able to see the appropriate commands for the smarter. Wireshark will also tell you about the ports btw.