Forum: Mikrocontroller und Digitale Elektronik ESP8266 Sinric Pro Problem


von Dominik (Gast)


Lesenswert?

Hallo,
ich habe ein Problem bei der Steuerung einer LED-Leiste mit einem 
NodeMCU ESP8266 über Sinric Pro. Wenn ich die LED-Leiste über Alexa an 
und ausschalte funktioniert alles einwandfrei. wenn ich sie allerdings 
auf eine bestimmte helligkeit stellen möchte wird die LED-Leiste kurz 
danach wieder ausgeschaltet da D8 also tast (im Code) anscheinend als 
HIGH erkannt wird. Ich verstehe nicht wie das passieren kann. Vorallem 
nur wenn ich eine bestimmte helligkeit einstelle. aus diesem Grund 
gleube ich auch nicht das es an der hardware liegt.
 Hier der Code:








//#define ENABLE_DEBUG
#define tast D8
#define LED D7
#define SWITCH_PIN1 D6
/*#ifdef ENABLE_DEBUG
       #define DEBUG_ESP_PORT Serial
       #define NODEBUG_WEBSOCKETS
       #define NDEBUG
#endif */
#include "fauxmoESP.h"
fauxmoESP fauxmo;
#include <Arduino.h>
#include <ESP8266WiFi.h>


int status=0;
#include "SinricPro.h"
#include "SinricProLight.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-333d65xxxx 
xx"
#define LIGHT_ID         ++++    // Should look like 
"5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE         9600                // Change baudrate to your 
need

// define array of supported color temperatures
int colorTemperatureArray[] = {2200, 2700, 4000, 5500, 7000};
int max_color_temperatures = sizeof(colorTemperatureArray) / 
sizeof(colorTemperatureArray[0]); // calculates how many elements are 
stored in colorTemperature array

// a map used to convert a given color temperature into color 
temperature index (used for colorTemperatureArray)
std::map<int, int> colorTemperatureIndex;

// initializes the map above with color temperatures and index values
// so that the map can be used to do a reverse search like
// int index = colorTemperateIndex[4000]; <- will result in index == 2
void setupColorTemperatureIndex() {
  Serial.printf("Setup color temperature lookup table\r\n");
  for (int i=0;i < max_color_temperatures; i++) {
    colorTemperatureIndex[colorTemperatureArray[i]] = i;
    Serial.printf("colorTemperatureIndex[%i] = %i\r\n", 
colorTemperatureArray[i], 
colorTemperatureIndex[colorTemperatureArray[i]]);
  }
}

// we use a struct to store all states and values for our light
struct {
  bool powerState = false;
  int brightness = 0;
  struct {
    byte r = 0;
    byte g = 0;
    byte b = 0;
  } color;
  int colorTemperature = colorTemperatureArray[0]; // set 
colorTemperature to first element in colorTemperatureArray array
} device_state;

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

}

bool onBrightness(const String &deviceId, int &brightness) {
  float helligkeit = brightness * 10.23;
  status=1;
  analogWrite(LED, round(helligkeit));
  return true;
}

bool onAdjustBrightness(const String &deviceId, int brightnessDelta) {

  return true;
}

bool onColor(const String &deviceId, byte &r, byte &g, byte &b) {
  device_state.color.r = r;
  device_state.color.g = g;
  device_state.color.b = b;
  Serial.printf("Device %s color changed to %d, %d, %d (RGB)\r\n", 
deviceId.c_str(), device_state.color.r, device_state.color.g, 
device_state.color.b);
  return true;
}

bool onColorTemperature(const String &deviceId, int &colorTemperature) {
  device_state.colorTemperature = colorTemperature;
  Serial.printf("Device %s color temperature changed to %d\r\n", 
deviceId.c_str(), device_state.colorTemperature);
  return true;
}

bool onIncreaseColorTemperature(const String &deviceId, int 
&colorTemperature) {
  int index = colorTemperatureIndex[device_state.colorTemperature]; 
// get index of stored colorTemperature
  index++; 
// do the increase
  if (index < 0) index = 0; 
// make sure that index stays within array boundaries
  if (index > max_color_temperatures-1) index = 
max_color_temperatures-1; // make sure that index stays within array 
boundaries
  device_state.colorTemperature = colorTemperatureArray[index]; 
// get the color temperature value
  Serial.printf("Device %s increased color temperature to %d\r\n", 
deviceId.c_str(), device_state.colorTemperature);
  colorTemperature = device_state.colorTemperature; 
// return current color temperature value
  return true;
}

