sending all sensor read in one time via xbee (my home monitor project)

[quote author=George Matthews date=1414250467 link=msg=1935113] When you declare local "int values[9];" it simply assigns 9 bytes or RAM for the array. [/quote]

Actually 18 bytes for integer, but the important point is it is not initialized.

i think this is the limit of arduino and xbee…

we can make sure that the 2 arduino run at the same time and process every instruction in the same time

there is delay between two because we use difference code in each one ,when we send the char “<” and receiving it in arduino 2 the arduino 2 still process the code without begin to enter the second round of loop to receive the next char while the arduino1 sent the the second char and in this situation we loss the second char and i thing the code is work fine…

is there any way to improve the code and make it run perfect to receive the same char that we send it.

we can make sure that the 2 arduino run at the same time and process every instruction in the same time

No, you can't. At least for what we understand "instruction" and "at the same time" and "in the same (amount of) time" to mean.

is there any way to improve the code

Probably. But, without seeing your code? Not a snowball's chance in hell.

PaulS: Probably. But, without seeing your code? Not a snowball's chance in hell.

do you man i cant send all sensor value and receive it without problem

so for what they make XBee ? did they make it for sending 1byte (0 or 1) to blinking led wireless or run electrical device

sounds like there is no one before me try to send stream string via XBee and receive it then split it without problem what ever i send it's stream and all what i want is just split it again and re store it in variable

do you man i cant send all sensor value and receive it without problem

No. I never said anything like that.

What I said was that if you don't post your code, you can't expect us to help you.

sounds like there is no one before me try to send stream string via XBee and receive it then split it without problem

Nonsense. Why do you think I was able to post the code I did?

what ever i send it's stream and all what i want is just split it again and re store it in variable

You have our permission to do that.

this code for Arduino 1 (Transmitter)

char dataPacket[64];

int gx = 123;
int gy = 321;
int gz = 456;
int ax = 654;
int ay = 789;
int az = 987; 
int mx = 147; 
int my = 258; 
int mz = 369;

void setup(){
 
 Serial.begin(9600);
 
}

void loop(){
     
     



   //sprintf(dataPacket, "X%d" ,gx);
    sprintf(dataPacket,"<%d,%d,%d,%d,%d,%d,%d,%d,%d>",gx, gy, gz, ax, ay, az, mx, my, mz);
      Serial.println(dataPacket);
 
}

and this code for Arduino 2 (Receiver)

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(9600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
  int values[9]; // Array to hold the values
byte index = 0; // index into array

char *token = strtok(inData, ","); // Get the first token
while(token)
{
   values[index] = atoi(token); // convert the token to an int and store it in the array
   index++; // Increment the index

   token = strtok(NULL, ","); // Keep parsing the same string
}
Serial.println("---------");
Serial.println(values[0]);
Serial.println(values[1]);
Serial.println(values[2]);
Serial.println(values[3]);
Serial.println(values[4]);
Serial.println(values[5]);
Serial.println(values[6]);
Serial.println(values[7]);
Serial.println(values[8]);
delay(1000);
}

On the sender end,

is there some reason for

leaving the commented out crap?

Is there some reason for the excessive white space?

Make it easy to help you by removing useless crap from your code.

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
  int values[9]; // Array to hold the values
byte index = 0; // index into array

char *token = strtok(inData, ","); // Get the first token
while(token)
{
   values[index] = atoi(token); // convert the token to an int and store it in the array
   index++; // Increment the index

   token = strtok(NULL, ","); // Keep parsing the same string
}
Serial.println("---------");
Serial.println(values[0]);
Serial.println(values[1]);
Serial.println(values[2]);
Serial.println(values[3]);
Serial.println(values[4]);
Serial.println(values[5]);
Serial.println(values[6]);
Serial.println(values[7]);
Serial.println(values[8]);

