SPI Connection Between Arduino ATMEGA 2560& ESP8266-ESP12 Not Working

can Someday Provide Me A Good Link or Demo Code On How to connect and esp and mega to transmit 84 byte test. Ihave Written 10 codes but none of works anda always leads to wdt(Soft Reset) or garbage data

Master

#include<SPI.h>
int swi=0;

void setup() {
  digitalWrite(SS,HIGH);
  SPI.begin();
  

}

void loop() {
  char c;
  digitalWrite(SS,LOW);
  if(swi==0)
  {
     for(const char *p="Hello, World!\n";c=*p;p++)
     SPI.transfer(c);
  }
  else
  {
     for(const char *p="Bye Bye !, World!\n";c=*p;p++)
     SPI.transfer(c);
  }
  swi=1-swi;
  digitalWrite(SS,HIGH);
}

Slave

#include<SPI.h>
char buf[100];
volatile byte pos;
volatile boolean process_it;

void setup() {
 Serial.begin(115200);
 pinMode(MISO,OUTPUT);
 SPCR|=_BV(SPE);
 pos=0;
 process_it=false;
 SPI.attachInterrupt();
}

ISR(SPI_STC_vect)
{
     byte c=SPDR;
     
     if(pos<sizeof buf)
     {
        buf[pos++]=c;
        if(c=='\n')
            process_it=true;
     }  
}

void loop() {
 if(process_it)
 {
    buf[pos]=0;
    Serial.println(buf);
    pos=0; 
    process_it=false;
 }

}

Ihave Written 10 codes but

shared none of them. If you want help with your code, post it.

If you want someone to write the code for you, head over to Gigs and Collaboration, and get your wallet out (AND define some real requirements!).

Here is the code problem is that slave is not recieving or is recieving garbage data; ESP8266 Master ATMEGA328 Slave Wiring Is Ok Checked It

Okgot it work can i post back in this thread only if need further help ??

Sorry for bothering again can you look at this code and tell me what wrong i am unable to match the send & the received data and for some reason the data recieved is being printed in new line;

#include<SPI.h>
int swi=0;
char buf[100];
int pos=0;
char *DataOut="Hello World , Bye Bye World !\n";
String DataIn="";

void setup() {
  Serial.begin(115200);
  digitalWrite(SS,HIGH);
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV8);
}

void loop() 
{
  String Vector1(DataOut);
  SendToDev(DataOut);
  NormalizeData(Vector1);
  NormalizeData(DataIn);
  if(Vector1.equals(DataIn)>=0)
  {
    Serial.print("Data Send OK : ");
    Serial.println(DataIn);
  }
  else
  {
      Serial.print("Error MisMatch Data Sent : ");
      Serial.println(DataIn);
  }
  delay(5); 
}

void NormalizeData(String S)
{
  S.replace("\n","");
  S.replace("\r","");
  S.replace("\0","");
}

void SendToDev(char *P)
{
  DataIn="";
  char C;byte R;
  digitalWrite(SS,LOW);
  for(const char *S=P;C=*S;S++)
     {
         R=SPI.transfer(C);
         C=R;
         DataIn.concat(C);
     }
  digitalWrite(SS,HIGH);
}

i am unable to match the send & the received data

You also seem completely unable to show what you are seeing. Without seeing what you do, there isn’t a hope in hell of helping you.

         C=R;

Why are you storing the return value from the SPI transfer in a byte? Why are you then overwriting C?

C=R is to capture the return message
and storing in byte to make it common on both sides so no confusion occurs
but i completed the code which worked fine for some time and then just for some reason decided to breakup

here is the code

Master Code :

#include<SPI.h>
int swi=0;
char buf[100];
int pos=0;
char *DataOut="Hello World !\n";
String DataIn="";int Retry=0;

void setup() {
  Serial.begin(115200);
  digitalWrite(SS,HIGH);
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV8);
}

