10-04-2021, 07:51 AM
(10-04-2021, 12:04 AM)admin Wrote: Can you try the above arduino code? now we suggest use raspberry pi running Node-Red for alexa or google home. because sinric.com not free now.
Code:
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#define ESPALEXA_MAXDEVICES 32
#include <Espalexa.h>
#define R2 2
// prototypes
boolean connectWifi();
//callback functions
void F1LightChanged(uint8_t brightness);
void F2LightChanged(uint8_t brightness);
void F3LightChanged(uint8_t brightness);
void F4LightChanged(uint8_t brightness);
void F5LightChanged(uint8_t brightness);
void F6LightChanged(uint8_t brightness);
void F7LightChanged(uint8_t brightness);
void F8LightChanged(uint8_t brightness);
void F9LightChanged(uint8_t brightness);
void F10LightChanged(uint8_t brightness);
void F11LightChanged(uint8_t brightness);
void F12LightChanged(uint8_t brightness);
void F13LightChanged(uint8_t brightness);
void F14LightChanged(uint8_t brightness);
void F15LightChanged(uint8_t brightness);
void F16LightChanged(uint8_t brightness);
void F17LightChanged(uint8_t brightness);
void F18LightChanged(uint8_t brightness);
void F19LightChanged(uint8_t brightness);
void F20LightChanged(uint8_t brightness);
void F21LightChanged(uint8_t brightness);
void F22LightChanged(uint8_t brightness);
void F23LightChanged(uint8_t brightness);
void F24LightChanged(uint8_t brightness);
void F25LightChanged(uint8_t brightness);
void F26LightChanged(uint8_t brightness);
void F27LightChanged(uint8_t brightness);
void F28LightChanged(uint8_t brightness);
void F29LightChanged(uint8_t brightness);
void F30LightChanged(uint8_t brightness);
void F31LightChanged(uint8_t brightness);
void F32LightChanged(uint8_t brightness);
// Change this!!
// WiFi Credentials
const char* ssid = "----SSID----";
const char* password = "----PASSWORD----";
// device names
String Device_1_Name = "luce salotto 1";
String Device_2_Name = "luce salotto 2";
String Device_3_Name = "luce cucina";
String Device_4_Name = "luce antibagno giorno";
String Device_5_Name = "luce bagno giorno";
String Device_6_Name = "luce corridoio";
String Device_7_Name = "luce camera";
String Device_8_Name = "luce studio";
String Device_9_Name = "luce bagno notte";
String Device_10_Name ="luce balcone";
String Device_11_Name ="luce portico";
String Device_12_Name ="luce giardino";
String Device_13_Name ="luce scala";
String Device_14_Name ="luce sottoscala";
String Device_15_Name ="luce cantina";
String Device_16_Name ="luce garage";
String Device_17_Name ="rele 17";
String Device_18_Name ="rele 18";
String Device_19_Name ="rele 19";
String Device_20_Name ="monitor";
String Device_21_Name ="rele 21";
String Device_22_Name ="rele 22";
String Device_23_Name ="rele 23";
String Device_24_Name ="rele 24";
String Device_25_Name ="rele 25";
String Device_26_Name ="rele 26";
String Device_27_Name ="rele 27";
String Device_28_Name ="rele 28";
String Device_29_Name ="rele 29";
String Device_30_Name ="rele 30";
String Device_31_Name ="rele 31";
String Device_32_Name ="rele 32";
//unsigned long Millis = 0;
//unsigned long PreviousMillis = 0;
//boolean Initialized = false;
boolean wifiConnected = false;
Espalexa espalexa;
void setup()
{
Serial.begin(115200);
pinMode(R2, OUTPUT);
pinMode(R2, LOW);
//PreviousMillis = millis();
// Initialise wifi connection
wifiConnected = connectWifi();
if (wifiConnected)
{
// Define your devices here.
espalexa.addDevice(Device_1_Name,F1LightChanged);
espalexa.addDevice(Device_2_Name,F2LightChanged);
espalexa.addDevice(Device_3_Name,F3LightChanged);
espalexa.addDevice(Device_4_Name,F4LightChanged);
espalexa.addDevice(Device_5_Name,F5LightChanged);
espalexa.addDevice(Device_6_Name,F6LightChanged);
espalexa.addDevice(Device_7_Name,F7LightChanged);
espalexa.addDevice(Device_8_Name,F8LightChanged);
espalexa.addDevice(Device_9_Name,F9LightChanged);
espalexa.addDevice(Device_10_Name,F10LightChanged);
espalexa.addDevice(Device_11_Name,F11LightChanged);
espalexa.addDevice(Device_12_Name,F12LightChanged);
espalexa.addDevice(Device_13_Name,F13LightChanged);
espalexa.addDevice(Device_14_Name,F14LightChanged);
espalexa.addDevice(Device_15_Name,F15LightChanged);
espalexa.addDevice(Device_16_Name,F16LightChanged);
espalexa.addDevice(Device_17_Name,F17LightChanged);
espalexa.addDevice(Device_18_Name,F18LightChanged);
espalexa.addDevice(Device_19_Name,F19LightChanged);
espalexa.addDevice(Device_20_Name,F20LightChanged);
espalexa.addDevice(Device_21_Name,F21LightChanged);
espalexa.addDevice(Device_22_Name,F22LightChanged);
espalexa.addDevice(Device_23_Name,F23LightChanged);
espalexa.addDevice(Device_24_Name,F24LightChanged);
espalexa.addDevice(Device_25_Name,F25LightChanged);
espalexa.addDevice(Device_26_Name,F26LightChanged);
espalexa.addDevice(Device_27_Name,F27LightChanged);
espalexa.addDevice(Device_28_Name,F28LightChanged);
espalexa.addDevice(Device_29_Name,F29LightChanged);
espalexa.addDevice(Device_30_Name,F30LightChanged);
espalexa.addDevice(Device_31_Name,F31LightChanged);
espalexa.addDevice(Device_32_Name,F32LightChanged);
espalexa.begin();
}
else
{
while (1)
{
Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
delay(2500);
}
}
}
void loop()
{
espalexa.loop();
delay(1);
}
//our callback functions
void F1LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,1,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,1,0");
digitalWrite(R2, LOW);
}
}
void F2LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,2,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,2,0");
digitalWrite(R2, LOW);
}
}
void F3LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,3,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,3,0");
digitalWrite(R2, LOW);
}
}
void F4LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,4,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,4,0");
digitalWrite(R2, LOW);
}
}
void F5LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,5,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,5,0");
digitalWrite(R2, LOW);
}
}
void F6LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,6,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,6,0");
digitalWrite(R2, LOW);
}
}
void F7LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,7,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,7,0");
digitalWrite(R2, LOW);
}
}
void F8LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,8,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,8,0");
digitalWrite(R2, LOW);
}
}
void F9LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,9,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,9,0");
digitalWrite(R2, LOW);
}
}
void F10LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,10,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,10,0");
digitalWrite(R2, LOW);
}
}
void F11LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,11,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,11,0");
digitalWrite(R2, LOW);
}
}
void F12LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,12,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,12,0");
digitalWrite(R2, LOW);
}
}
void F13LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,13,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,13,0");
digitalWrite(R2, LOW);
}
}
void F14LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,14,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,14,0");
digitalWrite(R2, LOW);
}
}
void F15LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,15,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,15,0");
digitalWrite(R2, LOW);
}
}
void F16LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,16,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,16,0");
digitalWrite(R2, LOW);
}
}
void F17LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,17,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,17,0");
digitalWrite(R2, LOW);
}
}
void F18LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,18,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,18,0");
digitalWrite(R2, LOW);
}
}
void F19LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,19,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,19,0");
digitalWrite(R2, LOW);
}
}
void F20LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,20,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,20,0");
digitalWrite(R2, LOW);
}
}
void F21LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,21,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,21,0");
digitalWrite(R2, LOW);
}
}
void F22LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,22,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,22,0");
digitalWrite(R2, LOW);
}
}
void F23LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,23,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,23,0");
digitalWrite(R2, LOW);
}
}
void F24LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,24,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,24,0");
digitalWrite(R2, LOW);
}
}
void F25LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,25,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,25,0");
digitalWrite(R2, LOW);
}
}
void F26LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,26,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,26,0");
digitalWrite(R2, LOW);
}
}
void F27LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,27,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,27,0");
digitalWrite(R2, LOW);
}
}
void F28LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,28,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,28,0");
digitalWrite(R2, LOW);
}
}
void F29LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,29,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,29,0");
digitalWrite(R2, LOW);
}
}
void F30LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,30,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,30,0");
digitalWrite(R2, LOW);
}
}
void F31LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,31,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,31,0");
digitalWrite(R2, LOW);
}
}
void F32LightChanged(uint8_t brightness)
{
//Control the device
if (brightness)
{
if (brightness == 255)
{
Serial.println("RELAY-SET-255,32,1");
digitalWrite(R2, HIGH);
}
//Serial.print("ON, brightness ");
//Serial.println(brightness);
}
else
{
Serial.println("RELAY-SET-255,32,0");
digitalWrite(R2, LOW);
}
}
// connect to wifi – returns true if successful or false if not
boolean connectWifi()
{
boolean state = true;
int i = 0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
//Serial.println("");
//Serial.println("Connecting to WiFi");
// Wait for connection
//Serial.print("Connecting...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
// Serial.print(".");
if (i > 20) {
state = false; break;
}
i++;
}
//Serial.println("");
if (state) {
//Serial.print("Connected to ");
Serial.println(ssid);
//Serial.print("IP address: ");
Serial.println(WiFi.localIP());
pinMode(R2, HIGH);
}
else {
//Serial.println("Connection failed.");
pinMode(R2, LOW);
}
return state;
}