Converting the received stream of data to an array MUST happen in the if(started && ended) block, as you have been told. All you'll get, otherwise, is crap. I was under the impression that you were tired of getting crap. I guess I was mistaken.

if i use this code in receiver i’ll got this results

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[64];
byte index;

void setup()
{
   Serial.begin(9600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 63)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
  int values[9]; // Array to hold the values
byte index = 0; // index into array

char *token = strtok(inData, ","); // Get the first token
while(token)
{
   values[index] = atoi(token); // convert the token to an int and store it in the array
   index++; // Increment the index

   token = strtok(NULL, ","); // Keep parsing the same string
}
Serial.println("---------");
Serial.println(values[0]);
Serial.println(values[1]);
Serial.println(values[2]);
Serial.println(values[3]);
Serial.println(values[4]);
Serial.println(values[5]);
Serial.println(values[6]);
Serial.println(values[7]);
Serial.println(values[8]);
//delay(1000);
}


the results as you see look very crap

@@@@@@@@@@@@

and if i use this code for receiver

#define SOP '<'
#define EOP '>'
#include <string.h>
#define MAX_STRING_LEN 100

//char *inputstring1 = "one,two,three,four,five,six,seven,eight,nine,ten"; //this is the string that will be split. this can be changed!
char *p, *i;

bool started = false;
bool ended = false;

char inData[64];
byte index;

char* gx ;
char* gy ;
char* gz ;
char* ax ;
char* ay ;
char* az ;
char* mx ;
char* my ;
char* mz ;

void setup()
{
   Serial.begin(9600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 63)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet

 
    
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
    
  }
gx = subStr(inData, ",", 1);
gy = subStr(inData, ",", 2);
gz = subStr(inData, ",", 3);
ax = subStr(inData, ",", 4);
ay = subStr(inData, ",", 5);
az = subStr(inData, ",", 6);
mx = subStr(inData, ",", 7);
my = subStr(inData, ",", 8);
mz = subStr(inData, ",", 9);
 
 Serial.println("---------------");
 Serial.println(gx);
 Serial.println(gy);
 Serial.println(gz);
 Serial.println(ax);
 Serial.println(ay);
 Serial.println(az);
 Serial.println(mx);
 Serial.println(my);
 Serial.println(mz);

}

  char* subStr (char* input_string, char *separator, int segment_number)
  {
 char *act, *sub, *ptr;
 static char copy[MAX_STRING_LEN];
 int i;
 
 strcpy(copy, input_string);
 
for (i = 1, act = copy; i <= segment_number; i++, act = NULL) {
 
 sub = strtok_r(act, separator, &ptr);
 if (sub == NULL) break;
 }
 return sub;
}

the results will be like this some time just one char missed

and sometimes 1 reading sensor missing and its most of the time the last reading sensor

and sometime i receive all the reading sensor without any missed char like this

i want to make code receive the reading and split it like the last photo without any missed char or any other problem like crap and other things…

I'm tired of telling you that you need to parse the data BEFORE you empty the array. Now, do it right or go away.

Pauls im sorry but I'm beginner so not all what i read understand it :-[

can you edit the first receiver or the second receivers code to make change that you want to tell me about it

then post the final code to try it

can you edit the first receiver or the second receivers code to make change that you want to tell me about it

I could, but I’m lazy.

All of this:

  int values[9]; // Array to hold the values
byte index = 0; // index into array

char *token = strtok(inData, ","); // Get the first token
while(token)
{
   values[index] = atoi(token); // convert the token to an int and store it in the array
   index++; // Increment the index

   token = strtok(NULL, ","); // Keep parsing the same string
}
Serial.println("---------");
Serial.println(values[0]);
Serial.println(values[1]);
Serial.println(values[2]);
Serial.println(values[3]);
Serial.println(values[4]);
Serial.println(values[5]);
Serial.println(values[6]);
Serial.println(values[7]);
Serial.println(values[8]);

goes INSIDE this block:

  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }

After the first comment, but before the second comment.

