Open jamsyogendra opened 3 years ago
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <SinricPro.h>
#include <SinricProSwitch.h>
#include <SinricProDimSwitch.h>
#include <RBDdimmer.h>
#define SSID "" // WiFi SSID
#define PASS "" // WiFi Password
#define APPKEY "" // SinricPro AppKey
#define APPSECRET "" // SinricPro AppSecret
#define RELAY1_ID "" // SinricPro deviceId for relay1
#define RELAY2_ID "" // SinricPro deviceId for relay2
#define DIMSWITCH_ID "" // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define RELAY1_PIN D3 // PIN relay1 is connected to
#define RELAY2_PIN D6 // PIN relay2 is connected to
#define outputPin D1
#define zerocross D7 // for boards with CHANGEBLE input pins
SinricProSwitch &relay1 = SinricPro[RELAY1_ID]; // declare SinricPro device for relay1 and add the device to SinricProSDK
SinricProSwitch &relay2 = SinricPro[RELAY2_ID]; // declare SinricPro device for relay2 and add the device to SinricProSD
SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID];
dimmerLamp dimmer(outputPin, zerocross);
int outVal = 0;
int dim_val = 0;
// callback for onPowerState (relay1)
bool onPowerStateRelay1(const String& deviceId, bool &state) {
digitalWrite(RELAY1_PIN, state); // turn RELAY1_PIN on/off (depending on state parameter)
Serial.printf("[Relay1]: turn %s\r\n", state ? "on" : "off"); // just some output on serial monitor
return true;
}
// callback for onPowerState (relay2)
bool onPowerStateRelay2(const String& deviceId, bool &state) {
digitalWrite(RELAY2_PIN, state); // turn RELAY1_PIN on/off (depending on state parameter)
Serial.printf("[Relay2]: turn %s\r\n", state ? "on" : "off"); // just some output on serial monitor
return true;
}
bool onPowerLevel(const String &deviceId, int &powerLevel) {
dim_val = map(outVal, 45, 100, 45, 100); // mapped the value for dimmer
dimmer.setPower(dim_val); // Set dimmer power
return true;
}
void setupPins() {
pinMode(RELAY1_PIN, OUTPUT); // initialize RELAY1_PIN as OUTPUT
pinMode(RELAY2_PIN, OUTPUT); // initialize RELAY2_PIN as OUTPUT
}
// setup part for connecting to WiFi (just to keep setup() nice and clean)
void setupWiFi() {
Serial.printf("[WiFi]: Connecting to %s", SSID); // print to which WiFi we are trying to connect to
WiFi.begin(SSID, PASS); // start connecting to WiFi
while (WiFi.status() != WL_CONNECTED) { // wait until WiFi has been connected
Serial.printf("."); // meanwhile print a dot
delay(250); // every 250ms
}
Serial.printf("connected\r\n"); // print to serial that we are connected to wifi now
}
// setup part for SinricProSDK (just to keep setup() nice and clean)
void setupSinricPro() {
relay1.onPowerState(onPowerStateRelay1); // assign onPowerStateRelay1 callback function to relay1 device
relay2.onPowerState(onPowerStateRelay2); // assign onPowerStateRelay2 callback function to relay2 device
myDimSwitch.onPowerLevel(onPowerLevel);
SinricPro.onConnected([]() { // instead of writing a full callback function we use a lambda function which get called when ESP is connected to SinricPro
Serial.printf("[SinricPro]: Connected\r\n"); // and this function does: output to serial "[SinricPro]: Connected"
});
SinricPro.onDisconnected([]() { // instead of writing a full callback function we use a lambda function which get called when ESP is disconnected from SinricPro
Serial.printf("[SinricPro]: Disconnected\r\n"); // and this function does: output to serial "[SinricPro]: Disconnected"
});
SinricPro.begin(APPKEY, APPSECRET); // initialize SinricProSDK with APPKEY and APPSECRET
}
void setup() {
Serial.begin(115200); // initialize Serial to baudrate 115200
Serial.println(); // print a single line (to get rid of the junk)
setupPins(); // call setupPin function
setupWiFi(); // call setupWiFi function
setupSinricPro(); // call setupSinricPro function
dimmer.begin(NORMAL_MODE, ON);
}
void loop() {
SinricPro.handle(); // do the magic...
dimmer.setPower(dim_val);
}
Here is my decoding result