I am trying to set up a network of multiple nodes, I am receiving the different analog reading from the two end devices on the network. When I discover them both they have their separate MAC address are few and their Node ID that I configured to them. However, when I use the remote device in the io_sample_callback function it always output the same node ID. I need help! Side note I am using XBee 2 modules not the Xbee 3. Please help, the following is the code and a screenshot of output:
Beginning of Code***
import serial
import time
from digi.xbee.devices import XBeeDevice
from digi.xbee.io import IOLine, IOMode
import mysql.connector
import datetime
import csv
import MySQLdb
Serial port on Raspberry Pi
SERIAL_PORT = “/dev/ttyS0”
BAUD rate for the XBee module connected to the Raspberry Pi
BAUD_RATE = 9600
Analog pin we want to monitor/request data
ANALOG_LINE1 = IOLine.DIO1_AD1
ANALOG_LINE2 = IOLine.DIO2_AD2
ANALOG_LINE3 = IOLine.DIO3_AD3
Sampling rate
SAMPLING_RATE = 15
Get an instance of the XBee device class
device = XBeeDevice(SERIAL_PORT, BAUD_RATE)
Method to connect to the network and discover the nodes
def discover_nodes():
“”“Get a list of the nodes (node ids) on the network
Returns:
“””
# Request the network class and search the network for the remote node
xbee_network = device.get_network()
xbee_network.start_discovery_process()
print(“Discovering network”, end=‘’)
while xbee_network.is_discovery_running():
print(“.”, end=‘’)
time.sleep(0.5)
print(“done.”)
devices = xbee_network.get_devices();
node_ids= []
for dev in devices:
print(“Found {0} at {1}.”.format(dev.get_node_id(), dev.get_64bit_addr()))
node_ids.append(dev.get_node_id())
if not node_ids:
print(“WARNING: No nodes found.”)
return node_ids
Method to connect to the network and get the remote node by id
def get_remote_devices(remote_node_ids):
“”“Get the remote node from the network
Returns:
“””
# Request the network class and search the network for the remote node
xbee_network = device.get_network()
remote_device = xbee_network.discover_devices(remote_node_ids)
if remote_device is None:
print(“ERROR: Remote node id {0} not found.”.format(remote_id))
exit(1)
#remote_device.set_dest_address(device.get_64bit_addr())
#remote_device.set_io_configuration(ANALOG_LINE1, IOMode.ADC)
#remote_device.set_io_sampling_rate(SAMPLING_RATE)
Method to get the data when available from the remote node
def io_sample_callback(sample, remote, time):
address = str(remote.get_64bit_addr())
# Get the raw temperature value
raw_temp = sample.get_analog_value(ANALOG_LINE1)
# Save results in the table
short_addr = address[-4:]
# Get the temperature in Celsius
temp_c = (raw_temp / 1023.0 * 1.25 - 0.5) * 100.0
# Calculate temperature in Fahrenheit
temp_f = ((temp_c * 9.0) / 5.0) + 32.0
print(" Temperature is {0:.2f}C. {1:.2f}F from node {2}".format(temp_c, temp_f,remote.get_64bit_addr()))
timenow = datetime.datetime.utcnow()
cur.execute(‘’‘INSERT INTO test(date_time, analog_1, analog_2, analog_3, id) VALUES(%s,%s,%s,%s,%s);’‘’,(timenow,sample.get_analog_value(ANALOG_LINE1),sample.get_analog_value(ANALOG_LINE2),sample.get_analog_value(ANALOG_LINE3),remote.get_node_id()))
db.commit()
Connect to database server
try:
print(“Connecting to MySQL…”, end=‘’)
db = MySQLdb.connect(host=“localhost”,user=“team1”, passwd=“team1”,db=“singletemp”) #connects to MySQL/MariaDB
cur = db.cursor()
print(“done.”)
except mysql.connector.Error as e:
raise Exception(“ERROR: Cannot connect to MySQL Server!”)
exit(1)
try:
# Read and save temperature data
print(“Welcome to example of storing data from a set of remote TMP36 sensors in MySQL!”)
device.open() # Open the device class
# Get the nodes on the network
remote_node_ids = discover_nodes()
# Setup the remote device
get_remote_devices(remote_node_ids)
device.add_io_sample_received_callback(io_sample_callback)
# Register a listener to handle the samples received by the local device
while True:
pass
except KeyboardInterrupt:
if device is not None and device.is_open():
device.close()
Disconnect from the server
try:
db_conn.disconnect()
except:
pass
END of CODE**
OUTPUT:
Python 3.7.3 (/usr/bin/python3)
>>> %Run pi_xbee_mysql.py
Connecting to MySQL…done.
Welcome to example of storing data from a set of remote TMP36 sensors in MySQL!
Discovering network…done.
Found South Node at 0013A20041E57931.
Found North Node at 0013A20041E57938.
Temperature is -50.00C. -58.00F from node 0013A20041E57931
Temperature is 23.31C. 73.96F from node 0013A20041E57931