ESP8266Audio
ESP8266Audio copied to clipboard
esp8266audio + IR remote sensor not working
i was try to create a queuing announcer where when i press ir remote, input the queue number, press OK, it will playback the mp3 files contain the numbers. after i press OK button, it does loop and said the queue number properly, but then i cannot input anymore, like the IR receiver has been not initialize. even i add ESP.restart() at the end of play, still not called, like ignored
here's my code so far
`#include <IRremoteESP8266.h> #include <Arduino.h> #ifdef ESP32 #include <WiFi.h> #include "SPIFFS.h" #else #include <ESP8266WiFi.h> #endif #include "AudioFileSourceSPIFFS.h" #include "AudioFileSourceID3.h" #include "AudioGeneratorMP3.h" #include "AudioOutputI2SNoDAC.h" #include "AudioFileSourceBuffer.h"
// To run, set your ESP8266 build to 160MHz, and include a SPIFFS of 512KB or greater. // Use the "Tools->ESP8266/ESP32 Sketch Data Upload" menu to write the MP3 to SPIFFS // Then upload the sketch normally.
// pno_cs from https://ccrma.stanford.edu/~jos/pasp/Sound_Examples.html
//-------------------------------------------------------------- IR global variable #define RECV_PIN D4 //an IR detector/demodulatord is connected to GPIO pin 2
IRrecv irrecv(RECV_PIN); decode_results results;
int remoteInput; String displayNumber; bool okBtn = false;
int song[10]; int i; int loopTrack = 0;
AudioGeneratorMP3 *mp3; AudioFileSourceSPIFFS *file; AudioFileSourceBuffer *buff; AudioOutputI2SNoDAC *out;
int playflag = 0;
void setup() { WiFi.mode(WIFI_OFF); Serial.begin(115200); delay(1000); irrecv.enableIRIn(); // Start the receiver
SPIFFS.begin(); out = new AudioOutputI2SNoDAC(); }
void loop() { if (playflag == 0) { if (irrecv.decode(&results)) { irDecoder(results.value); // display.showNumberDec(displayNumber.toInt()); //Display the numCounter value; irrecv.resume(); // Receive the next value } remoteInput = 0;
if (okBtn) {
playSound(song[0]);
playflag = 1;
}
}
if (playflag == 1) { if (mp3->isRunning()) { if (!mp3->loop())mp3->stop(); } else { Serial.printf("MP3 done\n"); StopPlaying(); delay(200); playflag = 0; okBtn = false; return; } if (okBtn == false) { Serial.printf("restart ESP\n"); ESP.restart(); } } }
void playSound(int track) { Serial.print("play track "); Serial.println(track); if (track == 1) file = new AudioFileSourceSPIFFS("/0001.mp3"); else if (track == 2) file = new AudioFileSourceSPIFFS("/0002.mp3"); else if (track == 3) file = new AudioFileSourceSPIFFS("/0003.mp3"); else if (track == 4) file = new AudioFileSourceSPIFFS("/0004.mp3"); else if (track == 5) file = new AudioFileSourceSPIFFS("/0005.mp3"); else if (track == 6) file = new AudioFileSourceSPIFFS("/0006.mp3"); else if (track == 7) file = new AudioFileSourceSPIFFS("/0007.mp3"); else if (track == 8) file = new AudioFileSourceSPIFFS("/0008.mp3"); else if (track == 9) file = new AudioFileSourceSPIFFS("/0009.mp3"); else if (track == 10) file = new AudioFileSourceSPIFFS("/0010.mp3"); else if (track == 11) file = new AudioFileSourceSPIFFS("/0011.mp3"); else if (track == 12) file = new AudioFileSourceSPIFFS("/0012.mp3"); else if (track == 13) file = new AudioFileSourceSPIFFS("/0013.mp3"); else if (track == 14) file = new AudioFileSourceSPIFFS("/0014.mp3"); else if (track == 15) file = new AudioFileSourceSPIFFS("/0015.mp3"); else file = new AudioFileSourceSPIFFS("/0101.mp3"); buff = new AudioFileSourceBuffer(file, 2048); mp3 = new AudioGeneratorMP3(); mp3->begin(buff, out); }
void StopPlaying() { if (mp3) { mp3->stop(); delete mp3; mp3 = NULL; } if (buff) { buff->close(); delete buff; buff = NULL; } if (file) { file->close(); delete file; file = NULL; } Serial.printf("STATUS(Stopped)\n"); Serial.flush(); }
i use wemos and i2CnoDAC since it's hard to find i2c converter locally i already use usb from computer also psu, but still same sorry if my english bad.