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

5050 led Strip Sinric Pro with Google Home onPowerState not responding and onColour strangely white #362

Closed Max-5210 closed 5 months ago

Max-5210 commented 5 months ago

Hello, I don't know if its just me but my sinric pro with esp32c3 xiao rarely responds to on and off commands from portal.sinric and google home, and for some reason the colour for the RGB 5050 led strips, when they display RGB colour, the colour is really white and almost not visible, and I have changed the colour temperature settings but it did nothing, and this only happens when I change the colour in the google home. But strangely if i use portal.sinric.pro and sometimes google home voice commands they display the colour i want. Is this a hardware or software problem because im using three IRF540N fets with a 10k pulldown resistor for each fet, and a 150ohm resistor from the esp32c3's output pins to the gates of the fets. Below is a schematic of my wiring. Any help would be appreaciated.

Schematic

//#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS 
#define NDEBUG 
#endif

#include <Arduino.h>
#include <WiFi.h>
#include <SinricPro.h>
#include <SinricProLight.h>
#include <map>

#define WIFI_SSID "xxxxxxxx"
#define WIFI_PASS "xxxxxxxx"
#define APP_KEY "e2a6e6ec-ccaexxxxxxxxxxxxxx"                                          
#define APP_SECRET "798f1f27-9c6f-47c7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
#define LIGHT_ID "65a1067fcccxxxxxxxxxx"                                              
#define BAUD_RATE 115200                                                        

#define BLUE_PIN 9    // PIN for BLUE Mosfet  - change this to your need (D5 = GPIO14 on ESP8266)
#define RED_PIN 8     // PIN for RED Mosfet   - change this to your need (D6 = GPIO12 on ESP8266)
#define GREEN_PIN 10  // PIN for GREEN Mosfet - change this to your need (D7 = GPIO13 on ESP8266)

struct Color {
  uint8_t r;
  uint8_t g;
  uint8_t b;
};

//// Colortemperature lookup table
using ColorTemperatures = std::map<uint16_t, Color>;
ColorTemperatures colorTemperatures{
  //  //  {Temperature value, {color r, g, b}}
  { 2000, { 255, 138, 18 } },
  { 2200, { 255, 147, 44 } },
  { 2700, { 255, 169, 87 } },
  { 3000, { 255, 180, 107 } },
  { 4000, { 255, 209, 163 } },
  { 5000, { 255, 228, 206 } },
  { 5500, { 255, 236, 224 } },
  { 6000, { 255, 243, 239 } },
  { 6500, { 255, 249, 253 } },
  { 7000, { 245, 243, 255 } },
  { 7500, { 235, 238, 255 } },
  { 9000, { 255, 225, 255 } }
};

struct DeviceState {                      // Stores current device state with following initial values:
  bool powerState = false;                // initial state is off
  Color color = colorTemperatures[3000];  // color is set to white (9000k)
  int colorTemperature = 3000;            // color temperature is set to 9000k
  int brightness = 255;                   // brightness is set to 100
} device_state;

SinricProLight& myLight = SinricPro[LIGHT_ID];  // SinricProLight device

void setStripe() {
  int rValue = map(device_state.color.r * device_state.brightness, 0, 255 * 100, 0, 1023);  // calculate red value and map between 0 and 1023 for analogWrite
  int gValue = map(device_state.color.g * device_state.brightness, 0, 255 * 100, 0, 1023);  // calculate green value and map between 0 and 1023 for analogWrite
  int bValue = map(device_state.color.b * device_state.brightness, 0, 255 * 100, 0, 1023);  // calculate blue value and map between 0 and 1023 for analogWrite

  if (device_state.powerState == false) {  // turn off?
    digitalWrite(RED_PIN, LOW);            // set
    digitalWrite(GREEN_PIN, LOW);          // mosfets
    digitalWrite(BLUE_PIN, LOW);           // low
  } else{
    analogWrite(RED_PIN, rValue);    // write red value to pin
    analogWrite(GREEN_PIN, gValue);  // write green value to pin
    analogWrite(BLUE_PIN, bValue);   // write blue value to pin
  }
}

bool onPowerState(const String& deviceId, bool& state) {
  device_state.powerState = state;  // store the new power state
  setStripe();                      // update the mosfets
  return true;
}

bool onBrightness(const String& deviceId, int& brightness) {
  device_state.brightness = brightness;  // store new brightness level
  setStripe();                           // update the mosfets
  return true;
}

