乐鑫技术交流
直播中

爱与友人

9年用户 953经验值
擅长:可编程逻辑 模拟技术 存储技术
私信 关注
[问答]

SSD1306和RC522不能在ESP8266上协同工作怎么解决?

大家好,
在我的项目中,我在 ESP8266 上使用了 RC522 RFID 模块和 SSD1306 OLED 显示器。当在 RFID 模块上检测到 RFID 标签时,我希望在屏幕上显示一些内容。
RFID 模块和 OLED 显示器都可以工作,但只能单独工作。如果我同时使用两者,例如在检测到 RFID 标签后我想在屏幕上显示文本,它就不起作用。
当然,问题出在屏幕和 RFID 阅读器使用相同的引脚。两者都用于 ESP8266 上的 SDA PIN D2 和 SCK PIN D1。不同的协议似乎相互干扰。但是,直到某个时候。所以我可以在 setup() 中在屏幕上显示一些东西,然后使用 RFID 模块。但是,使用RFID模块后,我无法在屏幕上显示任何内容。当我尝试时,RFID 模块在那之后没有工作。
我的解决方案想法是在通过 I2C 通信之前结束与 SPI.endTransaction() 的 SPI 通信,反之在使用 SPI 之前结束与 Wire.endTransmission() 的 I2c 通信。也想到了相应的启动命令。但是,它不起作用。
我很好奇是否有一个简单的解决方案。经过几个小时的谷歌,我没有找到一个。正如您可能会说的那样,我对微电子学还比较陌生。
我期待着答案,并提前非常感谢你。
这是我的项目的代码(特别是见最后一种方法):
代码:全选//RFID-MODUL
#define RST_PIN     5         
#define SS_PIN      4

#include      
#include   

MFRC522 mfrc522(SS_PIN, RST_PIN);

//OLED-DISPLAY
#include
#include
#include

#define OLED_RESET     -1
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//REST-API
#include
#include
#include


// Replace with your network credentials
const char* ssid     = "**********";
const char* password = "*******************";


const char* serverName = "*******************/*************/post-esp-data.php";

String apiKeyValue = "jhgdku7t7liu";

String sensorName = "RFID-Reader";
String sensorLocation = "Office";


void setup() {
//OLED-DISPLAY
  Serial.begin(115200);

//RFID-MODUL   
  SPI.begin();      
  mfrc522.PCD_Init();  

//REST-API  
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
  }


void loop() {
//RFID-MODUL
    if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  Serial.println();
  Serial.print(" UID tag  :");
  String content= "";
  for (byte i = 0; i < mfrc522.uid.size; i++) {
     Serial.print(mfrc522.uid.uidByte < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte, HEX);
     content.concat(String(mfrc522.uid.uidByte < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte, HEX));
  }
  content.toUpperCase();
  Serial.println();
  Serial.print(" PICC type: ");
  MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  Serial.println(mfrc522.PICC_GetTypeName(piccType));
  
  if (content.substring(1) == "39 7B 02 64" || content.substring(1) == "B2 38 DB 0E") {
    accessGranted();
  }
  else {
    accessRefused();
  }


//REST-API
  if(WiFi.status()== WL_CONNECTED){
    WiFiClient client;
    HTTPClient http;
   
    http.begin(client, serverName);
   
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
   
   String httpRequestData = "api_key=" + apiKeyValue + "&uid=" + content
                          + "&location=" + sensorLocation + "&value1=" + content
                          + "&value2=" + "test" + "&value3=" +  "test";
                        
    int httpResponseCode = http.POST(httpRequestData);
   
        
    if (httpResponseCode>0) {
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
    }
    else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }
    // Free resources
    http.end();
  }
  else {
    Serial.println("WiFi Disconnected");
  }
  //Send an HTTP POST request every 30 seconds
  delay(1000);
}

//RFID-MODUL
void accessGranted() {
  Serial.println(" Access Granted ");
  Serial.println();
  delay(300);
}

void accessRefused() {
  SPI.endTransaction();
  Wire.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // I2C address = 0x3C
  delay(1000);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.print("Access Refused");
  display.display();
  Wire.endTransmission();
  
  Serial.println(" Access Refused ");
  delay(300);
}



               


                        

回帖(1)

马占云

2024-1-9 15:43:15
这可能是因为你的 OLED 显示器使用的是 I2C 协议,而 RC522 RFID 模块使用的是 SPI 协议,这两者的引脚是相同的,因此会发生冲突。你可以尝试使用不同的引脚来避免这种冲突。

另外,你可以使用软件 I2C 或软件 SPI 这样的库来模拟这些协议,并将它们分别连接到不同的引脚上,这将使两者在 ESP8266 上同时协同工作成为可能。

最后,你也可以使用一个双口 RAM,在其中一个口输出 OLED 显示器,另一个口输出 RFID 模块,从而避免引脚冲突的问题。

希望这可以帮助到你。
举报

更多回帖

×
20
完善资料,
赚取积分