0% found this document useful (0 votes)
50 views

Room Automation Code

The document defines an Arduino sketch that connects an ESP32 or ESP8266 to WiFi and controls 4 devices (CFL, light, fan, plug) connected to relays via Alexa voice commands. It includes functions to initialize WiFi connection, add devices to the Espalexa library, and callbacks to turn devices on/off by controlling the relay pins.

Uploaded by

Hari Om
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Room Automation Code

The document defines an Arduino sketch that connects an ESP32 or ESP8266 to WiFi and controls 4 devices (CFL, light, fan, plug) connected to relays via Alexa voice commands. It includes functions to initialize WiFi connection, add devices to the Espalexa library, and callbacks to turn devices on/off by controlling the relay pins.

Uploaded by

Hari Om
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#ifdef ARDUINO_ARCH_ESP32

#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Espalexa.h>

// define the GPIO connected with Relays


#define RelayPin1 14 //D5
#define RelayPin2 12 //D6
#define RelayPin3 13 //D7
#define RelayPin4 15 //D8

// prototypes
boolean connectWifi();

//callback functions
void cfl(uint8_t brightness);
void light(uint8_t brightness);
void fan(uint8_t brightness);
void plug(uint8_t brightness);

// WiFi Credentials
const char* ssid = "Excellence";
const char* password = "vivek123";

// device names
String Device_1_Name = "CFL";
String Device_2_Name = "Light";
String Device_3_Name = "Fan";
String Device_4_Name = "Plug";

boolean wifiConnected = false;

Espalexa espalexa;

void setup()
{
Serial.begin(115200);

pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
// Initialise wifi connection
wifiConnected = connectWifi();

if (wifiConnected)
{
// Define your devices here.
espalexa.addDevice(Device_1_Name, cfl); //simplest definition, default state off
espalexa.addDevice(Device_2_Name, light);
espalexa.addDevice(Device_3_Name, fan); //simplest definition, default state off
espalexa.addDevice(Device_4_Name, plug);

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 cfl(uint8_t brightness)
{
//Control the device
if (brightness == 255)
{
digitalWrite(RelayPin1, HIGH);
Serial.println("Device1 ON");
}
else
{
digitalWrite(RelayPin1, LOW);
Serial.println("Device1 OFF");
}
}

void light(uint8_t brightness)


{
//Control the device
if (brightness == 255)
{
digitalWrite(RelayPin2, HIGH);
Serial.println("Device2 ON");
}
else
{
digitalWrite(RelayPin2, LOW);
Serial.println("Device2 OFF");
}
}

void fan(uint8_t brightness)


{
//Control the device
if (brightness == 255)
{
digitalWrite(RelayPin3, HIGH);
Serial.println("Device3 ON");
}
else
{
digitalWrite(RelayPin3, LOW);
Serial.println("Device3 OFF");
}
}

void plug(uint8_t brightness)


{
//Control the device
if (brightness == 255)
{
digitalWrite(RelayPin4, HIGH);
Serial.println("Device4 ON");
}
else
{
digitalWrite(RelayPin4, LOW);
Serial.println("Device4 OFF");
}
}

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

You might also like