Transmitting Data from Xbee Coordinator to Remote XBee on Arduino Uno

641 views Asked by At

I am trying to send a string of data from an XBee Series 1 device (Coordinator) connected to my computer to a remote XBee Series 1 device (End Device) to turn 2 LEDs on and off.

This is the Python code I use to send a string of data from the XBee coordinator to an XBee End Device:

import serial
import time

arduino = serial.Serial('COM13', 9600, timeout=1)
#msg = arduino.readline()  # read everything in the input buffer

time.sleep(3)

ASCIIdata = '121210'

for i in range(len(ASCIIdata)):

    if ASCIIdata[i] == '1':
        strin = '1'
        arduino.write(strin.encode())
        print strin.encode()
        time.sleep(0.2)
        # print(ASCIIdata[i])
        try:
            print ("Message from arduino: ")
            print arduino.readline()
            raise
        except Exception as e:
            print ("Fail to send!")

    if ASCIIdata[i] == '2':
        strin = '2'
        arduino.write(strin.encode())
        time.sleep(0.2)
        # print(ASCIIdata[i])
        try:
            print ("Message from arduino: ")
            print arduino.readline()
        except:
            print "Fail to send!"

    if ASCIIdata[i] == '0':
         strin = '0'
         arduino.write(strin.encode())
         time.sleep(0.2)
         # print(ASCIIdata[i])
         try:
             print ("Message from arduino: ")
             print arduino.readline()
         except:
             print "Fail to send!"

strin = 'p'
arduino.write(strin.encode()) # tell arduino a phase shifter setting has been finished
strin = 's'
arduino.write(strin.encode())

arduino.close()
time.sleep(0.5) # waits for 2 s
print('Data to be transfered: %s'%ASCIIdata)

This is the code on the Arduino end, where the remote XBee End Device is connected to:

#define pins
int pinLED1 = 5;
int pinLED2 = 12;
char c;

void setup() {
pinMode(pinLED1, OUTPUT);
pinMode(pinLED2, OUTPUT);
Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    c = Serial.read();
    Serial.print(c);
    if (c == '1') {
      digitalWrite(pinLED1, HIGH);
      digitalWrite(pinLED2, LOW);
      Serial.print("1 ON, 2 OFF\n");
    }
    else if (c == '2') {
      digitalWrite(pinLED1, LOW);
      digitalWrite(pinLED2, HIGH);
      Serial.print("1 OFF, 2 ON\n");
    }
    else if (c == '0') {
      digitalWrite(pinLED1, LOW);
      digitalWrite(pinLED2, LOW);
      Serial.print("Both unlit.\n");
    }
  }
  else{
    Serial.print("Nope");
  }
}

The correct output should be that the 2 LEDs at the Arduino will switch in an alternate fashion and then turn on, while Serial.print() outputs should be reflected on the Python terminal.

However, when I run the Python script, it kept throwing the exception "Fail to send!", i.e. there is no communications going on between Arduino and Python.

I have tested both codes above by communicating through the USB port which my Arduino (Uno) is attached to, and everything worked fine.

I have tried configuring my XBees in the following manner:

  1. XBee (Coordinator) and XBee (End Device) - Both in AT mode (API disabled)
  2. XBee (Coordinator) - API mode and XBee (End Device) - AT mode but yielded no results.

May I ask where did I go wrong?

Update 15-Feb-2018: I am not sure if supplying this information helps, but currently, this is how I have configured my XBee Series 1 modules:

XCTU Config. for Coordinator and End Device

I understand that there are many firmwares for XBee radios, and I was wondering if the problem I am facing is due to a limitation in the 802.15.4 protocol (though I think it is highly not the case).

2

There are 2 answers

3
TheEngineer On BEST ANSWER

I think you should break the problem and start from bottom. Have you tried connecting your Arduino to computer serial monitor and observe what you are receiving? If you were not receiving the characters properly, try sending them with xbee software terminal manually just to make sure you can get them properly and the connections between the xbees is working. Then, you can start debugging your code (if the problem was not solved already!)

0
galaxy_twirl On

I realized that I have set the addresses of my Coordinator and End Device wrongly~ The Coordinator could receive messages from my End Device because I configured the address of the End Device to point to the Coordinator, but I have forgotten to set the destination address of my Coordinator to be the same as my End Device.

I am really really sorry for my gross oversight.

Using TeraTerm on 2 separate devices helped solve my problem as it eliminated any issues with my code for Arduino.

Thank you @TheEngineer!