I am working on a home automation project.
I would like to have two arduino uno boards using xbee to communicate (send a request, reply sending the data of a sensor, …).
On the main board, I also stacked an ethernet shield to communicate with my computer.
Here is my development scenario :
1st board sending data (1, 42, 62) over the air with xbee (using Serial.print)
2nd board receiving data and communicating with my computer by ethernet (I use telnet to send requests)
The problem I have is that in my loop(), I apparently can't listen to Serial and use ethernet in the same time.
I can observe the data received by xbee using the Serial Monitor but the ethernet part is blocked.
But if I shut down the 1st board, I can communicate by ethernet…
Did anyone have that kind of problem ?
Here is my loop() on the 2nd board :
void loop() {
Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
// My FSM
}
}
}
if (Serial.available() > 0) {
incomingByte = Serial.read();
if (1 == incomingByte) {
receivedTab[0] = incomingByte;
receivedTab[1] = Serial.read();
receivedTab[2] = Serial.read();
Serial.print("Incoming data : ");
Serial.print(receivedTab[0]);
Serial.print(" ");
Serial.print(receivedTab[1]);
Serial.print(" ");
Serial.println(receivedTab[2]);
}
}
}
If there is at least one byte available, read all 3. Does that seem right to you?
I can observe the data received by xbee using the Serial Monitor but the ethernet part is blocked.
But if I shut down the 1st board, I can communicate by ethernet…
This sounds like the Arduino with two high-power-needs shields is underpowered. You may need to power the Ethernet shield independently.
If there is at least one byte available, read all 3. Does that seem right to you?
Maybe I was wrong to do that… (even though my 1st board emits 3 consecutive bytes every 3 sec).
So I tried something else :
if (Serial.available() > 0) {
incomingByte = Serial.read();
if (1 == incomingByte) {
receivedTab[0] = incomingByte;
Serial.print(receivedTab[0]);
}
}
Now here is what I observe :
I can connect to my board via TCP and it behaves normally.
But when I look at the serial monitor, it is blocked. When I close the telnet connection, all the data received via xbee is flushed.
It is really problematical because this would mean that I need to connect and disconnect permanently the connection to my PC…
If there is at least one byte available, read all 3. Does that seem right to you?
Maybe I was wrong to do that… (even though my 1st board emits 3 consecutive bytes every 3 sec).
Serial communications occurs asynchronously. When you perform the check to see if any serial data is available, you have no idea if that check occurs after the first byte has been received, the second byte has been received, or the third byte has been received.