ok thanks i'll try it and tell you the results :grin:

i edit the code as you told me

and i got great results (not the best as what i want)

there is some char missing in receiving loop

look for this picture the results look perfect and i receive all char with no craps

this is 3 capture screen for serial monitor i put it in one photo as you see the results have no problem

this capture for serial monitor show how there is missing char and craps

this capture for serial monitor also show how there is missing char and craps

this capture for serial monitor also show how there is missing char and craps

this capture for serial monitor also show how there is missing char and craps

where is the problem in xbee or arduino or code

is there buffer in xbee whe can use it to store the receive data while the arduino complete the processing loop for other task to in order to receive data from xbee without any losses

or can we put delay in transmitter or receiver to enhance the code and the results

I have read "802.15.4 based radios have error detection and correction via up to 3 retries in the wireless link, unless you disable ACKs - and this is an option. " ... "In transparent mode, I doubt you are told if the retries were exhausted. There is an AT register to check for such errors. If they are occurring, it's likely that you need to choose a different channel number."

https://forum.sparkfun.com/viewtopic.php?f=13&t=21421

From my own experience using RF24, dropping data because of interference, is something that has to be minimized and dealt with.

i come back to work on my project

now i want to simulate the sensor with real value

i have
MQ2 Gas Sensor = need 2 Char
MQ9 Gas Sensor = need 3 Char
AirQuality Sensor = need 1 Char
HCHO Sensor = need 2 Char
DHT22 Sensor = need 4 Char
Temperature Probe Sensor = need 4 Char
Noise Level Sensor = need 3 Char
Soil Moisture Sensor = need 3 Char
Rotary Encoder = need 2 Char

the starting flag by Pauls “<>” need 2 Char also

the , need 9 Char

so the total char is 35 char need to send every cycle which contain all info and data needed from transmitter to receiver

the transmitter code is

char dataPacket[64];

int MQ2 = 12; // 2 char
int MQ9 = 345; // 3 char
int AirQuality = 6; // 1 char
int HCHO = 78; // 2 char
int TMP = 91; // 2 char
int HUMB = 11; // 2 char
int TMPP = 2222; // 4 char
int NOISE = 333; // 3 char
int SOIL = 444; // 3 char
int RE = 555; // 3 char
void setup(){
 
 Serial.begin(9600);
 
}

void loop(){
     
     



   //sprintf(dataPacket, "X%d" ,gx);
    sprintf(dataPacket,"<%d,%d,%d,%d,%d,%d,%d,%d,%d,%d>",MQ2, MQ9, AirQuality, HCHO, TMP, HUMB, TMPP, NOISE, SOIL, RE);
      Serial.println(dataPacket);

}

and the receiver code is

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[64];
byte index;

void setup()
{
   Serial.begin(9600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 63)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
 int values[10]; // Array to hold the values
byte index = 0; // index into array

char *token = strtok(inData, ","); // Get the first token
while(token)
{
   values[index] = atoi(token); // convert the token to an int and store it in the array
   index++; // Increment the index

   token = strtok(NULL, ","); // Keep parsing the same string
}
Serial.println("---------");
Serial.println(values[0]);
Serial.println(values[1]);
Serial.println(values[2]);
Serial.println(values[3]);
Serial.println(values[4]);
Serial.println(values[5]);
Serial.println(values[6]);
Serial.println(values[7]);
Serial.println(values[8]);
Serial.println(values[9]);
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
 
//delay(1000);
}

as you see in the results

everything work great without any error or crap in data :grin:

i;m so happy ;D

now i complete the first part of my work
the second part will be about sending the controlling value

like servo motor and vibration motor

and the total char needed to send is 7
i will take rest for few days and start working on this part

thank you again

can anyone help in giving a code for using multiple sensor with xbee

shubham321: can anyone help in giving a code for using multiple sensor with xbee

The code in the post just above yours does just that. Why didn't you bother to read the thread you posted in?