How to program Node MCU pin to turn on for a sec then off

Hello,

Experience: I have no programming experience :slight_smile: I also did not want to pay $100 of the Bond device as I though this would be a fun project since I’m stuck at home.

Project Background:

Connecting my fan remote to Alexa to enable turning the fan light on/off ( dimming and fan speed TBD)

I hacked into my fan remote and connected the ESP8266 via PIN 2 to the contact button on the remote and programmed the ESP to connect with Alexa. At first I programmed it using Cayenne and was able to get the light to turn on/off with a delay(100). But I was having troubles figuring out how to connect Cayenne to Alexa so I changed it up to fauxmoESP & ESPAsyncTCP.

It now works with Alexa pretty well as I can tell Alexa to turn fan on/off and the voltage from pin 2 will turn on to ~3.4V and turn off at ~.04V. Problem is I only need pin 2 to turn on for a fraction of a second to enable to contact button on the remote. If pin 2 voltage is constant when I turn the fan on, like it is now, it will start dimming and brightening the lights.

I don’t think it really matters if I tell Alexa on or off since on/off needs to do the same thing ( supply current for a second then stop)

So the results I get from the monitor are:

WiFi Connected…IP Address:
xxx.xxx.x.xxx
Adding LED device
[MAIN] Device #0 (LED) state: ON value: 255
[MAIN] Device #0 (LED) state: OFF value: 255

The code I’m using is:

#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <fauxmoESP.h>

#define WIFI_SSID “xxxx”
#define WIFI_PASS “xxxxxx”

fauxmoESP fauxmo;

#define LED_PINK 2
#define ID_PINK “LED”

void setup() {
Serial.begin(115200);
if (connectWifi()) {
// Setup fauxmo
Serial.println(“Adding LED device”);
pinMode(LED_PINK, OUTPUT);
digitalWrite(LED_PINK, LOW);

fauxmo.setPort(80);
fauxmo.enable(true);
fauxmo.addDevice(ID_PINK);

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);
if (strcmp(device_name, ID_PINK)==0) {
digitalWrite(LED_PINK, state ? HIGH : LOW);

}
});
}
}
void loop() {
fauxmo.handle();
static unsigned long last = millis();
if (millis() - last > 5000) {
last = millis();
Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
}

}
boolean connectWifi() {
// Let us connect to WiFi
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("…");
Serial.println(“WiFi Connected…IP Address:”);
Serial.println(WiFi.localIP());
return true;
}

And the circuit diagram - just in case anyone would like to improve it :).

Thanks!

You have to change your onSetState() function definition to always turn pin 2 on, wait a bit and then turn it off rather than having it toggle the pin

Also, please use code tags when posting your code so it looks like this:

#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <fauxmoESP.h>

#define WIFI_SSID "xxxx"
#define WIFI_PASS "xxxxxx"

fauxmoESP fauxmo;

#define LED_PINK 2
#define ID_PINK "LED"


void setup() {
  Serial.begin(115200);
  if (connectWifi()) {
    // Setup fauxmo
    Serial.println("Adding LED device");
    pinMode(LED_PINK, OUTPUT);
    digitalWrite(LED_PINK, LOW);

    fauxmo.setPort(80);
    fauxmo.enable(true);
    fauxmo.addDevice(ID_PINK);


    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);
      if (strcmp(device_name, ID_PINK) == 0) {
        digitalWrite(LED_PINK, HIGH);
        delay(100);  // 100 msec
        digitalWrite(LED_PINK, LOW);
      }
    });
  }
}
void loop() {
  fauxmo.handle();
  static unsigned long last = millis();
  if (millis() - last > 5000) {
    last = millis();
    Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
  }


}
boolean connectWifi() {
  // Let us connect to WiFi
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(".......");
  Serial.println("WiFi Connected....IP Address:");
  Serial.println(WiFi.localIP());
  return true;
}
[/code[