Adafruit is open, safely ... and shipping all orders at this time! Read more!
0

ESP32 using SPIFFS and IRremote.h
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

ESP32 using SPIFFS and IRremote.h

by JackTheSE on Thu Nov 07, 2019 5:04 pm

I'm well aware of the ESP32 conflict between the IRremote.h library, and anything that uses flash memory, like SPIFFS.h and EEPROM.h . I bought the Feather HAZZAH ESP 32 for a WiFi and IOAdafruit project. My sketch works fine using SPIFFS.h to save some config data, but only if I comment out the line "myIRrcvr.enableIRIn();". The problem is there is no equivalent disable function like "myIRreceiver.disableIRIn();". Is there another way to suspend or kill the IR object while I take care of SPIFFS business, then re-establish it afterwards?

Second question: Aside from the Feather HAZZAH ESP32 and the Feather HAZZAH ESP8266 (I have both), does Adafruit have a microcontroller, other than the HAZZAHs, that can use a WiFi shield of some sort? Some combination that works with WiFi.h, EEPROM.h and IRremote.h?

JackTheSE
 
Posts: 27
Joined: Wed Jan 02, 2019 8:34 pm

Re: ESP32 using SPIFFS and IRremote.h

by JackTheSE on Thu Nov 07, 2019 5:26 pm

Just to move this project along a bit: Would a Metro M0 Express used with a WINC1500 WiFi shield have a chance of working properly?
I would just be using the Arduino IDE/programming environment. Could I just use the standard Arduino IRremote.h, EEPROM.h and WiFi.h libraries, or is something special needed? Thanks in advance...Jack

JackTheSE
 
Posts: 27
Joined: Wed Jan 02, 2019 8:34 pm

Re: ESP32 using SPIFFS and IRremote.h

by adafruit_support_mike on Fri Nov 08, 2019 3:50 am

Most of our microcontrollers work with the WINC1500 Wifi breakout, using the Arduino Wif101 library for software support.

I don't know of any conflicts between Wifi101 and other libraries, but also don't know if they're compatible.

adafruit_support_mike
 
Posts: 61107
Joined: Thu Feb 11, 2010 2:51 pm

Re: ESP32 using SPIFFS and IRremote.h

by JackTheSE on Sun Dec 08, 2019 2:38 pm

Well I found a simple 5-line modification to the IRremote library that makes SPIFFS work OK with the remote. Basically it implements a disable command that turns off the IR timers that cause the problems for the ESP32. The modifications were inspired by this linkhttps://hackaday.io/project/46280-muffsy-stereo-relay-input-selector/log/156747-full-functionality-rotational-encoder-and-remote-control, although the standard Arduino IRremote library was laid out somewhat differently than the one used in the link. My test code follows in three blocks (main program and two supporting tabs).
Code: Select all | TOGGLE FULL SIZE
#include <IRremote.h>                 // IR Remote Library
#include "FS.h"
#include "SPIFFS.h"                   // SPIFFS library

const int IR_RcvrPin = 26;            // IR receiver pin
#include "IRCommands.h"               // Header tab for IR Commands

bool SPIFFSMounted=false;
bool IR_Suspended = true;             // IR commands disabled initially
bool SPIFFS_On = false;               // SPIFFS not on initially

void setup() {
  Serial.begin(9600);
  pinMode(IR_RcvrPin, INPUT);
  Serial.println("Using SPIFFS in setup() before IR enabled");
  SPIFFS_Init();                      // SPIFFS initialization routine
  Config_Save();                      // Demo SPIFFS writes work if IR commanding off
  Config_Get();                       // Demos a read of data just saved
}

