/* myFinal This scetch helps you wake up in the morning and streamline day routine. 1. Morninig alarm a. Listen on pin V5 for a Blynk write that sets the alarm at speicific time b. Listen on pin V7 for a Blynk to change the alarm melody c. Play the melody note by note using timer 2. Send notification with recommendations on what to wear and if you should birghten the room based on the temprature and light sensors a. Trigger - tap twice with the device (accelerometer), where the top part should be exposed b. Generate helpful messages from the temp and light values collected c. Sends the messages to Blynk through pin V1, which makes a webhook request that gets to Integromat and send the info to the phone 3. Send notification about how many events are on the calendar for today a. Trigger - clap twice and the CPX will recognize it using it's microphone b. Sends a wirte to Blynk through pin V9, which makes a webhook request that gets to Integromat and send the info to the phone c. Listen on pin V8 for Blynk to forward HTTP request with the number of events in calendar and turns on that number of as LEDs on the CPX to signal how many events there are 4. Send articles of specific subject to a given email a. Trigger - shake the CPX and it's accelerometer will recognize b. Send through pin V1 to Blynk a write that triggers the webhook that goes out to Integromat and sends to the email the info 5. Play a melody whenever you get home a. Trigger - Listen on pin V4 for a Blynk write that happens by detecting GPS on the phone b. Play a melody (which also comes with a bit of lights) Video links: https://youtu.be/u-e5-8QuZGU https://youtu.be/I1ecSJn4NMA https://youtu.be/gVeri4ndx6w Created By: Alon Yunger 316299569 Uri Amiel 313160046 */ #define BLYNK_PRINT SerialUSB #include #include #include #include #include #include "tetris.h"; #include "nyanCat.h"; #include "pirates.h"; #include "coffinDance.h"; //Network stuff char auth[] = "[Blynk Auth]"; char ssid[] = "[SSID]"; char pass[] = "[SSID PASS]"; #define EspSerial Serial1 #define SerialMon Serial #define ESP8266_BAUD 115200 ESP8266 wifi(&EspSerial); //Variables: /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// bool stopAlarm = false; bool playAlarm = false; bool alarmPlaying = false; int currentNotePlaying = 0; float currentNoteDuration; float pauseBetweenNotes = 0; float elapsedTimeTillNextNote = 0; float noteDurationMultiplier = 0.6; float pauseBetweenNotesMultiplier = 0.2; int *melody; int *noteDurations; int numberOfNotes; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #define CLICKTHRESHHOLD 120 #define DOUBLECLICKTHRESHOLD 200; #define TIMETOADDAFTERTIMEOUT 500; const int roomTempOffset = 7; const int lowTemp = 16; const int midTemp = 22; const int highTemp = 26; const int brightThreshold = 100; int tapCurrTime; int tapPrevTime = 0; int tapTimeout; bool tapped = false; String tempMessage = ""; String lightMessage = ""; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// const int timeBetweenSounds = 500; float currentSound = 0; float prevSound = 0; float prevPrevSound = 0; float elapesdTimeTillNextSound = 0; int lastSoundValue; int soundValue; long lastNoiseTime = 0; long currentNoiseTime = 0; long lastLightChange = 0; int relayStatus = HIGH; const int timeTillPixelShut = 5000; bool colorEvents = false; float elapsedTimeTillPixelsShut; int numberOfEvents = 0; bool eventsColored = false; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #define ROLL_THRESHOLD 30 const int timeBetweenShakes = 3000; float elapsedTimeTillNextShake = 0; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Setup void setup() { CircuitPlayground.begin(); SerialUSB.begin(115200); EspSerial.begin(ESP8266_BAUD); SerialMon.begin(115200); delay(10); Blynk.begin(auth, wifi, ssid, pass); CircuitPlayground.setAccelRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G! CircuitPlayground.setAccelTap(1, CLICKTHRESHHOLD); tapCurrTime = millis(); delay(100); attachInterrupt(digitalPinToInterrupt(CPLAY_LIS3DH_INTERRUPT), tapTime, FALLING); initAlarm(); } //Main Loop void loop() { handleAlarm(); Blynk.run(); handleClap(); handleAlarm(); handleTap(); handleEventsColor(); shakeTime(); } //////////////////////////////////////////////////////////////////////// // Init default alarm melody void initAlarm(){ melody = melody1; noteDurations = noteDurations1; numberOfNotes = NUMOFNOTES1; } // Play alarm music from main loop void handleAlarm(){ if (playAlarm && !alarmPlaying){ alarmPlaying = true; } if (currentNotePlaying < numberOfNotes && elapsedTimeTillNextNote + pauseBetweenNotes <= millis() && !stopAlarm && alarmPlaying){ float noteDuration = 1000/noteDurations[currentNotePlaying]; for (int i = 0; i < 10; ++i) { int color = 0xFF00FF; CircuitPlayground.setPixelColor(i, color); } CircuitPlayground.playTone(melody[currentNotePlaying], noteDuration * noteDurationMultiplier); CircuitPlayground.clearPixels(); pauseBetweenNotes = noteDuration * pauseBetweenNotesMultiplier; currentNotePlaying++; elapsedTimeTillNextNote = millis(); } else if (currentNotePlaying >= numberOfNotes || stopAlarm){ currentNotePlaying = 0; playAlarm = false; alarmPlaying = false; CircuitPlayground.playTone(0, 1); CircuitPlayground.clearPixels(); } } // Alram Clock pin handle BLYNK_WRITE(V5) { int pinValue = param.asInt(); if (pinValue == 0){ stopAlarm = true; } else{ stopAlarm = false; playAlarm = true; } } // Get new melody request and change it accordingly BLYNK_WRITE(V7) { int pinValue = param.asInt(); alarmPlaying = false; playAlarm = false; if (pinValue == 1){ melody = melody1; noteDurations = noteDurations1; numberOfNotes = NUMOFNOTES1; } else if (pinValue == 2){ melody = melody2; noteDurations = noteDurations2; numberOfNotes = NUMOFNOTES2; } else if (pinValue == 3){ melody = melody3; noteDurations = noteDurations3; numberOfNotes = NUMOFNOTES3; } noteDurationMultiplier = 0.6; pauseBetweenNotesMultiplier = 0.2; } /////////////////////////////////////////////////////////////////////////////////////// // Check for 2 taps with the device void tapTime(void) { tapCurrTime = millis(); int threshold = DOUBLECLICKTHRESHOLD; if (tapCurrTime - tapPrevTime <= threshold && tapCurrTime >= tapTimeout && !tapped){ tapTimeout = TIMETOADDAFTERTIMEOUT + tapCurrTime; float temp = CircuitPlayground.temperature() - roomTempOffset; float lightValue = CircuitPlayground.lightSensor(); tempMessage = getTempMessage(temp); lightMessage = getLightMessage(lightValue); tapped = true; if (!stopAlarm){ stopAlarm = true; } } tapPrevTime = tapCurrTime; } // Get temp and dress recommendation by checking the room temp String getTempMessage(float i_Temp){ String tempMessage = ""; if (i_Temp <= highTemp && i_Temp >= midTemp){ tempMessage = "It's nice here, dress comfortably"; } else if (i_Temp > highTemp){ tempMessage = "It's a bit warm , put on something short"; } else if (i_Temp < midTemp && i_Temp >= lowTemp){ tempMessage = "Seems a little chilly, try on something warm"; } else { tempMessage = "It's a cold day, dress warm and stay home if possible"; } return tempMessage; } // Get message about light value in the room String getLightMessage(float i_Light){ String lightMessage = ""; if (i_Light < brightThreshold){ lightMessage = "It's a bit dark, open the light or a window"; } else { lightMessage = "Nice and bright day"; } return lightMessage; } // Send temp and light messages given 2 taps void handleTap(){ if (tapped){ Blynk.virtualWrite(V1, tempMessage, lightMessage); tapped = false; } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Check for two claps and send events request via webhook void handleClap(){ currentSound = CircuitPlayground.mic.soundPressureLevel(10); currentNoiseTime = millis(); if (currentSound >= 66) { if ((currentNoiseTime > lastNoiseTime + 200) && (prevSound < 63) && (currentNoiseTime < lastNoiseTime + 800) && (currentNoiseTime > lastLightChange + 1000)) { Blynk.virtualWrite(V9, 1); SerialMon.println("Clap"); lastLightChange = currentNoiseTime; } lastNoiseTime = currentNoiseTime; } prevSound = currentSound; } // Get number of events via HTTP BLYNK_WRITE(V8) { numberOfEvents = param.asInt(); colorEvents = true; elapsedTimeTillPixelsShut = millis(); } void handleEventsColor(){ if (colorEvents && !eventsColored){ for (int i = 0; i < numberOfEvents; ++i) { int green = 0x00FF00; CircuitPlayground.setPixelColor(i, green); } eventsColored = true; } if (eventsColored && elapsedTimeTillPixelsShut + timeTillPixelShut < millis()){ eventsColored = false; colorEvents = false; for (int i = 0; i < numberOfEvents; ++i) { CircuitPlayground.setPixelColor(i, 0x000000); } } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Get enter GPS area notification from Blynk BLYNK_WRITE(V4) { Blynk.virtualWrite(V4, 1); noteDurationMultiplier = 0.4; pauseBetweenNotesMultiplier = 0.0001; melody = melody4; noteDurations = noteDurations4; numberOfNotes = NUMOFNOTES4; stopAlarm = false; playAlarm = true; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Check Shake and send request for news articles void shakeTime(){ float X = 0; float Y = 0; float Z = 0; for (int i=0; i<10; i++) { X += CircuitPlayground.motionX(); Y += CircuitPlayground.motionY(); Z += CircuitPlayground.motionZ(); delay(1); } X /= 10; Y /= 10; Z /= 10; float totalAccel = sqrt(X*X + Y*Y + Z*Z); if (totalAccel > ROLL_THRESHOLD && elapsedTimeTillNextShake + timeBetweenShakes < millis()) { SerialMon.println("Shake"); elapsedTimeTillNextShake = millis(); Blynk.virtualWrite(V1, 1, 2); } }