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
236 stars 125 forks source link

Please tell us how to add AC Dimmer in Sinricpro Multi Switch lib #145

Closed jamsyogendra closed 3 years ago

jamsyogendra commented 3 years ago

Hello, Everyone greeting to all,

I wanted to control "4 channel relay module + 1 Ac Dimmer(RobotDyn)", Manually or by Alexa using Sinric Pro. with ESP32 wifi Module

I just completed my first part of the project which is "4 channel relay module, Manually or by Alexa using Sinric Pro." using MultiSwitch_advance.ino

How can I add RobotDyn Ac Dimmer coding or modify this code? So I can control manually using a rotary switch, and control using Alexa with live feedback

here is detail all about dimmer which is I using?

[https://robotdyn.com/ac-light-dimmer-module-1-channel-3-3v-5v-logic-ac-50-60hz-220v-110v.html]

jamsyogendra commented 3 years ago

https://github.com/nassir-malik/IOT-AC-Light-Dimmer-With-Alexa it can help but i don't know how?

https://github.com/RobotDynOfficial/RBDDimmer/commit/14544cd8aa85ddd8db9e7ba0238e91cb9e240ece

sivar2311 commented 3 years ago

I don't see SinricProDimSwitch here - which you will need. I don't see any implementation made by you. The only thing i can see is that you posted an exact copy of MultiSwitch_advance.ino (which is an advanced example).

If this is the first time you're using SinricPro and this library: Try to begin with a simple project. Use SinricProSwitch and a LED which can be turned on / off, then adding more stuff like a button for manually control. Then create a new project using SinricProDimSwitch to let the LED dim. Point here is: get a feeling about how things are working.

Please don't post a 1:1 copy of our own code here - it's already in the examples folder. If you have specific questions or issues, please post the code you wrote.

If you made your first steps sucessfuly you can continue implementing the sketch which this issue is about: You'll need 4x SinricProSwitch and 1x SinricProDimSwitch.

jamsyogendra commented 3 years ago

Dear Sir lot of love from India,,,,,, To be very honest, this is my first time with Sinric Pro as you know in my project I am controlling 4 channel relay module using Alexa, or with my physical board(Manually), I already completed my first part of the project, I am getting live feedback from Alexa with very respectively.

But, I am trying to add AC Dimmer Module in this project as a Fan Regulator.

as you said we need 4x SinricProSwitch and 1x SinricProDimSwitch. but i don't know how can I implement this bcz i have zero knowledge of coding, This project i just made for my Grandnani bcz at this time in India , summer is coming and my grandnani need fan

sivar2311 commented 3 years ago

The basic skeleton you need looks something like this.

#include <Arduino.h>
#include <SinricPro.h>
#include <SinricProSwitch.h>
#include <SinricProDimSwitch.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_1       "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_2       "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_3       "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_4       "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define DIMSWITCH_ID      "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"

SinricProSwitch    &mySwitch1   = SinricPro[SWITCH_ID_1];
SinricProSwitch    &mySwitch2   = SinricPro[SWITCH_ID_2];
SinricProSwitch    &mySwitch3   = SinricPro[SWITCH_ID_3];
SinricProSwitch    &mySwitch4   = SinricPro[SWITCH_ID_4];
SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID];

#define BAUD_RATE         9600                // Change baudrate to your need

bool onPowerState1(const String &deviceId, bool &state) {
  // put your code for relay 1 in here (digitalWrite etc)
  return true; // request handled properly
}

bool onPowerState2(const String &deviceId, bool &state) {
  // put your code for relay 2 in here (digitalWrite etc)
  return true; // request handled properly
}

bool onPowerState3(const String &deviceId, bool &state) {
  // put your code for relay 3 in here (digitalWrite etc)
  return true; // request handled properly
}

bool onPowerState4(const String &deviceId, bool &state) {
  // put your code for relay 4 in here (digitalWrite etc)
  return true; // request handled properly
}

bool onPowerLevel(const String &deviceId, int &powerLevel) {
  // put your code for your AC dimmer in here (analogWrite?)
  return true;
}

void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting to %s", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }
  Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}

// setup function for SinricPro
void setupSinricPro() {
  mySwitch1.onPowerState(onPowerState1);
  mySwitch2.onPowerState(onPowerState2);
  mySwitch3.onPowerState(onPowerState3);
  mySwitch4.onPowerState(onPowerState4);
  myDimSwitch.onPowerLevel(onPowerLevel);

  // 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);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(BAUD_RATE);
  setupWiFi();
  setupSinricPro();
}

