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

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[
1 Like