Over the last month or so I have had a bit of a thing for home automation and voice control. While I'm not completely computer illiterate I have very little experience with coding of any sort.
I came across some code for an ESP8266 get devices to work with Alexa I flashed this to a nodemcu and it worked fine, however it was only to control 2 devices i edited the code to control 4 devices the issue I have is Alexa will only discover 3 devices.
The code I used is below. Any advice as to why it wont discover the 4th device would be appreciated
/*
* Rui Santos
* Complete Project Details http://randomnerdtutorials.com
*/
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#define RF_RECEIVER 13
#define RELAY_PIN_1 12
#define RELAY_PIN_2 14
#else
#include <ESP8266WiFi.h>
#define RF_RECEIVER 5
#define RELAY_PIN_1 4
#define RELAY_PIN_2 14
#define RELAY_PIN_3 2
#define RELAY_PIN_4 15
#endif
#include "fauxmoESP.h"
#include <RCSwitch.h>
#define SERIAL_BAUDRATE 115200
#define WIFI_SSID "BTHub6-CFR5"
#define WIFI_PASS "RP3wpV9DcNMh"
#define LAMP_1 "lamp one"
#define LAMP_2 "lamp two"
#define LAMP_3 "lamp three"
#define LAMP_2 "lamp four"
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(".");
delay(100);
}
Serial.println();
// Connected!
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();
// LED
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);
mySwitch.enableReceive(RF_RECEIVER); // Receiver on interrupt 0 => that is pin #2
// By default, fauxmoESP creates it's own webserver on the defined port
// The TCP port must be 80 for gen3 devices (default is 1901)
// This has to be done before the call to enable()
fauxmo.createServer(true); // not needed, this is the default value
fauxmo.setPort(80); // This is required for gen3 devices
// You have to call enable(true) once you have a WiFi connection
// You can enable or disable the library at any moment
// Disabling it will prevent the devices from being discovered and switched
fauxmo.enable(true);
// You can use different ways to invoke alexa to modify the devices state:
// "Alexa, turn lamp two on"
// Add virtual devices
fauxmo.addDevice(LAMP_1);
fauxmo.addDevice(LAMP_2);
fauxmo.addDevice(LAMP_3);
fauxmo.addDevice(LAMP_4);
fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
// Callback when a command from Alexa is received.
// You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
// State is a boolean (ON/OFF) and value a number from 0 to 255 (if you say "set kitchen light to 50%" you will receive a 128 here).
// Just remember not to delay too much here, this is a callback, exit as soon as possible.
// If you have to do something more involved here set a flag and process it in your main loop.
Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
if ( (strcmp(device_name, LAMP_1) == 0) ) {
// this just sets a variable that the main loop() does something about
Serial.println("RELAY 1 switched by Alexa");
//digitalWrite(RELAY_PIN_1, !digitalRead(RELAY_PIN_1));
if (state) {
digitalWrite(RELAY_PIN_1, LOW);
} else {
digitalWrite(RELAY_PIN_1, HIGH);
}
}
if ( (strcmp(device_name, LAMP_2) == 0) ) {
// this just sets a variable that the main loop() does something about
Serial.println("RELAY 2 switched by Alexa");
if (state) {
digitalWrite(RELAY_PIN_2, LOW);
} else {
digitalWrite(RELAY_PIN_2, HIGH);
}
}
if ( (strcmp(device_name, LAMP_3) == 0) ) {
// this just sets a variable that the main loop() does something about
Serial.println("RELAY 2 switched by Alexa");
if (state) {
digitalWrite(RELAY_PIN_3, LOW);
} else {
digitalWrite(RELAY_PIN_3, HIGH);
}
}
if ( (strcmp(device_name, LAMP_4) == 0) ) {
// this just sets a variable that the main loop() does something about
Serial.println("RELAY 4 switched by Alexa");
if (state) {
digitalWrite(RELAY_PIN_4, LOW);
} else {
digitalWrite(RELAY_PIN_4, HIGH);
}
}
});
}
void loop() {
// fauxmoESP uses an async TCP server but a sync UDP server
// Therefore, we have to manually poll for UDP packets
fauxmo.handle();
static unsigned long last = millis();
if (millis() - last > 5000) {
last = millis();
Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
}
if (mySwitch.available()) {
/*Serial.print("Received ");
Serial.print( mySwitch.getReceivedValue() );
Serial.print(" / ");
Serial.print( mySwitch.getReceivedBitlength() );
Serial.print("bit ");
Serial.print("Protocol: ");
Serial.println( mySwitch.getReceivedProtocol() );*/
if (mySwitch.getReceivedValue()==6819768) {
digitalWrite(RELAY_PIN_1, !digitalRead(RELAY_PIN_1));
}
if (mySwitch.getReceivedValue()==9463928) {
digitalWrite(RELAY_PIN_2, !digitalRead(RELAY_PIN_2));
}
if (mySwitch.getReceivedValue()==9463928) {
digitalWrite(RELAY_PIN_3, !digitalRead(RELAY_PIN_3));
}
if (mySwitch.getReceivedValue()==9463928) {
digitalWrite(RELAY_PIN_4, !digitalRead(RELAY_PIN_4));
}
delay(600);
mySwitch.resetAvailable();
}
}