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
227 stars 121 forks source link

Dimmer switch not working #363

Closed nikhilcuiet closed 5 months ago

nikhilcuiet commented 5 months ago

Dimmer switch not working, but test program works well. it connects and receive message from mobile app but bulb brightness not adjusting.

kakopappa commented 5 months ago

sounds like a hardware issue. Can you control the dimmer brightness without SinricPro?

nikhilcuiet commented 5 months ago

ys.

//Libraries
#include <RBDdimmer.h>//https://github.com/RobotDynOfficial/RBDDimmer

//Parameters
const int zeroCrossPin   = 4;
const int acdPin  = 2;
int MIN_POWER   = 0;
int MAX_POWER   = 100;
int POWER_STEP  = 5;

//Variables
int power   = 0;

//Objects
dimmerLamp acd(acdPin,zeroCrossPin);

void setup()
{
//Init Serial USB
Serial.begin(115200);
Serial.println(F("ESP8266 System"));
acd.begin(NORMAL_MODE, OFF);
}

void loop(){
 //testDimmer();
}

void testDimmer()
{
  for(power=MIN_POWER;power<=MAX_POWER;power+=POWER_STEP){
      acd.setPower(power); // setPower(0-100%);
          Serial.print("lampValue -> ");
          Serial.print(acd.getPower());
          Serial.println("%");
      delay(1000);
  }

  for(power=MAX_POWER;power>=MIN_POWER;power-=POWER_STEP){
      acd.setPower(power); // setPower(0-100%);
          Serial.print("lampValue -> ");
          Serial.print(acd.getPower());
          Serial.println("%");
      delay(1000);
  }
}

this works well

nikhilcuiet commented 5 months ago
//Libraries
#include <RBDdimmer.h>//https://github.com/RobotDynOfficial/RBDDimmer

//Parameters
const int zeroCrossPin   = 4;
const int acdPin  = 2;
int MIN_POWER   = 0;
int MAX_POWER   = 100;
int POWER_STEP  = 5;

//Variables
int power   = 0;

//Objects
dimmerLamp acd(acdPin,zeroCrossPin);

void setup()
{
//Init Serial USB
Serial.begin(115200);
Serial.println(F("ESP8266 System"));
acd.begin(NORMAL_MODE, ON);
}

void loop(){
 //testDimmer();
}

void testDimmer()
{
  for(power=MIN_POWER;power<=MAX_POWER;power+=POWER_STEP){
      acd.setPower(power); // setPower(0-100%);
          Serial.print("lampValue -> ");
          Serial.print(acd.getPower());
          Serial.println("%");
      delay(1000);
  }

  for(power=MAX_POWER;power>=MIN_POWER;power-=POWER_STEP){
      acd.setPower(power); // setPower(0-100%);
          Serial.print("lampValue -> ");
          Serial.print(acd.getPower());
          Serial.println("%");
      delay(1000);
  }
}
kakopappa commented 5 months ago

Example looks correct.

There’s a tutorial which shows how to use the robodyn dimmer here.

https://help.sinric.pro/pages/tutorials/dimmable-switch/robotdyn-ac-light-dimmer

On Tue, 16 Jan 2024 at 8:30 AM nikhilcuiet @.***> wrote:

//Libraries

include //https://github.com/RobotDynOfficial/RBDDimmer

//Parameters const int zeroCrossPin = 4; const int acdPin = 2; int MIN_POWER = 0; int MAX_POWER = 100; int POWER_STEP = 5;

//Variables int power = 0;

//Objects dimmerLamp acd(acdPin,zeroCrossPin);