void loop() {
  // put your main code here, to run repeatedly:
  SinricPro.handle();
}

In onPowerState1 .. onPowerState4 functions put the code to control your relays. The parameter state will be true if your relay should turn on and false if your relay should turn off.

In onPowerLevel function put in the code for your AC dimmer. The parameter powerLevel gives you the value your AC dimmer should be set to. It will be between 0 (no power) and 100 (full power).

jamsyogendra commented 3 years ago

Dear Sir, Firstly I thank you from the bottom of my heart,

I try this


#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif

#include <Arduino.h>
#include <SinricPro.h>
#include <SinricProSwitch.h>
#include <SinricProDimSwitch.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_1       "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_2       "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_3       "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_4       "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define DIMSWITCH_ID      "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"

SinricProSwitch    &mySwitch1   = SinricPro[SWITCH_ID_1];
SinricProSwitch    &mySwitch2   = SinricPro[SWITCH_ID_2];
SinricProSwitch    &mySwitch3   = SinricPro[SWITCH_ID_3];
SinricProSwitch    &mySwitch4   = SinricPro[SWITCH_ID_4];
SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID];

#define BAUD_RATE         9600                // Change baudrate to your need

const int RELAY1_PIN = 15;
const int RELAY2_PIN = 2;
const int RELAY3_PIN = 4;
const int RELAY4_PIN = 22;

const byte zcPin = 12;
const byte pwmPin = 13;

byte fade = 1;
byte state = 1;
byte tarBrightness = 255;
byte curBrightness = 0;
byte zcState = 0; // 0 = ready; 1 = processing;

bool onPowerState1(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY1_PIN, LOW);
  return true; // request handled properly
}

bool onPowerState2(const String &deviceId, bool &state) {
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);
    digitalWrite(RELAY2_PIN, LOW);
  return true; // request handled properly
}

bool onPowerState3(const String &deviceId, bool &state) {
   Serial.print("Turn on device id: ");
    Serial.println(deviceId);
    digitalWrite(RELAY3_PIN, LOW);
  return true; // request handled properly
}

bool onPowerState4(const String &deviceId, bool &state) {
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);
    digitalWrite(RELAY4_PIN, LOW);
  return true; // request handled properly
}

bool onPowerLevel(const String &deviceId, int &powerLevel) {
   if (fade == 1) {
      if (curBrightness > tarBrightness || (state == 0 && curBrightness > 0)) {
        --curBrightness;
      }
      else if (curBrightness < tarBrightness && state == 1 && curBrightness < 255) {
        ++curBrightness;
      }
    }
    else {
      if (state == 1) {
        curBrightness = tarBrightness;
      }
      else {
        curBrightness = 0;
      }
    }

    if (curBrightness == 0) {
      state = 0;
      digitalWrite(pwmPin, 0);
    }
    else if (curBrightness == 255) {
      state = 1;
      digitalWrite(pwmPin, 1);
    }
    else {
      digitalWrite(pwmPin, 1);
    }

    zcState = 0;
}

void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting to %s", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }
  Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}

// setup function for SinricPro
void setupSinricPro() {
  mySwitch1.onPowerState(false);
  mySwitch2.onPowerState(false);
  mySwitch3.onPowerState(false);
  mySwitch4.onPowerState(false);
  myDimSwitch.onPowerLevel(onPowerLevel);

  // 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);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(BAUD_RATE);
  setupWiFi();
  setupSinricPro();
}

