sinricpro / esp8266-esp32-sdk

Library for https://sinric.pro - simple way to connect your device to Alexa, Google Home, SmartThings and cloud
https://sinric.pro
Other
230 stars 124 forks source link

how to modify the example switch code to add multiple device ids in sinric.pro #19

Closed doc-96 closed 4 years ago

doc-96 commented 4 years ago

how to modify the example switch code to add multiple device ids and control multiple switches with one nodemcu with sinric.pro like was possible with sinric.com. please help

sivar2311 commented 4 years ago

Here is my Sketch to control 10 433MHz power sockets with just one nodemcu. I am using a std::map to store the deviceId's, RC-configuration and powerstates.

#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
//#define NODEBUG_SINRIC

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <SinricPro.h>
#include <SinricProSwitch.h>
#include <RCLSwitch.h>

#include <map>

#define HOSTNAME "alexa-rcl-bridge"

#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 BAUD_RATE         9600                // Change baudrate to your need

#define RCLpin D5
CRCLSwitch<RCLpin> RCLSwitch;

typedef struct {
  byte group;
  byte device;
  bool state;
} config_t;

std::map<String, config_t> deviceConfig = {
// SinricPro deviceId        , RC-Group, RC-Device 
  {"5dffxxxxxxxxxxxxxxxxxxx1", {0b10000, 0b10000, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx2", {0b10000, 0b01000, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx3", {0b10000, 0b00100, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx4", {0b10000, 0b00010, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx5", {0b10000, 0b00001, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx5", {0b01000, 0b10000, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx6", {0b01000, 0b01000, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx7", {0b01000, 0b00100, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx8", {0b01000, 0b00010, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx0", {0b01000, 0b00001, false}}
};

bool onPowerState(String deviceId, bool &state) {
  config_t config = deviceConfig[deviceId];
  Serial.printf("%s, %d, %d: %s\r\n", deviceId.c_str(), config.group, config.device, state?"on":"off");
  for (int i=0; i<3; i++) {
    RCLSwitch.write(config.group, config.device, state);
  }
  deviceConfig[deviceId].state = state;
  return true;
}

void setupWiFi() {
  WiFi.hostname(HOSTNAME);

  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 %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
}

void setupSinricPro() {
  for (auto& config : deviceConfig) {
    const char* deviceId = config.first.c_str();
    SinricProSwitch &mySwitch = SinricPro[deviceId];
    mySwitch.onPowerState(onPowerState);
  }

  SinricPro.begin(APP_KEY, APP_SECRET);
  SinricPro.restoreDeviceStates(true);
}

void setup() {
  Serial.begin(BAUD_RATE);
  setupWiFi();
  setupSinricPro();
  RCLSwitch.begin();
}

void loop() {
  SinricPro.handle();
}
doc-96 commented 4 years ago

@sivar2311 thanks a ton for prompt reply...however i do get a warning while compling : "

WARNING: library RCLSwitch claims to run on avr architecture(s) and may be incompatible with your current board which runs on esp8266 architecture(s).

also please help me where to change or define the output pins in the following code please excuse me if this is something silly. i am no expert at this

doc-96 commented 4 years ago

I plan to use nodemcu v1 esp8266 to control multiple switches using sinric.pro and alexa

kakopappa commented 4 years ago

Haven't tested it.

// 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 "SinricProSwitch.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 SWITCH_ID_FIRST         "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_SECOND        "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"

#define BAUD_RATE         9600                // Change baudrate to your need

bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off");
  return true; // request handled properly
}

// setup function for WiFi connection
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 %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
}

// setup function for SinricPro
void setupSinricPro() {
  // add device to SinricPro
  SinricProSwitch& myFirstSwitch = SinricPro[SWITCH_ID_FIRST];
  myFirstSwitch.onPowerState(onPowerState);

  SinricProSwitch& mySecondSwitch = SinricPro[SWITCH_ID_SECOND];
  mySecondSwitch.onPowerState(onPowerState);

  // setup SinricPro
  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); Serial.printf("\r\n\r\n");
  setupWiFi();
  setupSinricPro();
}

void loop() {
    SinricPro.handle();
}
sivar2311 commented 4 years ago

@sivar2311 thanks a ton for prompt reply...however i do get a warning while compling : "

WARNING: library RCLSwitch claims to run on avr architecture(s) and may be incompatible with your current board which runs on esp8266 architecture(s).

also please help me where to change or define the output pins in the following code please excuse me if this is something silly. i am no expert at this

This was just an example, how i did it for multiple devices which are controlled over 433mhz (by using RCL Switch library).

I think you don't need RCLSwitch library, because i think you want to switch only some pins on your ESP?

What kind of ESP do you have? What pins do you want to switch? Please describe your project.

doc-96 commented 4 years ago

@sivar2311 My project is controlling a 4 channel relay using the nodemcu with esp 12e so as to make a switch which can be controlled using alexa. i had made it to work on sinric.com with the following attached code. However now i want to make it using sinric.pro and im having trouble altering the code for that.

i want to switch D1, D2, D3, D4 pins corresponding to GPIO5, GPIO4, GPIO0, GPIO2

include

include

include

include // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries

include // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries (use the correct version)

include

int device_1 = 5; int device_2 = 4; int device_3 = 0; int device_4 = 2;

ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;

define MyApiKey "APIKEY" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard

define MySSID "MYSSID" // TODO: Change to your Wifi network SSID

define MyWifiPassword "MYPASSWORD" // TODO: Change to your Wifi network password

define HEARTBEAT_INTERVAL 300000 // 5 Minutes

uint64_t heartbeatTimestamp = 0; bool isConnected = false;

// deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it here

void turnOn(String deviceId) { if (deviceId == "5e0f194ac30eb51fa1cab9a8") // Device ID of first device {
Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_1, LOW); } else if (deviceId == "5e0f1972c30eb51fa1cab9aa") // Device ID of second device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_2, LOW); } else if (deviceId == "5e0f1a46c30eb51fa1cab9ad") // Device ID of third device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_3, LOW); } else if (deviceId == "5e0f1a7cc30eb51fa1cab9af") // Device ID of fourth device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_4, LOW); } else { Serial.print("Turn on for unknown device id: "); Serial.println(deviceId);
}
}

