#define BLYNK_TEMPLATE_ID "TMPL6kUCFOkwK"
#define BLYNK_TEMPLATE_NAME "Smart Pillow"
#define BLYNK_AUTH_TOKEN "zkhztsyRHOYJqR26aFxK7di0kGJQ9ht9"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DFRobotDFPlayerMini.h" // Install from library manager
#include <OneWire.h>
#include <DallasTemperature.h>
// virtual pins assignment
// v3 for snoring
// v6 for updating currnt temperature
// v4 for mp3 button
// v5 for heating button
// v8 for slider widget
// Replace these with your Blynk authentication token and Wi-Fi credentials
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST"; // Replace this with your wifi SSID
char pass[] = ""; // Replace this with your wifi password
DFRobotDFPlayerMini myDFPlayer; // Create an instance for mp3 player
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
const int maxAllowedTemp = 55; // For safety set maximum allowed temperature
const int tempTolerance = 4; // Temperature tolerance value to be used to decide on/off function for relay
// Define the relay pin and solenoid lock control
const int relayPin = 15; // Change this to the pin connected to the relay for Temperature PTC
int tempC = 0; // variable to hold temperature readings
int userSetTemprature = 0; // Variable to hold user set temperature
bool turnOnHeater = false; // flag to be set when user will choose to start heating
bool heatingOn = false; // flag to check if relay is on or not
const byte soundSensorPin = 23; // Sound sensor analog pin
const int soundThreshold = 750; // Sound threshold for making decision of snoring
bool soundLevel = HIGH; // variable to hold sound sensor value
bool relayLogic = HIGH;
BlynkTimer timer;
// This function will run every time Blynk connection is established
BLYNK_CONNECTED() {
// Request Blynk server to re-send latest values for all pins
Blynk.syncAll();
}
void checkTemp() {
sensors.requestTemperatures(); // Send the command to get temperatures
tempC = sensors.getTempCByIndex(0); // Get temperature reading from sensor
if (turnOnHeater) { // Check if use has set the heat on
// if (tempC != DEVICE_DISCONNECTED_C) { // Check if reading was successful
Blynk.virtualWrite(V6, tempC); // Write current temperature on blynk
// if (heatingOn == true) {
if ((tempC > (userSetTemprature + tempTolerance))) // Check if temperature of heating element has reached max limit turn off relay
{
Serial.println("temperature exceed set point");
// heatingOn = false; // reset heating on flag
digitalWrite(relayPin, relayLogic); // Turn off heater
} else if ((tempC < (userSetTemprature - tempTolerance))) // Check if temperature is dropping below set threshold turn on relay
{
Serial.println("Temperature is below set point");
// heatingOn = true; // Set heating on flag
digitalWrite(relayPin, relayLogic); // Turn heater
}
// }
// } else {
// Serial.println("Error: Could not read temperature data");
// digitalWrite(relayPin, !relayLogic); // Turn off heating element if temperature sensor is not working
// heatingOn = false; // Reset heating on flag
// }
// } else {
// Serial.println("Errror reading temp");
// Blynk.virtualWrite(V6, tempC); // Write current temperature on blynk
// }
}
}
void checkSnoring() {
// Check if sound sensor detect snoring
soundLevel = digitalRead(soundSensorPin); // Read sound level from sound sensor
if (soundLevel == LOW) {
Blynk.virtualWrite(V3, 1); // Set 1 for snoring
} else {
Blynk.virtualWrite(V3, 0); // Set 0 for no snoring
}
}
void setup() {
analogReadResolution(10); // set analog read resolution as 10 bytes
pinMode(soundSensorPin, INPUT); // Set sound sensor pin as input
pinMode(relayPin, OUTPUT); // Set relay pin for heater as output
digitalWrite(relayPin, !relayLogic); // Initialize the solenoid as locked
// delay(1000);
// digitalWrite(relayPin, relayLogic); // Initialize the solenoid as locked
// delay(1000);
// digitalWrite(relayPin, !relayLogic); // Initialize the solenoid as locked
// delay(1000);
// digitalWrite(relayPin, relayLogic); // Initialize the solenoid as locked
Serial.begin(115200);
if (!myDFPlayer.begin(Serial2)) { //Use serial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
// lcd.print("MP3 Error");
// while (true) {
// delay(0); // Code to compatible with ESP8266 watch dog.
// }
}
myDFPlayer.volume(25); //Set volume value. From 0 to 30
sensors.begin();
Blynk.begin(auth, ssid, pass);
Blynk.syncVirtual(V8, V4, V5); // Get the values from the app on start-up
timer.setInterval(3000L, checkTemp); // Check temperature function every 3 second
}
void loop() {
Blynk.run();
timer.run();
}
BLYNK_WRITE(V4) { // Get MP3 Button State
int mp3Button = param.asInt();
if (mp3Button == 1) {
Serial.println("Play music");
myDFPlayer.play(1); //Play the mp3 of current prayer
} else {
myDFPlayer.pause(); //pause the mp3
}
}
BLYNK_WRITE(V5) { // Get heating button state
int heatingButton = param.asInt();
if (heatingButton == 1) {
Serial.println("Turn on heat");
digitalWrite(relayPin, relayLogic); // Turn off heating element
heatingOn = true; // set heating on flag true
turnOnHeater =true;
} else {
Serial.println("Turn off heat");
heatingOn = false; // Reset heating on flag
digitalWrite(relayPin, !relayLogic); // Turn off heating element
turnOnHeater = false;
}
}
BLYNK_WRITE(V8) { // Read user set temperature
userSetTemprature = param.asInt();
Serial.print("User Set Temperature:");
Serial.println(userSetTemprature);
if (userSetTemprature > maxAllowedTemp) {
Serial.println("User set temperature exceed Allowed temprature value liimt it.");
userSetTemprature = maxAllowedTemp;
}
Blynk.virtualWrite(V9, param.asInt());
}