void loop() {
  // put your main code here, to run repeatedly:
  SinricPro.handle(); 

```}
After i Don't know how to write this step
myDimSwitch.onPowerLevel(onPowerLevel); 
sivar2311 commented 3 years ago

onPowerState

You wrote:

bool onPowerState1(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY1_PIN, LOW);
  return true; // request handled properly
}

This will always write LOW to the pin. It looks like your Relay is inverted (LOW = on / HIGH = off)?

The state parameter gives you the information for on / off where true is for on and false is for off - so it needs to be inverted. Do it like this example:

bool onPowerState1(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY1_PIN, !state);
  return true; // request handled properly
}

The only difference here is to write the invers (thats what the ! is for) of state parameter instead of LOW. This allows you to turn on AND off your relays. (In Arduino LOW is the same as false and HIGH is the same as true)

onPowerLevel

You wrote:

bool onPowerLevel(const String &deviceId, int &powerLevel) {
   if (fade == 1) {
      if (curBrightness > tarBrightness || (state == 0 && curBrightness > 0)) {
        --curBrightness;
      }
      else if (curBrightness < tarBrightness && state == 1 && curBrightness < 255) {
        ++curBrightness;
      }
    }
    else {
      if (state == 1) {
        curBrightness = tarBrightness;
      }
      else {
        curBrightness = 0;
      }
    }

    if (curBrightness == 0) {
      state = 0;
      digitalWrite(pwmPin, 0);
    }
    else if (curBrightness == 255) {
      state = 1;
      digitalWrite(pwmPin, 1);
    }
    else {
      digitalWrite(pwmPin, 1);
    }
    zcState = 0;
}

I don't get the meaning of the variables and the logic here. Dimming is usually done by analogWrite(pin, value) (where value is between 0 and 1023 on a ESP) The powerLevel variable gives you a value between 0 and 100 So, this needs analogWrite instead of digitalWrite and mapped values from 0..100 to 0..1023 Example:

bool onPowerLevel(const String &deviceId, int &powerLevel) {
  int analogValue = map(powerLevel, 0, 100, 0, 1023);  // maps values between 0..100 to 0..1023 
  analogWrite(pwmPin, analogValue);
  return true;
}

This might require some additonal logic (if you want to take device's powerState into account) like: don't analogWrite if device is turned off.

setupSinricPro

You wrote:

void setupSinricPro() {
  mySwitch1.onPowerState(false); // <-- wrong
  mySwitch2.onPowerState(false); // <-- wrong
  mySwitch3.onPowerState(false); // <-- wrong
  mySwitch4.onPowerState(false); // <-- wrong
  myDimSwitch.onPowerLevel(onPowerLevel); // <-- this one is correct!
  ...
}

Sorry to say, but 4 of 5 are wrong. device.onPowerState(callbackFunction) is to set the onPowerState (1..4) callback functions you have written before.

Correct would be this:


void setupSinricPro() {
  mySwitch1.onPowerState(onPowerState1);
  mySwitch2.onPowerState(onPowerState2);
  mySwitch3.onPowerState(onPowerState3);
  mySwitch4.onPowerState(onPowerState4);
  myDimSwitch.onPowerLevel(onPowerLevel); 
  ...
}
jamsyogendra commented 3 years ago

I try again your suggestion

#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif

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

#define WIFI_SSID         "Yogendra"    
#define WIFI_PASS         "Jamsariya"
#define APP_KEY           "167aa480-1d06-4c30-9c5e-61679e23e582"      // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        "c75f4d59-d113-4340-8b6e-5e55146e4a11-60f1b139-1df5-4be0-979e-40131ddf96f4"   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"

#define SWITCH_ID_1       "6038b4123517a339659cf355"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_2       "6038b50753a7f9397bebd61c"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_3       "603b0d36bd5c040a18254494"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_4       "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define DIMSWITCH_ID      "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"

SinricProSwitch    &mySwitch1   = SinricPro["6038b4123517a339659cf355"];
SinricProSwitch    &mySwitch2   = SinricPro["6038b50753a7f9397bebd61c"];
SinricProSwitch    &mySwitch3   = SinricPro["603b0d36bd5c040a18254494"];
SinricProSwitch    &mySwitch4   = SinricPro[SWITCH_ID_4];
SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID];

#define BAUD_RATE         9600                // Change baudrate to your need

const int RELAY1_PIN = 15;
const int RELAY2_PIN = 2;
const int RELAY3_PIN = 4;
const int RELAY4_PIN = 22;

const int zcPin = 12;
const int pwmPin = 13;

bool onPowerState1(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY1_PIN, !state);
  return true; // request handled properly
}

bool onPowerState2(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY2_PIN, !state);
  return true; // request handled properly
}

bool onPowerState3(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY3_PIN, !state);
  return true; // request handled properly
}

bool onPowerState4(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY4_PIN, !state);
  return true; // request handled properly
}

bool onPowerLevel(const String &deviceId, int &powerLevel) {
  int analogValue = map(powerLevel1, 0, 100, 0, 1023); 
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(pwmPin, analogValue);
  return true; 
}

void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting to %s", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }
  Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}

// setup function for SinricPro
void setupSinricPro() {
  mySwitch1.onPowerState(onPowerState1);
  mySwitch2.onPowerState(onPowerState2);
  mySwitch3.onPowerState(onPowerState3);
  mySwitch4.onPowerState(onPowerState4);
  myDimSwitch.onPowerLevel(onPowerLevel);

  // 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);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(BAUD_RATE);
  setupWiFi();
  setupSinricPro();
}

void loop() {
  // put your main code here, to run repeatedly:
  SinricPro.handle();
}`

In Alexa app we can see all my device but the problem is it shows the device is unresponsive
connecting , disconnecting issue , with wi-fi

sivar2311 commented 3 years ago
#define SWITCH_ID_1       "6038b4123517a339659cf355"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_2       "6038b50753a7f9397bebd61c"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_3       "603b0d36bd5c040a18254494"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_4       "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define DIMSWITCH_ID      "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"

SWITCH_ID4 and DIMSWITCH_ID does not contain valid deviceId's!! You need 5 devices for your project! The three devices available for free are not enough!


if you #define ID's, make use of them!

SinricProSwitch    &mySwitch1   = SinricPro["6038b4123517a339659cf355"]; // use the defines!
SinricProSwitch    &mySwitch2   = SinricPro["6038b50753a7f9397bebd61c"]; // use the defines!
SinricProSwitch    &mySwitch3   = SinricPro["603b0d36bd5c040a18254494"]; // use the defines!
SinricProSwitch    &mySwitch4   = SinricPro[SWITCH_ID_4]; // here you did 
SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID]; // here you did 

