Chat here is you are connecting ESP-xx type modules to existing AVR based Arduino

Moderator: igrr

User avatar
By JorgenVikingGod
#26379 I try to use RFID module (SPI) and OLED display (I2C)
both are working as standalone - but if I connect both and want to use them, nothing works.
In the case I disconnect and comment out the initial of the display - the RFID module works and if I only have initial the display, it lights up and worked.

Have anyone an idea - why only one serial peripheral is working the same time?

Thanks,
Jorgen
User avatar
By JorgenVikingGod
#26431 I know that they are different - but I can only see what I can see - and it seems that the ESP8266 can not handle multiple serials - only one at a time.

Wiring is following:
https://github.com/Jorgen-VikingGod/ESP8266-MFRC522

wiring the MFRC522 to ESP8266 (ESP-12)
RST = GPIO15
SDA(SS) = GPIO2
MOSI = GPIO13
MISO = GPIO12
SCK = GPIO14
GND = GND
3.3V = 3.3V


wiring the OLED display to ESP8266 (ESP-12)
RST = GPIO16 (not used)
SDA = GPIO4 // SDA-PIN for I2C OLED
SCL = GPIO5 // SCL-PIN for I2C OLED
GND = GND
3.3V = 3.3V


Here is a short demo source code:
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <SPI.h>
#include <Wire.h>
#include <MFRC522.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 16  // RST-PIN for OLED (not used)
#define OLED_SDA    4  // SDA-PIN for I2C OLED
#define OLED_SCL    5  // SCL-PIN for I2C OLED
Adafruit_SSD1306 display(OLED_RESET);

#define RST_PIN   15  // RST-PIN for RC522 - RFID - SPI - Modul GPIO15
#define SS_PIN     2  // SDA-PIN for RC522 - RFID - SPI - Modul GPIO2
MFRC522::Uid oldUid;
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

const char* ssid     = "YOUR-SSID";
const char* password = "YOUR-PASSWD";

const char* host     = "192.168.0.x";
const char* client1  = "192.168.0.1";
const char* client2  = "192.168.0.2";

WiFiClient client;

void setup() {
  Serial.begin(115200);
  delay(250);

  // initial SPI bus for RFID module
  SPI.begin();           // Init SPI bus
  mfrc522.PCD_Init();    // Init MFRC522

  // initial I2C bus and OLED display
  Wire.begin(OLED_SDA, OLED_SCL);
  Wire.setClock(400000);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x64)

  Serial.print("connecting");
  display.clearDisplay();
  // text display tests
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("connecting...");
  display.display();

  WiFi.begin(ssid, password);

  int retries = 0;
  while ((WiFi.status() != WL_CONNECTED) && (retries < 10)) {
    retries++;
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  display.clearDisplay();
  display.setCursor(0,0);
  display.println("WiFi connected");
  display.println("IP address: ");
  display.print(WiFi.localIP());
  display.display();

  delay(5000);
}

int value = 0;

void loop() {

  display.clearDisplay();
  display.setCursor(0,0);
  display.println("Processing RFID");
  display.display();

// Look for new cards
  if (!mfrc522.PICC_IsNewCardPresent()) {
    Serial.println("!mfrc522.PICC_IsNewCardPresent()");
    delay(500);
    return;
  }
  // Select one of the cards
  if (!mfrc522.PICC_ReadCardSerial()) {
    Serial.println("!mfrc522.PICC_ReadCardSerial()");
    delay(500);
    return;
  }
  // only process new cards
  if (mfrc522.uid.uidByte == oldUid.uidByte && mfrc522.uid.size == oldUid.size && mfrc522.uid.sak == oldUid.sak) {
    Serial.println("mfrc522.uid.uidByte == oldUid.uidByte && mfrc522.uid.size == oldUid.size && mfrc522.uid.sak == oldUid.sak");
    delay(500);
    return;
  }
  Serial.println("woohooo");

// Show some details of the PICC (that is: the tag/card)
  display.clearDisplay();
  display.println("Card UID:");
  Serial.print("Card UID:");
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println();
  display.display();
  // remember old card
  rememberCard();

  delay(2000);

#if 0
  delay(3000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);

  // Use WiFiClient class to create TCP connections

  const int httpPort = 3000;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  // We now create a URI for the request
  String url = "/vote/client/";
  if (value % 2)
    url += client1;
  else
    url += client2;
  /*
  url += "?private_key=";
  url += privateKey;
  url += "&value=";
  url += value;
  */
  display.clearDisplay();
  display.setCursor(0,0);
  display.print("Requesting URL: ");
  display.println(url);
  display.display();

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(1000);

  display.println("response: ");
  Serial.println("response:");

  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');

    display.print(line);
    Serial.print(line);
  }
  display.display();

  delay(2000);
#endif 
}

// Helper routine to dump a byte array as hex values to Serial
void dump_byte_array(byte *buffer, byte bufferSize) { 
  for (byte i = 0; i < bufferSize; i++) {
    display.print(buffer[i] < 0x10 ? " 0" : " ");
    display.print(buffer[i], HEX);
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

// Helper method to copy card information
void rememberCard() {
  oldUid.size    = mfrc522.uid.size;
  oldUid.sak     = mfrc522.uid.sak;
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    oldUid.uidByte[i] = mfrc522.uid.uidByte[i];
  }
}
User avatar
By martinayotte
#26471 There must be a glitch somewhere... :?

It is hard for me to help you figure it out, since I have neither MFRC522 or SSD1306.

But I can say that I'm using a MCP23017 I2C GPIO Expander along with a SDCard on SPI, and they are working fine even if I used both of them in the same sketch.