/* HOME AUTOMATION
Implemented using an ESP32, It can Remotely control a Light , fan and a door knob with
the help of inbuilt wifi module , using Thingspeak and MIT App inventor. Here the
stepper motor is used to contol the door knob and instead of a fan here a red LED is
used*/
#include <WiFi.h>
#include <AccelStepper.h>
#include<Stepper.h>
#include <ThingSpeak.h>
const char* SSID = "Wokwi-GUEST";
const char* PWD = "";
WiFiClient client;
unsigned long int ChannelNumber = 2592973;
const char* writeAPIKey = "IKIKMW969PHA1AWE";
const char* readAPIKey = "C21BRVU22BUJC8ED";
const int lightPin = 16;
const int fanPin = 17;
AccelStepper stepper(AccelStepper::DRIVER, 12, 14);
void setup() {
Serial.begin(115200);
WiFi.begin(SSID, PWD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println("WiFi Connected.");
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
pinMode(lightPin, OUTPUT);
pinMode(fanPin, OUTPUT);
stepper.setMaxSpeed(1000);
stepper.setAcceleration(500);
}
void loop() {
int A = ThingSpeak.readLongField(ChannelNumber,1,readAPIKey);
int B = ThingSpeak.readLongField(ChannelNumber,2,readAPIKey);
int C = ThingSpeak.readLongField(ChannelNumber,3,readAPIKey);
if (A == 1) {
Serial.println("DOOR LOCKED");
stepper.runToNewPosition(100);
} else if (A == 0) {
Serial.println("DOOR UNLOCKED");
stepper.runToNewPosition(0);
}
if (B == 1) {
Serial.println("LIGHTS ON");
digitalWrite(lightPin, HIGH);
} else if (B == 0) {
Serial.println("LIGHTS OFF");
digitalWrite(lightPin, LOW);
}
if (C == 1) {
Serial.println("FAN ON");
digitalWrite(fanPin, HIGH);
} else if (C == 0) {
Serial.println("FAN OFF");
digitalWrite(fanPin, LOW);
}
delay(500);
}