ESP8266 Sinric Pro Problem

Gast #6204883
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]
Gast #6204964
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.

Antwort schreiben

Hinweis: der ursprüngliche Beitrag ist mehr als 6 Monate alt. Bitte hier nur auf die ursprüngliche Frage antworten, für neue Fragen einen neuen Beitrag erstellen.

Formatierung
  • [c]C-Code[/c]
  • [code]Code in anderen Sprachen, ASCII-Zeichnungen[/code]
  • [math]Formel in LaTeX-Syntax[/math]
  • [[Titel]] – Link zu einem Artikel
  • Verweis auf einen anderen Beitrag: Rechtsklick auf dessen Beitrags-ID (#8078628), „Adresse kopieren“, in den Text einfügen

Mehr zur Formatierung

Längeren Sourcecode als Anhang, nicht in den Text. JPG nur für Fotos und Scans; Zeichnungen und Screenshots als PNG oder GIF. Siehe Bildformate.

Schon registriert? Anmelden

Wird nicht angezeigt. Wir schicken dir einen Link; sobald du damit bestätigst, erscheint dein Beitrag.

Mit dem Abschicken legst du einen kostenlosen Account an und erkennst die Nutzungsbedingungen und die Datenschutzerklärung an.