Hello again, I have a question, or maybe 2 questions, but first I will tell you what I have... I have an Arduino Uno and an Esp8266, both are connected via SPI, Esp8266 is the Master and the Arduino is the Slave. There is almost no information about SPI on the internet, much less related to Esp8266, but I found these two codes that allow me to send info from Esp8266 (Master) to Arduino (Slave).
It works well and there are no problems with it, but my problem comes when I want the Arduino (Slave) to send info to Esp8266, I DON'T KNOW HOW TO DO IT, I don't know why I can't find example codes about this (sorry if there are any, but I CAN'T FIND THEM), I tried to do it by myself with what little I knew, but it didn't work...
============
Code of UNO
#include <SPI.h>;
char buff[100];
volatile byte index;
volatile bool receivedone;
void setup(void) {
Serial.begin(9600);
SPCR |= bit(SPE);
pinMode(MISO, OUTPUT);
index = 0;
receivedone = false;
SPI.attachInterrupt();
}
void loop(void) {
if (receivedone) {
buff[index] = 0;
Serial.println(buff);
index = 0;
receivedone = false;
}
}
ISR (SPI_STC_vect) {
uint8_t oldsrg = SREG;
cli();
char c = SPDR;
if (index < sizeof buff) {
buff[index++] = c;
if (c == '\n') {
receivedone = true;
}
}
SREG = oldsrg;
}
===============
Code of Esp8266
#include <SPI.h>;
char buff[] = "Hello Slave\n";
void setup() {
Serial.begin(9600);
SPI.begin();
}
void loop() {
for (int i = 0; i < sizeof buff; i++) {
SPI.transfer(buff[i]);
}
Serial.println("Hello Slave__");
delay(2000);
}
So my first question is how can I make the Arduino (Salve) send info to the Esp8266 (Master) (I know that when the Master activates the SS pin on a Slave, the Master info can be received by the Slave, but also I want the Slave to send info about his status)?
When I already know this, maybe I wouldn't have to ask my question because it could already be answered, but if not, then here goes... How can I control the Arduino and the SD reader at the same time? The SD reader also works with the SPI protocol, so they would share the Bus. And the problem is in this code...
===========
SD Reader
#include <SD.h>
File myFile;
void setup() {
Serial.begin(9600);
Serial.print("Starting SD...");
if (!SD.begin(4)) {
Serial.println("Could not start");
return;
}
Serial.println("Successful initialization");
myFile = SD.open("file.txt");
if (myFile) {
Serial.println("file.txt:");
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
Serial.println("Error");
}
}
void loop() {}
At no time is it seen that the code sets the SS pin high or something (I think that is done in the SD library), nor in the Esp8266 code, and I would need that to control two Slaves...
Seriously, I would really appreciate it if you could help me, thanks!