0% found this document useful (0 votes)
26 views4 pages

Home Automation Using ESP32 and ANN Main Code

Uploaded by

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

Home Automation Using ESP32 and ANN Main Code

Uploaded by

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

// Home automation using ESP32 and ANN

// WiFi and Blynk credentials


#define BLYNK_TEMPLATE_ID "TMPL6PetjPfTP"
#define BLYNK_TEMPLATE_NAME "ESP32 Home Automation"
#define BLYNK_AUTH_TOKEN "YWchcU-3QFMxW8HESYfJnK3iHlM-QK0C"

#define BLYNK_PRINT Serial

#include <Arduino.h>
#include <ArduTFLite.h>
#include "model.h" // Include the TensorFlow Lite model header file
#include "DHT11.h"
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;


char ssid[] = "Galaxy A52";
char pass[] = "12222222";

BlynkTimer timer;

// DHT11 Sensor
#define DHT_PIN 2
DHT11 dht11(DHT_PIN);

// LED Matrix
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define CS_PIN 5
#define MAX_DEVICES 4
MD_Parola display = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

// TensorFlow Lite configuration


constexpr int kTensorArenaSize = 2000; // Adjust size if necessary
alignas(16) uint8_t tensor_arena[kTensorArenaSize];

// Button and relay pin definitions


#define button1_pin 26
#define button2_pin 25
#define button3_pin 33
#define button4_pin 32

#define relay1_pin 13
#define relay2_pin 12
#define relay3_pin 14
#define relay4_pin 27

int relay1_state = 0;
int relay2_state = 0;
int relay3_state = 0;
int relay4_state = 0;

#define button1_vpin V1
#define button2_vpin V2
#define button3_vpin V3
#define button4_vpin V4

// Sync states with Blynk


BLYNK_CONNECTED() {
Blynk.syncVirtual(button1_vpin);
Blynk.syncVirtual(button2_vpin);
Blynk.syncVirtual(button3_vpin);
Blynk.syncVirtual(button4_vpin);
}

// Virtual button handlers


BLYNK_WRITE(button1_vpin) {
relay1_state = param.asInt();
digitalWrite(relay1_pin, relay1_state);
}

BLYNK_WRITE(button2_vpin) {
relay2_state = param.asInt();
digitalWrite(relay2_pin, relay2_state);
}

BLYNK_WRITE(button3_vpin) {
relay3_state = param.asInt();
digitalWrite(relay3_pin, relay3_state);
}

BLYNK_WRITE(button4_vpin) {
relay4_state = param.asInt();
digitalWrite(relay4_pin, relay4_state);
}

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

// Initialize WiFi and Blynk


Blynk.begin(auth, ssid, pass);

// Initialize pins
pinMode(button1_pin, INPUT_PULLUP);
pinMode(button2_pin, INPUT_PULLUP);
pinMode(button3_pin, INPUT_PULLUP);
pinMode(button4_pin, INPUT_PULLUP);
pinMode(relay1_pin, OUTPUT);
pinMode(relay2_pin, OUTPUT);
pinMode(relay3_pin, OUTPUT);
pinMode(relay4_pin, OUTPUT);
digitalWrite(relay1_pin, HIGH);
digitalWrite(relay2_pin, HIGH);
digitalWrite(relay3_pin, HIGH);
digitalWrite(relay4_pin, HIGH);

// Initialize LED Matrix


display.begin();
display.setIntensity(0);
display.displayClear();

// Initialize DHT11
Serial.println("DHT11 initialized.");
// Initialize TensorFlow Lite
Serial.println("Initializing TensorFlow Lite model...");
if (!modelInit(model, tensor_arena, kTensorArenaSize)) {
Serial.println("Model initialization failed!");
while (1);
}
Serial.println("TensorFlow Lite model initialized.");
}

void loop() {
Blynk.run();
timer.run();
listen_push_buttons();
read_and_display_data();
}

void read_and_display_data() {
int temperature = 0, humidity = 0;
if (dht11.readTemperatureHumidity(temperature, humidity) == 0) {
// Use TensorFlow Lite model to predict rain likelihood
modelSetInput(float(temperature), 0);
modelSetInput(float(humidity), 1);

float rain_likelihood = 0;
if (modelRunInference()) {
rain_likelihood = modelGetOutput(0) * 100;

// Display data on LED matrix


displayMessage("Temp: " + String(temperature) + "C");
displayMessage("Hum: " + String(humidity) + "%");
displayMessage("Rain: " + String(rain_likelihood, 1) + "%");

// Auto-control relay based on rain likelihood


if (rain_likelihood >= 70.0) {
relay4_state = 0; // Turn off Relay 4
digitalWrite(relay4_pin, relay4_state);
Blynk.virtualWrite(button4_vpin, relay4_state);
} else {
relay4_state = 1; // Turn on Relay 4
digitalWrite(relay4_pin, relay4_state);
}
} else {
displayMessage("Model Error");
Serial.println("Model inference failed!");
}
} else {
displayMessage("Sensor Error");
Serial.println("Failed to read from DHT11 sensor.");
}

delay(1000); // Delay between updates


}

void displayMessage(String message) {


display.displayClear();
display.displayText(
message.c_str(),
PA_CENTER,
100,
500,
PA_SCROLL_LEFT,
PA_SCROLL_LEFT
);

while (!display.displayAnimate()) {
// Wait for the scrolling animation to complete
}
}

void listen_push_buttons() {
if (digitalRead(button1_pin) == LOW) {
delay(200);
control_relay(1);
Blynk.virtualWrite(button1_vpin, relay1_state);
}
if (digitalRead(button2_pin) == LOW) {
delay(200);
control_relay(2);
Blynk.virtualWrite(button2_vpin, relay2_state);
}
if (digitalRead(button3_pin) == LOW) {
delay(200);
control_relay(3);
Blynk.virtualWrite(button3_vpin, relay3_state);
}
if (digitalRead(button4_pin) == LOW) {
delay(200);
control_relay(4);
Blynk.virtualWrite(button4_vpin, relay4_state);
}
}

void control_relay(int relay) {


if (relay == 1) {
relay1_state = !relay1_state;
digitalWrite(relay1_pin, relay1_state);
Serial.print("Relay1 State = ");
Serial.println(relay1_state);
}
if (relay == 2) {
relay2_state = !relay2_state;
digitalWrite(relay2_pin, relay2_state);
}
if (relay == 3) {
relay3_state = !relay3_state;
digitalWrite(relay3_pin, relay3_state);
}
if (relay == 4) {
relay4_state = !relay4_state;
digitalWrite(relay4_pin, relay4_state);
}
}

You might also like