void turnOff(String deviceId) { if (deviceId == "5e0f194ac30eb51fa1cab9a8") // Device ID of first device {
Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_1, HIGH); } else if (deviceId == "5e0f1972c30eb51fa1cab9aa") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_2, HIGH); } else if (deviceId == "5e0f1a46c30eb51fa1cab9ad") // Device ID of third device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_3, HIGH); } else if (deviceId == "5e0f1a7cc30eb51fa1cab9af") // Device ID of fourth device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_4, HIGH); } else { Serial.print("Turn off for unknown device id: "); Serial.println(deviceId);
} }

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { switch(type) { case WStype_DISCONNECTED: isConnected = false;
Serial.printf("[WSc] Webservice disconnected from sinric.com!\n"); break; case WStype_CONNECTED: { isConnected = true; Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload); Serial.printf("Waiting for commands from sinric.com ...\n");
} break; case WStype_TEXT: { Serial.printf("[WSc] get text: %s\n", payload); // Example payloads

    // For Switch or Light device types
    // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html

    // For Light device type
    // Look at the light example in github

if ARDUINOJSON_VERSION_MAJOR == 5

    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.parseObject((char*)payload);

endif

if ARDUINOJSON_VERSION_MAJOR == 6

    DynamicJsonDocument json(1024);
    deserializeJson(json, (char*) payload);      

endif

    String deviceId = json ["deviceId"];     
    String action = json ["action"];

    if(action == "setPowerState") { // Switch or Light
        String value = json ["value"];
        if(value == "ON") {
            turnOn(deviceId);
        } else {
            turnOff(deviceId);
        }
    }
    else if (action == "SetTargetTemperature") {
        String deviceId = json ["deviceId"];     
        String action = json ["action"];
        String value = json ["value"];
    }
    else if (action == "test") {
        Serial.println("[WSc] received test command from sinric.com");
    }
  }
  break;
case WStype_BIN:
  Serial.printf("[WSc] get binary length: %u\n", length);
  break;

} }

