kaivalya Ahir
kaivalya Ahir

Reputation: 17

I want to Emulate my nodemcu with 8 switch relay with fauxmo how can i do that?

So i viewed a program and used it. I want to Emulate my nodemcu with 8 switch relay with fauxmo how can i do that? that code worked with 2 relays but if i tried with 8 relays alexa didn't find any devices. My code first worked when i used it for only 2 relay but didn't work when i changed it to this code given below.

Tell me how can i fix it. My code is written below.

#include <Arduino.h>
#ifdef ESP32
  #include <WiFi.h>
  #define RELAY_PIN_1 5
  #define RELAY_PIN_2 4
  #define RELAY_PIN_3 0
  #define RELAY_PIN_4 2
  #define RELAY_PIN_5 14
  #define RELAY_PIN_6 12
  #define RELAY_PIN_7 13
  #define RELAY_PIN_8 15
#else
  #include <ESP8266WiFi.h>
  //#define RF_RECEIVER 5
  #define RELAY_PIN_1 5
  #define RELAY_PIN_2 4
  #define RELAY_PIN_3 0
  #define RELAY_PIN_4 2
  #define RELAY_PIN_5 14
  #define RELAY_PIN_6 12
  #define RELAY_PIN_7 13
  #define RELAY_PIN_8 15
#endif
#include "fauxmoESP.h"
#define SERIAL_BAUDRATE 115200

#define WIFI_SSID "Deco M5"
#define WIFI_PASS "123123123"
#define app1 "Light 1"
#define app2 "Light 2"
#define app3 "Light 3"
#define app4 "Light 4"
#define app5 "Light 5"
#define app6 "Light 6"
#define app7 "Light 7"
#define app8 "Light 8"
fauxmoESP fauxmo;

void wifiSetup() {
  // Set WIFI module to STA mode
  WiFi.mode(WIFI_STA);
  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }
  Serial.println();
  Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}

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

  // Wi-Fi connection
  wifiSetup();

  pinMode(RELAY_PIN_1, OUTPUT);
  digitalWrite(RELAY_PIN_1, HIGH);

  pinMode(RELAY_PIN_2, OUTPUT);
  digitalWrite(RELAY_PIN_2, HIGH);

  pinMode(RELAY_PIN_3, OUTPUT);
  digitalWrite(RELAY_PIN_3, HIGH);

  pinMode(RELAY_PIN_4, OUTPUT);
  digitalWrite(RELAY_PIN_4, HIGH);

  pinMode(RELAY_PIN_5, OUTPUT);
  digitalWrite(RELAY_PIN_5, HIGH);

  pinMode(RELAY_PIN_6, OUTPUT);
  digitalWrite(RELAY_PIN_6, HIGH);

  pinMode(RELAY_PIN_7, OUTPUT);
  digitalWrite(RELAY_PIN_7, HIGH);

  pinMode(RELAY_PIN_8, OUTPUT);
  digitalWrite(RELAY_PIN_8, HIGH);


  fauxmo.createServer(true); 
  fauxmo.setPort(80); 
  fauxmo.enable(true);
  fauxmo.addDevice(app1);
  fauxmo.addDevice(app2);
  fauxmo.addDevice(app3);
  fauxmo.addDevice(app4);
  fauxmo.addDevice(app5);
  fauxmo.addDevice(app6);
  fauxmo.addDevice(app7);
  fauxmo.addDevice(app8);


  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, app1) == 0) ) {
      Serial.println("RELAY 1 switched by Alexa");
      if (state) {
        digitalWrite(RELAY_PIN_1, LOW);
      } else {
        digitalWrite(RELAY_PIN_1, HIGH);
      }
    }
    if ( (strcmp(device_name, app2) == 0) ) {
      Serial.println("RELAY 2 switched by Alexa");
      if (state) {
        digitalWrite(RELAY_PIN_2, LOW);
      } else {
        digitalWrite(RELAY_PIN_2, HIGH);
      }
    }
    if ( (strcmp(device_name, app1) == 0) ) {
      Serial.println("RELAY 3 switched by Alexa");
      if (state) {
        digitalWrite(RELAY_PIN_3, LOW);
      } else {
        digitalWrite(RELAY_PIN_3, HIGH);
      }
    }
    if ( (strcmp(device_name, app1) == 0) ) {
      Serial.println("RELAY 4 switched by Alexa");
      if (state) {
        digitalWrite(RELAY_PIN_4, LOW);
      } else {
        digitalWrite(RELAY_PIN_4, HIGH);
      }
    }
    if ( (strcmp(device_name, app1) == 0) ) {
      Serial.println("RELAY 5 switched by Alexa");
      if (state) {
        digitalWrite(RELAY_PIN_5, LOW);
      } else {
        digitalWrite(RELAY_PIN_5, HIGH);
      }
    }
    if ( (strcmp(device_name, app1) == 0) ) {
      Serial.println("RELAY 6 switched by Alexa");
      if (state) {
        digitalWrite(RELAY_PIN_6, LOW);
      } else {
        digitalWrite(RELAY_PIN_6, HIGH);
      }
    }
    if ( (strcmp(device_name, app1) == 0) ) {
      Serial.println("RELAY 7 switched by Alexa");
      if (state) {
        digitalWrite(RELAY_PIN_7, LOW);
      } else {
        digitalWrite(RELAY_PIN_7, HIGH);
      }
    }
    if ( (strcmp(device_name, app1) == 0) ) {
      Serial.println("RELAY 8 switched by Alexa");
      if (state) {
        digitalWrite(RELAY_PIN_8, LOW);
      } else {
        digitalWrite(RELAY_PIN_8, HIGH);
      }
    }
  });

}

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());
  }
}

Upvotes: 0

Views: 338

Answers (1)

Cakes
Cakes

Reputation: 108

You didn't change "(strcmp(device_name, app1)" for the 6 added cases, It should be (strcmp(device_name, app3), (strcmp(device_name, app4),etc...

Upvotes: 0

Related Questions