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

Main code

The document is an Arduino sketch for a smart home system that integrates various components such as servos, DHT sensors, and an OLED display. It utilizes the Blynk platform for remote control and monitoring of devices like LEDs, windows, curtains, and appliances. The code includes functions to control the state of these devices based on user inputs from the Blynk app.

Uploaded by

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

Main code

The document is an Arduino sketch for a smart home system that integrates various components such as servos, DHT sensors, and an OLED display. It utilizes the Blynk platform for remote control and monitoring of devices like LEDs, windows, curtains, and appliances. The code includes functions to control the state of these devices based on user inputs from the Blynk app.

Uploaded by

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

#include <Servo.

h>
#include<Arduino.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define DHTTYPE DHT11
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

#include <SPI.h>
#include <Wire.h>
#include <MFRC522.h>
#define SS_PIN 5
#define RST_PIN 0
MFRC522 mfrc522(SS_PIN, RST_PIN);

#define SCREEN_WIDTH 128


#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

#define BLYNK_PRINT Serial

#define BLYNK_TEMPLATE_ID "TMPL2bMworSrg"


#define BLYNK_TEMPLATE_NAME "Smart Home"
#define BLYNK_AUTH_TOKEN "rYSn8ZevfFcnIUAnqgUYhYhtULYnEP63"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

1
char ssid[] = "Memo";
char pass[] = "Mom159753";

BlynkTimer timer;

#define k_led_pin 14
#define r_led_pin 26
#define h_led_pin 27
#define r_window_pin 32
#define h_window_pin 33
#define h_curtain_pin 26
#define h_tv_pin 36
#define h_condition_pin 12
#define m_door_pin 25
#define SS 5
#define RAIN 34
#define DHTPIN 17
#define DS 16
#define SHCP 2
#define STCP 4
#define valve 13
#define GAS_SENS 35

DHT dht( DHTPIN , DHTTYPE);

int pos = 0;
int k_led_state = 0;
int r_led_state = 0;
int h_led_state = 0;
int r_window_state = 0;
int h_window_state = 0;
int h_curtain_state = 0;
int h_tv_state = 0;
int h_condition_state = 0;
int main_door_state = 0;

#define k_led_vpin V1
#define r_led_vpin V2
#define h_led_vpin V3
#define r_window_vpin V4
#define h_window_vpin V0
2
#define h_curtain_vpin V5
#define h_tv_vpin V6
#define h_condition_vpin V7
#define m_door_vpin V8

BLYNK_CONNECTED() {
Blynk.syncVirtual(k_led_vpin);
Blynk.syncVirtual(r_led_vpin);
Blynk.syncVirtual(h_led_vpin);
Blynk.syncVirtual(r_window_vpin);
Blynk.syncVirtual(h_window_vpin);
Blynk.syncVirtual(h_curtain_vpin);
Blynk.syncVirtual(h_tv_vpin);
Blynk.syncVirtual(h_condition_vpin);
Blynk.syncVirtual(m_door_vpin);

}
BLYNK_WRITE(k_led_vpin) {
k_led_state = param.asInt();
digitalWrite(k_led_pin, k_led_state);
}
BLYNK_WRITE(r_led_vpin) {
r_led_state = param.asInt();
digitalWrite(r_led_pin, r_led_state);
}
BLYNK_WRITE(h_led_vpin) {
h_led_state = param.asInt();
digitalWrite(h_led_pin, h_led_state);
}
BLYNK_WRITE(r_window_vpin) {
r_window_state = param.asInt();
if(r_window_state == 1){
servo1_on();
}else{
servo1_off();
}
}
BLYNK_WRITE(h_window_vpin) {
h_window_state = param.asInt();

3
if(h_window_state == 1){
servo2_on();
}else{
servo2_off();
}
}
BLYNK_WRITE(h_curtain_vpin) {
h_curtain_state = param.asInt();
digitalWrite(h_curtain_pin, h_curtain_state);
}
BLYNK_WRITE(h_tv_vpin) {
h_tv_state = param.asInt();
if(h_tv_state == 1){
lcd_on();
}else{
lcd_off();
}
}
BLYNK_WRITE(h_condition_vpin) {
h_condition_state = param.asInt();
digitalWrite(h_condition_pin, h_condition_state);
}
BLYNK_WRITE(m_door_vpin) {
main_door_state = param.asInt();
if(main_door_state == 1){
servo3_on();
}else{
servo3_off();
}
}