void setup() { Serial.begin(115200); pinMode(device_1, OUTPUT); digitalWrite(device_1, HIGH); pinMode(device_2, OUTPUT); digitalWrite(device_2, HIGH); pinMode(device_3, OUTPUT); digitalWrite(device_3, HIGH); pinMode(device_4, OUTPUT); digitalWrite(device_4, HIGH);

WiFiMulti.addAP(MySSID, MyWifiPassword); Serial.println(); Serial.print("Connecting to Wifi: "); Serial.println(MySSID);

// Waiting for Wifi connect while(WiFiMulti.run() != WL_CONNECTED) { delay(500); Serial.print("."); } if(WiFiMulti.run() == WL_CONNECTED) { Serial.println(""); Serial.print("WiFi connected. "); Serial.print("IP address: "); Serial.println(WiFi.localIP()); }

// server address, port and URL webSocket.begin("iot.sinric.com", 80, "/");

// event handler webSocket.onEvent(webSocketEvent); webSocket.setAuthorization("apikey", MyApiKey);

// try again every 5000ms if connection has failed webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets }

void loop() { webSocket.loop();

if(isConnected) { uint64_t now = millis();

  // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
  if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
      heartbeatTimestamp = now;
      webSocket.sendTXT("H");          
  }

}
}

doc-96 commented 4 years ago

Haven't tested it.

// 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 "SinricProSwitch.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 SWITCH_ID_FIRST         "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_SECOND        "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"

#define BAUD_RATE         9600                // Change baudrate to your need

bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off");
  return true; // request handled properly
}

// setup function for WiFi connection
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 %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
}

// setup function for SinricPro
void setupSinricPro() {
  // add device to SinricPro
  SinricProSwitch& myFirstSwitch = SinricPro[SWITCH_ID_FIRST];
  myFirstSwitch.onPowerState(onPowerState);

  SinricProSwitch& mySecondSwitch = SinricPro[SWITCH_ID_SECOND];
  mySecondSwitch.onPowerState(onPowerState);

  // setup SinricPro
  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); Serial.printf("\r\n\r\n");
  setupWiFi();
  setupSinricPro();
}

void loop() {
    SinricPro.handle();
}

the code compiles with no error but the relay does not turn off or on and using sinric.pro dashboard although the device is shown online

sivar2311 commented 4 years ago