Enable Debug

#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG

Start with simple project!

Again: please start with a simple sketch first, as I have already recommended. It looks like you have to learn a lot, and you have to do it step by step.

jamsyogendra commented 3 years ago

Thanks for your advice sir , I think I can't do this with sinric pro ,,, let see what can I do

sivar2311 commented 3 years ago

Surely you can do that with SinricPro! Why not?

You give up so quickly when you have already reached 80%?

What is the reason?

jamsyogendra commented 3 years ago
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif

#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG

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

#define WIFI_SSID         "Yogendra"    
#define WIFI_PASS         "Jamsariya"
#define APP_KEY           "167aa480-1d06-4c30-9c5e-61679e23e582"      // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        "c75f4d59-d113-4340-8b6e-5e55146e4a11-60f1b139-1df5-4be0-979e-40131ddf96f4"   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"

#define SWITCH_ID_1       "6038b4123517a339659cf355"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_2       "6038b50753a7f9397bebd61c"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_3       "603b0d36bd5c040a18254494"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_4       "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define DIMSWITCH_ID      "YOUR-DEVICE-ID"    // Should look like "5dc1564130xxxxxxxxxxxxxx"

SinricProSwitch    &mySwitch1   = SinricPro["6038b4123517a339659cf355"];
SinricProSwitch    &mySwitch2   = SinricPro["6038b50753a7f9397bebd61c"];
SinricProSwitch    &mySwitch3   = SinricPro["603b0d36bd5c040a18254494"];
SinricProSwitch    &mySwitch4   = SinricPro[SWITCH_ID_4];
SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID];

#define BAUD_RATE         9600                // Change baudrate to your need

const int RELAY1_PIN = 15;
const int RELAY2_PIN = 2;
const int RELAY3_PIN = 4;
const int RELAY4_PIN = 22;

const int zcPin = 12;
const int pwmPin = 13;

bool onPowerState1(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY1_PIN, !state);
  return true; // request handled properly
}

bool onPowerState2(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY2_PIN, !state);
  return true; // request handled properly
}

bool onPowerState3(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY3_PIN, !state);
  return true; // request handled properly
}

bool onPowerState4(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY4_PIN, !state);
  return true; // request handled properly
}

bool onPowerLevel(const String &deviceId, int &powerLevel) {
  int analogValue = map(powerLevel 1, 0, 100, 0, 1023); 
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(pwmPin, analogValue);
  return true; 
}

void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting to %s", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }
  Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}

// setup function for SinricPro
void setupSinricPro() {
  mySwitch1.onPowerState(onPowerState1);
  mySwitch2.onPowerState(onPowerState2);
  mySwitch3.onPowerState(onPowerState3);
  mySwitch4.onPowerState(onPowerState4);
  myDimSwitch.onPowerLevel(onPowerLevel);

  // 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);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(BAUD_RATE);
  setupWiFi();
  setupSinricPro();
}

void loop() {
  // put your main code here, to run repeatedly:
  SinricPro.handle();
}

