Below are my codes for transferring string data between two Arduinos from Master to Slave via SPI. Similarly, how can I transfer string data from Slave to Master? Any suggestions?
Master Code:
#include <SPI.h>
char input;
const int size_arr = 150; // define array size (must be less than size of slave)
char buf[size_arr]; // define char array, use for convert from string for send to slave
String main_dat = "hello from master"; // real String data
void setup() {
Serial.begin(9600);
pinMode(SS, OUTPUT);
digitalWrite(SS, HIGH);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV16);
}
void loop() {
input = Serial.read();
if(input == 's'){
main_dat.toCharArray(buf, size_arr); // convert String data to char array
Spi_tranmis(buf); // send char array
}
delay(500);
}
void send_spi(byte pram) {
digitalWrite(SS, LOW);
SPI.transfer(pram);
digitalWrite(SS, HIGH);
delayMicroseconds(5);
}
void Spi_tranmis(char buf[]) {
for (int i = 0 ; i < size_arr ; i ++) {
send_spi(buf[i]);
}
send_spi('\n');
}
Slave Code:
const int size_arr = 151; // define array size (must be more than size of master)
char buf[size_arr]; // char array , use for recive from master
#define SS_Pin 9
volatile byte pos = 0;
volatile boolean process = false;
String msg; // real data
void setup() {
Serial.begin(9600);
pinMode(MISO, OUTPUT);
digitalWrite(MISO, LOW);
pinMode(SS_Pin, OUTPUT);
SPCR |= bit(SPE);
SPCR |= bit(SPIE);
}
ISR(SPI_STC_vect) {
char c = SPDR;
if (pos < sizeof buf) {
if (c == '\n') {
buf[pos] = '\0';
process = true;
}
else {
buf[pos++] = c;
}
}
}
void loop() {
recvHandle();
}
void recvHandle() {
if (process) { // when SPI available
msg = String(buf); // convert char array to String
Serial.println(msg);
process = false;
pos = 0;
}
}
I moved your topic to an appropriate forum category @enesorhaan.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
Well, with some additional signal lines, external hardware (some gates) and if you access the SPI registers directly, I guess you could kludge something together but I won't be able to help you with that.
If you need bidirectional asynchronous communications, I would use the serial UART.
SPI has no specific "request data function" you have to make your own.
Send a one byte code to the slave that the slave can examine so it knows what the master wants.
E.G. send an 0xAA to tell the slave that the master wants data xx.
In my example above, I am sending the "hello from master" expression, which is already a string expression. I don't know how to get a response from the slave after the data I sent is finished and how I can receive and see it on the master!
I can summarize it as follows; I want to convert JSON type data into string data type between my two Arduino Uno cards and transfer it from Master to Slave. So I need to send string. I also tried with I2C and UART, but according to my research, SPI was faster. What are you thinking?
What I'm saying is quite simple: In the project, I want to transfer the JSON type data I receive from Ethernet from the master to the slave. A simple ethernet stepper motor drive project?
Actually, I told you about my project. Let me summarize again as follows:
I have an Ethernet Server on my own computer. This server is connected to my master device, Arduino, via ethernet and streams JSON type data. To give an example, this JSON data contains speed, direction and location information.
Then, my Master device receives this data and processes it within itself, and I send this data in JSON type separately to the Slave Arduino devices and try to control the Stepper Motors at the end. This is the plot of the project. I'm trying to communicate between Master and Slave right now. What more can I say? That is all.