ESP8266 NodeMCU Based IoT Alarm Clock With Blynk
by Ryan Jonathan in Circuits > Microcontrollers
6441 Views, 5 Favorites, 0 Comments
ESP8266 NodeMCU Based IoT Alarm Clock With Blynk
Hello and welcome to our instructable about using ESP8266 NodeMCU Based IoT Alarm Clock With Blynk.
This project is made by two students named Valentino Kevin and Ryan Jonathan from BINUS taking a degree in Computer Engineering, and in this project we are making an IoT device involving a microcontroller, specifically the ESP8266 NodeMCU because it is a very small device and also a popular choice for IoT projects.
Without further ado, below is the list of components used for this project.
Supplies
List of needed components:
1. ESP8266 NodeMCU
2. LCD I2C Screen 16x2
3. Mini or Long Breadboards
4. A Speaker
5. RTC DS3231
6. Ultrasonic Sensor HC-SR04
7. Jumper wires
8. 5V Adapter
9. Female DC jack
10. NPN Transistor BC547
11. 220 Ω Resistor
Tools and software needed:
1. Solder lead
2. Soldering iron
3. Blynk App on Android, IOS or on an emulator
4. Arduino IDE
Configuring ESP8266 NodeMCU
For this project, we are using ESP8266 NodeMCU microcontroller because it is a very small and cheap WiFi capable microcontroller that is now commonly used for any IoT projects. In this step, we will just configure the ESP8266 NodeMCU so that there won't be any miscommunications throughout the project.
You can skip this step if you have configured your ARDUINO IDE to connect ESP8266 NodeMCU.
Now first off, to test the ESP8266, we have to make sure your ESP8266 NodeMCU can connect to the Arduino IDE. You can download it here and after installation open Arduino IDE.
Select Preferences under the File Tab and copy this link into Additional Boards Manager URLs: http://arduino.esp8266.com/stable/package_esp8266com_index.json
Then open Board Manager under the tools tab, from there use the search bar to search ESP8266 by the ESP8266 Community, install that package and restart your Arduino IDE.
In this project we will be using NodeMCU 1.0 (ESP-12E Module), you can choose this under the Tools tab close to where Board Manager is located.
In the pictures above, we have given step by step direction.
LCD I2C Screen and RTC DS3231
Here we are using a 16x2 LCD display with an I2C interface which will make it easier to wire as it only need 2 pins to wire to the board, along with it is a Real-Time Clock module or a DS3231 RTC Module that we will connect together to show time and date.
Both LCD I2C and the DS3231 module uses the same SDA and SCL pin lines, which make sense to combine both modules together as a pair, in the breadboard we will connect pin SDA to D2 or GPIO 4 and pin SCL to D1 or GPIO 5, the Power pin will be connected to Vin and Ground pin to any Gnd.
Speaker
The Speaker used here is a 3in 5w speaker, but if you are trying this project you can use any kind of speaker even 1w or 3w speakers. Now, we will require the 220 Ω Resistor and NPN Transistor for the speaker, the transistor here is used as an amplifier for the speaker but the resistor is optional.
The speaker will be wired to the Collector pin of the transistor and Vin pin on the ESP8266. On the transistor the Base pin will connected to the 220 Ω resistor and then to the Rx pin on the ESP8266, the Rx pin will act as audio output for the ESP8266 and then amplified by the transistor. The Emitter pin on the transistor will be connected to ground.
Ultrasonic Sensor
The Ultrasonic sensor used here will be a HC-SR04, a commonly known ultrasonic sensor that we will be using as a trigger to turn off the alarm system. As the ultrasonic sensor emits ultrasonic waves, the time it takes to come back will determine the range for the sensor and we will set a specific range to trigger off the alarm.
The HC-SR04 has 4 pins, Vcc, Gnd, Trig and Echo. The Trig pin will be connected to D5 and the Echo pin will be connected to D6, Vcc is connected to Vin and Gnd to Gnd as follows.
Using the Blynk App
What we will be using to control all of the components together is with Blynk, it is an IoT platform that was designed specifically for IoT applications.
How it works is through the Blynk App where we need to design with many kinds of widgets that suits your needs, through the app it connects to a Blynk server which will be responsible for running the communications from our smartphones to our hardware.
We can run a private local Blynk server as it is open-source and easily handled by small computers such as a Raspberry Pi. All that we need to use Blynk are two things, hardware and a device.
Make sure to download the Blynk App on your Android, IOS, or any other device.
After downloading, you need to create a new account. After that, create a new project. An authentication token will be sent to your email. This token needs to be saved. It should be put in the code. It's used so that the NodeMCU can connect to specifically the Blynk app that we're building.
The Blynk application is really simple, just need 4 unused pins on the NodeMCU, use it as push buttons, and that's it.
Converting Audio to WAV File
This is the part where the audio for the alarm speaker is used. The ESP8266Audio library has a sketch example for this. The sketch simply plays an audio, which you can test if you've connected the speaker to the NodeMCU. The audio used is WAV, converted into hexadecimal. To convert audio files into WAV format, the application Audacity is used.
https://www.audacityteam.org/download/
Not only can Audacity convert the audio file into the WAV format, it can edit it as well. For this, about 4 seconds of audio is used, preferably loud music. In the picture of the application, there's an option to choose the frequency. The lower the frequency, the more memory will be saved, but the quality drops. The 48000 Hz is used, but feel free to try other options. After editing, export the audio as a WAV file. To convert it to hexadecimal code, there are lots of websites that can get the job done. I use this one:
http://tomeko.net/online_tools/file_to_hex.php?lan...
To use the file, simply follow the sketch example. Don't worry about the file size. Simply click 'sketch' in Arduino IDE. If it doesn't show any error, the file size can be uploaded to the NodeMCU. The one used here about 2 MB.
Coding
Before we dive into the code, there's a couple of things about the project that needs to be mentioned:
1. Don't forget to ground all the components, including the NodeMCU, like in the picture,
2. When uploading the code, DO NOT plug the adapter into the Vin. This can potentially cause the module to break because of the conflicting source of power,
3. Also, the code can't be uploaded if the RX pin is connected.
With that out of the way, we can discuss the code briefly. The code has lots of comment, but discussing its overview function can help too. Firstly, all the library needed are included. Then the necessary variables are created. To really understand how each of this works, it's advisable to test each module first, as the libraries usually include example sketches.
To connect to Blynk, the authentication token is sent by email, as mentioned a couple steps before. The NodeMCU also needs to connect to a wifi. That's what the variables ssid and password are for.
In this code, delays aren't used. Why? Because when dealing with clocks and speakers, delay can cause lag. In this case, the speaker plays an awful laggy audio when there's a delay. So, the millis and micros functions are used. To understand these, Arduino has a great example:
https://www.arduino.cc/en/Tutorial/BuiltInExamples...
Basically, when called, it returns how many millisecond/microsecond has passed since the NodeMCU is turned on.
The DateTime now = rtc.now(); line can also cause the ultrasonic to stop functioning properly if it's placed in the loop() function. That's why in the code it uses millis to stop it from being called to often and messing up the other modules.
The rest of the code can be followed through the comments. The overview is the setup mainly initializes the modules and the serial communication line, the loop's main function is to read the distance through the ultrasonic sensor, fetch the time information from the RTC, displays it on the LCD, checks if the clock is the same with the alarm. If it's the same, then the speaker plays, and a function to stop the alarm when the ultrasonic sensor reads a distance of less than 10 is run.
Conclusion
This project may not be the best solution for an alarm clock in some cases but from a student's perspective this was great learning experience that gave many insight on how these different components work together, and learning to make IoT devices with Blynk has definitely given a lot of ideas for a lot of IoT based projects. We hope that anyone that reads this instructable learns something beneficial and enjoyed it.