Arduino框架下ESP8266与ESP8266之间SPI主从收发通讯示例

Posted perseverance52

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arduino框架下ESP8266与ESP8266之间SPI主从收发通讯示例相关的知识,希望对你有一定的参考价值。

Arduino框架下ESP8266与ESP8266之间SPI主从收发通讯示例


  • ✨SPI主从收发通讯演示

✨本示例来原于核心固件库自带示例。可以在C:\\Users\\Administrator\\AppData\\Local\\Arduino15\\packages\\esp8266\\hardware\\esp8266\\3.0.2\\libraries\\SPISlave\\examples中找到对应示例。

🚩注意事项

⚡在使用SPISlave_MasterSPISlave_Test分别烧录到对应的目标板上进行通讯测试时,SPISlave_Master设备端可以进行复位重启操作,但是SPISlave_Test设备端不能进行此类重启复位操作,因为在没有设置的情况下,esp8266在重启过程中如果有SPI信号会进来,会导致无法正常启动。请参考使用SPISlave_SafeMaster程序。如果要进行此类操作,先断开SPI通讯线,再重启。

  • 📌SPI通讯需要使用到的引脚
*   MISO ----> GPIO 12 (D6)
*   MOSI ----> GPIO 13 (D7)
*   CS -----> GPIO 15 (D8)
*   SCK ----> GPIO 14 (D5)
*   GND ----> GND

📢相同2块esp8266板的连接,将以上引脚直接相连即可,如果采用不同电源供电,注意将GND相连一起。

🌼主从模式端

🍁主机模式。

/*
    SPI Master Demo Sketch
    Connect the SPI Master device to the following pins on the esp8266:

    GPIO    NodeMCU   Name  
   ===================================
     15       D8       SS(CS)    
     13       D7      MOSI  
     12       D6      MISO 
     14       D5      SCK   

    Note: If the ESP is booting at a moment when the SPI Master has the Select line HIGH (deselected)
    the ESP8266 WILL FAIL to boot!
    See SPISlave_SafeMaster example for possible workaround

*/
#include <SPI.h>

class ESPMaster 
  private:
    uint8_t _ss_pin;

  public:
    ESPMaster(uint8_t pin): _ss_pin(pin) 
    void begin() 
      pinMode(_ss_pin, OUTPUT);
      digitalWrite(_ss_pin, HIGH);
    

    uint32_t readStatus() 
      digitalWrite(_ss_pin, LOW);
      SPI.transfer(0x04);
      uint32_t status = (SPI.transfer(0) | ((uint32_t)(SPI.transfer(0)) << 8) | ((uint32_t)(SPI.transfer(0)) << 16) | ((uint32_t)(SPI.transfer(0)) << 24));
      digitalWrite(_ss_pin, HIGH);
      return status;
    

    void writeStatus(uint32_t status) 
      digitalWrite(_ss_pin, LOW);
      SPI.transfer(0x01);
      SPI.transfer(status & 0xFF);
      SPI.transfer((status >> 8) & 0xFF);
      SPI.transfer((status >> 16) & 0xFF);
      SPI.transfer((status >> 24) & 0xFF);
      digitalWrite(_ss_pin, HIGH);
    

    void readData(uint8_t * data) 
      digitalWrite(_ss_pin, LOW);
      SPI.transfer(0x03);
      SPI.transfer(0x00);
      for (uint8_t i = 0; i < 32; i++) 
        data[i] = SPI.transfer(0);
      
      digitalWrite(_ss_pin, HIGH);
    

    void writeData(uint8_t * data, size_t len) 
      uint8_t i = 0;
      digitalWrite(_ss_pin, LOW);
      SPI.transfer(0x02);
      SPI.transfer(0x00);
      while (len-- && i < 32) 
        SPI.transfer(data[i++]);
      
      while (i++ < 32) 
        SPI.transfer(0);
      
      digitalWrite(_ss_pin, HIGH);
    

    String readData() 
      char data[33];
      data[32] = 0;
      readData((uint8_t *)data);
      return String(data);
    

    void writeData(const char * data) 
      writeData((uint8_t *)data, strlen(data));
    
;

ESPMaster esp(SS);

void send(const char * message) 
  Serial.print("Master: ");
  Serial.println(message);
  esp.writeData(message);
  delay(10);
  Serial.print("Slave: ");
  Serial.println(esp.readData());
  Serial.println();

🌻SPI 应答设备端程序

🌿应答模式

/*
    SPI Slave Demo Sketch
    Connect the SPI Master device to the following pins on the esp8266:

    GPIO    NodeMCU   Name 
  ===================================
     15       D8       SS (CS)   
     13       D7      MOSI  
     12       D6      MISO  
     14       D5      SCK   

    Note: If the ESP is booting at a moment when the SPI Master has the Select line HIGH (deselected)
    the ESP8266 WILL FAIL to boot!
    See SPISlave_SafeMaster example for possible workaround

*/

#include "SPISlave.h"

void setup() 
  Serial.begin(115200);
  Serial.setDebugOutput(true);

  // data has been received from the master. Beware that len is always 32
  // and the buffer is autofilled with zeroes if data is less than 32 bytes long
  // It's up to the user to implement protocol for handling data length
  SPISlave.onData([](uint8_t * data, size_t len) 
    String message = String((char *)data);
    (void) len;
    if (message.equals("Hello Slave!")) 
      SPISlave.setData("Hello Master!");
     else if (message.equals("Are you alive?")) 
      char answer[33];
      sprintf(answer, "Alive for %lu seconds!", millis() / 1000);
      SPISlave.setData(answer);
     else 
      SPISlave.setData("Say what?");
    
    Serial.printf("Question: %s\\n", (char *)data);
  );

  // The master has read out outgoing data buffer
  // that buffer can be set with SPISlave.setData
  SPISlave.onDataSent([]() 
    Serial.println("Answer Sent");
  );

  // status has been received from the master.
  // The status register is a special register that bot the slave and the master can write to and read from.
  // Can be used to exchange small data or status information
  SPISlave.onStatus([](uint32_t data) 
    Serial.printf("Status: %u\\n", data);
    SPISlave.setStatus(millis()); //set next status
  );

  // The master has read the status register
  SPISlave.onStatusSent([]() 
    Serial.println("Status Sent");
  );

  // Setup SPI Slave registers and pins
  SPISlave.begin();

  // Set the status register (if the master reads it, it will read this value)
  SPISlave.setStatus(millis());

  // Sets the data registers. Limited to 32 bytes at a time.
  // SPISlave.setData(uint8_t * data, size_t len); is also available with the same limitation
  SPISlave.setData("Ask me a question!");


void loop() 

以上是关于Arduino框架下ESP8266与ESP8266之间SPI主从收发通讯示例的主要内容,如果未能解决你的问题,请参考以下文章

Arduino ESP8266/32 自定义IO组网页状态显示与控制

arduino下esp8266+web实现远程开关窗和雨滴感应开关窗

Arduino开发ESP8266——安装与配置ESP8266开发板

Arduino开发ESP8266——安装与配置ESP8266开发板

Arduino框架下ESP32/8266使用PROGMEM功能将数据存储到flash中的使用规范

怎么用Arduino esp8266 框架下调用微信airkiss接口