getting error sir

Arduino: 1.8.12 (Windows 10), Board: "NodeMCU 0.9 (ESP-12 Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

C:\Users\jamsa\OneDrive\Desktop\sketch_mar01b\sketch_mar01b.ino: In function 'bool onPowerLevel(const String&, int&)':

sketch_mar01b:75:25: error: 'powerLevel1' was not declared in this scope

int analogValue = map(powerLevel1, 0, 100, 0, 1023);

                     ^

exit status 1 'powerLevel1' was not declared in this scope

This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

jamsyogendra commented 3 years ago

Dear Sir I think This Link Can Help Us! please can your try it

jamsyogendra commented 3 years ago

https://robotdyn.com/Blog/category/ac-power-control.html

sivar2311 commented 3 years ago
bool onPowerLevel(const String &deviceId, int &powerLevel) {
//  int analogValue = map(powerLevel 1, 0, 100, 0, 1023);  // <-- here is the issue! (see correct line below)
  int analogValue = map(powerLevel, 0, 100, 0, 1023); 
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(pwmPin, analogValue);
  return true; 
}

You didnt apply the advice i gave you before

  1. use the defines!
  2. you need 2 more devices (deviceIds!)
#define SWITCH_ID_1       "6038b4123517a339659cf355"   
#define SWITCH_ID_2       "6038b50753a7f9397bebd61c"  
#define SWITCH_ID_3       "603b0d36bd5c040a18254494" 
#define SWITCH_ID_4       "YOUR-DEVICE-ID"    // You need valid deviceId!!
#define DIMSWITCH_ID      "YOUR-DEVICE-ID"    // You need valid deviceId!!

SinricProSwitch    &mySwitch1   = SinricPro[SWITCH_ID_1];
SinricProSwitch    &mySwitch2   = SinricPro[SWITCH_ID_2];
SinricProSwitch    &mySwitch3   = SinricPro[SWITCH_ID_3];
SinricProSwitch    &mySwitch4   = SinricPro[SWITCH_ID_4];
SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID];
jamsyogendra commented 3 years ago

sorry sir I try again

sivar2311 commented 3 years ago

Dear Sir I think This Link Can Help Us!

If you're using RBDimmer library, you dont't need analogWrite and you don't need to map the values. RB dimmer takes values from 0..100

See RBdimmer example

jamsyogendra commented 3 years ago

I am not using any RBdimmer library

sivar2311 commented 3 years ago

To get a better understanding for you project:

sivar2311 commented 3 years ago

I am not using any RBdimmer library

But this library is used in the link you posted.

jamsyogendra commented 3 years ago
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif

#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG

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

#define WIFI_SSID         "Yogendra"    
#define WIFI_PASS         "Jamsariya"
#define APP_KEY           "167aa480-1d06-4c30-9c5e-61679e23e582"      // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        "c75f4d59-d113-4340-8b6e-5e55146e4a11-60f1b139-1df5-4be0-979e-40131ddf96f4"   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"

#define SWITCH_ID_1       "6038b4123517a339659cf355"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_2       "6038b50753a7f9397bebd61c"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define DIMSWITCH_ID      "603b0d36bd5c040a18254494"    // Should look like "5dc1564130xxxxxxxxxxxxxx"

SinricProSwitch    &mySwitch1   = SinricPro["6038b4123517a339659cf355"];
SinricProSwitch    &mySwitch2   = SinricPro["6038b50753a7f9397bebd61c"];
SinricProDimSwitch &myDimSwitch = SinricPro["603b0d36bd5c040a18254494"];

#define BAUD_RATE         9600                // Change baudrate to your need

const int RELAY1_PIN = 15;
const int RELAY2_PIN = 2;
const int RELAY3_PIN = 4;
const int RELAY4_PIN = 22;

const int zcPin = 12;
const int powerLevel1 = 13;

bool onPowerState1(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY1_PIN, !state);
  return true; // request handled properly
}

bool onPowerState2(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY2_PIN, !state);
  return true; // request handled properly
}

bool onPowerState3(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY3_PIN, !state);
  return true; // request handled properly
}

bool onPowerState4(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY4_PIN, !state);
  return true; // request handled properly
}

bool onPowerLevel(const String &deviceId, int &powerLevel) {
  int analogValue = map(powerLevel, 0, 100, 0, 1023); 
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(powerLevel1, analogValue);
  return true; 

}

void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting to %s", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }
  Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}