void servo1_off(){
servo1.write(0);
}
void servo1_on(){
servo1.write(90);
}
void servo2_off(){
servo2.write(0);
}
void servo2_on(){
4
servo2.write(90);
}
void servo3_off(){
servo3.write(0);
}
void servo3_on(){
servo3.write(90);
}
void servo4_off(){
servo4.write(0);
}
void servo4_on(){
servo4.write(90);
}
static const uint8_t image_data_Saraarray[1024] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xfc, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x3f, 0xff, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x0f, 0xff, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x07, 0xff, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x03, 0xff, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0xff, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x7f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x3f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x3f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x1f, 0xff, 0xff,
0xff,
5
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x80, 0x00, 0x00, 0x0f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xf0, 0x1c, 0x45, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xf8, 0x3f, 0xf7, 0xfe, 0x83, 0xc0, 0x00, 0x00, 0x0f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x03, 0x39, 0xc0, 0x00, 0x00, 0x07, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xfc, 0x3f, 0xfd, 0x79, 0xff, 0xe0, 0x00, 0x06, 0x07, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xfe, 0x7f, 0xff, 0xff, 0xff, 0x20, 0x00, 0x07, 0x07, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x3b, 0x87, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x7b, 0x8f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x04, 0x39, 0x8f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe7, 0xff, 0xec, 0x00, 0x06, 0x79, 0x1f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x07, 0xff, 0xde, 0x00, 0x04, 0x7a, 0x1f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0xff, 0xff, 0x9e, 0x00, 0x01, 0xc4, 0x3f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xec, 0x00, 0x01, 0xa8, 0x3f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x03, 0xe0, 0x7f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xfc, 0x00, 0x01, 0x00, 0xff, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xf0, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff,
0xff,

6
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf3, 0xe0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x0f, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x80, 0x07, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x07, 0xfe, 0x00, 0x01, 0xff, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x0c, 0x3f, 0xfc, 0x00, 0x00, 0x01, 0xff,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf9, 0x00, 0x03, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x0f,
0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0x0f, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00,
0x7f,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00,
0x01,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0xe1, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x0f, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x0f, 0x3f, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x1d, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x17, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x17, 0xff, 0x93, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x27, 0xff, 0xe7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x2f, 0xfb, 0x9e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x3f, 0xfe, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf9, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,

7
0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xf7, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xf8, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xf8, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xf8, 0x00, 0x00, 0x00, 0x03, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xf8, 0x00, 0x00, 0x00, 0x07, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xf8, 0x00, 0x00, 0x00, 0x07, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xf0, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xe0, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0xc0, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0xff, 0x80, 0x00, 0x00, 0x00, 0x3f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00
};
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Blynk.virtualWrite(V10, t);
Blynk.virtualWrite(V9, h);
}
void lcd_on(){
display.clearDisplay();

// Draw bitmap on the screen


display.drawBitmap(0, 0, image_data_Saraarray, 128, 64, 1);
display.display();
8
}
void lcd_off(){
display.clearDisplay();
display.display();
}
void gas(){
int gasValue = analogRead(GAS_SENS);
if(gasValue > 200){
digitalWrite(valve,HIGH);
}else{
digitalWrite(valve,LOW);
}
}
void setup()
{
SPI.begin();
mfrc522.PCD_Init();

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000); // Pause for 2 seconds
dht.begin();
timer.setInterval(100L, sendSensor);
servo1.attach(r_window_pin);
servo2.attach(h_window_pin);
servo3.attach(m_door_pin);
servo4.attach(15);
Serial.begin(115200);
pinMode(k_led_pin, OUTPUT);
pinMode(r_led_pin, OUTPUT);
pinMode(h_led_pin, OUTPUT);
pinMode(GAS_SENS, INPUT);
pinMode(valve, OUTPUT);
pinMode(h_curtain_pin, OUTPUT);
pinMode(h_condition_pin, OUTPUT);
pinMode(RAIN, INPUT);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
//myservo.write(pos);
}
9
void check(){
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// if the card was read
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Read the UID of the card and write to the serial port
Serial.println();
Serial.print("UID Tag :");
String content = "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
Serial.println();
if (content.substring(1) == "D3 7E 56 C5")
{
access_granted();
}
else
{
access_denied();
}
}
void access_granted(){
Serial.println("Access Granted! ");

servo4_on();
delay(1000);
servo4_off();
delay(1000);

10
}

void access_denied(){
Serial.println("Access Denied! ");

void loop()
{
//myservo.write( 90 );
check();
Blynk.run();
timer.run();
gas();
//Serial.println(r_window_state);
}

11

You might also like