Esp8266 NFC multi door lock system



Neither does SPIFFS unless you ask for it to be erased.



I changed the file output to the SPIFFS.

I made it look as close to the FRAM as I could so that I did not need to change the code to change where i store the data.

bool openspiffs(){

    datafile =, "r+b");        // exsisting file opened
    if (!datafile){
          datafile =, "w+b");  // create new file
  return datafile;

void closespiffs(){

    if (datafile){

void writestringdata(uint16_t whatbyte, int howlong, String databuffer){
    byte s[maxnamelength+1];, SeekSet);
    databuffer.getBytes(s, maxnamelength);     
    datafile.write(s, howlong);

String readstringdata(uint16_t whatbyte, int howlong){
  char s[maxnamelength+1];
  byte s2[maxnamelength+1];, SeekSet);, howlong);
  for ( int i = 0; i < howlong; i++)
    s[i] = char(s2[i]);
  return String(s);

uint16_t read16data(uint16_t whatbyte){
  uint16_t out;
  uint8_t high;
  uint8_t low;, SeekSet);, 1);, 1);

  out = high << 8;
  out += low;
  return out;

void write16data(uint16_t whatbyte, uint16_t databuffer){
  uint8_t high;
  uint8_t low;
  high = highByte(databuffer);
  low = lowByte(databuffer);, SeekSet);
  datafile.write(&high, 1);
  datafile.write(&low, 1);  

void getdatafromfile(){
  if (openspiffs()){
  rowIndex = read16data(0);                                                   //read the card count from fram 
  uint16_t buffer_offset = recordlength;                                    //start at record 1
    Blynk.virtualWrite(vcount, rowIndex);       // write the rowIndex to the server
  for (int i = 1; i <= rowIndex; i++){                                             // read all the card data
//  read16data(buffer_offset, tempindex);                                       // skip the index
    buffer_offset += 2;
    cardHolder[i] = readstringdata(buffer_offset, maxnamelength);
    buffer_offset += maxnamelength;
    cardId[i] = readstringdata(buffer_offset, maxuidlength);
    buffer_offset += maxuidlength;
    accessFlags[i] = read16data(buffer_offset);
    buffer_offset += 2;

void dumpdatafromfile(){
  String ch;
  String cId;
  int af;
  int ri;
  int tempIndex;
  uint16_t buffer_offset = recordlength;                                    //start at record 1

    if ( openspiffs()){
    tempIndex = read16data(0);                                                   //read the card count from fram address 0
    terminal.println("index "+String(tempIndex));
//  for (int i = 1; i <= tempIndex; i++){                                             // read all the card data
    ri = read16data(buffer_offset);
    buffer_offset += 2;
    ch = readstringdata(buffer_offset, maxnamelength);
    buffer_offset += maxnamelength;
    cId = readstringdata(buffer_offset, maxuidlength);
    buffer_offset += maxuidlength;
    af = read16data(buffer_offset);
    buffer_offset += 2;
    terminal.println("index: " + String(ri) + " holder:  " + ch + " ID:  " + cId + " flags: " + String(af));
//  }


I posted the project on github


Added OTA for updates and fixed a few bugs. Updated all the hardware to use the SPIFFS memory. I am now working on the hardware getting it ready for permanent installation. I will post new code on github tonight,


ESP8266, power supply and relay installed in electrical box. The NFC module will be installed in a second box outside the door.



Updating the code to make the number of locks unlimited. I should have it up in a day or two.


Project now supports unlimited locks. Copy all is now working. Locks use wifi manager to set the BLYNK tokens for the master and current lock as well as connect to the wifi.

It does have a problem if the device does not connect to the wifi. I think I will work on making the wifi connection non blocking and use provision on demand.