At the top of your application add a include line for the device you want to use.
For example SinricProSwitch:
#include <SinricProSwitch.h>Create a new class MySwitch that inherits from SinricProSwitch
class MySwitch : public SinricProSwitch {
};Our constructor needs at least the deviceId and must initialize the inherited SinricProSwitch constructor with that id.
In this example, we will give our new class a variable switch_pin and initialize this by the constructor's switch_pin parameter.
Inside the constructer we use the pin variable to set the pinMode to OUTPUT:
class MySwitch : public SinricProSwitch {
public:
MySwitch(const String& deviceId, int switch_pin)
: SinricProSwitch(deviceId)
, switch_pin(switch_pin) {
pinMode(switch_pin, OUTPUT);
}
protected:
int switch_pin;
};The SinricProSwitch class offers the function onPowerState which gets called when our device is requested to turn on or off.
All those functions have to return a bool to indicate that the request was handled properly return true; or not return false;
In our new class we override this function to turn on or off the pin we have defined before. And since there should nothing go wrong
turning a pin on or off, we will return the value true.
class MySwitch : public SinricProSwitch {
public:
MySwitch(const String& deviceId, int switch_pin)
: SinricProSwitch(deviceId)
, switch_pin(switch_pin) {
pinMode(switch_pin, OUTPUT);
}
bool onPowerState(bool& state) override {
digitalWrite(switch_pin, state);
return true;
}
protected:
int switch_pin;
};Create a new instance mySwitch from our class and initialize the deviceId and the switch_pin.
For this example we will use pin (gpio) number 13.
MySwitch mySwitch("your-device-id-here", 13);
Note: You have to create a new Switch device in SinricPro portal.
After you created the device you will get the deviceId and additionally the App Key and App Secret which is needed later.
Replace the placeholder your-device-id-here with the deivce ID you received from the portal.
Of course we need the WiFi functionality. For this we use the "good old" standard wifi initialization code.
To keep our code clean, we will seperate this into a seperate function setupWiFi() which is then called
by the setup() function later:
void setupWiFi() {
Serial.println("Connecting WiFi Network");
WiFi.begin("your-wifi-ssid", "your-wifi-password");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(250);
}
Serial.println("connected!");
}Note: replace the placeholders with your wifi credentials (ssid and password)
We need to initialize the SinricPro library with our App Key and App Secret which we got from the SinricPro portal.
For this we have to call the function SinricPro.begin() which needs the App Key as first parameter and the App secret as second parameter.
To keep the code clean, we will also do this in a seperate function setupSinricPro which is called from setup() later.
void setupSinricPro() {
SinricPro.begin("your-app-key-here", "your-app-secret-here");
}Note: replace the placeholders with the App Key and App Secret you got from the step before
First we initialize the Serial output with a baudrate of 115200.
Then we initialize the WiFi by calling our seperate setupWiFi() function.
Last but not least we initialize the SinricPro library by calling our setupSinricPro()function.
void setup() {
Serial.begin(115200);
setupWiFi();
setupSinricPro();
}All wee need to run our code is to call SinricPro.handle() in our loop() function:
void loop() {
SinricPro.handle();
}This is how our finals sketch looks now:
#include <SinricProSwitch.h>
class MySwitch : public SinricProSwitch {
public:
MySwitch(const String& deviceId, int switch_pin)
: SinricProSwitch(deviceId)
, switch_pin(switch_pin) {
pinMode(switch_pin, OUTPUT);
}
bool onPowerState(bool& state) override {
digitalWrite(switch_pin, state);
return true;
}
protected:
int switch_pin;
};
MySwitch mySwitch("your-device-id-here", 13);
void setupWiFi() {
Serial.println("Connecting WiFi Network");
WiFi.begin("your-wifi-ssid", "your-wifi-password");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(250);
}
Serial.println("connected!");
}
void setupSinricPro() {
SinricPro.begin("your-app-key-here", "your-app-secret-here");
}
void setup() {
Serial.begin(115200);
setupWiFi();
setupSinricPro();
}
void loop() {
SinricPro.handle();
}Since we can reuse our class, this is a very simple task
.
First, we need additional deviceId's. Visit the SinricPro portal and create a few more switch devices and note their deviceId's
.
Then create new instances of our class like so:
MySwitch mySwitch1("device_id_1", 13);
MySwitch mySwitch2("device_id_2", 14);
MySwitch mySwitch3("device_id_3", 15);Note: Replace the placeholders device_id_1, device_id_2 and device_id_3 with the device IDs you received from the portal.
Part 2 - Adding a button for local control
Part 3 - Organizing the project