ESP8266 Alexa RF Bridge Code help please?

Hi,

I have made myself an Alexa RF Bridge using an ESP8266 so I can control my Christmas lights with 433mhz RF plugs. I have also incorporated a touch switch so I can manually switch the lights on and off if Alexa doesn't play ball.

I have written some code for this that controls all the RF plugs at the same time, this works as it should and acts as one device.

I have modified the code and separated the outside lights and inside lights on different RF signals so Alexa can control them independently, so it now acts as two devices. While this works fine with Alexa, the touch button is randomly not working and status LED's are out of sync (green off and red on etc). The ESP 8266 then resets. The only difference between the 2 codes below is that I have added more devices and 2 separate RF transmit codes for inside and outside. The ESP 8266 doesn't reset if I just use Alexa and do not touch the switch.

Below are the 2 pieces of code, the first being just 1 device and working fine and the second having the extra devices added and not working properly.

1 Device

#include <RCSwitch.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#include <mechButton.h>


#define WIFI_SSID "**********"
#define WIFI_PASS "**********"
#define Christmas_Lights "Christmas Lights"

const int GreenLED = 5;   //Connected to pin D1.
const int RedLED = 4;     //Connected to pin D2.
const int buttonPin = 14; //Connected to pin D5.

bool SwitchState;   // the current state of the output pin

mechButton  buttonPress(buttonPin);

fauxmoESP fauxmo;

RCSwitch mySwitch = RCSwitch();

// Wi-Fi Connection
void wifiSetup() {

  // Set WIFI module to STA mode
  WiFi.mode(WIFI_STA);
 

  // Connect
  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);


  // Wait
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    digitalWrite(GreenLED, LOW);
    digitalWrite(RedLED, HIGH);
    delay(200);
    digitalWrite(GreenLED, HIGH);
    digitalWrite(RedLED, LOW);
    delay(200);
     
  }

  Serial.println();

  // Connected!
  Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());

  digitalWrite(GreenLED, HIGH);
  digitalWrite(RedLED, HIGH);



}

void setup() {

  pinMode (GreenLED, OUTPUT);
  pinMode (RedLED, OUTPUT);

  buttonPress.setCallback(touchButton);

  // Setup 433mhz Transmitter.
  mySwitch.enableTransmit(16); // Transmitter DATA Pin is connected to Pin D0.
  mySwitch.setProtocol(1); // Set protocol.
  mySwitch.setPulseLength(165); // Set pulse length.
  mySwitch.setRepeatTransmit(100); // Set number of transmission repetitions.

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

  // Wi-Fi connection
  wifiSetup();

  fauxmo.createServer(true);

  fauxmo.setPort(80);

  fauxmo.enable(true);

  fauxmo.addDevice(Christmas_Lights);

  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, Christmas_Lights) == 0) ) {

      if (state) {
        Serial.println("Christmas Lights switched ON ...");
        digitalWrite(GreenLED, HIGH);
        digitalWrite(RedLED, LOW);
        mySwitch.send("000001000101010100110011");
      } 
      else 
      {
        Serial.println("Christmas Lights switched OFF ...");
        digitalWrite(RedLED, HIGH);
        digitalWrite(GreenLED, LOW);
        mySwitch.send("000001000101010100111100");
      }
    }
  });
}

void touchButton() {

  if (!buttonPress.trueFalse()) {

    //  Changes the status of the valve
    if (SwitchState) {
      Serial.println("Christmas Lights switched OFF ...");
      digitalWrite(RedLED, HIGH);
      digitalWrite(GreenLED, LOW);
      mySwitch.send("000001000101010100111100");
      SwitchState = false;
    }
    else
    {
      Serial.println("Christmas Lights switched ON ...");
      digitalWrite(GreenLED, HIGH);
      digitalWrite(RedLED, LOW);
      mySwitch.send("000001000101010100110011");
      SwitchState = true;
    }
  }
}

void loop() {
  

  fauxmo.handle();

  idle();
  

}

Extra Devices and not working properly

#include <RCSwitch.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#include <mechButton.h>



#define WIFI_SSID "**********"
#define WIFI_PASS "**********"
#define Inside_Christmas_Lights "Inside Christmas Lights"
#define Outside_Christmas_Lights "Outside Christmas Lights"
#define All_Christmas_Lights "All Christmas Lights"

const int GreenLED = 5;   //Connected to pin D1.
const int RedLED = 4;     //Connected to pin D2.
const int buttonPin = 14; //Connected to pin D5.

