Could you help me with with basic comunication example with Xbee+Processing?

Hi guys!

I'm newbie with XBEE and I'm doing something wrong to comunicate different Arduino Mega 2560.

To start I just trying to active a simple BLINK example if I send a '1' or desactive it if I send '0'.

If I get this, I will be able to applicate to my project with other keypressed.

I have configurated my XBees with 1 Coordinators and 6 End-Devices in BROADCAST.
(In XCTU i can send messages from one to other correctly).

Then, with processing I open the serial port to send data from the computer, via coordinator connected to the computer, to the others XBEEs.

The XBEES are connected in Arduinos Mega, waiting for the data. In this
simple code, just waiting 0 or 1 to decide if blink or no blink the led.

But I'm doing something wrong because it does not work.

PROCESSING CODE

import processing.serial.*;

Serial myPort; // Create object from Serial class

int value = 0;

void setup()
{
size(200,200); //make our canvas 200 x 200 pixels big
String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to
match your port
myPort = new Serial(this, portName, 9600);
}

void draw() {
fill(value);
rect(25, 25, 50, 50);
}

void keyPressed(){
if (key == '0') {
myPort.write('0');
println("SEND 0");
}
if (key == '1') {
myPort.write("1");
println("SEND 1");
}
}

ARDUINO CODE

char serialData;

void setup() {
Serial.begin(9600);
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);

serialData = (char)(('0'));
}

void loop() {

//if (Serial.available()) {
if (serialData == ('0')) {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("HAS ENVIAT ORDRE 0");
}
if (serialData == ('1')) {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("HAS ENVIAT ORDRE 1");
}
// }
}

I hope someone can help me!

Best!
Joan

It looks like you deleted the code for reading data from the serial port.
See

Thanks for your reply @mikb55!

I review my code and now is working fine between 2 XBEE devices (1 Coordinator + 1 EndDevice).

char val; // Data received from the serial port
int ledPin = 13; // Set the pin to digital I/O 13

void setup() {
  pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
  Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {
  if (Serial.available())
  { // If data is available to read,
    val = Serial.read(); // read it and store it in val
    Serial.println("HOLA");
  }
  if (val == '0')
  { // If 0 was received
    digitalWrite(ledPin, LOW);
    Serial.println("HAS ENVIAT ORDRE 0");// otherwise turn it off
  }
  if (val == '1')
  { // If 1 was received
    digitalWrite(ledPin, HIGH); // turn the LED on
    Serial.println("HAS ENVIAT ORDRE 1");
  }
}

Now I'm having problems with the comunication with more EndDevices, the answer is not immediate;(

I need to send a lot of '0' or '1' to comunicate the XBEEs.

I have setting them in broadcast mode, with DH 0000 and DL FFFF (with Digi XCTU software).

Any idea how I can get better communication with more than 1 EndDevice?

Thanks again!
Joan

Search for a tutorial that matches your particular model of Xbee.

There is no general solution as there are many different versions of Xbee hardware, firmware and 20+ settings that affect communication.

Hi @mikb55, thanks again for your reply!

I had seening differents tutorials and they explain how set the XBEEs in broadcast but no shows a finish working project .

I don't want to do any communication difficult system, just send from a Coordinator to the other EndDevices.

Maybe anyone could recomend me another forum and/or tutorial in this way??

Thanks in advance!
Joan

The Xbee is made by the Digi company and they have a support forum where you can ask for help.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.