void loop() {
  if (SPIFFS_On) {                                // True if using SPIFFS
    Serial.println("Enabling SPIFFS");
    SPIFFS_Init();                                // SPIFFS initialization routine
    Config_Save();                                // Demo SPIFFS writes work if IR commanding off
    Config_Get();                                 // Demos a read of data just saved
    SPIFFS_On = false;                            // Flag SPIFFS as being off
  }
  if (!SPIFFS_On) {                               // true if not using SPIFFS
    IRrecv myIRreceiver(IR_RcvrPin);              // Create new IR receiver object.
    decode_results results;                       // IR code is in results.value after button push.
    if (IR_Suspended) {
      myIRreceiver.enableIRIn(true);              // Enable IR commanding
      Serial.println("IR remote enabled");
      IR_Suspended = false;
    }
    delay(300);                                   // Seems to be needed to get IR commands
    if (myIRreceiver.decode(&results)) {          // returns TRUE if IR command received.
      switch (results.value) {
        case Button_1:                            // Turns off IR commanding so SPIFFS can work
          Serial.printf("Button 1 pushed: IR commanding off to demo SPIFFS writes\r\n");
          myIRreceiver.enableIRIn(false);         // Disable IR commanding
          IR_Suspended = true;
          SPIFFS_On = true;
          break;
        case Button_2:                            // Demos that IR commanding on
          Serial.println("Button 2 pushed.");
      }
    }
    myIRreceiver.resume();
  }
}


Code: Select all | TOGGLE FULL SIZE
void SPIFFS_Init() {                             // open SPIFFS, or establish if needed
  if (SPIFFS.begin(true)) {
    Serial.println("SPIFFS Mounted Successfully");
    SPIFFSMounted = true;
  } else {
    Serial.println("SPIFFS Mount Failed");
  }
}

void Config_Save() {                             // save data to SPIFFS
  char myfile[] = "/test.txt";
  Serial.println("Saving data to SPIFF:");       // make sure SPIFFS is mounted
  if (!SPIFFSMounted) {
    SPIFFS_Init();
  }
  Serial.printf("Opening file %s\r\n", myfile);
  File fout = SPIFFS.open(myfile, FILE_WRITE);
  if (fout) {
    Serial.printf("- file %s opened for writing successfully\n", myfile);
  } else {
    Serial.printf("- failed to  open file %s successfully\n", myfile);
    return;
  }
  int iRout = 3;                                // typical write commands...
  Serial.print("Saving iRout...");
  if (fout.printf("% d\n", iRout)) {
    Serial.println("iRout written");
  } else {
    Serial.println("iRout write failed");
  }
  //  for (int irec = 1; irec < NTP1; irec++) {
  //    fout.printf(" % d\n", tmr[irec].relay[0]);
  //  }
  Serial.printf(" % s saved successfully\n", myfile);
  fout.close();
  listDir(SPIFFS, " / ", 0);
}

void Config_Get() {                             // get data from SPIFFS
  char myfile[] = " / test.txt";
  Serial.print("Recovering saved info...");
  if (!SPIFFSMounted) {
    SPIFFS_Init();
  }
  listDir(SPIFFS, " / ", 0);                      // list directory at root
  File fin = SPIFFS.open(myfile);
  if (fin) {
    Serial.printf(" - file % s opened for reading successfully\n", myfile);
  } else {
    Serial.printf(" - failed to open file % s\n", myfile);
    return;
  }
  Serial.print("Reading iRout = ");
  int iRout = fin.readStringUntil('\n').toInt();// typical read commands...
  Serial.println(iRout);
  //  for (int irec = 1; irec < NTP1; irec++) {
  //    tmr[irec].relay[0] = fin.readStringUntil('\n').toInt();
  //  }
  fin.close();
}

void listDir(fs::FS & fs, const char * dirname, uint8_t levels) { // List root directory
  Serial.printf("File Directory : % s\r\n", dirname);
  File root = fs.open(dirname);
  if (!root) {
    Serial.println(" - failed to open directory");
    return;
  }
  if (!root.isDirectory()) {
    Serial.println(" - not a directory");
    return;
  }

  File file = root.openNextFile();
  while (file) {
    if (file.isDirectory()) {
      Serial.print("  DIR : ");
      Serial.println(file.name());
      if (levels) {
        listDir(fs, file.name(), levels - 1);
      }
    } else {
      Serial.print("  FILE : ");
      Serial.print(file.name());
      Serial.print("\tSIZE : ");
      Serial.println(file.size());
    }
    file = root.openNextFile();
  }
}

