How to Control an ESP32 or ESP8266 with Alexa and Google Home

This guide shows how to connect an ESP32 or ESP8266 (NodeMCU, Wemos D1 mini) to Amazon Alexa and Google Home, so you can turn a relay, light or any other output on and off with your voice.

You do not need to write an Alexa skill or a Google Home action, and you do not need to open any ports on your router.

How it works

Sinric Pro esp8266 esp32 picow

  1. Your ESP32 or ESP8266 runs the Sinric Pro library and keeps a secure connection open to the Sinric Pro server.
  2. You link the Sinric Pro Alexa skill or Google Home action to your Sinric Pro account once.
  3. When you say “Alexa, turn on the switch”, Alexa sends the command to Sinric Pro, and Sinric Pro forwards it to your board.

Because the board connects out to the server, it works from anywhere and on any home network.

Sinric Pro is an official Alexa Smart Home skill and Google Home action. It does not use device emulation. If you are coming from Espalexa or fauxmoESP, see Espalexa or fauxmoESP “Device Not Found”?

What you need

  1. ESP32 or ESP8266 development board x 1.
  2. A relay module or an LED to control.
  3. Arduino IDE or PlatformIO.
  4. A free Sinric Pro account. See pricing for the limits of the free plan.

Step 1 : Create a device in Sinric Pro

Sinric Pro create device alexa

Sinric Pro copy device id

The device name is what you will say to Alexa or Google Home, so choose a name that is easy to pronounce.

Step 2 : Install the Sinric Pro library

In Arduino IDE open Tools > Manage Libraries, search for SinricPro and click Install.

Sinric Pro install SinricPro library

For PlatformIO, dependencies and common errors, see How to install the Sinric Pro library.

Step 3 : Upload the code

The same sketch works on ESP32 and ESP8266. Enter your Wi-Fi details and the three values from Step 1.

#include <Arduino.h>
#if defined(ESP8266)
  #include <ESP8266WiFi.h>
#elif defined(ESP32)
  #include <WiFi.h>
#endif

#include <SinricPro.h>
#include <SinricProSwitch.h>

#define WIFI_SSID   "YOUR-WIFI-SSID"
#define WIFI_PASS   "YOUR-WIFI-PASSWORD"
#define APP_KEY     "YOUR-APP-KEY"
#define APP_SECRET  "YOUR-APP-SECRET"
#define SWITCH_ID   "YOUR-DEVICE-ID"

#if defined(ESP8266)
  #define RELAY_PIN 12   // D6
#elif defined(ESP32)
  #define RELAY_PIN 16
#endif

// called when Alexa, Google Home or the Sinric Pro app turns the switch on or off
bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("Device %s turned %s\r\n", deviceId.c_str(), state ? "on" : "off");
  digitalWrite(RELAY_PIN, state ? HIGH : LOW);
  return true; // request handled
}

void setup() {
  Serial.begin(115200);
  pinMode(RELAY_PIN, OUTPUT);

  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.print(".");
  }

  SinricProSwitch &mySwitch = SinricPro[SWITCH_ID];
  mySwitch.onPowerState(onPowerState);

  SinricPro.begin(APP_KEY, APP_SECRET);
}

void loop() {
  SinricPro.handle();
}

If you do not want to write code, the Zero Code feature in the Portal can generate the sketch for you.

Once the board connects, the device shows as online in the Portal and you can switch it on and off from there. For wiring a relay, see Switch Tutorial Part 1.

Step 4 : Connect to Amazon Alexa

More about discovery, routines and troubleshooting: Amazon Alexa.

Step 5 : Connect to Google Home

More: Google Home.

Samsung SmartThings

More: Samsung SmartThings.

What else can you build?

A switch is only the starting point. The same steps work for other device types:

Frequently asked questions

Do I need to create my own Alexa skill?

No. You enable the existing Sinric Pro skill and link your account.

Does it work with boards other than ESP32 and ESP8266?

Yes. There are SDKs for Raspberry Pi Pico W, Arduino UNO R4 WiFi, Nano 33 IoT, MKR WiFi 1010, MicroPython, Python and NodeJS. See the SDK list.

Can I control more than one device from one board?

Yes. One ESP32 or ESP8266 can host several Sinric Pro devices, for example a 4-channel relay board. See Modules and Part 3 of the switch tutorial.

Can I use it with Apple HomeKit or Home Assistant?

Yes. See Homebridge (HomeKit) and Home Assistant.

Troubleshooting

Please refer to our Troubleshooting page for possible solutions to your issue.

This document is open source. See a typo? Please create an issue