Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
126 lines (106 sloc) 4.25 KB
/*
* If you encounter any issues:
* - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
* - ensure all dependent libraries are installed
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
* - open serial monitor and check whats happening
* - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
* - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues 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
#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif
#include <Arduino.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif
#include "SinricPro.h"
#include "SinricProPowerSensor.h"
#define WIFI_SSID "YOUR-WIFI-SSID"
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
#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");
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
Serial.printf(".");
delay(250);
}
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
myPowerSensor.onPowerState(onPowerState);
// setup SinricPro
SinricPro.restoreDeviceStates(true);
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() {
Serial.begin(BAUD_RATE);
setupWiFi();
setupSinricPro();
}
void loop() {
SinricPro.handle();
sendPowerSensorData();
}
You can’t perform that action at this time.