Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
XBee Gateway Python app string-to-number conversion
# Add "my_data" to the data_topics list
# https://github.com/digidotcom/xbgw-app/blob/master/xbgw/xbee/manager.py#L91
data_topics = ["xbee.analog", "xbee.digitalIn", "xbee.serialIn", "my_data"]
# Or, change xbgw_main.py to call `dcrep.start_reporting("my_data")`.
# ...
# Use this in place of the handle_serial function in manager.py:
# https://github.com/digidotcom/xbgw-app/blob/master/xbgw/xbee/manager.py#L156
def handle_serial(self, addr, data):
logger.debug("Received serial data from %s: %s", addr, data)
# Assumes data looks like "temperature/humidity",
# both as floating point numbers.
try:
temp_str, humid_str = data.split("/")
temp = float(temp_str)
humidity = float(humid_str)
except ValueError:
# If the value can't be parsed, skip.
logger.exception("Failed to parse temperature/humidity")
return
# Send the temperature and humidity to the rest of the system.
# By adding "my_data" to `data_topics`,
# or by calling start_reporting in xbgw_main.py,
# the Digi Remote Manager reporter component will upload this data! :)
# By setting ident to (addr[0], "temperature") like this,
# the datapoints created will have a name like
# 00000000-00000000-00409DFF-FF123456/my_data/[00:13:A2:00:12:34:56:78]!/temperature.
# Since we converted the values to float, we will upload the data as type DOUBLE.
# Ints would be type INTEGER. If we kept them as strings, then they will be uploaded
# like our normal serial data (strings, which will be base64-encoded depending on the
# application settings), which cannot be graphed by Digi Remote Manager.
#
# Instead of handling the data from the remote XBee device as "serial" data,
# you could send to another endpoint/cluster/profile combination,
# but handling the data would then require development of a new "event manager"
# component for data on that endpoint. Observe that xbgw/xbee/manager.py binds
# a socket to endpoint 0xE8.
pubsub.pub.sendMessage("my_data",
ident=(addr[0], "temperature"),
value=temp)
pubsub.pub.sendMessage("my_data",
ident=(addr[0], "humidity"),
value=humidity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.