I_Mihalache_AudioPlayer.ino 12 KB

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