Buenas, tengo configurado un esp8266 como servidor web el cual se comunica con un arduino uno para ir recibiendo datos de las variables e ir actualizándolas en el navegador.
Para la parte de SPI tengo configurado como maestro el esp8266 con el siguiente codigo:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include<SPI.h>
void actualizar_humedad_habitaciones()
{
int t=50;
byte Mastersend,Mastereceive;
digitalWrite(SS, LOW);
//Starts communication with Slave connected to master
variable_send_spi=5;
Mastersend=variable_send_spi;
//delay(500);
Mastereceive=SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slave
delay(t);
Mastereceive=SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slave
delay(t);
//Serial.println(Mastereceive);
hhabmatrimonio=Mastereceive;
variable_send_spi=6;
Mastersend=variable_send_spi;
//delay(500);
Mastereceive=SPI.transfer(Mastersend);
delay(t);
Mastereceive=SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slave
delay(t);
//Serial.println(Mastereceive);
hsalon=Mastereceive;
variable_send_spi=7;
Mastersend=variable_send_spi;
//delay(500);
Mastereceive=SPI.transfer(Mastersend);
delay(t);
Mastereceive=SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slave
delay(t);
//Serial.println(Mastereceive);
hhabordenador=Mastereceive;
variable_send_spi=8;
Mastersend=variable_send_spi;
//delay(500);
Mastereceive=SPI.transfer(Mastersend);
delay(t);
Mastereceive=SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slave
delay(t);
//Serial.println(Mastereceive);
hhabpablo=Mastereceive;
digitalWrite(SS,HIGH);
Serial.println("HUMEDAD");
Serial.println(hhabmatrimonio);
Serial.println(hsalon);
Serial.println(hhabordenador);
Serial.println(hhabpablo);
}
void setup(){
// Serial port for debugging purposes
Serial.begin(115200); //Starts Serial Communication at Baud Rate 115200
SPI.begin(); //Begins the SPI commnuication
SPI.setClockDivider(SPI_CLOCK_DIV8); //Sets clock for SPI communication at 8 (16/8=2Mhz)
pinMode(SS,OUTPUT);
digitalWrite(SS,HIGH); // Setting SlaveSelect as HIGH (So master doesnt connnect with slave)
}
void loop(){
actualizar_humedad_habitaciones();
delay(10000); //delay 10 segundos
}
En el arduino uno configurado como esclavo tengo el siguiente código:
#include<SPI.h>
volatile boolean received;
volatile byte Slavereceived,Slavesend;
int variable_send_spi;
void setup()
{
Serial.begin(115200);
pinMode(MISO,OUTPUT); //Sets MISO as OUTPUT (Have to Send data to Master IN
SPCR |= _BV(SPE); //Turn on SPI in Slave Mode
received = false;
SPI.attachInterrupt(); //Interuupt ON is set for SPI commnucation
}
ISR (SPI_STC_vect) //Inerrrput routine function
{
Slavereceived = SPDR; // Value received from master if store in variable slavereceived
received = true; //Sets received as True
}
void loop()
{ if(received) //Logic to SET LED ON OR OFF depending upon the value recerived from master
{
if (Slavereceived==1){variable_send_spi=25;}
else if (Slavereceived==5){variable_send_spi=60;}
else if (Slavereceived==2){variable_send_spi=30;}
else if (Slavereceived==6){variable_send_spi=65;}
else if (Slavereceived==3){variable_send_spi=35;}
else if (Slavereceived==7){variable_send_spi=70;}
else if (Slavereceived==4){variable_send_spi=40;}
else if (Slavereceived==8){variable_send_spi=75;}
else {variable_send_spi=55;};
Slavesend=variable_send_spi;
SPDR = Slavesend; //Sends the x value to master via SPDR
//delay(5);
}
}
Las conexiones son:
ESP8266: GIOP14,15,16,17
Arduino Pin 10,11,12,13.
Ejecutando el código aparece lo siguiente en el monitor del puerto serie:
HUMEDAD
60.00
65.00
70.00
75.00
HUMEDAD
60.00
65.00
70.00
75.00
HUMEDAD
60.00
65.00
70.00
75.00
HUMEDAD
60.00
65.00
70.00
75.00
HUMEDAD
60.00
65.00
70.00
75.00
HUMEDAD
60.00
65.00
7.00
8.00
HUMEDAD
30.00
65.00
70.00
75.00
HUMEDAD
60.00
65.00
6.00
8.00
Como se puede ver, cuando se repite varias veces la comunicación entre ellos, aparecen valores que no corresponden con los que debería recibir el esp8266. He intentado ajustar los tiempos por si fuera eso pero lo único que consigo es que funcione las primeras veces. ¿A que puede ser debido?
Gracias