bool SwitchState;   // the current state of the output pin

mechButton  buttonPress(buttonPin);

fauxmoESP fauxmo;

RCSwitch mySwitch = RCSwitch();

// Wi-Fi Connection
void wifiSetup() {

  // Set WIFI module to STA mode
  WiFi.mode(WIFI_STA);


  // Connect
  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);


  // Wait
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    digitalWrite(GreenLED, LOW);
    digitalWrite(RedLED, HIGH);
    delay(200);
    digitalWrite(GreenLED, HIGH);
    digitalWrite(RedLED, LOW);
    delay(200);
//LED's alternate between Red and Green to show that the WIFI is connecting
  }

  Serial.println();

  // Connected!
  Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());

  digitalWrite(GreenLED, HIGH);
  digitalWrite(RedLED, HIGH);
 // Both LED's light up as Orange to show that the WiFi network has connected.


}

void setup() {

  pinMode (GreenLED, OUTPUT);
  pinMode (RedLED, OUTPUT);

  buttonPress.setCallback(touchSwitch);

  // Setup 433mhz Transmitter.
  mySwitch.enableTransmit(16); // Transmitter DATA Pin is connected to Pin D0.
  mySwitch.setProtocol(1); // Set protocol.
  mySwitch.setPulseLength(165); // Set pulse length.
  mySwitch.setRepeatTransmit(100); // Set number of transmission repetitions.

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

  // Wi-Fi connection
  wifiSetup();

  fauxmo.createServer(true);

  fauxmo.setPort(80);

  fauxmo.enable(true);

  fauxmo.addDevice(Inside_Christmas_Lights);  //Device 1

  fauxmo.addDevice(Outside_Christmas_Lights); //Device 2 

  fauxmo.addDevice(All_Christmas_Lights);     //Device 3 Controls both Inside and Outside lights together.

  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, Inside_Christmas_Lights) == 0) ) {

      if (state) {
        Serial.println("Inside Christmas Lights switched ON ...");
        digitalWrite(GreenLED, HIGH);
        digitalWrite(RedLED, LOW);
        mySwitch.send("000001000101010100110011");
      }
      else
      {
        Serial.println("Inside Christmas Lights switched OFF ...");
        digitalWrite(RedLED, HIGH);
        digitalWrite(GreenLED, LOW);
        mySwitch.send("000001000101010100111100");
      }
    }
    if ( (strcmp(device_name, Outside_Christmas_Lights) == 0) ) {

      if (state) {
        Serial.println("Outside Christmas Lights switched ON ...");
        digitalWrite(GreenLED, HIGH);
        digitalWrite(RedLED, LOW);
        mySwitch.send("000001000101010111000011");
      }
      else
      {
        Serial.println("Outside Christmas Lights switched OFF ...");
        digitalWrite(RedLED, HIGH);
        digitalWrite(GreenLED, LOW);
        mySwitch.send("000001000101010111001100");
      }
    }
    if ( (strcmp(device_name, All_Christmas_Lights) == 0) ) {

      if (state) {
        Serial.println("All Christmas Lights switched ON ...");
        digitalWrite(GreenLED, HIGH);
        digitalWrite(RedLED, LOW);
        mySwitch.send("000001000101010100110011");
        mySwitch.send("000001000101010111000011");
      }
      else
      {
        Serial.println("All Christmas Lights switched OFF ...");
        digitalWrite(RedLED, HIGH);
        digitalWrite(GreenLED, LOW);
        mySwitch.send("000001000101010100111100");
        mySwitch.send("000001000101010111001100");
      }
    }
  });
}

void touchSwitch() {

  if (!buttonPress.trueFalse()) {

    //  Changes the status of the valve
    if (SwitchState) {
      Serial.println("All Christmas Lights switched OFF ...");
      digitalWrite(RedLED, HIGH);
      digitalWrite(GreenLED, LOW);
      mySwitch.send("000001000101010100110011");
      mySwitch.send("000001000101010111000011");
      SwitchState = false;
    }
    else
    {
      Serial.println("All Christmas Lights switched ON ...");
      digitalWrite(GreenLED, HIGH);
      digitalWrite(RedLED, LOW);
      mySwitch.send("000001000101010100111100");
      mySwitch.send("000001000101010111001100");
      SwitchState = true;
    }
  }
}

void loop() {


  fauxmo.handle();

  idle();


}

Many Thanks,
Mike.

Looking further into it, it seems that the "All Christmas Lights" both Alexa and Touch switch code is the problem. I can't see any obvious issues though.