void deleteFile(fs::FS & fs, const char * path) {// delete a SPIFFS file
  Serial.printf("Deleting file : % s\r\n", path);
  if (fs.remove(path)) {
    Serial.println(" - file deleted");
  } else {
    Serial.println(" - delete failed");
  }
}


Code: Select all | TOGGLE FULL SIZE
// IR Remote Declarations for three different remotes.
#define USED_REMOTE 17

#if USED_REMOTE==21
// Twenty One button Remote
# define Button_0     0xFF6897
# define Button_1     0xFF30CF
# define Button_2     0xFF18E7
# define Button_3     0xFF7A85
# define Button_4     0xFF10EF
# define Button_5     0xFF38C7
# define Button_6     0xFF5AA5
# define Button_7     0xFF42BD
# define Button_8     0xFF4AB5
# define Button_9     0xFF52AD
# define Squiggle     0xFF9867
# define U_SD         0xFFB04F
# define EQ           0xFFE01F
# define Minus        0xFFA857
# define Plus         0xFF906F
# define PlayPause    0xFF22DD
# define Rewind       0xFF02FD
# define FastFwd      0xFFC23D
# define Power        0xFFA25D
# define Mode         0xFF629D
# define Mute         0xFFE21D
#endif

#if USED_REMOTE == 17
// Seventeen button (with navigation arrows)
# define Button_0       0xFF9867
# define Button_1       0xFFA25D
# define Button_2       0xFF629D
# define Button_3       0xFFE21D
# define Button_4       0xFF22DD
# define Button_5       0xFF02FD
# define Button_6       0xFFC23D
# define Button_7       0xFFE01F
# define Button_8       0xFFA857
# define Button_9       0xFF906F
# define Star           0xFF6897
# define Pound          0xFFB04F
# define UpArrow        0xFF18E7
# define LeftArrow      0xFF10EF
# define OK             0xFF38C7
# define RightArrow     0xFF5AA5
# define DownArrow      0xFF4AB5
#endif

// AdafruitIO remote returns (Haven't done much with this one yet)
#define UpArrow_IO      5
#define IO_DownArrow_IO 13



Hope this helps someone out there...

JackTheSE
 
Posts: 27
Joined: Wed Jan 02, 2019 8:34 pm

Re: ESP32 using SPIFFS and IRremote.h

by JackTheSE on Sun Dec 08, 2019 2:52 pm

Here are the mods I did to the IR library, using NotePad++...
in IRremote.h, line 179, changed
void enableIRIn () ;
to
void enableIRIn (bool enable) ;

In irRecv.cpp, lines 123 through 140 changed to

Code: Select all | TOGGLE FULL SIZE
void  IRrecv::enableIRIn (bool enable)
{
// Interrupt Service Routine - Fires every 50uS
#ifdef ESP32
   // ESP32 has a proper API to setup timers, no weird chip macros needed
   // simply call the readable API versions :)
   // 3 timers, choose #1, 80 divider nanosecond precision, 1 to count up
   if (enable){
   timer = timerBegin(1, 80, 1);
   timerAttachInterrupt(timer, &IRTimer, 1);
   // every 50ns, autoreload = true
   timerAlarmWrite(timer, 50, true);
   timerAlarmEnable(timer);
   }
   else {
      timerEnd(timer);
      timerDetachInterrupt(timer);
   }

JackTheSE
 
Posts: 27
Joined: Wed Jan 02, 2019 8:34 pm

Re: ESP32 using SPIFFS and IRremote.h

by zener on Sun Dec 08, 2019 2:59 pm

I am sure that will help someone!

Maybe you could help this person from a thread a week ago. I was not able to provide much help.

https://forums.adafruit.com/viewtopic.php?f=8&t=159313

zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Please be positive and constructive with your questions and comments.