11

(Marcin) #1

Alexa-controlled wheel of fortune


TUTORIAL


FAUX PAS
Instructions for installing the FauxmoESP library via
the Arduino IDE can be found here: hsmag.cc/egSrrp.
You need to install the two libraries again, and if all is
well, there is a FauxmoESP example script that you
can test.
Upload the fortune code via the IDE. You can find
the code here: hsmag.cc/issue12.
The code works as follows:
Firstly, you need to include the various libraries for
WiFi, the FauxmoESP library, and the servo library.

#include <ESP8266WiFi.h>
#include <fauxmoESP.h>
#include <Servo.h>

You’ll need to add your own WiFi credentials for
your home router:

#define SSID “<YOUR SSID HERE>”
#define PASSWORD “<YOUR PASSWORD HERE>”

Next, create objects for the Fauxmo device
and the servo:

fauxmoESP fauxmo;
Servo servo;

You only need one variable for the code –
spinning, which is a Boolean (true or false)
indicator of whether or not we have started the
wheel spinning.

bool spinning=false;

In setup, first of all, the code connects to your
home WiFi. Then, set the pin for the servo (we used
pin 2, which equates to the physical pin 4 that we
plugged the servo’s signal wire into).
You also need to enable the device and give it a
name to identify it to Alexa (we used ‘fortune’ but you
can call it anything that isn’t already a device on your
network). Continuous rotation servos work differently
from ordinary servos, which are usually limited to
180 degrees. As the name implies, a continuous
rotation servo can just keep on spinning. To make it
turn, you write a value of 0 to the servo object and
90 to stop it:

void setup()
{
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
}

servo.attach(2);
servo.write(90);

fauxmo.enable(true);
fauxmo.addDevice(“fortune”);
fauxmo.onMessage(callback);
}

The main program loop is quite simple. You have
to call fauxmo.handle() to keep an eye on the device.
If the code sees spinning has been set to true, it
pauses a random amount of time to let the wheel
spin, stop the servo, and set the value of spinning
back to false.

We used PLA, but
any plastic will do,
with an infill of about
15%. All pieces are
orientated so no
support material
is required.

QUICK TIP


Below
Servo fitted and
secured (making
sure the shaft is
in the centre)
Free download pdf