I will post the sample code of home appliance operation by ECHONET Lite with python.
What i want to do
The Ruby version can be found in Controlling ECHONET Lite Home Appliances with Ruby, so please have a look there.
● Air purifier (compatible with ECHONET Lite) ⇒ Sharp KI-EX100 ● Windows 7 ● Python 2.7.11
ip = Please use the IP of your device for the "192.168. ○. ○" part.
ON_send.py
# coding: utf-8
import socket
ip = "192.168.2.166"
ECHONETport = 3610
message = "1081000005FF010135016101800130"
msg = message.decode("hex")
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(msg, (ip, ECHONETport))
OFF_send.py
# coding: utf-8
import socket
ip = "192.168.2.166"
ECHONETport = 3610
message = "1081000005FF010135016101800131"
msg = message.decode("hex")
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(msg, (ip, ECHONETport))
What is different in the above program Only in the message variable on the 7th line
When ON message = "1081000005FF010135016101800130"
When OFF message = "1081000005FF010135016101800131"
With a program that sends a request for the operating status of home appliances The explanation is divided into programs that receive the operating status from home appliances.
get_send.py
# coding: utf-8
import socket
ip = "192.168.2.166"
ECHONETport = 3610
message = "1081000005FF0101350162018000"
msg = message.decode("hex")
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(msg, (ip, ECHONETport))
receive.py
# coding: utf-8
import socket
port = 3610
bufsize = 4096
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("0.0.0.0", port))
data = sock.recvfrom(bufsize)
print data[0].encode('hex')
print data
sock.close()
If you can receive it well, the following message should be displayed The correct information is on the first line. The second line is not encoded in hexadecimal and will not be displayed correctly. I just wanted to tell you something useful because recvform tells you the IP of the source. This time I put the 11th line of the sample code for testing, but please delete it because it is unnecessary code.
By the way, the 9th line of the sample code
data[0].endode("hex")
Seems to work only with python2 series. So I haven't tried 3 series, so please find and execute the encoding method to make it a hexadecimal number by yourself.
If you don't understand what you are saying, such as 3 or 2, execute the following at the command prompt or terminal.
$ python --version
The display is In the case of python 2. ○. ○, it is 2 system In the case of python 3. ○. ○, it is 3 series
Recommended Posts