socket zigbee cluster id and endpoint fields, things when using threads

Hi people
I´ve been testing a CPX8 Gateway for a system that I´m creating.
I need to keep checking a signal but at the same time I need to do other task with the Xbee module. Problem is that I need to do both tasks simultaneously.
I´ve read about using threads in this job, but I have also read about some care to keep in mind when using this service. I´d like to know what cares must I have when using threads in the gateway, this question is because I´m not an expert on this. and what could happen to CPX8 gateway if I make a mistake in programming? some important damage? sorry if this question is so stupid but I really need to know what could happen.
Other question is about some fields in the tuple to address packets of information using sockets in python. the format of tuple is like this (address, endpoint, profile_id,cluster_id). what do the cluster id, profile id and endpoint fields mean? For what are they useful? Can I do the job that I´ve explained before using this fields? How?
According to what I´ve read, an endpoint is a distinct task or application that runs on an Zigbee device and a cluster is a function on a device but I don´t know how to use this information to make the job done.I didn´t understand this information completely

that´s all, I really hope you can answer and help me. I´ll thankful. Have a good day. seeya

Here is a good resource for information about Python running on Digi products:

Hopefully someone more knowledgeable in Python programming can answer your more specific questions.


The danger of using threads in the python environment on a CPX8 are few issues that threads on any platform have to face plus one additional issue. None of these if not taken care of will result in damage to your product that a reboot won’t fix.

One common issue would resource sharing and locking. How to make sure you’re the only thread manipulating an object at that moment in time. For this, mutex’s and locks from the python guide will solve this.

Another one would be exception handling and good error reporting. Doing both will greatly ease in debugging your code.

And the issue that is only on the Digi platform is if the script or application exits with a currently active child thread, the device will reboot. This means when you want to shut down, you need to take care of all the child threads.

Some important libraries to look at:

threading (For the threading.Thread class object and Lock object)
Queue (For safe resource sharing)

These standard libraries are supported in the Digi python environment.

Using a few threads is incredibly useful - the error detection issue is critical because any faults in the child become difficult for the parent to detect & you end up with a half-running system until you reboot. Also, threads eat lots of memory - assume 50K or more per thread, so don’t expect to run 50 threads.

Keep the threads small and to the point - for example, if you want a thread to watch a serial port and the main code to do network things, keep the serial thread simple and to the point. Have to read/write the serial port and use the “Queue” module to pass the data to the parent. Let the main code/parent do all the complex protocol parsing because that’s where you’ll suffer the most bad-data faults. This also makes your thread small & reusable.

Also, use “too many” try/expect in the thread. Be paranoid. Your goal should be to run and forget your child thread; that it just does it’s work. In fact, with very little work you can do a serial-port thread which runs on both a Digi gateway and PC using a boolean flag to if-then-else the port access.

if( sys.platform.startswith(‘digi’)):
__# then we are running on a Digi gateway
__digiSerial = True
__# else is Windows - pyserial also works under Linux
__# print ‘running on Windows’
__from serialwin32 import *
__digiSerial = False

(Of course you need to do more work! Don’t think setting this digiSerial value does anything useful! :slight_smile: It is just a boolean value.)

Message was edited by: lynnl

Message was edited by: lynnl

Hi everyone and thanks for answer.
What I´ve understood from you is that worst thing could happen to CPX8 if I make a mistake in programming threads is solve just with a reboot.(which makes me feel calm).
Also what I´ve understood is that the big problem in threads is the close the main before all the child and I must put special care in handling exceptions. I know that python doesn´t let to close threads with specific commands but using some other ways like breaking a loop, or with flags etc, I´d like you to share with me your way of treat or deal with this issue, that´s because I´d like to watch other points of view of this and choose one or improve my own code.
Other important thing that I want to mention is about if I can do two jobs at the same time using just 1 Xbee module. What I´ve read that 1 zigbee socket can just manage 1 connection per time, but I found what I comment last post about de cluster ID or endpoint services. I know that threads could manage process “simultaneously” but a gateway level because the python compiler. My question is if I can assign and specific task to an endpoint or cluster and other task to another cluster or endpoint to manage multitask in the same Xbee module without blocking the program.
Other question is if I can create just 1 or more than that Zigbee sockets in Gateway.
I ask you this because I tried to create two Zigbee sockets, to manage two Zigbee modules separately, logically to every socket I gave different tuples (with different address) but with the same cluster id,endopoint and profile id, and the compiler gave me an error.

All these questions go to make a program that can manage using threads in the gateway, different services (like checking a signal constantly or that a signal it´s received constantly for the Xbee module which blocks the socket for sending information or do other tasks,sending data, etc) using the same Xbee module if it can. I think for what I read that it could be possible using endpoint or cluster id options but I´m not sure but I hope you know the answer or you can give me some options to do this.

In advanced thanks for answer me and I hope you can help me with this issue. seeya.

hay all,
I need to send and receive data using gateway from a xbeee transceiver connected by a serial port with a pic, but the module-( attached above)- i found in order to do this function is not working properly, so please any body can help me to be to create communication between gateway and xbee module connected to pic.

thank you

My method of stopping a child (for example servicing a socket with select() or reading a serial port) is to include within the object/class which is the child a parameter called “self.please_stop = False”.

So the loop in the thread’s run is:

while True:
__x = do_something which breaks or wakes every 5-10 seconds
__if self.please_stop:

__Do the main loop stuff

HOWEVER, in a Digi gateway stopping a python app is not as predictable as just rebooting.

But to stop, the parent sets the child.please_stop=True within each child, which is probably blocked in the select() or other. Then the parent does a child.join(), which never succeeds if the child is really blocked. So you need the child to notice the self.please_stop and exit. There may be a fancier way to “kill” a child, but this method is graceful and works for me.

But I usually just reboot if I need to restart so that everything is fresh and new.

thanks lynnl for your opinion.
but you let me thinking about one thing.
In part when you said “HOWEVER, in a Digi gateway stopping a python app is not as predictable as just rebooting” you mean that it´s not good stopping threads, or it´s not good stopping the entire app because the non predictable behavior.

and again thanks for you advices.

Now about using the options cluster id, profile id, endpoint, Have you ever used them? Do you know how to configurate them?
The app that I´m writing has to keep receiving a signal from a microcontroller by the XBee module through a zigbee socket and at the same time It must be able to send information through same XBee module, all these jobs must be done simultaneously, Of course I know that in programming, threads allow me to do that, but the real problem is on the XBee module what I know I can just manage 1 job per time, since that job takes the “channel” and doesn´t allow other tasks to be done. At least when I made a test before, I tried to send from two XBee modules simultaneously to the gateway and got an exception in python.
I wonder if this job can be done using endpoint, cluster id, or profile id fields, so I pass this question to you, or if there´s another way please you can mention it.
I also know that I can manage the “channel” by code in python, like using channel control, but using this I´m doing the program to run in a sincronized way, What I want to know if I can do this but unsincronized.

Any process can interrupt and send data in any time without blocking and without using channel control that´s what I need.

Thanks all of you for answering me, and sorry if I´ve been annoying.
I´ll be really thankful if you can help.

PD lynnl thanks for all your answers

I design my threads to be stopped only because most of my code runs on either a PC or an X4 gateway. However on the gateway I always reboot if the application does stop to make sure the Python environment is fresh and predictable.

I do NOT want to waste my time debugging errors which occur because I restarted the application and something “old” was consuming resources. So I reboot to always start the same.

If your code is only designed to run on the X8, then I would use a reboot to ‘stop’ the code and I would not waste my time trying to allow it to shutdown gracefully.