I changed my example above to your needs. You only have to put in your credentials (see #define's) and your device id's (see std::map)

#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
//#define NODEBUG_SINRIC

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <SinricPro.h>
#include <SinricProSwitch.h>

#include <map>

#define HOSTNAME "ALEXA4RELAY"

// REPLACE IT WITH YOUR OWN CREDENTIALS !!!!!
#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 BAUD_RATE         9600                // Change baudrate to your need

typedef struct {
  int pin;
  bool state;
} config_t;

// REPLACE WITH YOUR OWN DEVICE IDs !!!!!!!
std::map<String, config_t> deviceConfig = {
// SinricPro deviceId        ,  PIN, state (on/high=true, off/low = false) 
  {"5dffxxxxxxxxxxxxxxxxxxx1", {D1, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx2", {D2, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx3", {D3, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx4", {D4, false}}
};

bool onPowerState(String deviceId, bool &state) {
  config_t config = deviceConfig[deviceId]; // get config for corresponding deviceId
  Serial.printf("DeviceId: %s, PIN: %d, state: %s\r\n", deviceId.c_str(), config.pin, state?"on":"off");
  digitalWrite(config.pin, state);
  deviceConfig[deviceId].state = state;
  return true;
}

void setupWiFi() {
  WiFi.hostname(HOSTNAME);

  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 %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
}

void setupSinricPro() {
  for (auto& config : deviceConfig) {
    const char* deviceId = config.first.c_str();
    SinricProSwitch &mySwitch = SinricPro[deviceId];
    mySwitch.onPowerState(onPowerState);
    pinMode(config.second.pin, OUTPUT);
  }

  SinricPro.begin(APP_KEY, APP_SECRET);
  SinricPro.restoreDeviceStates(true);
}

void setup() {
  Serial.begin(BAUD_RATE);
  setupWiFi();
  setupSinricPro();
}

void loop() {
  SinricPro.handle();
}
doc-96 commented 4 years ago

@sivar2311 thanks a lot...the code works like a charm. Appreciate the time and effort you spared for this it helped alot. one small issue is that the conrols are inverted. in off state the relay is actually on and in on state it is off

sivar2311 commented 4 years ago

Please check your relay wiring. There are three terminals on the high side (NO, NC, GND). (NO = normally open, NC = normally closed) Plese see https://electronics.stackexchange.com/a/30956

doc-96 commented 4 years ago

@sivar2311 all is sorted now. all thanks to you.

doc-96 commented 4 years ago

i am closing this issue since it is resolved.

footballguy05 commented 4 years ago

I changed my example above to your needs. You only have to put in your credentials (see #define's) and your device id's (see std::map)

#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
//#define NODEBUG_SINRIC

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <SinricPro.h>
#include <SinricProSwitch.h>

#include <map>

#define HOSTNAME "ALEXA4RELAY"

// REPLACE IT WITH YOUR OWN CREDENTIALS !!!!!
#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 BAUD_RATE         9600                // Change baudrate to your need

typedef struct {
  int pin;
  bool state;
} config_t;

// REPLACE WITH YOUR OWN DEVICE IDs !!!!!!!
std::map<String, config_t> deviceConfig = {
// SinricPro deviceId        ,  PIN, state (on/high=true, off/low = false) 
  {"5dffxxxxxxxxxxxxxxxxxxx1", {D1, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx2", {D2, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx3", {D3, false}},
  {"5dffxxxxxxxxxxxxxxxxxxx4", {D4, false}}
};

bool onPowerState(String deviceId, bool &state) {
  config_t config = deviceConfig[deviceId]; // get config for corresponding deviceId
  Serial.printf("DeviceId: %s, PIN: %d, state: %s\r\n", deviceId.c_str(), config.pin, state?"on":"off");
  digitalWrite(config.pin, state);
  deviceConfig[deviceId].state = state;
  return true;
}

void setupWiFi() {
  WiFi.hostname(HOSTNAME);

  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 %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
}

void setupSinricPro() {
  for (auto& config : deviceConfig) {
    const char* deviceId = config.first.c_str();
    SinricProSwitch &mySwitch = SinricPro[deviceId];
    mySwitch.onPowerState(onPowerState);
    pinMode(config.second.pin, OUTPUT);
  }

  SinricPro.begin(APP_KEY, APP_SECRET);
  SinricPro.restoreDeviceStates(true);
}

void setup() {
  Serial.begin(BAUD_RATE);
  setupWiFi();
  setupSinricPro();
}

void loop() {
  SinricPro.handle();
}

Thanks for the great bit of code. I've stumbled upon this thread as I'm venturing into arduino/nodemcu/etc for the first time. I've set up this bit of code on a nodemcu and a 4 channel relay as the OP was doing. However, i'm attempting to modify this bit to control a linear actuator. As two of the channels will be controlling one motor, do you have a clean way of ensuring that both channels cannot be on at the same time? If the d1 pin is active, the actuator extends. d1 should be deactivated prior to d2 going hot to retract the actuator. Any help would be great! Thanks

sivar2311 commented 4 years ago

Hi @footballguy05 and welcome to the world of microcontrollers.

I would recommend first do a digital write LOW on all pins first, then digital write HIGH on the corresponding pin. Doing so, you're always safe.

Please note

antoniomorsello commented 4 years ago

I downloaded the library and loaded the code it gives me an error while compiling the esp32 dev module board

sivar2311 commented 4 years ago

@antoniomorsello

  1. Please open a new issue
  2. Please give detailed information about
    • Which example did you try
    • What exact error messages did you get
PDPR commented 3 years ago

Hi, Is it possible to combine 2 different devices, as a switch and a temperature sensor on the same ESP8266? I've been reviewing this thread and all of them are for devices of the same type. I have reviewed as well all the Sinric examples but none of them have a combined code example. Thanks

sivar2311 commented 3 years ago

This is one of the main advantages of SinicPro. Short example how a setupSinric() could look like:

  ...
  SinricProSwitch &mySwitch = SinricPro[SWITCH_ID];
  SinricProLight &myLight = SinricPro[LIGHT_ID];
  SinricProTV &myTV = SinricPro[TV_ID];
  ...

Now you have 3 different devices running on same ESP chip.

This example needs a lot more code to setup all the necessary callback functions. But generally that's the way this works.

For further questions on this, please open a new issue. This issue is closed and your question doesnt correspond to the original issue. Thank you for your understanding

plamsky07 commented 1 year ago

Hi, how to modify the example code to add multiple device ids and control multiple relays and one dht22 sensor with one nodemcu with sinric.pro. The sensor will be use only for information. Please help!

sivar2311 commented 1 year ago

Hi @plamsky07! Please see the examples multiswitch beginner and DHT22.

Implement the digitalWrite commands for controlling the relays in the callback functions onPowerState1 to onPowerState4.