SinricPro: an ESP32 switch for Alexa and Google Home
Yes, you can build with the SinricPro library online in Mokxi with nothing to install. Its hardware is not simulated, so a sketch says plainly that it cannot reach the device. The example below, "A SinricPro switch, and why Alexa cannot reach it here", runs on the ESP32-C3-DevKitM-1.
Built in on Mokxi: #include <SinricPro.h> and it builds, with nothing to install. Free, and no account needed.
What SinricPro does
SinricPro is a cloud service that makes an ESP32 or ESP8266 look like a smart home device to Amazon Alexa, Google Home and SmartThings. You create a device on the SinricPro website, link the SinricPro skill in the Alexa or Google app, and then "Alexa, turn on the lamp" reaches your sketch as a callback that switches a relay.
The library keeps a WebSocket open to ws.sinric.pro. A sketch declares the app key, app secret and device ID from the website, gets a typed device with SinricPro[deviceId], and attaches callbacks: onPowerState() for a switch, others for dimmers, thermostats and locks. When a physical button changes the state, sendPowerStateEvent() tells the cloud so the app stays in step. SinricPro.handle() must run on every pass of loop().
In Mokxi the library compiles and the Switch example runs on an ESP32-C3, with WiFi joining the simulated network, but the WebSocket with TLS to ws.sinric.pro is not carried. begin() says once that the board never connects, onConnected and onPowerState never fire, and sendPowerStateEvent() returns false. Only the switch device header is included here.
| Library Manager name | SinricPro |
|---|---|
| Author | Boris Jaeger |
| Include | #include <SinricPro.h>#include <SinricProSwitch.h> |
| Boards | ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP32-S3-CAM, ESP8266 NodeMCU |
| In the simulator | Builds and runs, and says plainly that the hardware behind it is not simulated. |
Install it
It builds on ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP32-S3-CAM, ESP8266 NodeMCU.
In Mokxi it is built in: #include <SinricPro.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "SinricPro" by Boris Jaeger from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add SinricPro to lib_deps in PlatformIO. Mokxi follows the calls of release 5.1.0, so a sketch written here builds there too.
What to know in the simulator: The library needs a WebSocket (TCP with TLS) to ws.sinric.pro, which the network does not carry, so begin() says once that the board never connects: onConnected and onPowerState never fire and sendPowerStateEvent() returns false. Only the switch device header is here.
The calls you will use
In the order a sketch meets them.
SinricProSwitch &mySwitch = SinricPro[SWITCH_ID]
Gets the switch device for an ID from the SinricPro website, creating it on first use. Keep it as a reference.
mySwitch.onPowerState(onPowerState)
Registers the function called when Alexa, Google or the app turns the device on or off. It must return true to confirm.
bool onPowerState(const String &deviceId, bool &state) {
digitalWrite(RELAY_PIN, state ? HIGH : LOW);
return true;
}SinricPro.onConnected(onConnected)
Registers a function run each time the WebSocket to the server opens. onDisconnected() is its partner.
SinricPro.begin(APP_KEY, APP_SECRET)
Starts the connection with the app key and secret from the Credentials page. Call it after WiFi is connected and the callbacks are set.
SinricPro.handle()
Keeps the WebSocket alive and runs callbacks. Call it on every pass of loop() with no long delays.
mySwitch.sendPowerStateEvent(state)
Reports a local change, such as a wall button press, so the apps show the right state. Returns false when it was not sent.
SinricPro.isConnected()
True while the WebSocket to the server is open.
Example: A SinricPro switch, and why Alexa cannot reach it here
On the ESP32-C3-DevKitM-1. It runs in the browser exactly as printed.
// After the library's Switch example.
#include <WiFi.h>
#include <SinricPro.h>
#include <SinricProSwitch.h>
#define APP_KEY "YOUR-APP-KEY"
#define APP_SECRET "YOUR-APP-SECRET"
#define SWITCH_ID "YOUR-DEVICE-ID"
bool myPowerState = false;
unsigned long lastBtnPress = 0;
bool onPowerState(const String &deviceId, bool &state) {
Serial.printf("Device %s turned %s (via SinricPro)\r\n", deviceId.c_str(), state ? "on" : "off");
myPowerState = state;
digitalWrite(2, myPowerState ? HIGH : LOW);
return true;
}
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT);
WiFi.begin("Mokxi-Lab", "breadboard");
while (WiFi.status() != WL_CONNECTED) delay(250);
Serial.println("WiFi connected");
SinricProSwitch &mySwitch = SinricPro[SWITCH_ID];
mySwitch.onPowerState(onPowerState);
SinricPro.onConnected([] { Serial.printf("Connected to SinricPro\r\n"); });
SinricPro.onDisconnected([] { Serial.printf("Disconnected from SinricPro\r\n"); });
SinricPro.begin(APP_KEY, APP_SECRET);
}
void loop() {
SinricPro.handle();
if (millis() - lastBtnPress > 1000) {
lastBtnPress = millis();
myPowerState = !myPowerState;
SinricProSwitch &mySwitch = SinricPro[SWITCH_ID];
bool sent = mySwitch.sendPowerStateEvent(myPowerState);
Serial.printf("event sent: %d, connected: %d\r\n", sent, SinricPro.isConnected());
}
}Common errors and how to fix them
The device shows as offline in the SinricPro app
The board never connected. Check that APP_KEY and APP_SECRET come from the same account as the device, that SWITCH_ID is the device ID rather than the app key, and that begin() runs after WiFi connects. In Mokxi the connection is never made.
Alexa says the device is not responding
onPowerState() did not return true, or loop() blocks so SinricPro.handle() answers too late. Return true when the change succeeded, and replace delay() calls with millis() timers so the reply goes out within a second.
Events are ignored after a few button presses
The library and the server limit how often a device may send events, so a bouncing button or a loop that sends constantly gets throttled. Debounce the input and only send when the state really changes.
Questions and answers
Is SinricPro free?
Plans and device limits are set by the SinricPro service and change over time, so check its website. The library itself is free and open source.
Try SinricPro without installing anything
Open the example, change the sketch, and keep your version in a free account.