bool onAdjustBrightness(const String& deviceId, int& brightnessDelta) {
  device_state.brightness += brightnessDelta;  // calculate and store new absolute brightness
  brightnessDelta = device_state.brightness;   // return absolute brightness
  setStripe();                                 // update the mosfets
  return true;
}

bool onColor(const String& deviceId, byte& r, byte& g, byte& b) {
  device_state.color.r = r;  // store new red value
  device_state.color.g = g;  // store new green value
  device_state.color.b = b;  // store new blue value
  setStripe();               // update the mosfets
  return true;
}

bool onColorTemperature(const String& deviceId, int& colorTemperature) {
  device_state.color = colorTemperatures[colorTemperature];  // set rgb values from corresponding colortemperauture
  device_state.colorTemperature = colorTemperature;          // store the current color temperature
  setStripe();                                               // update the mosfets
  return true;
}

bool onIncreaseColorTemperature(const String& devceId, int& colorTemperature) {
  auto current = colorTemperatures.find(device_state.colorTemperature);  // get current entry from colorTemperature map
  auto next = std::next(current);                                        // get next element
  if (next == colorTemperatures.end()) next = current;                   // prevent past last element
  device_state.color = next->second;                                     // set color
  device_state.colorTemperature = next->first;                           // set colorTemperature
  colorTemperature = device_state.colorTemperature;                      // return new colorTemperature
  setStripe();
  return true;
}

bool onDecreaseColorTemperature(const String& devceId, int& colorTemperature) {
  auto current = colorTemperatures.find(device_state.colorTemperature);  // get current entry from colorTemperature map
  auto next = std::prev(current);                                        // get previous element
  if (next == colorTemperatures.end()) next = current;                   // prevent before first element
  device_state.color = next->second;                                     // set color
  device_state.colorTemperature = next->first;                           // set colorTemperature
  colorTemperature = device_state.colorTemperature;                      // return new colorTemperature
  setStripe();
  return true;
}

void setupWiFi() {
  Serial.printf("WiFi: connecting");
  WiFi.setSleep(false);
  WiFi.setAutoReconnect(true);

  WiFi.begin(WIFI_SSID, WIFI_PASS);

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

void setupSinricPro() {
  myLight.onPowerState(onPowerState);                              // assign onPowerState callback
  myLight.onBrightness(onBrightness);                              // assign onBrightness callback
  myLight.onAdjustBrightness(onAdjustBrightness);                  // assign onAdjustBrightness callback
  myLight.onColor(onColor);                                        // assign onColor callback
  myLight.onColorTemperature(onColorTemperature);                  // assign onColorTemperature callback
  myLight.onDecreaseColorTemperature(onDecreaseColorTemperature);  // assign onDecreaseColorTemperature callback
  myLight.onIncreaseColorTemperature(onIncreaseColorTemperature);  // assign onIncreaseColorTemperature callback

  SinricPro.begin(APP_KEY, APP_SECRET);  // start SinricPro
}

void setup() {
  Serial.begin(BAUD_RATE);  // setup serial

  pinMode(RED_PIN, OUTPUT);    // set red-mosfet pin as output
  pinMode(GREEN_PIN, OUTPUT);  // set green-mosfet pin as output
  pinMode(BLUE_PIN, OUTPUT);   // set blue-mosfet pin as output

  setupWiFi();       // connect wifi
  setupSinricPro();  // setup SinricPro
}

void loop() {
  SinricPro.handle();  // handle SinricPro communication
}
sivar2311 commented 5 months ago

The software side looks okay.

if (device_state.powerState == false) { // turn off?
    digitalWrite(RED_PIN, LOW); // set
    digitalWrite(GREEN_PIN, LOW); // mosfets
    digitalWrite(BLUE_PIN, LOW); // low

I think it's the hardware (using the mosfet and the voltage divider). But I am not an expert when it comes to hardware.

kakopappa commented 5 months ago

Does it work correctly without sinricpro? Have you measured the voltage when it’s meant to be turned off

Max-5210 commented 5 months ago

Hello, Thank you for your quick response, I've accidentally made a wiring error and changed the gate wiring to this, and now the colours work with google home and they aren't that white any more. Im wondering if this is normal that when i change the brightness of the light strip, the colour changes too. For example when I set the lights to red in google home, the lights at full brightness are purple, and when they are at half brightness they are a greenish white and they only show the real colour at around 10% brightness. Also in Sinric Pro when I set the colour, the lights match perfectly but in google home they don't. Is this a google home issue? The on button works but the off button doesn't work, and I've probed across all RGB and when I press on in google home, it is 12v but when I turn it off it is still 12V, is this my FET's gate resistor problem? Any help would be appreciated.

image