/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL6JlvyMlkd"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "wASkvdw63BlxIE-74arhG3TjkzuhNbth"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
#define LDR_PIN 2
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
#include "DHTesp.h"
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
// Your WiFi credentials.
// Set password to "" for open networks.
const float GAMMA = 0.7;
const float RL10 = 50;
const int DHT_PIN = 15;
DHTesp dhtSensor;
Servo servo_9;
BlynkTimer timer;
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 20, 4);
int lullabyMode = 0;
int sleepTime = 20;
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
// Set incoming value from pin V0 to a variable
lullabyMode = param.asInt();
if(lullabyMode==1){
Blynk.virtualWrite(V1, "ON");
}else{
Blynk.virtualWrite(V1, "OFF");
}
}
BLYNK_WRITE(V4)
{
// Set incoming value from pin V0 to a variable
sleepTime = param.asInt();
lcd.setCursor(13, 3);
lcd.print(" ");
lcd.setCursor(13, 3);
lcd.print(sleepTime);
}
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
// Change Web Link Button message to "Congratulations!"
Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
Blynk.setProperty(V3, "onImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}
void myTimerEvent()
{
int pos = 0;
DateTime now = rtc.now();
lcd.setCursor(14, 0);
if((lullabyMode == 1) || (digitalRead(LDR_PIN) == !HIGH) || (now.hour()==sleepTime)){
lcd.print(" ");
lcd.setCursor(14, 0);
lcd.print("ON");
// Update state
Blynk.virtualWrite(V1, "ON");
for (pos = 90; pos <= 120; pos += 1) {
servo_9.write(pos);
delay(20); // Wait for 15 millisecond(s)
}
for (pos = 120; pos >=60; pos -= 1) {
servo_9.write(pos);
delay(20); // Wait for 15 millisecond(s)
}
for (pos = 60; pos <= 90; pos += 1) {
servo_9.write(pos);
delay(20); // Wait for 15 millisecond(s)
}
}else{
lcd.print("OFF");
servo_9.write(90);
Blynk.virtualWrite(V1, "OFF");
}
}
void myTimerEvent2()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
if (digitalRead(LDR_PIN) == !HIGH) {
Serial.print("Baby crying ");
Blynk.logEvent("babys_crying", "Baby's Crying");
} else {
Serial.print("baby sleeping");
}
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Blynk.virtualWrite(V2, data.temperature);
Blynk.virtualWrite(V3, data.humidity);
lcd.setCursor(2, 1);
lcd.print("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
lcd.setCursor(2, 2);
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
lcd.print("Humidity: " + String(data.humidity, 1) + "%");
if(data.temperature>40){
Blynk.logEvent("room_condition_warning","Room is too hot");
}else if(data.temperature <20){
Blynk.logEvent("room_condition_warning","Room is too cold");
}else if(data.humidity >70){
Blynk.logEvent("room_condition_warning", "Room's humidity is too high");
}
}
void setup()
{
// Debug console
Serial.begin(115200);
Serial.print("Connecting to WiFi");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
// Setup a function to be called every second
Blynk.begin(BLYNK_AUTH_TOKEN, WIFI_SSID, WIFI_PASSWORD);
servo_9.attach(4, 500, 2500);
pinMode(LDR_PIN, INPUT);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
lcd.init();
lcd.backlight();
timer.setInterval(1000L, myTimerEvent);
timer.setInterval(500L, myTimerEvent2);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
//prepare LCD:
//lullaby mode
lcd.setCursor(1, 0);
lcd.print("Lullaby mode:");
//sleep time
lcd.setCursor(1, 3);
lcd.print("Sleep time: ");
lcd.setCursor(13, 3);
lcd.print(sleepTime);
lcd.setCursor(15, 3);
lcd.print("h");
}
void loop()
{
Blynk.run();
timer.run();
}