#include #include #include #include #include #include //Sensor smoothing and hysteresys #include #include /* ESP commands * AT+MODE=1 * AT+CWLAP lists wifi AP * AT+CWJAP="IM_AP","raspberry" connect to IM ap * AT+CIFSR get IP * AT+CIPSTART="UDP","192.168.3.1",8000 connect to ** * */ /* SdWav1 : sample voix | * SdWav2 : sample attente |-> mix1 -> Lout & RMS1 * * SdWav3 : sample ambiance -> mix2 -> Rout & RMS2 */ // GUItool: begin automatically generated code AudioPlaySdWav LoopPlayer; //xy=152,429 LOOP AudioPlaySdWav PresencePlayer; //xy=153,217 PRESENCE AudioPlaySdWav WaitPlayer; //xy=155,307 WAIT AudioMixer4 mixer1; //xy=572,215 AudioMixer4 mixer2; //xy=579,344 AudioAnalyzeRMS rms1; //xy=875,315 AudioOutputI2S i2s1; //xy=876,225 AudioAnalyzeRMS rms2; //xy=876,365 AudioConnection patchCord1(LoopPlayer, 0, mixer2, 0); AudioConnection patchCord2(PresencePlayer, 0, mixer1, 0); AudioConnection patchCord3(WaitPlayer, 0, mixer1, 1); AudioConnection patchCord4(mixer1, 0, i2s1, 0); AudioConnection patchCord5(PresencePlayer, rms1); AudioConnection patchCord6(mixer2, 0, i2s1, 1); AudioConnection patchCord7(mixer2, rms2); AudioControlSGTL5000 sgtl5000_1; //xy=573,537 // GUItool: end automatically generated code bool UDPalive ; Metro checkUDP = Metro(1000); Metro checkESPserial = Metro(50); Metro sendRMS = Metro(50); #define LOOP_WAV "LOOP.WAV" #define PRESENCE_WAV "PRESENCE.WAV" #define WAIT_WAV "WAIT.WAV" int WAIT_TIME=10000 ; const int LDR_PIN = A7; ResponsiveAnalogRead ldr(LDR_PIN, false); int threshold = 700; int hysteresis = 100; int sensorPin = A7; SetPoint setPoint; bool presence ; uint32_t LastPresence ; // Use these with the Teensy Audio Shield #define SDCARD_CS_PIN 10 #define SDCARD_MOSI_PIN 7 #define SDCARD_SCK_PIN 14 void ESPSerial(){ // Send bytes from ESP8266 -> Teensy to Computer bool printLine ; while ( Serial1.available() ) { //String buf = Serial1.read(); //Serial.write(F(buf)); Serial.write( Serial1.read() ); // if(buf.readStringUntil("\n")=="OK") {Serial.println("trouvé");} printLine = 1 ; } // if (printLine) {Serial.println("");} printLine = 0; // Send bytes from Computer -> Teensy back to ESP8266 while ( Serial.available() ) { Serial1.write( Serial.read() ); } } /////////////////////////////// SETUP ///////////////////////// void setup() { Serial.begin(115200); Serial1.begin(115200); //LDR digital inputs pinMode(16, INPUT); pinMode(20,INPUT); AudioMemory(16); sgtl5000_1.enable(); sgtl5000_1.lineOutLevel(13); // set output to 3.16 Vpp sgtl5000_1.volume(1); //set volume to maximum SPI.setMOSI(SDCARD_MOSI_PIN); SPI.setSCK(SDCARD_SCK_PIN); if (!(SD.begin(SDCARD_CS_PIN))) { // stop here, but print a message repetitively while (1) { Serial.println("Unable to access the SD card"); delay(500); } } Serial.println("Playing LOOP and WAIT file"); LoopPlayer.play(LOOP_WAV); WaitPlayer.play(WAIT_WAV); // A brief delay for the library read WAV info delay(10); setPoint.begin(threshold, hysteresis); setPoint.attach(RISING_EDGE, Presence); setPoint.attach(FALLING_EDGE, Absence); //ESP8266 Serial1.println("AT+GMR"); //while (!Serial1.available()) {}; delay(200); ESPSerial(); Serial.println(""); Serial.println(""); while(millis()<10000){} Serial1.println("AT+CIFSR"); ESPSerial(); delay(200); Serial.println (""); Serial1.println("AT+CIPSTART=\"UDP\",\"192.168.3.1\",8002"); ESPSerial(); } /////////////////////////////////////////////////////////////////// void playFile(const char *filename) { Serial.print("Playing file: "); Serial.println(filename); PresencePlayer.play(filename); // A brief delay for the library read WAV info delay(5); } void printLDR(){ Serial.print(analogRead(A3)); Serial.print(" "); Serial.print(digitalRead(16)); Serial.print(" "); Serial.print(analogRead(A7)); Serial.print(" "); Serial.print(digitalRead(20)); Serial.println(); } bool checkPresence(int Pin1, int Pin2) { bool isPresent = analogRead(A7)>1010; //digitalRead(Pin2); //|| digitalRead(Pin2); Serial.println(isPresent); return isPresent ; } bool checkUDPconnected() { Serial1.println("AT+CIPSTATUS"); delay(50); while ( Serial1.available() ) { String buf = Serial1.readStringUntil('\n'); //StringUntil('\n') ; if(buf.startsWith("STATUS:2")){ Serial.println("connected"); return true ; } else return false; } } void sendUDP() { if(rms1.available() ) { //&& UDPalive Serial1.println("AT+CIPSEND=3"); delay(20); Serial1.write(byte(rms1.read()*255)); Serial1.println(""); //Serial.println("udp"); } } /////////////////////////////// LOOP //////////////////////////////////// void loop() { // LOOP FILE ldr.update(); int value = ldr.getValue(); setPoint.update(value); // LOOP is alwaiys playing if (!LoopPlayer.isPlaying()) { Serial.println("Playing LOOP file"); LoopPlayer.play(LOOP_WAV); // A brief delay for the library read WAV info delay(10); } if ( (millis() - LastPresence > WAIT_TIME) && !WaitPlayer.isPlaying() && !PresencePlayer.isPlaying()) { WaitPlayer.play(WAIT_WAV); mixer1.gain (1, 1.0); Serial.println("Playing wait file"); delay(10); } if (presence) { LastPresence = millis() ; } //WAIT fades up if PresencePlayer stops ( if (millis()-LastPresence > WAIT_TIME && PresencePlayer.isPlaying() && presence == false) { Serial.println("Stopping PRESENCE file"); PresencePlayer.stop(); for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) { mixer1.gain(1, fadeRamp/100) ; } } // // PRESENCE FILE // presence = checkPresence(16, 20); // //printLDR(); // //Serial.println(presence); // // //PRESENCE DETECTED // if (presence ) { // // else if (!presence ) { // if (checkUDP.check() ==1) { // UDPalive = checkUDPconnected(); // } // if (checkESPserial.check() == 1) { // // } // ESPSerial(); // // if (sendRMS.check() == 1) { // sendUDP(); // } // } ////////////////////// LDR event handlers ///////////////////////////// void Presence() { Serial.println("Presence detected"); presence = true ; if (!PresencePlayer.isPlaying()) { //stop WAIT_WAV if playing if (WaitPlayer.isPlaying()) { //fade out WaitPlayer Serial.println("Fade out Wait file"); for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) { mixer1.gain(1, 1.0 - fadeRamp/100) ; } // WaitPlayer.stop() ; // mixer1.gain (1, 1.0); // set volume for next play // delay(2000) ; } PresencePlayer.play(PRESENCE_WAV) ; delay(15); Serial.println("Playing presence file"); } // if presence and playing, don't do anything for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) { mixer1.gain(0, fadeRamp/100) ; } } void Absence() { LastPresence = millis() ; presence = false ; Serial.println("No presence detected"); if (PresencePlayer.isPlaying()) { //what if presence gone while playing ? Serial.println("Fading out Presence file"); for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) { mixer1.gain(0, 1.0 - fadeRamp/100) ; delay(10); } // while(rms1.read() > 0) { } } }