I_Mihalache_AudioPlayer.ino 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #include <Audio.h>
  2. #include <Wire.h>
  3. #include <SPI.h>
  4. #include <SD.h>
  5. #include <SerialFlash.h>
  6. #include <Metro.h>
  7. //Sensor smoothing and hysteresys
  8. #include <SetPoint.h>
  9. #include <ResponsiveAnalogRead.h>
  10. /* ESP commands
  11. * AT+MODE=1
  12. * AT+CWLAP lists wifi AP
  13. * AT+CWJAP="IM_AP","raspberry" connect to IM ap
  14. * AT+CIFSR get IP
  15. * AT+CIPSTART="UDP","192.168.3.1",8000 connect to **
  16. *
  17. */
  18. /* SdWav1 : sample voix |
  19. * SdWav2 : sample attente |-> mix1 -> Lout & RMS1
  20. *
  21. * SdWav3 : sample ambiance -> mix2 -> Rout & RMS2
  22. */
  23. // GUItool: begin automatically generated code
  24. AudioPlaySdWav LoopPlayer; //xy=153,217
  25. AudioPlaySdWav PresencePlayer; //xy=155,307
  26. AudioMixer4 mixer1; //xy=572,215
  27. AudioMixer4 mixer2; //xy=579,344
  28. AudioAnalyzeRMS rms1; //xy=875,315
  29. AudioOutputI2S i2s1; //xy=876,225
  30. AudioAnalyzeRMS rms2; //xy=876,365
  31. AudioConnection patchCord1(PresencePlayer, 0, mixer1, 0);
  32. AudioConnection patchCord2(LoopPlayer, 0, mixer1, 1);
  33. AudioConnection patchCord3(LoopPlayer, 1, mixer2, 0);
  34. AudioConnection patchCord4(mixer1, 0, i2s1, 0);
  35. AudioConnection patchCord5(mixer1, rms1);
  36. AudioConnection patchCord6(mixer2, 0, i2s1, 1);
  37. AudioConnection patchCord7(mixer2, rms2);
  38. AudioControlSGTL5000 sgtl5000_1; //xy=573,537
  39. // GUItool: end automatically generated code
  40. // Config variables
  41. int configPort = 8001 ;
  42. String ID = "ina" ;
  43. //float hysteresis = 5;
  44. bool UDPalive ;
  45. Metro checkUDP = Metro(1000);
  46. Metro checkESPserial = Metro(50);
  47. Metro sendRMS = Metro(50);
  48. //#define LOOP_WAV "LOOP2.WAV"
  49. //#define PRESENCE_WAV "PRESENCE.WAV"
  50. //#define WAIT_WAV "WAIT.WAV"
  51. String LOOP_WAV;
  52. String PRESENCE_WAV ;
  53. #define configFileName "config.txt"
  54. int WAIT_TIME=10000 ;
  55. const int LDR_PIN = A7;
  56. ResponsiveAnalogRead ldr(LDR_PIN, false);
  57. float threshold = 935;
  58. float hysteresis = 5;
  59. int sensorPin = A7;
  60. SetPoint setPoint;
  61. bool presence ;
  62. unsigned long int LastPresence ;
  63. // Use these with the Teensy Audio Shield
  64. #define SDCARD_CS_PIN 10
  65. #define SDCARD_MOSI_PIN 7
  66. #define SDCARD_SCK_PIN 14
  67. ////////////////////////////// CONFIG FUNCTIONS ///////////////////////////
  68. String getSubstring(String dataString, char separator, int index)
  69. {
  70. int found = 0;
  71. int strIndex[] = { 0, -1 };
  72. int maxIndex = dataString.length() - 1;
  73. for (int i = 0; i <= maxIndex && found <= index; i++) {
  74. if (dataString.charAt(i) == separator || i == maxIndex) {
  75. found++;
  76. strIndex[0] = strIndex[1] + 1;
  77. strIndex[1] = (i == maxIndex) ? i+1 : i;
  78. }
  79. }
  80. return found > index ? dataString.substring(strIndex[0], strIndex[1]) : "";
  81. }
  82. bool readSDconfig(){
  83. File configFile = SD.open(configFileName);
  84. while (configFile.available()) { //as long as there is data available
  85. String buf = configFile.readStringUntil('\n'); // read a line
  86. String keyword = getSubstring(buf, ' ', 0); // update variable according to keyword
  87. if (keyword == "port") { configPort = getSubstring(buf, ' ', 1).toInt() ; Serial.print("port : ") ; Serial.println(configPort); }
  88. if (keyword == "hysteresis") { hysteresis = getSubstring(buf, ' ', 1).toInt() ; Serial.print("hysteresis : ") ; Serial.println(hysteresis);}
  89. if (keyword == "ID") {
  90. ID = getSubstring(buf, ' ', 1) ;
  91. Serial.print("ID : ") ;
  92. Serial.println(ID);
  93. LOOP_WAV = ID + "_L.wav";
  94. PRESENCE_WAV = ID + "_A.wav" ;
  95. }
  96. }
  97. configFile.close();
  98. }
  99. ////////////////////////////// LDR & AUDIO FUNCTIONS ///////////////////////////
  100. void playFile(const char *filename)
  101. {
  102. Serial.print("Playing file: ");
  103. Serial.println(filename);
  104. PresencePlayer.play(filename);
  105. // A brief delay for the library read WAV info
  106. delay(5);
  107. }
  108. void printLDR(){
  109. Serial.print(analogRead(A3));
  110. Serial.print(" ");
  111. Serial.print(digitalRead(16));
  112. Serial.print(" ");
  113. Serial.print(analogRead(A7));
  114. Serial.print(" ");
  115. Serial.print(digitalRead(20));
  116. Serial.println();
  117. }
  118. bool checkPresence(int Pin1, int Pin2) {
  119. bool isPresent = analogRead(A7)>1010;
  120. //digitalRead(Pin2); //|| digitalRead(Pin2);
  121. Serial.println(isPresent);
  122. return isPresent ;
  123. }
  124. ////////////////////////////// ESP FUNCTIONS ///////////////////////////
  125. void ESPSerial(){
  126. // Send bytes from ESP8266 -> Teensy to Computer
  127. bool printLine ;
  128. while ( Serial1.available() ) {
  129. //String buf = Serial1.read();
  130. //Serial.write(F(buf));
  131. Serial.write( Serial1.read() );
  132. // if(buf.readStringUntil("\n")=="OK") {Serial.println("trouvé");}
  133. printLine = 1 ;
  134. }
  135. // if (printLine) {Serial.println("");}
  136. printLine = 0;
  137. // Send bytes from Computer -> Teensy back to ESP8266
  138. while ( Serial.available() ) {
  139. Serial1.write( Serial.read() );
  140. }
  141. }
  142. bool checkUDPconnected() {
  143. //Serial.println ("checking UDP");
  144. Serial1.println("AT+CIPSTATUS");
  145. delay(50);
  146. while ( Serial1.available() ) {
  147. String buf = Serial1.readStringUntil('\n'); //StringUntil('\n') ;
  148. if(buf.startsWith("STATUS:2")){
  149. Serial.println("connected");
  150. return true ;
  151. }
  152. else
  153. {
  154. int port = configPort;
  155. Serial1.print("AT+CIPSTART=\"UDP\",\"192.168.3.1\",");
  156. Serial1.println(port);
  157. Serial.println ("Reconnecting UDP");
  158. ESPSerial();
  159. return false;
  160. }
  161. }
  162. }
  163. void sendUDP() {
  164. if(rms1.available() ) { //&& UDPalive
  165. Serial1.println("AT+CIPSEND=3");
  166. delay(20);
  167. Serial1.write(byte(rms1.read()*255));
  168. Serial1.println("");
  169. //Serial.println("udp");
  170. }
  171. }
  172. ////////////////////// LDR event handlers /////////////////////////////
  173. void Presence() {
  174. Serial.println("Presence detected");
  175. presence = true ;
  176. if (!PresencePlayer.isPlaying()) { //!PresencePlayer.isPlaying()) {
  177. // Fade out wait file
  178. Serial.println("Fade out Wait file");
  179. for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
  180. mixer1.gain(1, 1.0 - fadeRamp/99) ;
  181. }
  182. // start presence file
  183. mixer1.gain(0, 1.0) ;
  184. PresencePlayer.play(PRESENCE_WAV.c_str()) ;
  185. delay(15);
  186. Serial.println("Playing presence file");
  187. }
  188. }
  189. void Absence() {
  190. LastPresence = millis() ;
  191. presence = false ;
  192. Serial.println("No presence detected");
  193. // if (PresencePlayer.isPlaying()) {
  194. // // if presence gone while playing ?
  195. // Serial.println("Fading out Presence file");
  196. // for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
  197. // mixer1.gain(0, 1.0 - fadeRamp/100) ;
  198. // delay(10);
  199. // }
  200. //
  201. //
  202. //
  203. // }
  204. }
  205. /////////////////////////////// SETUP /////////////////////////
  206. void setup() {
  207. Serial.begin(115200);
  208. Serial1.begin(115200);
  209. //LDR digital inputs
  210. pinMode(16, INPUT);
  211. pinMode(20,INPUT);
  212. // load SD config file
  213. SPI.setMOSI(SDCARD_MOSI_PIN);
  214. SPI.setSCK(SDCARD_SCK_PIN);
  215. if (!(SD.begin(SDCARD_CS_PIN))) {
  216. // stop here, but print a message repetitively
  217. while (1) {
  218. Serial.println("Unable to access the SD card");
  219. delay(500);
  220. }
  221. }
  222. readSDconfig();
  223. delay(500);
  224. // Start audio
  225. AudioMemory(16);
  226. sgtl5000_1.enable();
  227. sgtl5000_1.lineOutLevel(13); // set output to 3.16 Vpp
  228. sgtl5000_1.volume(1); //set volume to maximum
  229. Serial.print("Playing LOOP file ");
  230. Serial.println(LOOP_WAV);
  231. LoopPlayer.play(LOOP_WAV.c_str());
  232. // LoopPlayer.play(PRESENCE_WAV.c_str());
  233. // A brief delay for the library read WAV info
  234. delay(10);
  235. // //Start sensors for calibration
  236. // setPoint.begin(threshold, hysteresis);
  237. // setPoint.attach(RISING_EDGE, Presence);
  238. // setPoint.attach(FALLING_EDGE, Absence);
  239. //ESP8266
  240. Serial1.println("AT+GMR");
  241. //while (!Serial1.available()) {};
  242. delay(200);
  243. ESPSerial();
  244. Serial.println("");
  245. Serial.println("");
  246. float sum ;
  247. float counter ;
  248. while(millis()<10000){ // wait for ESP to connect to its configured wifi
  249. //make a mean value of LDR readings in between
  250. ldr.update();
  251. sum += ldr.getValue();
  252. counter++;
  253. delay(100);
  254. }
  255. threshold = sum / counter + hysteresis/2 ;
  256. Serial.print("Threshold calibrated at : ");
  257. Serial.println(threshold);
  258. Serial1.println("AT+CIFSR");
  259. ESPSerial();
  260. delay(200);
  261. Serial.println ("");
  262. int port = configPort;
  263. Serial1.print("AT+CIPSTART=\"UDP\",\"192.168.3.1\",");
  264. Serial1.println(port);
  265. ESPSerial();
  266. Serial.println ("");
  267. // Start sensors handlers
  268. setPoint.begin(threshold, hysteresis);
  269. setPoint.attach(RISING_EDGE, Presence);
  270. setPoint.attach(FALLING_EDGE, Absence);
  271. setPoint.update(0); // force no presence state at startup
  272. }
  273. ///////////////////////////////////////////////////////////////////
  274. /////////////////////////////// LOOP ////////////////////////////////////
  275. void loop() {
  276. // LOOP FILE
  277. ldr.update();
  278. int value = ldr.getValue();
  279. //Serial.println(value);
  280. setPoint.update(value);
  281. // LOOP is always playing
  282. if (!LoopPlayer.isPlaying()) {
  283. Serial.println("Playing LOOP file");
  284. LoopPlayer.play(LOOP_WAV.c_str());
  285. // A brief delay for the library read WAV info
  286. delay(10);
  287. }
  288. if (presence ) { //|| millis()-LastPresence < WAIT_TIME
  289. // update presence timer
  290. LastPresence = millis() ;
  291. // restart presence player if ended
  292. if (!PresencePlayer.isPlaying()) {
  293. PresencePlayer.play(PRESENCE_WAV.c_str());
  294. delay(10);
  295. }
  296. }
  297. //WAIT fades up if no presence during longer than WAIT_TIME
  298. if (millis()-LastPresence > WAIT_TIME && presence == false ) { // &&
  299. if (PresencePlayer.isPlaying()){
  300. Serial.println("Stopping PRESENCE file");
  301. Serial.println("Fading down presence and fading up WAIT file");
  302. for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
  303. mixer1.gain(1, fadeRamp/99) ;
  304. mixer1.gain(0, 1.0 - fadeRamp/99) ;
  305. delay(10);
  306. }
  307. }
  308. // Serial.println("Stopping PRESENCE file");
  309. //
  310. // Serial.println("Fading down presence and fading up WAIT file");
  311. // for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
  312. // mixer1.gain(1, fadeRamp/100) ;
  313. // mixer1.gain(0, 1.0 - fadeRamp/100) ;
  314. //
  315. // }
  316. PresencePlayer.stop();
  317. delay(10);
  318. //Serial.println(PresencePlayer.isPlaying());
  319. // }
  320. // PresencePlayer.stop();
  321. }
  322. // // PRESENCE FILE
  323. // presence = checkPresence(16, 20);
  324. // //printLDR();
  325. // //Serial.println(presence);
  326. //
  327. // //PRESENCE DETECTED
  328. // if (presence ) {
  329. //
  330. // else if (!presence ) {
  331. if (checkUDP.check() ==1) {
  332. UDPalive = checkUDPconnected();
  333. }
  334. if (checkESPserial.check() == 1) {
  335. }
  336. ESPSerial();
  337. if (sendRMS.check() == 1) {
  338. sendUDP();
  339. }
  340. }