// setup function for SinricPro
void setupSinricPro() {
  mySwitch1.onPowerState(onPowerState1);
  mySwitch2.onPowerState(onPowerState2);
  myDimSwitch.onPowerLevel(onPowerLevel);

  // 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);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(BAUD_RATE);
  setupWiFi();
  setupSinricPro();
}

void loop() {
  // put your main code here, to run repeatedly:
  SinricPro.handle();
}

Compiling successfully No any Error Sir let see in action

jamsyogendra commented 3 years ago

I am using 4 Channel relay module controlled by Esp8266. as a fan regulator i am using Robotdyn dimmer

sivar2311 commented 3 years ago

I am using 4 Channel relay module controlled by Esp8266.

What are the relays for?

jamsyogendra commented 3 years ago

4 relay - ON/OFF

jamsyogendra commented 3 years ago

For Lighting Control

jamsyogendra commented 3 years ago

and dimmer i use as a fan regulator

jamsyogendra commented 3 years ago

4 relay - on/off for on-off lighting 1 ac dimmer - as a fan regulator

jamsyogendra commented 3 years ago

Not Working Sir,,,

sivar2311 commented 3 years ago

You should consider to use the correct device types (later) SinricPro have

This allows you to voice control your devices correctly

About your latest code: You didn't take my advice again!

What you have posted:

#define SWITCH_ID_1       "6038b4123517a339659cf355"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_2       "6038b50753a7f9397bebd61c"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define DIMSWITCH_ID      "603b0d36bd5c040a18254494"    // Should look like "5dc1564130xxxxxxxxxxxxxx"

SinricProSwitch    &mySwitch1   = SinricPro["6038b4123517a339659cf355"]; 
SinricProSwitch    &mySwitch2   = SinricPro["6038b50753a7f9397bebd61c"];
SinricProDimSwitch &myDimSwitch = SinricPro["603b0d36bd5c040a18254494"];

What i suggested to do:

#define SWITCH_ID_1       "6038b4123517a339659cf355"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_2       "6038b50753a7f9397bebd61c"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define DIMSWITCH_ID      "603b0d36bd5c040a18254494"    // Should look like "5dc1564130xxxxxxxxxxxxxx"

SinricProSwitch    &mySwitch1   = SinricPro[SWITCH_ID_1]; 
SinricProSwitch    &mySwitch2   = SinricPro[SWITCH_ID_2];
SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID];

Not Working Sir

This doesnt help us both! WHY it is not working?

Edit: and more important question: WHAT is not working?

jamsyogendra commented 3 years ago
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif

#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG

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

#define WIFI_SSID         "Yogendra"    
#define WIFI_PASS         "Jamsariya"
#define APP_KEY           "167aa480-1d06-4c30-9c5e-61679e23e582"      // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        "c75f4d59-d113-4340-8b6e-5e55146e4a11-60f1b139-1df5-4be0-979e-40131ddf96f4"   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"

#define SWITCH_ID_1       "603c908cbd5c040a18256d2a"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_2       "603c90a99c7d260a60ba47ad"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define DIMSWITCH_ID      "603c90d5bd5c040a18256d38"    // Should look like "5dc1564130xxxxxxxxxxxxxx"

SinricProSwitch    &mySwitch1   = SinricPro[SWITCH_ID_1]; 
SinricProSwitch    &mySwitch2   = SinricPro[SWITCH_ID_2];
SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID];

#define BAUD_RATE         9600                // Change baudrate to your need

const int RELAY1_PIN = 16;
const int RELAY2_PIN = 5;

const int zcPin = 15;
const int powerLevel1 = 13;

bool onPowerState1(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY1_PIN, !state);
  return true; // request handled properly
}

bool onPowerState2(const String &deviceId, bool &state) {
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(RELAY2_PIN, !state);
  return true; // request handled properly
}

bool onPowerLevel(const String &deviceId, int &powerLevel) {
  int analogValue = map(powerLevel, 0, 100, 0, 1023); 
  Serial.print("Turn on device id: ");
  Serial.println(deviceId);
  digitalWrite(powerLevel1, analogValue);
  return true; 

}

void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting to %s", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }
  Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}

// setup function for SinricPro
void setupSinricPro() {
  mySwitch1.onPowerState(onPowerState1);
  mySwitch2.onPowerState(onPowerState2);
  myDimSwitch.onPowerLevel(onPowerLevel);

  // 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);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(BAUD_RATE);
  setupWiFi();
  setupSinricPro();
}

