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:
Filed Under: Electronic Projects, ESP8266
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.