vintlabs / fauxmoESP

Add voice control of your ESP32 and ESP8266 devices using Amazon Alexa
MIT License
383 stars 69 forks source link

GPIO issue #106

Closed pvint closed 3 years ago

pvint commented 4 years ago

Original report by Lmm Cams (Bitbucket: [Lmm Cams](https://bitbucket.org/Lmm Cams), ).


Hi, im having trouble switching the state of gpio outputs on esp8266 nodemcu using arduino ide.

Any switching of state to HIGH or LOW simply fails or is ignored after fauxmo.onSetState([] …..

The sketch is supposed to trigger a relay that trigers the pushing of a remote control btn for 1second.

Here is the code -

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"

#define WIFI_SSID "lemainemachon"
#define WIFI_PASS "jibajaba"
#define REMOTE_BTN_1 4 //up (D2)
#define REMOTE_BTN_2 14//down (D5)
#define REMOTE_BTN_3 12  //select (D6)
#define REMOTE_BTN_4 5//stop (D1)
#define UP "blinds Up"
#define Down "blinds Down"
#define Light_Block "Blinds light Block"

fauxmoESP fauxmo;

void setup() {
  // Init serial port and clean garbage
  Serial.begin(115200);
  Serial.println();

  // Wi-Fi connection
  wifiSetup();

  // Set Buttons
  pinMode(REMOTE_BTN_1, OUTPUT);          //up
  digitalWrite(REMOTE_BTN_1, HIGH);
  pinMode(REMOTE_BTN_2, OUTPUT);          //down
  digitalWrite(REMOTE_BTN_2, HIGH);
  pinMode(REMOTE_BTN_3, OUTPUT);          //select
  digitalWrite(REMOTE_BTN_3, HIGH);
  pinMode(REMOTE_BTN_4, OUTPUT);          //stop
  digitalWrite(REMOTE_BTN_4, HIGH);

  fauxmo.createServer(true); // not needed, this is the default value
  fauxmo.setPort(80); // This is required for gen3 devices
  fauxmo.enable(true);

  fauxmo.addDevice(UP);
  fauxmo.addDevice(Down);
  fauxmo.addDevice(Light_Block);

  fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {                          //command recieved

    // Callback when a command from Alexa is received. 
    Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);

    if ( (strcmp(device_name, UP) == 0) ) { 
      Serial.println("BtnUP 1 called by Alexa");//blinds up
      digitalWrite(REMOTE_BTN_1, LOW);
      delay(1000);//1 second
      Serial.println("low");
      digitalWrite(REMOTE_BTN_1, HIGH);
      Serial.println("high");    
    }

    if ( (strcmp(device_name, Down) == 0) ) {              
      Serial.println("BtnDown 2 called by Alexa");
      digitalWrite(REMOTE_BTN_2, LOW);
      delay(1000);//1 second
      Serial.println("low");
      digitalWrite(REMOTE_BTN_2, HIGH);
      Serial.println("high");
    }
  });
}

void loop() {
  fauxmo.handle();
}

// Wi-Fi Connection
void wifiSetup() {
  WiFi.mode(WIFI_STA);
  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
    Serial.print("attempting to connect to wifi");
  while (WiFi.status() != WL_CONNECTED) {    
    Serial.print(".");
    delay(100);
  }
  Serial.println();
  Serial.printf("Connected to - SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}

pvint commented 3 years ago

I believe the problem is using delay(1000) in fauxmo.onSetState. You should try to have the code in onSetState complete as quickly as possible.

I would suggest that instead of toggling the LED directly from onSetState set a flag and handle it in your main loop. Also, avoid delay() there as well, as it can block other threads (See https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay for another method without using delay())