Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
* If you encounter any issues:
* - check the at
* - ensure all dependent libraries are installed
* - see
* - see
* - open serial monitor and check whats happening
* - check full user documentation at
* - visit and check for existing issues or open a new one
* How this example works:
* - if this device is turned on, it makes a power measurement
* - after SAMPLE_EVERY_SEC seconds, this data is sent to SinricPro server and a new mesurement is taken (this step repeats until device is turned off)
// Uncomment the following line to enable serial debug output
//#define ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NDEBUG
#include <Arduino.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#ifdef ESP32
#include <WiFi.h>
#include "SinricPro.h"
#include "SinricProPowerSensor.h"
#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define POWERSENSOR_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE 9600 // Change baudrate to your need (used for serial monitor)
#define SAMPLE_EVERY_SEC 60 // Send every 60 seconds new data to server
bool powerState = false;
// struct to store measurement from powersensor
struct {
float voltage;
float current;
float power;
float apparentPower;
float reactivePower;
float factor;
} powerMeasure;
// this is where you read from power sensor
// in this example, random values are used
void doPowerMeasure() {
powerMeasure.voltage = random(2200,2300) / 10.0f;
powerMeasure.current = random(1,20) / 10.0f;
powerMeasure.power = powerMeasure.voltage * powerMeasure.current;
powerMeasure.apparentPower = powerMeasure.power + (random(10,20)/10.0f);
bool onPowerState(const String &deviceId, bool &state) {
Serial.printf("Device %s power turned %s \r\n", deviceId.c_str(), state?"on":"off");
powerState = state;
if (powerState) doPowerMeasure(); // start a measurement when device is turned on
return true; // request handled properly
bool sendPowerSensorData() {
// dont send data if device is turned off
if (!powerState) return false;
// limit data rate to SAMPLE_EVERY_SEC
static unsigned long lastEvent = 0;
unsigned long actualMillis = millis();
if (actualMillis - lastEvent < (SAMPLE_EVERY_SEC * 1000)) return false;
lastEvent = actualMillis;
// send measured data
SinricProPowerSensor &myPowerSensor = SinricPro[POWERSENSOR_ID];
bool success = myPowerSensor.sendPowerSensorEvent(powerMeasure.voltage, powerMeasure.current, powerMeasure.power, powerMeasure.apparentPower);
// if measured data was sent do a new measure
if (success) doPowerMeasure();
return success;
void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
while (WiFi.status() != WL_CONNECTED) {
IPAddress localIP = WiFi.localIP();
Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
void setupSinricPro() {
SinricProPowerSensor &myPowerSensor = SinricPro[POWERSENSOR_ID];
// set callback function to device
// setup SinricPro
//SinricPro.restoreDeviceStates(true); // Uncomment to restore the last known state from the server.
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); });
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
SinricPro.begin(APP_KEY, APP_SECRET);
// main setup function
void setup() {
void loop() {