SPI Send and Receive String Between Multiple Arduinos

Hi guys
I have a master Arduino and 3 slave Arduinos.
I have tried to research how to send and receive string between them, but the answers i get are vague and I need some help to understand how to implement this into my project.

Say for example I have a serial number stored as a String, we'll call it SN
Now i want to send that String over my SPI bus to my first slave Arduino, but, the slave must respond with a String if the Serial Number is correct.
How would I go about this?

This is what i have so far:

String SN = "Some Serial Number";

void setup()
{
     Serial.begin(9600);
     digitalWrite(SLAVE1, LOW);
     digitalWrite(SLAVE2, HIGH);
     digitalWrite(SLAVE3, HIGH);

     SPI.begin();
     SPI.setClockDivider(SPI_CLOCK_DIV4);
}

void loop()
{
    for(int i = 0; i < SN.length(); i ++)
    {
          SPI.transfer(SN[i]);
    }
}

Why is the serial number a String?
What's wrong with a char array?

a char array would also work, my main concern is how to transfer said data over the SPI bus and receive a response from the slave

1 Like

https://www.tutorialspoint.com/arduino/arduino_serial_peripheral_interface.htm

this explains nothing of a return response from the slave

https://forum.arduino.cc/t/bidirectional-spi-arduino-raspberry-pi/150678/2

https://forum.arduino.cc/t/spi-bi-directional-communtion-issue/440367/4

Is it the first time you are working with SPI Port Based Serial Data Communication?
What Arduins do you have for the Master and the Slaves?

Another good webpage (which is linked in some of the posts in the links provided above)

http://www.gammon.com.au/spi

Easy method would be for the master to send the char array to the slave, then subsequently request data from the slave, at which time it sends back the response char array. Depends on how much processing time the slave needs as to whether that could all be done in the same SPI communications, or if there needs to be a delay between the two.

This thread explains the hurdle you are creating. I found code for binary (struct) transfer, but I feel it to complex to implement easily.

https://forum.arduino.cc/t/spi-slave-sending-back-a-string/469926/3

The usual questions:

Why a master and three slaves, instead of a single arduino.

Why SPI instead of serial or I2C?

Are the serial number and response string fixed or variable length? If variable length, is there a known maximum length?

@mr_tropica
Carry out the following steps to send Serial Number (1234) from Master UNO to Slave NANO.
1. Connect UNO-Master and NANO-Slave as per Fig-1 using SPI Port.
SPIUnoNAno
Figure-1:

2. Upload the following sketch in UNO-AMster.

//Master sends: 1234 as Serial Number at 1-sec interval 
#include <SPI.h>

int sNo = 1234;  //0x04D2

void setup ()
{
  Serial.begin(9600);
  SPI.begin();  //LH --> SPE-bit, LH--> I-bit, LL-->SPIE
  SPI.setClockDivider(SPI_CLOCK_DIV16); // 16 MHz/4 = 1 Mbits/sec
  digitalWrite(SS, LOW);   //Slave NANO is selcete
}

void loop()
{
  SPI.transfer(highByte(sNo));
  SPI.transfer(lowByte(sNo));
  delay(1000);
}

3. Upload the following sketch into NANO-Slave.

#include<SPI.h> 
byte rxData[2];
volatile int i = 0;
volatile bool flag = false;

void setup()
{
  Serial.begin(9600);
  bitSet(SPCR, MSTR);//this is Slave
  bitSet(SPCR, SPE);
  pinMode(SS, INPUT_PULLUP);
  pinMode(MISO, OUTPUT);
  SPI.attachInterrupt();
}

void loop()
{
  if (flag == true)
  {
    int sNo = rxData[0] << 8 | rxData[1];
    Serial.println(sNo, DEC);
    flag = false;
    Serial.println("==================");
  }
}

ISR(SPI_STC_vect)
{
  if (flag == false)//this flag syncheonizes data with Master
  {
    rxData[i] = SPDR;
    i++;
    if (i == 2)
    {
      flag = true;
      i = 0;
    }
  }
}

4. Check that SM2 shows 1234 at 1-sec interval.

==================
1234
==================
1234
==================
1234
==================

5. Consider the above sketches as building blocks, add codes with Master and Slave sketches to receive the following message from Slave and show it on SM1.

Serial No. has Matchaed!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.