#include "WiFi.h"
const char* ssid = "";
const char* password = "";
int soundPin = 2; // Change this to the pin connected to the sound sensor
int ledPin = 27; // Change this to the pin connected to the LED
void setup() {
Serial.begin(115200);
// Connect to WiFi
connectToWiFi();
pinMode(soundPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int soundValue = digitalRead(soundPin);
if (soundValue == HIGH) {
Serial.println("Baby is crying!");
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(5000); // Wait for 5 seconds to avoid repeated notifications
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
}
void connectToWiFi() {
Serial.println("This program will notify you when the baby is crying.");
Serial.println("To get started, please connect the baby care monitor to WiFi.");
// Scan for available networks
Serial.println("Scanning for WiFi networks...");
int numNetworks = WiFi.scanNetworks();
// Print available networks
Serial.println("Available WiFi networks:");
for (int i = 0; i < numNetworks; ++i) {
Serial.println(WiFi.SSID(i));
}
// Ask the user to select a WiFi network
Serial.print("Enter the number of the WiFi network you want to connect to: ");
while (Serial.available() == 0) {
// Wait for user input
}
int selectedNetwork = Serial.parseInt();
ssid = WiFi.SSID(selectedNetwork - 1).c_str(); // Subtract 1 to convert to zero-based index
// Ask for the password
Serial.print("Enter the WiFi password: ");
while (Serial.available() == 0) {
// Wait for user input
}
password = Serial.readStringUntil('\n').c_str();
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi");
Serial.println("Thank you for connecting the baby care monitor to WiFi.");
}