bool onDecreaseColorTemperature(const String &deviceId, int 
&colorTemperature) {
  int index = colorTemperatureIndex[device_state.colorTemperature]; 
// get index of stored colorTemperature
  index--; 
// do the decrease
  if (index < 0) index = 0; 
// make sure that index stays within array boundaries
  if (index > max_color_temperatures-1) index = 
max_color_temperatures-1; // make sure that index stays within array 
boundaries
  device_state.colorTemperature = colorTemperatureArray[index]; 
// get the color temperature value
  Serial.printf("Device %s decreased color temperature to %d\r\n", 
deviceId.c_str(), device_state.colorTemperature);
  colorTemperature = device_state.colorTemperature; 
// return current color temperature value
  return true;
}

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

void setupSinricPro() {
  // get a new Light device from SinricPro
  SinricProLight &myLight = SinricPro[LIGHT_ID];

  // set callback function to device
  myLight.onPowerState(onPowerState);
  myLight.onBrightness(onBrightness);
  myLight.onAdjustBrightness(onAdjustBrightness);
  myLight.onColor(onColor);
  myLight.onColorTemperature(onColorTemperature);
  myLight.onIncreaseColorTemperature(onIncreaseColorTemperature);
  myLight.onDecreaseColorTemperature(onDecreaseColorTemperature);

  // 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() {

  pinMode(tast, INPUT);
  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
  pinMode(13,OUTPUT);
  pinMode(12,OUTPUT);
   // Init serial port and clean garbage

    Serial.println();
    Serial.println();
    Serial.println("FauxMo Setup");
      // Fauxmo
  fauxmo.addDevice("Bürolicht1");
  fauxmo.setPort(80);
  fauxmo.enable(true);


   fauxmo.onSetState([](unsigned char device_id, const char * 
device_name, bool state, unsigned char value)
   {
      Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", 
device_id, device_name, state ? "ON" : "OFF", value);

      // 
------------------------------------------------------------------------ 
-----
      // Define queries
      // 
------------------------------------------------------------------------ 
-----

      if ( (strcmp(device_name, "Bürolicht1") == 0) ) {
        if (state) {
          digitalWrite(SWITCH_PIN1, LOW);
          Serial.printf("Bürolicht1 tet.");
        } else {
          digitalWrite(SWITCH_PIN1, HIGH);
          Serial.printf("Bürolicht1 wird offgeschaltet.");
        }
      }
      }

       );

  pinMode(LED, OUTPUT);

  setupColorTemperatureIndex(); // setup our helper map
  setupWiFi();
  setupSinricPro();
}

void loop() {
  SinricPro.handle();
  fauxmo.handle();
  if (digitalRead(tast)==HIGH) {}
  if (digitalRead(tast)==HIGH && status==1){analogWrite(LED, 0); 
Serial.println("Taster gedrückt");delay(1000); status=0; 
Serial.println("olaf wird ausgeschaltet.");}
  if (digitalRead(tast)==HIGH && status==0) {analogWrite(LED, 1023); 
Serial.println("Taster gedrückt");delay(1000); status=1; 
Serial.println("olaf wird eingeschaltet.");}



}[/c]

von Dominik (Gast)


Lesenswert?

Noch ein kleiner Beitrag wenn ich sie auf 100% stelle geht auch alles 
aber ab 99% geht sie sofort wieder aus.

von Stefan F. (Gast)


Lesenswert?

Du könntest das Problem durch Hinzufügen von noch mehr Debug Meldungen 
einkreisen.

von Dominik (Gast)


Lesenswert?

Stefan hab schon vieles ausprobiert, aber Danke.

Konnte das Problem jetzt beheben aber nicht verstehen.


Anscheinend hat der eingebaute PullDown Widerstand an D8 nicht gereicht. 
hab jetzt nochmal 1kOhm hinzugefügt und jetzt geht alles. Ich verstehe 
trotzdem nicht was da der unterschied zwischen 99% und 100% ist.

von Stefan F. (Gast)


Lesenswert?

Dominik schrieb:
> hab jetzt nochmal 1kOhm hinzugefügt und jetzt geht alles.

Wie viel Ohm hatte er denn vorher? Zeige mal deinen Schaltplan und ein 
Foto vom Aufbau, wo man die Leitungsführung (Leiterbahnen und Kabel) 
sehen kann.

von Joachim B. (jar)


Lesenswert?

Stefan ⛄ F. schrieb:
> Zeige mal deinen Schaltplan und ein
> Foto vom Aufbau
+1
liest sich wie fehlender Pegelwandler, mit 3,3V aus dem ESP sind WS oder 
APA  LED Stripes selten zufrieden!

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.