Hello.I'm new to arduino and xbee. I'm receiving sensor data from 2 different xbee. i set one as coordinator and the other 2 as endpoints. Now when i serial.read, twice nothing is shown in the serial interface. I want to add a character infront of the data eg. 'H' so that the coordinator can know which sensor is sending the signal. this is my code in my endpoint:
int analogInPin = A1; // Analog input pin that the potentiometer is attached to
int sensorValue;
float Celsius, Kelvin ;// value read from the pot
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
Kelvin = (sensorValue * 0.004882812 * 100.0);
Celsius = Kelvin - 273.15;
Serial.println("H");
Serial.print("sensor1 = " );
Serial.print(Celsius);
Serial.println();
delay(2000);
}
You are sending 'H', , , "sensor1 = ", some characters that represent the temperature, , and .
if ( Serial.available()>0) {
// read a byte
inByte = Serial.read();
if (inByte == 'H'){
sensor = Serial.read();
Serial data arrives SLOWLY. Especially at 9600 baud. You read one byte, of the one or more that has arrived. If that byte is an H, then, you expect that the next byte will have arrived. It may not.
Even if it has, look at the string of characters you are sending. The next byte is a . So, you print that.
Then, loop() runs again. Until the next H arrives, you don't print anything. All the sensor data that might have arrived is simply discarded.
When I do this, I just look for ASCII codes
The following waits for "$GPRMC," to come over and then it captures the time data:
```
byte GPSchar [13];
byte GPSchk [7] = {36,71,80,82,77,67,44};
// $ G P R M C ,
void getGPS()
{
while (serial_idx < 7)
{
if(Serial.available() > 0) // wait for $GPRMC,
{
GPSchar[serial_idx] = Serial.read();
if(GPSchar[serial_idx] == GPSchk[serial_idx]) // $ G P R M C ,
{
serial_idx++;
}
else
{
serial_idx = 0; //
}
}
}
while (serial_idx < 13) // loop - capture hhmmss bytes
// GPSchar[07]..[12]
{
if(Serial.available() > 0) // get hhmmss
{
GPSchar[serial_idx] = Serial.read();
serial_idx++;
}
}
}
```
Thanks for replying so quickly. so now how should the code be so that i can start reading the data for sensor one once the serial read detects a 'H' or any other character. Is there any other way i can seperate the data i read in the serial.read? And do i have to increase the baud rate?
GPS is 4800 bps, that's slower than 9600.
In my example, I know that the 6 bytes after $GPRMC are the time bytes.
This part:
if(GPSchar[serial_idx] == GPSchk[serial_idx]) // $ G P R M C ,
{
serial_idx++;
}
is where it's waiting for that sentence's header to come in.
Had I required a single trigger, an "H", then I'd have made that:
if (GPSchar == 0x48) [or GPSchar == 72, decimal equiv.]