hi there, I’m trying to send requests from a master (Mega) to two slaves (Uno) using the packetSerial library. all three Arduinos are on the same TX/RX bus.
the master sends first a request ‘A’ and then two seconds later ‘B’, the idea is that the slaves respond with the right identifier and number.
When I have only one of the Unos connected, it works, however, as soon as I connect both I only get ‘B’ to reply; I cannot figure out what’s going on, can anyone advise? this is my first time trying this sort of thing and I am not sure how to go about figuring out why it won’t work.
Code master:
#include <PacketSerial.h>
PacketSerial masterPacketSerial; // initialize the packetserial object
byte identifier1 = 'A'; // ID of the first SLAVE Arduino we want to request
byte identifier2 = 'B'; // ID of the secoond SLAVE Arduino we want to request
int message = 0; // received integer
void setup() {
Serial.begin(9600); // Debug output
Serial1.begin(9600); // Hardware serial link to the SLAVE
// Setup PacketSerial on Serial1
masterPacketSerial.setStream(&Serial1); // set serial one as the packetserial stream
masterPacketSerial.setPacketHandler(&onPacketReceived);
}
void loop() {
// Update PacketSerial to look for packets
masterPacketSerial.update(); // contiuously look for updates
// --- Send request to slave 01 ---
uint8_t requestPacket1[1] = { identifier1 }; // create array: a message to send (in this case our identifier)
masterPacketSerial.send(requestPacket1, 1); // send the array "requestpacket", the size is "1"
Serial.println("sentID: A");
unsigned long startTime = millis();
while (millis() - startTime < 100) {
masterPacketSerial.update();
}
delay(2000); // Next request in 2 seconds
// --- Send request to slave 01 ---
uint8_t requestPacket2[1] = { identifier2 }; // create array: a message to send (in this case our identifier)
masterPacketSerial.send(requestPacket2, 1); // send the array "requestpacket", the size is "1"
Serial.println("sentID: B");
startTime = millis();
while (millis() - startTime < 100) {
masterPacketSerial.update();
}
delay(2000); // Next request in 2 seconds
}
// Called when a complete packet from the SLAVE arrives
void onPacketReceived(const uint8_t* buffer, size_t size) {
// Expect exactly 3 bytes (int16)
if (size != 3) {
Serial.println("Invalid packet size");
return;
}
char ident = buffer[0];
byte highB = buffer[1]; // save into byte the first byte of buffer
byte lowB = buffer[2]; // save into byte the second byte of buffer
message = (highB << 8) | lowB; // reconstruct int16
// Debug print
Serial.print("Received Identifier: ");
Serial.println(ident);
Serial.print("Received: ");
Serial.println(message);
// Serial.print("Binary: ");
// Serial.println(message, BIN);
}
Code slave (this is ‘B’ now – ‘A’ has the same code but a different identifier and dataToSend1):
#include <PacketSerial.h>
PacketSerial myPacketSerial;
byte identifier1 = 'B'; // This Arduino's ID
float dataToSend1 = 22222.54; // Your data (could be load cell reading)
void setup() {
Serial.begin(9600);
//Serial1.begin(9600);
// Setup PacketSerial
myPacketSerial.setStream(&Serial);
myPacketSerial.setPacketHandler(&onPacketReceived);
}
void loop() {
// Update PacketSerial to check for incoming packets
myPacketSerial.update();
// You can update dataToSend here if needed
// dataToSend = readLoadCell(); // example
}
// This function is automatically called when a complete packet arrives
void onPacketReceived(const uint8_t* buffer, size_t size) {
// Check if we received at least 1 byte
if (size < 1) return;
char request = buffer[0];
//Serial.println(request);
// If the request matches our identifier, send data
if (request == identifier1) {
int dataInt = (int)dataToSend1; // convert float to int by chopping off the .54
// Prepare response packet with 2 bytes
uint8_t response[3];
response[0] = identifier1;
response[1] = highByte(dataInt);
response[2] = lowByte(dataInt);
// Send the packet
myPacketSerial.send(response, 3);
}
}
