Smart Home Automation Forum
[Arduino demo source code for KC868-A4]-07 voice control by alexa speaker - Printable Version

+- Smart Home Automation Forum (https://www.kincony.com/forum)
+-- Forum: Technical Support (https://www.kincony.com/forum/forumdisplay.php?fid=20)
+--- Forum: KC868-A4 (https://www.kincony.com/forum/forumdisplay.php?fid=21)
+--- Thread: [Arduino demo source code for KC868-A4]-07 voice control by alexa speaker (/showthread.php?tid=1646)



[Arduino demo source code for KC868-A4]-07 voice control by alexa speaker - KinCony Support - 01-07-2022

Code 6: //The demo code is Alexa control     You can copy the code to your Arduino IDE

Code:
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Espalexa.h>

#define Relay1 2
#define Relay2 15
#define Relay3 5
#define Relay4 4

// 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);

// Change this!!

// WiFi Credentials
const char* ssid = "KinCony";           //input your router's SSID
const char* password = "a12345678";    // input your router's password

// device names
String Device_1_Name = "Yellow light";
String Device_2_Name = "Green light";
String Device_3_Name = "Blue light";
String Device_4_Name = "Red light"; 


boolean wifiConnected = false;

Espalexa espalexa;

void setup()
{
  Serial.begin(115200);
  pinMode(Relay1, OUTPUT);
  pinMode(Relay1, LOW);

  pinMode(Relay2, OUTPUT);
  pinMode(Relay2, LOW);

  pinMode(Relay3, OUTPUT);
  pinMode(Relay3, LOW);

  pinMode(Relay4, OUTPUT);
  pinMode(Relay4, LOW);     

  // 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.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)
    {
      digitalWrite(Relay1,HIGH);
    }

  }
  else
  {
   digitalWrite(Relay1, LOW);
  }
}

void F2LightChanged(uint8_t brightness)
{

  //Control the device
  if (brightness)
  {
    if (brightness == 255)
    {
      digitalWrite(Relay2,HIGH);
    }

  }
  else
  {
   digitalWrite(Relay2,LOW);
  }
}

void F3LightChanged(uint8_t brightness)
{

  //Control the device 
  if (brightness)
  {
    if (brightness == 255)
    {
     digitalWrite(Relay3,HIGH);
    }
  }
  else
  {
     digitalWrite(Relay3,LOW);
  }
}

void F4LightChanged(uint8_t brightness)
{

  //Control the device
  if (brightness)
  {
    if (brightness == 255)
    {
     digitalWrite(Relay4,HIGH);
    }
  }
  else
  {
    digitalWrite(Relay4,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);

  // 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());
  }
  else {
    Serial.println("Connection failed.");
  }
  return state;
}



RE: [Arduino IDE demo source code for KC868-A4]--#07-Alexa control - ahmedwizza - 05-11-2022

can we add all codes in one code to more benefit.

for example ....
Alex code 
and K box code 
and Ir code
Etc.....
Because when i upload code cancelled old and active the new. Big Grin


RE: [Arduino IDE demo source code for KC868-A4]--#07-Alexa control - admin - 05-12-2022

yes, you can add all in one. some enable , some disable.