Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By GreenBlood
#85263 Ive clean the code up, exact same wiring as the above, but no gpio action! although alexa is doing her thing just fine.

I must be missing something.

Code: Select all#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("calling up() sub funct ");//blinds up
      up();     
    }
   
    if ( (strcmp(device_name, Down) == 0) ) {                                                                                         //blinds down
      Serial.println("calling down() sub funct ");//blinds down
      down();
    }
  });
}

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

 void up() {
  Serial.println("sub funct called");
      digitalWrite(REMOTE_BTN_1, LOW);
      delay(1000);//1 second
      Serial.println("low");
      digitalWrite(REMOTE_BTN_1, HIGH);
      Serial.println("high");
 }
 void down() {
      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");
 }
 
// 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());
}
User avatar
By btidey
#85265 Strange I can't see anything obvious at the moment.

Couple of things that may be worth trying.

Try leaving all the fauxmo stuff in but call up() and down() in main loop to see if those are working outside of the fauxmo call back.

Maybe include explicit pinMode in up and down in case somehow that is getting reset.
User avatar
By GreenBlood
#85266 Yes I tried that but no joy.
After some considerable research, it seem the issue is related to the fact you can’t have delays in a callback function, nor any function called by it.
So how to do that is the next challenge!
Steve
User avatar
By btidey
#85267 That make sense. So maybe do what one of the comments in the code said where the callback just sets a variable with the command to do and code in the loop actions it. This might best be done with a simple state machine. I.e callback sets to up, down or block then the loop calls a simple state machine where up sets relay on. It can be refined where setting a state also sets the time for next change. That way separate states can turn relay on, then turn it off without any delay or blocking code.