1

I'm trying to make project to monitor my home. I am using some gas sensors and temperature and humidity sensors. I want to do this wirelessly and I think the best thing to do that is XBee.

I use this code for the transmitter Arduino:

char dataPacket[64];

int gx = 1;
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, "A%dB%dC%dD%dE%dF%dG%dH%dI%d", gx, gy, gz, ax, ay, az, mx,     my, mz);
Serial.println(dataPacket);


    }

gx gy gz .... these are the sensor reading variables

and in the receiving Arduino I use this code

void setup(){
    // Start up our serial port, we configured our XBEE devices for 9600 bps.
    Serial.begin(9600); 
}

void loop()
{

    if(Serial.available() > 0)
{
     while(Serial.available() > 0)
 {
        char dataPacket[64];

for (int i = 0; i < 64; i++)
{
  dataPacket[i] = Serial.read();
 Serial.print(dataPacket[0]);

}

String show = Serial.readStringUntil('C') ;
delay(500);

}
}
}

but i can't split the values from the stream and put them into their variables (gx gy ...)

Does anyone have idea how to split it?

JRobert
  • 15,407
  • 3
  • 24
  • 51
Haider
  • 43
  • 4

1 Answers1

2

Try a communication protocol with a , as a delimiter like:

messageId,data1,data2,data3,....,dataN\n

And use the Messenger library to parse it.

See my answer on comms protocols here:

Communication Protocol Best Practices and Patterns

An how to use the Messenger library here:

Send Processing color data into Arduino

geometrikal
  • 2,925
  • 16
  • 21