void loop() 
{
  long int time = millis(); int Time=0;
  while(time+5000>millis())
  {
  String Vector1(DataOut);
  SendToDev(DataOut);
  NormalizeData(Vector1);
  NormalizeData(DataIn);
  if((Vector1.indexOf(DataIn)>=0)&&(!DataIn.equals("")))
  {
    Serial.print("Data Send OK : ");
    Serial.println(DataIn);
  }
  else
  {
      Serial.println();
      Serial.print("Error MisMatch Echo Data : ");
      Serial.println(DataIn);
      Serial.println("Origin Data : "+Vector1);
  }
    delay(500);
    Time=ValidDev();
  if(Time==0)
  {
    Serial.print("OOPS SomeThing Went Wrong Retrying...");
    Serial.println(Retry);
    Retry++;
  }
  else
  {
    Serial.print("Waiting For Processing To Complete....");
    Serial.println(Time);
    delay(Time+10);
    time = millis();
    Retry=0;
    GetDev("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
    Serial.print("Processed : ");
    Serial.println(DataIn);
    delay(5);
  }
 } 
  Serial.print("Sorry Something Broke Trying 2 Fix.....");
}

void NormalizeData(String S)
{
  S.replace("\n","");
  S.replace("\r","");
  S.replace("\0","");
}

void SendToDev(char *P)
{
  DataIn="";
  char C;byte R;
  digitalWrite(SS,LOW);
  for(const char *S=P;C=*S;S++)
     {
         R=SPI.transfer(C);
         C=R;
         DataIn.concat(C);
     }
  DataIn=DataIn.substring(1);
  digitalWrite(SS,HIGH);
}

int ValidDev()
{
   byte R;int T;
   digitalWrite(SS,LOW);
   R=SPI.transfer('F');
   R=SPI.transfer('F');
   T=R;
   digitalWrite(SS,HIGH);
   Serial.println(T);
   return T;
}

void GetDev(char *P)
{
  DataIn="";
  char C;byte R;
  digitalWrite(SS,LOW);
  while((pos<255)&&(C!='\n'))
     {
         R=SPI.transfer(C);
         C=R;
         DataIn.concat(C);
         if(C=='\n')
            break;
         pos++;
     }
  digitalWrite(SS,HIGH);
  pos=0;
}

Slave Code :

#include<SPI.h>
char buf[100];
volatile byte pos;
volatile boolean process_it;
String DataIn;
volatile int waitime=75,stage=0;
char P[]="0000Data Processed \n\n\n";

void setup() 
{
 Serial.begin(115200);
 pinMode(MISO,OUTPUT);
 SPCR|=_BV(SPE);
 pos=0;
 process_it=false;
 SPI.attachInterrupt();
}

ISR(SPI_STC_vect)
{
   byte C=SPDR;
   if(stage==0)
   {
       SPDR=C;
     if(pos<sizeof buf)
     {
        buf[pos++]=C;
        if(C=='\n')
        {
            process_it=true;
            stage=1;
        }
      }
    }
    else if(stage==1)
    {
      SPDR=(byte)waitime;
      pos=0;
      stage=2;
    }
    else if(stage==2)
    {
         
         if(P[pos]=='\n')
          {
            stage=0;
            pos=0;
          }
          else
          {
            SPDR=P[pos];
            pos++;
          }
    }
}

void loop() 
{
 Serial.print(stage);
 Serial.print("Value Of Pos");
 Serial.println(pos);
 if(process_it)
 {
    buf[pos]=0;
    DataIn.concat(buf);
    if(strcmp(buf,"Hello World !"))
    {
     // Serial.println("Command OK ");
    }
    pos=0; 
    process_it=false;
    DataIn="";
 }

}

For some reason code broke after few runs and now for some reason pos value seems to gotten stuck at 100.
My goal is to retrieve text string as output after waiting for specific time length of string / char may vary from 84-164 bytes

Hardware Used Are ESP8266-ESP12 And Atmega328

i m attaching the output screenshot it looks like synchronization issue

can you run the code and confirm if thats ok

Master Code

#include<SPI.h>
int swi=0;
char buf[100];
int pos=0;
char *DataOut="Hello World !\n";
String DataIn="";int Retry=0;

void setup() {
  Serial.begin(115200);
  digitalWrite(SS,HIGH);
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV8);
}

