Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to design an IoT-based smart alarm

By Nikhil Agnihotri November 19, 2023

We might not always appreciate them, but that morning wake-up alarm is important for everyday life. It ensures we wake up in time for work, school, meetings, appointments, or other essential tasks. Alarms can also help us keep a regular sleep/wake schedule. 

In this project, we’ll use the Internet of Things (IoT) to set a morning alarm for sunrise. This device automatically detects your geological location and lets you set the alarm accordingly. 

We’ll build this IoT device using the ESP8266 microcontroller board and employ its Weather API. You first must register an account on www.weatherapi.com and get an API key for building this IoT project.

Components required
The following components are needed to prototype this device. 

1. ESP8266 x1
2. Buzzer x1
3. Push button x1
4. Resistor 330Ω x2
5. LED x1
6. Breadboard
7. Connecting wires/Dupont wires

Circuit connections
To build this device, you’ll need an ESP8266 microcontroller board.

  • Start by interfacing the push button with pin GPIO4. To do so, connect one terminal of the push button to ESP8266’s 3V pin via the 330Ω resistor and connect the same terminal to GPIO4.
  • Connect the other terminal of the push button to the ground.
  • Next, interface a buzzer at ESP8266’s GPIO5.
  • Connect one of the buzzer’s terminals to pin GPIO5, and the other terminal to the GND.
  • Interface an LED with ESP8266’s GPIO10 via the 330Ω series resistor.

Registering with Weather API
This device uses the Weather API for its functioning. You’ll need to register an account on www.weatherapi.com (click the “Sign up” button on its home page) and get an API key.

Fill in your details to complete the sign-up process.

You should receive a confirmation email at the registered email account. Go to your email and confirm the Weather API sign-up.

Then, login at www.weatherapi.com with your registered credentials.

After you login, an API key will be provided. Copy and note the API key, which we’ll use as the device code.

Arduino sketch
After making the circuit connections, upload the below Arduino sketch to ESP8266.

How this IoT device works
This is a smart IoT device that automatically sets a morning alarm to sunrise time, according to the current location of the user. The device connects with Weather API to determine the geolocation data. 

When the device is switched ON, it immediately connects with the local WiFi network. The network credentials are hard-coded in the device. Once connected to the Internet, the device detects the current geographical location based on its IP address. It then determines the time of sunrise for the next day, which is converted into minutes from 12 a.m. onwards and stored as a global variable.

The user can set the sunrise alarm by pressing a button on the device. If the alarm is set, an LED indicator glows. The user can also turn OFF the alarm by pressing the same button once. 

If the alarm is set, the device continuously polls for the local time at a one-minute interval. The local time is also converted into minutes from 12 a.m. onwards. The device also compares the local time in minutes (from 12 a.m. onwards) with the sunrise time in minutes (from 12 a.m. onwards). When the difference between the two times is zero, the device buzzes its alarm for one minute.   

The code
The Arduino sketch for the device begins by importing the ESP8266WiFi.h, ESP8266HTTPClient.h, WiFiClient.h, and ArduinoJson.h libraries. The ESP8266WiFi.h and WiFiClient.h libraries are required for WiFi connectivity. The ESP8266HTTPClient.h library is used to handle HTTP requests for the Weather API. The ArduinoJson.h library is used to process the JSON data from the Weather API.

The variables are declared to store the SSID and network key for the WiFi network. It’s necessary to replace these values in the user program with the SSID and network key of your own WiFi connection. A variable must be declared to store the API key from weatherapi.com. Again, you must replace this key with your own API key from your Weather API registered account.

The variables are declared to store the:

  • Sunrise time in minutes
  • Current local time in minutes
  • Sunrise alarm status
  • Indicator LED status

The variables are initialized to hold the assignments for the interfacing pins. In the setup() function, the pin interfacing button is set as input and the pins interfacing buzzer and LED are set as a digital output. The baud rate for serial communication is set to 115200 bps. The device is connected with the WiFi network by calling the WiFi.begin() method. Once WiFi is connected, the IP address and local IP address are printed on the serial port.

  • The user-defined convertToMinutes() function converts the time provided from Weather API by using a 12-hour format with the number of minutes from 12 a.m. onwards.
  • The user-defined stringToCharArray() function is defined to convert strings received from Weather API into character arrays.
  • The getSunriseTime() function connects with Weather API and retrieves the time for the next sunrise. The sunrise time is stored in a variable using the number of minutes from 12 a.m. onwards.
  • The getCurrentTime() function connects with Weather API, retrieving the current local time and storing it in a variable as the number of minutes from 12 a.m. onwards.
  • In the loop() function, the device checks if it’s connected to WiFi. If it is, it gets the sunrise time for the next day by calling the getSunriseTime() function.
  • If the ON/OFF button is pressed, sunrise time is set or unset on the alarm. If the alarm is set, the device retrieves the local time by calling the getCurrentTime() function.
  • The difference between sunrise time and current local time, both in minutes from 12 a.m. onwards, is calculated. If it’s zero, the alarm buzzes for one minute.

Results
Below is a screenshot of the messages communicated by the smart IoT morning alarm to the serial port. When the sunrise alarm (according to the user’s geolocation) is set, “Sunrise Alarm is Set” is printed at the end of the serial port’s message. This can be a userful feature to add to smart watches and fitness gadgets.

 

You may also like:


  • The top mobile app development tools for IoT and electronics

  • The top long-range Bluetooth modules of 2023

  • The top IoT platforms of 2023

  • How to send SMS alerts from ESP32 without a GSM…

  • How to build a MicroPython-based sensorless, weather station

Filed Under: Electronic Projects, ESP8266
Tagged With: alarm, esp8266, internetofthings, IoT
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Reducing "shoot-through" in offline Full Bridge SMPS?
  • High Side current sensing
  • How to simulate power electronics converter in PSpice?
  • Voltage mode pushpull is a nonsense SMPS?
  • Layout IRN reduction in Comparator

RSS Electro-Tech-Online.com Discussions

  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors
  • Siemens large industrial PLC parts

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe