Alarm clocks usually come with a variety of buttons and switches. As a minimum, we need to set the clock time, set the alarm time, and turn the alarm on and off. We might also want to deal with daylight savings time (DST), and a 12 hour vs. 24 display.
As a fun project to 1) try out many of the features of the Arduino Cloud, and 2) try the new Nano ESP32, I'm presenting here an alarm clock with no external controls at all. The clock itself gets the time from NTP (the Internet's Network Time Protocol). Then all the controls to set time zone, DST, 12 hour/ 24 hour clock, set the the alarm time, and turn the alarm on and off are set on your smart phone through the Arduino Cloud App.
To keep the hardware as simple as possible, the time is displayed on a 4 digit, 7 segment display with an integrated I2C interface. The alarm sound is provided by a small piezo buzzer module. Both the display and the buzzer are powered at 3.2 volts directly from the Nano ESP32. Power for the Nano ESP32 is supplied through the USB cable and a USB wall charger brick.
If you have access to a 3D printer, I have also included the STL files for a small case (two files - one for the clock case itself and another for a base that it snaps into).
The two photos above show how all the components are mounted in the case. They are all held in place with a small amount of hot glue. See the schematic below for details of how the components are interconnected.
Software OverviewThe software for our alarm clock is a little more complicated than the hardware. We need an Arduino Cloud account. We need to set up an App in that account which consists of three parts: a device (our Nano ESP32) linked to the cloud, a dashboard app that runs on a mobile smart phone, and a sketch or code that runs on our Nano. That sketch in the case of our alarm clock is basically a mash-up of three different sketches:
- The ESP32 NTP Client-Server example posted many places, such as: https://randomnerdtutorials.com/esp32-date-time-ntp-client-server-arduino/The program gets the time from an NTP server and sets up the built-in RTC (real time clock).
- The demo_displayTime example contained in the HT16K33 library. This sketch displays the time on our 4 digit, 7 segment display. The HT16K33 is the IC built into the display that provides the I2C interface.
- The Arduino Cloud sketch resulting from the Dashboard and Thing setups to control the alarm clock from the smart phone app.
I have provided the main code file for the alarm clock and cloud interface, but you will need to duplicate it in your own Arduino Cloud account, setting up your own Nano ESP32 under Devices, your own Dashboard on your smartphone, and your own Cloud Thing.
Instructions for setting up an Arduino Cloud account are pretty clear, so I won't attempt to duplicate them here. Setting up your ESP32 as a device linked to the Cloud is also fairly straight forward. So let's move on to setting up the dashboard. Mine is shown below in Mobile Layout view:
We have two Push Buttons at the top to turn the alarm on and off. Next we have two Value controls and an LED (red & green) to show the values the alarm is set to and whether it is actually set or not. (The Value controls and LED are giving you actual feedback from the ESP32 itself as to the status of the alarm, as opposed to the app telling you what they are supposed to be set at). Next we have two Sliders to set alarm hours and minutes. Next we have two Switches - the first to select DST is desired, the second to select a 12 hour clock display if desired. Finally we have a Value Dropdown control to select the correct time zone. (The dropdown control is a little more trouble to set up initially than a slider, but it does a much better job of reliably holding its value when the clock is powered down / unplugged.)
Next we turn to setting up the Cloud Thing, which is where we create the variables that are shared between the cloud app and the Nano ESP32, and also where we create the code that runs on the Nano ESP32.
Here is a list of the shared variables.
All these variables have Read and Write permissions, update on Change, and thresholds of 0.
Nano ESP32 SoftwareIf your Arduino Cloud account and app look exactly like mine, with all controls and variables named exactly the same, you can just replace the code your Arduino Cloud has generated with my code, and you are all done. A somewhat slower but safer approach would be to start with your Arduino Cloud code, and add in the missing pieces by copying them from my code into yours. Either way, what you end with is the Arduino Cloud sketch with added pieces to 1) support the 7 segment display, 2) Get NTP time and set the RTC, 3) Control the buzzer, 4) Get the shared variable to control your alarm clock.
Additional Notes on SoftwareOne thing I couldn't find anywhere about the NTP Client Server was how to get the components of time (hours, minutes, and seconds) extracted as numbers. Every example always output the time as strings that could not be converted to numbers. I finally found a fairly obscure posting that explained that the actual value as a number can be obtained by mytime.tm_hour or mytime.tm_min.
I wanted my alarm clock to set itself to the correct local time when plugged in. In theory, the settings on the Arduino Cloud dashboard should automatically configure the clock to display the correct local time. During the start-up, however, the display showed several wrong times before displaying the correct one. This was fixed by setting the display to 00:00 and then disabling further updates for about 15 seconds while the Cloud controls get everything set up correctly.
There are subroutines to handle changes to each cloud generated variables. In my code, several of these are not used, i.e. they are empty and contain no action. In theory, they could/should be removed. However, Arduino Cloud apparently wants them there, as I received compile errors when I attempted to remove them.
The piezo buzzer I used has power applied continuously and a data line (pin 3) turns it on when it goes low. If you use a more common 2 lead buzzer, you will likely just tie the +lead to +3.3 volts and the -lead to pin3, assuming you leave the software as is, with pin 3 low to turn on the buzzer.
The best way to debug this software, if you encounter any issues, is probably to revert back to the three separate sketches that I described at the beginning of this section. They would likely be considerably easy to work with than the combined mashup sketch that I have provided.
Comments