HackSpace – September 2019

(Jacob Rumans) #1

Robotic Music Glockenspiel


FEATURE


Our ‘clock’ will now chime the same four patterns
every hour, which is cool, but nothing that couldn’t
have been achieved using clockwork in the 19th
century. Let’s bring things up to date by making our
clock read the correct time from the internet, and
allow an alarm to be set with a custom melody via a
website. By adding an Ethernet shield to the Arduino,
we can connect the robot glockenspiel to the Internet
of Things.
The Ethernet shield is perhaps one of the simplest
and cheapest ways to connect an Arduino to the
internet. To add it to your circuit, simply remove the
jumper wires from the Arduino, insert the shield into
the Arduino’s pins, and then reconnect the jumper
wires in the same pin sockets as before. Now, plug
the shield into your router using an
Ethernet cable.
At this stage, both the previous
sketches should still work – you
should try them out to verify that
the circuit is intact.
We’re going to use a service
called Adafruit IO, which facilitates
IoT projects. Go to adafruit.io and
sign up (it’s free). Adafruit IO
consists of a series of ‘feeds’ which
can be read or written to. There is an
Arduino library which handles the
process of either reading or writing
data, so we want to install that and
try an example.

Go to the Arduino library manager, install the
Adafruit MQTT Library, then open the sketch found
in File > Examples > Adafruit MQTT Library >
mqtt_ethernet. Read through the code – this sketch
subscribes to a simple binary feed called ‘onoff’,
reading changes from an on/off button in the Adafruit
IO web interface. The sketch also sends dummy data
to a feed called ‘photocell’, and the data sent from
the Arduino can then be seen live on a web interface
called a ‘dashboard’.
Set up these two test feeds, ‘onoff’ and ‘photocell’,
within your account, and create a dashboard with a
button to send data to the ‘onoff’ feed, and a graph
to read data from the ‘photocell’ feed. This is a
little complex, so check out these sites –
hsmag.cc/HRSKql and hsmag.cc/GF0brB – to get an
overview of what feeds and dashboards are all about.
Once you think you’re up to speed, enter your
username and key in the mqtt_ethernet example
sketch, and upload the code to your Arduino. Open the
serial monitor in the Arduino software and, with luck,
you should see details of a successful connection to
the Adafruit server. Try playing with the dashboard –
you should now have two-way communication
between the dashboard and your Arduino.

SOUND THE ALARM
If that works, you can download the glock_iot_alarm
sketch, which is a mash-up of the previous example
and our glockenspiel code. As before, enter your
Adafruit IO username and key, and make sure that
timeZoneOffset and daylightSavingsOffset are
correct for your region (if you want an advanced project,
you could try to obtain these settings automatically
somehow). Next, on the Adafruit IO site, create a new
dashboard called ‘alarm glock’ and populate it with
three new feeds: ‘alarmtoggle’ (a toggle button),
‘alarmtime’ (a text block), and ‘alarmmelody’ (another
text block).
Upload the code to the Arduino and check the serial
monitor to see whether everything appears to be
running smoothly. If the sketch is working, you should
be able to set an alarm from the dashboard. Changing
the alarm time or melody on the dashboard should
cause the alarm time or melody to be changed on the
Arduino (and cause the LED to flash briefly), while
the toggle button should activate or deactivate the
alarm (with the LED either on or off to show the
current state).

Internet-connected alarm clock


Above
(n Ethernet
shieSd aSSows `our
(rduino to taSR to
the outside worSd
Below
;he JoTpSeted
NSoJRenspieS Jan
Ie JontroSSed ]ia
the internet or
MroT a Re`Ioard
Free download pdf