void setup() { //Init Serial USB Serial.begin(115200); Serial.println(F("ESP8266 System")); acd.begin(NORMAL_MODE, ON); }

void loop(){ //testDimmer(); }

void testDimmer() { for(power=MIN_POWER;power<=MAX_POWER;power+=POWER_STEP){ acd.setPower(power); // setPower(0-100%); Serial.print("lampValue -> "); Serial.print(acd.getPower()); Serial.println("%"); delay(1000); }

for(power=MAX_POWER;power>=MIN_POWER;power-=POWER_STEP){ acd.setPower(power); // setPower(0-100%); Serial.print("lampValue -> "); Serial.print(acd.getPower()); Serial.println("%"); delay(1000); } }

— Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/363#issuecomment-1892942214, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZSAQPD3CK2PDYJHKXDYOXJ2LAVCNFSM6AAAAABB4AAEOCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOJSHE2DEMRRGQ . You are receiving this because you commented.Message ID: @.***>

nikhilcuiet commented 5 months ago

I already gone through that but unable to find error. Kindy find error in code.

kakopappa commented 5 months ago

Where’s the code?

nikhilcuiet commented 5 months ago
//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>
#if defined(ESP8266)
  #include <ESP8266WiFi.h>
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
  #include <WiFi.h>
#endif

#include <RBDdimmer.h> //https://github.com/RobotDynOfficial/RBDDimmer

#include "SinricPro.h"
#include "SinricProDimSwitch.h"

#define WIFI_SSID         ""    
#define WIFI_PASS         ""
#define APP_KEY           ""   // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        ""   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define DIMSWITCH_ID      ""   // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE         115200                // Change baudrate to your need

const int ZC_PIN  = 4;
const int PWM_PIN  = 2;
const int MIN_POWER  = 0;
const int MAX_POWER  = 80;
int power  = 0;

dimmerLamp acd(PWM_PIN, ZC_PIN);

// we use a struct to store all states and values for our dimmable switch
struct {
  bool powerState = false;
  int powerLevel = 0;
} device_state;

void setPWM(int powerLevel) {
   int power = map(powerLevel, 0, 100, MIN_POWER, MAX_POWER); // Map power level from 0 to 100, to a value between 0 to 80
   acd.setPower(power);     
   delay(30);
} 

bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("Device %s power turned %s \r\n", deviceId.c_str(), state?"on":"off");
  device_state.powerState = state;  
  setPWM(state ? 100: 0);
  return true; // request handled properly
}

bool onPowerLevel(const String &deviceId, int &powerLevel) {
  device_state.powerLevel = powerLevel; 
  setPWM(powerLevel);
  Serial.printf("Device %s power level changed to %d\r\n", deviceId.c_str(), device_state.powerLevel);
  return true;
}

bool onAdjustPowerLevel(const String &deviceId, int &levelDelta) {
  device_state.powerLevel += levelDelta;
  Serial.printf("Device %s power level changed about %i to %d\r\n", deviceId.c_str(), levelDelta, device_state.powerLevel);
  levelDelta = device_state.powerLevel;
  setPWM(levelDelta);
  return true;
}

void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting");

  #if defined(ESP8266)
    WiFi.setSleepMode(WIFI_NONE_SLEEP); 
    WiFi.setAutoReconnect(true);
  #elif defined(ESP32)
    WiFi.setSleep(false); 
    WiFi.setAutoReconnect(true);
  #endif

  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() {
  SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID];

  // set callback function to device
  myDimSwitch.onPowerState(onPowerState);
  myDimSwitch.onPowerLevel(onPowerLevel);
  myDimSwitch.onAdjustPowerLevel(onAdjustPowerLevel);

  // 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 setupRBDdimmer(){
   acd.begin(NORMAL_MODE, ON);
} 

// main setup function
void setup() {
  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
  setupRBDdimmer();
  setupWiFi();
  setupSinricPro();
}

void loop() {
  SinricPro.handle();
}
nikhilcuiet commented 5 months ago

this example not working. Mobile app working with device. Messages are receiving but no action.

nikhilcuiet commented 5 months ago

Kindly provide a solution

kakopappa commented 5 months ago

If you are using ESP8266 you need to use a different pin for PWM. PIN 2 does not support PWM. Tutorial is using ESP32 so PIN 2 works for it.

image

nikhilcuiet commented 5 months ago

I am aslo using ep32 pin 4 and 2. the sample program works fine but complete program not working.

sivar2311 commented 5 months ago
void loop(){
 //testDimmer();
}

Is this just a typo? Otherwise your working sketch would not work either.

nikhilcuiet commented 5 months ago
//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>
#if defined(ESP8266)
  #include <ESP8266WiFi.h>
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
  #include <WiFi.h>
#endif

#include <RBDdimmer.h> //https://github.com/RobotDynOfficial/RBDDimmer

#include "SinricPro.h"
#include "SinricProDimSwitch.h"

#define WIFI_SSID         ""    
#define WIFI_PASS         ""
#define APP_KEY           ""   // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        ""   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define DIMSWITCH_ID      ""   // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE         115200                // Change baudrate to your need

const int ZC_PIN  = 4;
const int PWM_PIN  = 2;
const int MIN_POWER  = 0;
const int MAX_POWER  = 80;
int power  = 0;

dimmerLamp acd(PWM_PIN, ZC_PIN);

// we use a struct to store all states and values for our dimmable switch
struct {
  bool powerState = false;
  int powerLevel = 0;
} device_state;

void setPWM(int powerLevel) {
   int power = map(powerLevel, 0, 100, MIN_POWER, MAX_POWER); // Map power level from 0 to 100, to a value between 0 to 80
   acd.setPower(power);     
   delay(30);
} 

bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("Device %s power turned %s \r\n", deviceId.c_str(), state?"on":"off");
  device_state.powerState = state;  
  setPWM(state ? 100: 0);
  return true; // request handled properly
}

bool onPowerLevel(const String &deviceId, int &powerLevel) {
  device_state.powerLevel = powerLevel; 
  setPWM(powerLevel);
  Serial.printf("Device %s power level changed to %d\r\n", deviceId.c_str(), device_state.powerLevel);
  return true;
}

bool onAdjustPowerLevel(const String &deviceId, int &levelDelta) {
  device_state.powerLevel += levelDelta;
  Serial.printf("Device %s power level changed about %i to %d\r\n", deviceId.c_str(), levelDelta, device_state.powerLevel);
  levelDelta = device_state.powerLevel;
  setPWM(levelDelta);
  return true;
}

void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting");

  #if defined(ESP8266)
    WiFi.setSleepMode(WIFI_NONE_SLEEP); 
    WiFi.setAutoReconnect(true);
  #elif defined(ESP32)
    WiFi.setSleep(false); 
    WiFi.setAutoReconnect(true);
  #endif

  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() {
  SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID];

  // set callback function to device
  myDimSwitch.onPowerState(onPowerState);
  myDimSwitch.onPowerLevel(onPowerLevel);
  myDimSwitch.onAdjustPowerLevel(onAdjustPowerLevel);

  // 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 setupRBDdimmer(){
   acd.begin(NORMAL_MODE, ON);
} 

// main setup function
void setup() {
  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
  setupRBDdimmer();
  setupWiFi();
  setupSinricPro();
}

void loop() {
  SinricPro.handle();
}
nikhilcuiet commented 5 months ago

this is the code not that

sivar2311 commented 5 months ago

this is the code not that

I meant the working sketch you mentioned before.

Note: Please use code-blocks when you post code. Otherwise your code is very hard to read. To do this, enclose the source code with three backticks ``` before and after the code:

your code here

nikhilcuiet commented 5 months ago

I found the issue pin 2 not supported for PWM. Please not publish this kind mistake in a tutorial.

nikhilcuiet commented 5 months ago

My send issue is that this code not ruining in ESP8266. After programming ESP reset automatically and continuously. pins also I have tried different.

sivar2311 commented 5 months ago

ESP32: All pins that can act as outputs can be used as PWM pins (GPIOs 34 to 39 can’t generate PWM). Check https://randomnerdtutorials.com/esp32-pinout-reference-gpios/

But keep in mind that gpio 2 is connected to on-board LED, must be left floating or LOW to enter flashing mode

ESP8266 Note the comment from @kakopappa as he wrote: If you are using ESP8266 you need to use a different pin for PWM. PIN 2 does not support PWM. Tutorial is using ESP32 so PIN 2 works for it.