SSD1306 and RC522 are not working together on ESP8266
Posted: Fri Jan 07, 2022 10:31 am
Hello all,
in my project I use a RC522 RFID module and a SSD1306 OLED display on a ESP8266. When a RFID tag is detected on the RFID module, I want something to be displayed on the screen.
The RFID module and the OLED display both work, but only individually. If I use both at the same time, e.g. after a RFID tag has been detected and I want to display text on the screen, it does not work.
Of course, the problem comes from the fact that the screen and the RFID reader use the same pins. Both use for SDA PIN D2 on the ESP8266 and for SCK PIN D1. The different protocols seem to interfere with each other. However, not until sometime. So I can display something on the screen in setup() and then use the RFID module. However, I cannot display anything on the screen after using the RFID module. When I tried, the RFID module did not work after that.
My solution idea was to end the SPI communication with SPI.endTransaction() before communication via I2C and the other way around to end the I2c communication with Wire.endTransmission() before using SPI. I also thought of the corresponding start commands. However, it does not work.
I am curious if there is a simple solution for this. After hours of Google I didn't manage to find one. I'm still relatively new to microelectronics, as you can probably tell.
I look forward to answers and thank you very much in advance.
Here is the code for my project (especially see last method):
in my project I use a RC522 RFID module and a SSD1306 OLED display on a ESP8266. When a RFID tag is detected on the RFID module, I want something to be displayed on the screen.
The RFID module and the OLED display both work, but only individually. If I use both at the same time, e.g. after a RFID tag has been detected and I want to display text on the screen, it does not work.
Of course, the problem comes from the fact that the screen and the RFID reader use the same pins. Both use for SDA PIN D2 on the ESP8266 and for SCK PIN D1. The different protocols seem to interfere with each other. However, not until sometime. So I can display something on the screen in setup() and then use the RFID module. However, I cannot display anything on the screen after using the RFID module. When I tried, the RFID module did not work after that.
My solution idea was to end the SPI communication with SPI.endTransaction() before communication via I2C and the other way around to end the I2c communication with Wire.endTransmission() before using SPI. I also thought of the corresponding start commands. However, it does not work.
I am curious if there is a simple solution for this. After hours of Google I didn't manage to find one. I'm still relatively new to microelectronics, as you can probably tell.
I look forward to answers and thank you very much in advance.
Here is the code for my project (especially see last method):
Code: Select all
//RFID-MODUL
#define RST_PIN 5
#define SS_PIN 4
#include <SPI.h>
#include <MFRC522.h>
MFRC522 mfrc522(SS_PIN, RST_PIN);
//OLED-DISPLAY
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#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 <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
// 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[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], 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);
}