void loop() {
  // put your main code here, to run repeatedly:
  SinricPro.handle();
}
jamsyogendra commented 3 years ago

So this is my editing code

jamsyogendra commented 3 years ago

You should consider to use the correct device types (later) SinricPro have

* Light

* Fan

This allows you to voice control your devices correctly

About your latest code: You didn't take my advice again!

What you have posted:

#define SWITCH_ID_1       "6038b4123517a339659cf355"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_2       "6038b50753a7f9397bebd61c"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define DIMSWITCH_ID      "603b0d36bd5c040a18254494"    // Should look like "5dc1564130xxxxxxxxxxxxxx"

SinricProSwitch    &mySwitch1   = SinricPro["6038b4123517a339659cf355"]; 
SinricProSwitch    &mySwitch2   = SinricPro["6038b50753a7f9397bebd61c"];
SinricProDimSwitch &myDimSwitch = SinricPro["603b0d36bd5c040a18254494"];

What i suggested to do:

#define SWITCH_ID_1       "6038b4123517a339659cf355"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_ID_2       "6038b50753a7f9397bebd61c"    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define DIMSWITCH_ID      "603b0d36bd5c040a18254494"    // Should look like "5dc1564130xxxxxxxxxxxxxx"

SinricProSwitch    &mySwitch1   = SinricPro[SWITCH_ID_1]; 
SinricProSwitch    &mySwitch2   = SinricPro[SWITCH_ID_2];
SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID];

Not Working Sir

This doesnt help us both! WHY it is not working?

* Enable serial monitor as written before.

* See what happens

* post error messages!

* What do you see in portal?

Edit: and more important question: WHAT is not working?

* Does not connect?

* Voice commands are not working?

* Controlling relay does not work?

ok sir but how i try my best if my code will work i am sure 1 will bye 2 more device

but i want to control 4 channal relay module with 1 fan using ac dimmer

sivar2311 commented 3 years ago

Dimmer switch is not working

your code:

bool onPowerLevel(const String &deviceId, int &powerLevel) {
  int analogValue = map(powerLevel, 0, 100, 0, 1023); 
  Serial.print("Turn on device id: "); // makes no...
  Serial.println(deviceId);  // ...sense here
  digitalWrite(powerLevel1, analogValue); // wrong
  return true; 
}

Correct:

bool onPowerLevel(const String &deviceId, int &powerLevel) {
  int analogValue = map(powerLevel, 0, 100, 0, 1023); 
  Serial.printf("Device %s powerLevel: %d\r\n", deviceId.c_str(), powerLevel); // print deviceId and powerLevel
  analogWrite(powerLevel1, analogValue);
  return true; 
}

Other issues you have: It seems your device does not connect. Check serial monitor output! Make sure you use the latest Arduino ESP core and latest Websocket Version!

ok sir but how i try my best if my code will work i am sure 1 will bye 2 more device but i want to control 4 channal relay module with 1 fan using ac dimmer

Do this step by step... 1st: let us bring your current sketch working (2x relay, 1x dimmer)

jamsyogendra commented 3 years ago

Sir be thankful dimmer is working now But relay function not working still

When we click on alexa app on function relay not on and when be click off all devices goes unresponsive

jamsyogendra commented 3 years ago

On serial monitor all functions working properly

sivar2311 commented 3 years ago

Please post serial output and screenshots from portal

jamsyogendra commented 3 years ago

i just mailed your on sivar2311@gmail.com sivar2311@gmail.com;

jamsyogendra commented 3 years ago

Thank you, I will do that.

let's take a break sir, you are so hardworking, you are such an inspiration for all off us

about my project, i will update you soon

sivar2311 commented 3 years ago

Issue solved via e-mail.

jamsyogendra commented 3 years ago

Dear sir , we both try our best but thing are not happening ,,,,

I will get back to you with new code of my this project then I say anything

But right now I get solution

On Mon, Mar 1, 2021, 8:14 PM Boris Jäger notifications@github.com wrote:

Issue solved via e-mail.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/145#issuecomment-788002194, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALXAOEMGBBGELMT4WOSHK2TTBOR6PANCNFSM4YJY7T5A .

easyyu commented 2 years ago

Hi jamsyogendra Can you please at end post your fully working code here? Thank you

jamsyogendra commented 1 year ago

easyyu Rbdimmer lib is not compatible with sinric pro