# Import the 'machine' module for hardware-related functions
# Import the 'time' module for time-related functions
import machine, time
# Import the 'Pin' class from the 'machine' module for GPIO pin control
from machine import Pin
import network # Imports 'network' for connecting baby monitor to wifi
from utime import sleep # Imports 'sleep' from 'utime' for introducing delays
import time # Imports 'time' for time-related functions
# Get SSID and password from user
ssid = input("Enter WiFi SSID: ") # Prompts the user to enter the WiFi SSID and store the input in the 'ssid' variable.
password = input("Enter WiFi Password: ") # Prompt the user to enter the WiFi password and store the input in the 'password' variable.
print("Attempting to connect to WiFi") # Print a message indicating that the baby monitor is attempting to connect to a WiFi network
sta_if = network.WLAN(network.STA_IF) # Create a variable representing a Station (STA) interface for managing Wi-Fi connections
sta_if.active(True) # Activate the Station (STA) interface for managing Wi-Fi connections
if password: # if password creates condition to be met
sta_if.connect(ssid, password) # Attempt to connect to a Wi-Fi network with the input SSID and password taken from user
else: # Code to be executed when the Wi-Fi connection attempt in the 'if' block fails
sta_if.connect(ssid) # Attempt to connect to a Wi-Fi network with the input SSID provided by user(without a password)
while not sta_if.isconnected(): # Wait until the Wi-Fi connection is established
print(".", end="") # then print a dot to indicate that it is trying to connect to wifi
time.sleep(0.1) # Pause the program execution for 0.1 second
print("\nConnected!")# print Connected! in next line
mic = machine.ADC(machine.Pin(35)) # Create an ADC (Analog-to-Digital Converter) for microphone connected to pin 35
# Define a Pin object named 'red' connected to D15,
# configured as an output. This represents the connection to
# the red LED, allowing control over its state (ON/OFF).
red = Pin(15, Pin.OUT)
# Define a Pin object named 'yellow' connected to D21,
# configured as an output. This represents the connection to
# the yellow LED, allowing control over its state (ON/OFF).
yellow = Pin(21, Pin.OUT)
# Define a Pin object named 'green' connected to D5,
# configured as an output. This represents the connection to
# the green LED, allowing control over its state (ON/OFF).
green = Pin(5, Pin.OUT)
# Create an indefinite loop to continuously execute the following code block.
while True:
mic_reading = int((2**(16-10))*mic.read()) # we want 16 bits, mic_reading.read() returns 10 bits
if mic_reading == 131000 or mic_reading < 131000: #the range in which the green LED will turn on and other LEDS will be off
yellow.off()# Set the initial state of the yellow LED to OFF
red.off()# Set the initial state of the red LED to OFF
green.on()# Set the initial state of the green LED to ON
sleep(0.5)
print("attention not needed")# Display a message indicating that attention is not required.
elif mic_reading > 131000 and mic_reading < 135000: #the range in which the yellow LED will turn on and other LEDS will be off
green.off()# Set the initial state of the green LED to OFF
red.off()# Set the initial state of the red LED to OFF
yellow.on()# Set the initial state of the yellow LED to ON
sleep(0.5)
print("attention needed")# Display a message indicating that attention needed is required.
else: #if LED not in range of green or yellow then it will turn red
green.off()# Set the initial state of the green LED to OFF
yellow.off()# Set the initial state of the yellow LED to OFF
red.on()# Set the initial state of the red LED to ON
sleep(0.5) # Pause the program's execution for 0.5 seconds.
# Now the red, yellow, and green Pin objects are ready to control the corresponding LEDs
print("attention needed urgently")# Display a message indicating that urgent attention is required.
print(mic_reading) #prints the sound level
time.sleep(1/44100)