void loop() 
{
  long int time = millis(); int Time=0;
  while(time+5000>millis())
  {
  String Vector1(DataOut);
  SendToDev(DataOut);
  NormalizeData(Vector1);
  NormalizeData(DataIn);
  if((Vector1.indexOf(DataIn)>=0)&&(!DataIn.equals("")))
  {
    Serial.print("Data Send OK : ");
    Serial.println(DataIn);
  }
  else
  {
      Serial.println();
      Serial.print("Error MisMatch Echo Data : ");
      Serial.println(DataIn);
      Serial.println("Origin Data : "+Vector1);
  }
    delay(500);
    Time=ValidDev();
  if(Time==0)
  {
    Serial.print("OOPS SomeThing Went Wrong Retrying...");
    Serial.println(Retry);
    Retry++;
  }
  else
  {
    Serial.print("Waiting For Processing To Complete....");
    Serial.println(Time);
    delay(Time+10);
    time = millis();
    Retry=0;
    GetDev("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
    Serial.print("Processed : ");
    Serial.println(DataIn);
  }
 } 
  Serial.print("Sorry Something Broke Trying 2 Fix.....");
}

void NormalizeData(String S)
{
  S.replace("\n","");
  S.replace("\r","");
  S.replace("\0","");
}

void SendToDev(char *P)
{
  DataIn="";
  char C;byte R;
  digitalWrite(SS,LOW);
  for(const char *S=P;C=*S;S++)
     {
         R=SPI.transfer(C);
         C=R;
         DataIn.concat(C);
     }
  DataIn=DataIn.substring(1);
  digitalWrite(SS,HIGH);
}

int ValidDev()
{
   byte R;int T;
   digitalWrite(SS,LOW);
   R=SPI.transfer('F');
   R=SPI.transfer('F');
   T=R;
   digitalWrite(SS,HIGH);
   Serial.println(T);
   return T;
}

void GetDev(char *P)
{
  DataIn="";
  char C;byte R;
  digitalWrite(SS,LOW);
  while(pos<255)
     {
         R=SPI.transfer(C);
         C=R;
         DataIn.concat(C);
         if(C=='\n')
            break;
         pos++;
         delayMicroseconds(55); //added for test
     }
  digitalWrite(SS,HIGH);
  pos=0;
}

Slave Code

#include<SPI.h>
char buf[100];
volatile byte pos;
volatile boolean process_it;
String DataIn;
volatile int waitime=75,stage=0;
char P[]="Data Processed \n";
int long time=millis();

void setup() 
{
 Serial.begin(115200);
 pinMode(MISO,OUTPUT);
 SPCR|=_BV(SPE);
 pos=0;
 process_it=false;
 SPI.attachInterrupt();
}

ISR(SPI_STC_vect)
{
   byte C=SPDR;
   if(stage==0)
   {
       SPDR=C;
     if(pos<sizeof buf)
     {
        buf[pos++]=C;
        if(C=='\n')
        {
            process_it=true;
            stage=1;
            time=millis();
        }
      }
    }
    else if(stage==1)
    {
      SPDR=(byte)waitime;
      pos=0;
      stage=2;
    }
    else if(stage==2)
    {
         SPDR=P[pos];
         if(P[pos++]=='\n')
          {
            stage=0;
            pos=0;
          }
    }
}

void loop() 
{
 if(process_it)
 {
    buf[pos]=0;
    DataIn.concat(buf);
    if(strcmp(buf,"Hello World !"))
    {
      Serial.println("Command OK ");
    }
    pos=0; 
    process_it=false;
    DataIn="";
 }

}

This code works partially with only one exception still receiving 70 instead of 75
Fixed it and it broke again when i modified “GetDev(“FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF”);”