Using socket() and bind() [basic transmission questions]

What I’m trying to do is use the ConnectPortX gateway to set up a network with two additional endpoints. I simply want to discover the two devices and send them an 8 bit command.

I’m looking through the and example files and trying to make sense of their code so I can manipulate it to suit my demo application.

I understand most of ex4_disc, in that it simply gives me a two variable array with the nodes and its respective data. The print part is helpful but the list is really what I need.

So I assume that at this point I have enough information to send out data, I have the short and long addresses for my zigbee radios. So moving on to ex2_rw, and the stuff I don’t completely understand; sockets. I was trying to read some basic stuff about Python and sockets but it was a bit over my head with TCP/UDP and that or it wasn’t transparent/evident enough what the code was doing. Now, first the code creates a socket:
sd = socket(‘’,‘’,‘’)

Now I would assume I have to do this for each endpoint:
ep1= socket(‘’,‘’,‘’)
ep2= socket(‘’,‘’,‘’)

Now comes the part that I don’t get

I haven’t really figured out what relevance ‘bind’ has, does it send the end address 0xe8 to the socket I’ve created? So that in my code it would be something like:

The last part of the example code is:
sd.sendto(data, 0, src_addr)

Now data will obviously be my 8 bit code, the 0 is for non-blocking but the problem I have is with src_addr. When looking at the Digi Python handbook I see that the command sendto() the tuple addr format is (addr_string, endpoint, profile_id, cluster_id) and the node class itself contains: type, addr_extend, addr_short, addr_parent,profile_id,manufact_id,label) so my question would be, can I pass just addr_short to it or should I pass everything it wants? Something like:

Or should I set up a ep1_addr = [node.addr_string,0x34,node.profile_id,node.cluster_id]

and call ep1.sendto(8bit,0,ep1_addr) ?

Thanks in advance.

I don’t know if you have figured out your issue yet (I hope so), but I am in about the same place and thought I would share what works for me. I have the endpoint configured with a loop back connector, so that what I send to it gets echoed back. I have also connected a terminal at the endpoint and verified that “Hello, World!” is really being sent out of the remote serial port.

I loaded the library and on my ConnectPort X2 and then wrote my own file and downloaded it as well. I then telnetted to the ConnectPort and typed in “py” to execute my test. The result was a display of all of my Zigbee nodes, followed by “Hello, World!” followed by “All Done Now”. is as follows:

import sys


import zigbee

from socket import *


# Perform a node discovery and print out
# the list of discovered nodes to stdio.

node_list = zigbee.getnodelist()

# Print the table:
print "%12s %12s %8s %24s" % \
("Label", "Type", "Short", "Extended")
print "%12s %12s %8s %24s" % \
("-" * 12, "-" * 12, "-" * 8, "-" * 24)
for node in node_list:
	print "%12s %12s %8s %12s" % \
	(node.label, node.type, \
	node.addr_short, node.addr_extended)

# Use a discovered addresses as endpoint:
DESTINATION=("00:13:a2:00:40:52:a1:c2!", \
0xe8, 0xc105, 0x11)

# Create the socket, proprietary transport:

# Bind to endpoint 0xe8 (232):
sd.bind(("", 0xe8, 0, 0))

# Send "Hello, World!" to the endpoint,
# using profile_id and cluster_id specified in
sd.sendto("Hello, World!", 0, DESTINATION)	    
print response
print "All Done Now"

except Exception, e:
print e

For the most part, this code was pulled from the Digi Python Programming Guide, so I can take no credit for it, but it was not clear to me that I needed or that I needed to extend the path to it to make things work.

I am now trying to figure out how to_socket_addr() is used so that I do not need to hard code the DESTINATION.

If anyone has an example of the usage of to_socket_addr() I would be very thankful.


Hello World!

Here is another new xbee user having problems with bind() and basic transmissions. I have tried simply telnetting the gateway and driving simple python code which attempts to send message to a xbee board with a loopback tester connected to it. The code is as follows:

include the sockets module into the namespace:

import zigbee
from socket import *
print 0
DESTINATION = (“00:13:a2:00:40:34:1a:88!”,
0xe8, 0xc105, 0x11)
print 1

Create the socket, datagram mode, proprietary transport:

print 2

Bind to endpoint 0xe8 (232):

sd.bind((“”, 0xe8, 0, 0))
print 3
sd.sendto(“Hello”, 0, DESTINATION)
print 4

Block until a single frame is received, up to 72 bytes:

response, src_addr = sd.recvfrom(72)
print 5
print response

Telnet says:

Traceback :
File “”, line 21, in ?
File “”, line 1, in bind
socket.error: <22, ‘Invalid argument’>

(line 21 is “sd.bind((”“, 0xe8, 0, 0))” )

If I delete the line 21 Telnet says:

Traceback :
File “”, line 24, in ?
socket.error: <9, ‘Bad file number’>

(line 24 is “sd.sendto(“Hello”, 0, DESTINATION)” )

Since the code is very close to the example one, I have no idea what could be wrong. Does anyone have hints for me what I am doing wrong? The gateway sees the 00:13:a2:00:40:34:1a:88! as a router in the Xbee network.

For 00:13:a2:00:40:34:1a:88! (radio module in Rs232 interface board) in the X-CTU there is
Modem: XB24-B
Function Set: Zigbee Router/END DEVICE AT
Version: 1220

Gateway is
X8 Gateway
Firmware: 0x1142

I have uploaded to the gateway. I also did go trough the link posted above but I didnt find help there.

I appreciate any help or hints, Thanks!


here’s an example of to_socket_addr. It gets the node list and sends a message to all the routers.


Bind to endpoint 0xe8 (232):

sd.bind((“”, 0xe8, 0, 0))

Perform a node discovery:

node_list = zigbee.getnodelist()

for node in node_list:
if node.type == “router”:
print “Yay we found a router!!”
dest = node.to_socket_addr()
print “dest is:”
print dest
print “sending data to router.”
x = sd.sendto(“Hello, router!”, 0, dest)
print “bytes sent:”
print x

I’ve got ConnectPort and XbeePro USB module. After executing aboce script I was hoping to get “Hello world” in X-CTU terminal, but I didn’t get anything.

Q: How to do that and how to receice response?

Hi Kafka,

You may want to start at some of the basic XBee network on the Wiki:

They should help guide you through the send and receive functionality.

Start by confirming that no other Python scripts are running in the system, potentially bound to the same endpoint. The units ship from the factory with a sample script installed. From the Digi CLI, you can execute the “set python” command to determine whether any scripts are enabled to auto-start, and to disable them if they are. A reboot is recommended after changing the “set python” configuration.

Hey again and thank you for the quick response! There was running on gateway by default. I turned it off and now this problem got solved. Thanks again!