Difference between revisions of "Code Snippets"

From piMyHome Project
Jump to navigation Jump to search
Line 30: Line 30:
 
# -*- coding: utf-8 -*-
 
# -*- coding: utf-8 -*-
 
import socket
 
import socket
 
+
# IP address and port to connect to the gateway, please
+
gateway_host = "192.168.60.201"     # set here the IP of your gateway
# change these values according to your environment
+
gateway_port = 20000               # set here the port of your gateway
default_gateway_host = "192.168.60.201"
+
default_gateway_port = 20000
+
gateway_addr_port = gateway_host, gateway_port
 
+
gateway_addr_port = default_gateway_host, default_gateway_port
 
 
 
 
def monitor():
 
def monitor():
 
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Line 43: Line 41:
 
         sock.connect(gateway_addr_port)
 
         sock.connect(gateway_addr_port)
 
         data = sock.recv(1024)
 
         data = sock.recv(1024)
        # expect ACK from gateway
+
         if data != "*#*1##":       # expect ACK from gateway
         if data != "*#*1##":
 
 
             raise Exception("Did not receive expected ACK, but: "+data)
 
             raise Exception("Did not receive expected ACK, but: "+data)
        # Switch session to MONITOR mode
+
         sock.send("*99*1##")       # Switch session to MONITOR mode
         sock.send("*99*1##")
 
 
         data = ""
 
         data = ""
 
         while 1:
 
         while 1:
            # Read data from MyHome BUS
+
             next = sock.recv(1024) # now read data from MyHome BUS
             next = sock.recv(1024)
 
 
             if next == "":
 
             if next == "":
 
                 break              # EOF
 
                 break              # EOF
Line 60: Line 55:
 
             if data[0] != "*":
 
             if data[0] != "*":
 
                 raise Exception("Message does not start with '*': "+data)
 
                 raise Exception("Message does not start with '*': "+data)
             msg = data[1:eom]
+
             msg = data[0:eom+2]     # message is from position 0 until end of ##
             data = data[eom+2:]
+
             data = data[eom+2:]     # next message starts after ##
            # print the stripped message without leading * and trailing ##
 
 
             print msg
 
             print msg
 
     finally:
 
     finally:
 
         sock.close()
 
         sock.close()
 
+
monitor()
+
monitor()                           # start the monitor
 
</source>
 
</source>

Revision as of 12:56, 17 November 2014

Sending messages with netcat

The Linux command netcat makes it very easy to send messages from a Raspberry Pi to a Bticino Gateway. The syntax is:

pi@raspberry ~ $ echo "<message>" | netcat <gateway-IP> <gateway-port>

For example: turn off light 94 (9.4) on interface 5:

pi@raspberry ~ $ echo "*1*0*94#4#05##" | netcat 192.168.1.55 20000

A Python Monitor Session with Bticino Gateway

To understand the OWN messages better, you should start a monitor session on your Raspberry Pi and watch the OWN messages flying in from your home automation system. Analyzing these messages gives you an idea how it works. Below is a simple monitor script in Python.

For Raspberry Pi

Start editor an editor (in our example nano) and create monitor.py

nano monitor.py

Copy and paste the script below into that file and save it. After you saved the file, you have to make the file executable:

chmod 755 monitor.py

Now start the monitor and watch the messages fly by. Exit with CTRL-C.

./monitor.py

For Windows

Download and install Python for Windows and create the file monitor.py and edit with an text editor. Copy and paste the source code below in that file and save it. Then just double click monitor.py.

The Python Script monitor.py

Copy the content of this script to monitor.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
 
gateway_host = "192.168.60.201"     # set here the IP of your gateway
gateway_port = 20000                # set here the port of your gateway
 
gateway_addr_port = gateway_host, gateway_port
 
def monitor():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        sock.connect(gateway_addr_port)
        data = sock.recv(1024)
        if data != "*#*1##":        # expect ACK from gateway
            raise Exception("Did not receive expected ACK, but: "+data)
        sock.send("*99*1##")        # Switch session to MONITOR mode
        data = ""
        while 1:
            next = sock.recv(1024)  # now read data from MyHome BUS
            if next == "":
                break               # EOF
            data = data + next
            eom = data.find("##")
            if eom < 0:
                continue;           # Not a complete message, need more
            if data[0] != "*":
                raise Exception("Message does not start with '*': "+data)
            msg = data[0:eom+2]     # message is from position 0 until end of ##
            data = data[eom+2:]     # next message starts after ##
            print msg
    finally:
        sock.close()
 
monitor()                           # start the monitor