ZB communication

Hi,

I was trying to communicate several ZB modules between them. There was several routers and one coordinator, but I couldn’t, so instead I try to make a P2P communication.

I tried to used two modules in API mode (2, with escaped character), they associated right, but I don’t receive anything. I couldn’t find the problem.

After that, I tried the most simple example, with two modules in AT mode (transparent), one send, another receive, so I write down my codes because the coordinator, the one which receives the data, didn’t show it well. It only shows wrong data.

I don’t know what is going wrong with this, because if I use x-ctu’s terminal it works fine!!

Can someone help me??

SEND

char *str = {"HOLA"};

int statusLed = 13;
int errorLed = 13;

unsigned long start = millis();
//___________________________________________________________________________________________

void flashLed(int pin, int times, int wait) {

  for (int i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(wait);
    digitalWrite(pin, LOW);

    if (i + 1 < times) {
      delay(wait);
    }
  }
}
//___________________________________________________________________________________________

void setup() {
  pinMode(statusLed, OUTPUT);
  pinMode(errorLed, OUTPUT);

  Serial.begin(57600);
  Serial.println("
	... XBee - ZB ...
");

  flashLed(statusLed, 1, 500);
}
//___________________________________________________________________________________________

void loop() {   
//  Serial.flush();

  Serial.println(str);
  delay(2000);			//wait 2 seconds

  Serial.flush();
//  delay(5000);
}


RECEIVE

int statusLed = 13;
int errorLed = 13;
int dataLed = 13;

char *str;
//___________________________________________________________________________________________

void flashLed(int pin, int times, int wait) {

  for (int i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(wait);
    digitalWrite(pin, LOW);

    if (i + 1 < times) {
      delay(wait);
    }
  }
}
//___________________________________________________________________________________________

void setup() {
  pinMode(statusLed, OUTPUT);
  pinMode(errorLed, OUTPUT);
  pinMode(dataLed,  OUTPUT);
  
  Serial.begin(57600);
  Serial.println("
	... XBee - ZB ...
");

  flashLed(statusLed, 1, 500);
}
//___________________________________________________________________________________________

void loop() {
  int i=0;
  
  /*if*/while (Serial.available()) {	//If there is data in the Serial Line
//    int dataByte = Serial.read(); //save data into integer variable dataByte
    str[i] = Serial.read();
    i++;
    
    //Serial.print(dataByte,BYTE); //Print to screen the variable received
    flashLed(statusLed, 12, 1000);        
    Serial.print(i,DEC);
    Serial.print(". ");
    Serial.println(str);
  }
  
  Serial.flush();
//  delay(5000);
}

konnbannwa,

in the RECEIVE side.

char *str; ------ The memory is not obtained although only pointer is obtained.

void loop() {
int i=0; ------ “i” has 0 substituted eternally.

str[i] = Serial.read(); ------ Although the memory is not obtained, substitution is performed compulsorily.

Hi,

first of all, thanks for your reply.

But I can’t understand what you mean with ‘memory is not obtained’ and ‘substitution is performed compulsory’. How can I fix it?? (char str[20]:wink:

The fact is that, sender sends almost 30 messages while receiver only shows 3 or 4 character.

I don’t know why is this happening.

Regards.

konnnichiwa,

Yes, I think that str [20] is a good method.

void loop() {
int i=0;
i++;

In this, whenever i goes into loop, it will be initialized by 0, and it will increase to 1 after that.
If you would like to increase i whenever it goes into loop,

static int i;

It carries out.
However, since the size of str will be exceeded, before exceeding the size of str, the processing which returns the value of i to 0 is required.

flashLed(statusLed, 12, 1000);

It is the biggest problem.
If this processing is started, you cannot return for 24 seconds, but is it OK?

Hi,

thanks!!

First of all, ‘flashLed’ function was erased. It only was for checking behaviour.

I finally get how communicate both modules in AT mode, my network fails sometimes at receive way, I think is because the delay’s I’m using in the code and the ‘Serial.flush()’ instruction I used at the end of the ‘loop’ function.

But I have to deal with it and look for fixing it.

My receive code is next:

int pin = 13;
char str;
//___________________________________________________________________________________________

void setup() {
  pinMode(pin,  OUTPUT);

  Serial.begin(57600);
  Serial.println("
	... XBee - ZB ...
");
}
//___________________________________________________________________________________________

void loop() {
  digitalWrite(pin, HIGH);

  while (Serial.available() > 0) {	//If there is data in the Serial Line
    str = Serial.read(); //save data into integer variable dataByte
    Serial.print(str);   //Print to screen the variable received
  }
  Serial.println("_____________________________");

  Serial.flush();

  delay(500);
  digitalWrite(pin, LOW);
  delay(5000);
}

Instead of ‘char *str’, I’m using only a char, but I’m being able to read fine by the UART.

I’ll thank any tip you can tell me.

Regards.