0% found this document useful (0 votes)
13 views8 pages

Code

Uploaded by

Kavan Sketcher
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)
13 views8 pages

Code

Uploaded by

Kavan Sketcher
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/ 8

Arduino code:-

#include <SoftwareSerial.h>

#include <DHT.h>

int dhtpin = 2;

DHT dht(dhtpin, DHT11);

const int minTemp = 22;

const int maxTemp = 30;

const int minSpeed = 0;

const int maxSpeed = 255;

int fanPin = 9;

int pump = 10;

int mosPin = A1;

int moistureValue;

int limit = 550;

#define MQ5_ANALOG_PIN A0

#define THRESHOLD 185

void setup() {

Serial.begin(9600);

dht.begin();

pinMode(pump,OUTPUT);

pinMode(fanPin,OUTPUT);

void loop() {

int h = dht.readHumidity();

Serial.print("humi:");

Serial.println(h);

int temp = dht.readTemperature();

Serial.print("temp:");

Serial.println(temp);

if (temp <=30){

int fanSpeed = map(temp, minTemp, maxTemp, minSpeed, maxSpeed);

fanSpeed = constrain(fanSpeed, minSpeed, maxSpeed);


Serial.print("fan-");

Serial.println(fanSpeed);

analogWrite(fanPin, fanSpeed);

else{

analogWrite(fanPin,255);

int gas_sensorValue = analogRead(MQ5_ANALOG_PIN);

Serial.print("sensorValue: ");

Serial.println(gas_sensorValue);

moistureValue = analogRead(mosPin);

Serial.print("moisture Value : ");

Serial.println(moistureValue);

if (moistureValue>limit) {

analogWrite(pump, 255);

Serial.print("motor: ");

Serial.println(1);

else if(moistureValue<limit){

analogWrite(pump,0);

Serial.print("motor: ");

Serial.println(0);

delay(1000);

----------------------------------------------------------------------------------------------------------------------

Esp code:---(wifi module)

#include <Arduino.h>

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h> // Include the Blynk library

#include <SinricPro.h>
#include <SinricProSwitch.h>

#include <SoftwareSerial.h>

#include <DHT.h>

#define RELAYPIN_1 D5

#define RELAYPIN_2 D6

#define RELAYPIN_3 D7

#define RELAYPIN_4 D8

#define PIR_PIN D0

#define gaspin V4

#define temp V5

#define humi V6

#define motorpin V7

#define moistureValue V8

#define pir V9

BlynkTimer timer;

int pirsensor = 0;

struct RelayInfo {

String deviceId;

String name;

int pin;

};

std::vector<RelayInfo> relays = {

{"65d08a33b0a460b25b45652d", "Relay 1", RELAYPIN_1},

{"65d089f9b0a460b25b456501", "Relay 2", RELAYPIN_2},

{"65d08970b0a460b25b456480", "Relay 3", RELAYPIN_3}

};

enum WiFiState {
WIFI_CONNECTING,

WIFI_CONNECTED,

WIFI_FAILED

};

WiFiState wifiState = WIFI_CONNECTING;

bool onPowerState(const String &deviceId, bool &state) {

for (auto &relay : relays) {

if (deviceId == relay.deviceId) {

digitalWrite(relay.pin, !state);

return true;

return false;

void setupRelayPins() {

for (auto &relay : relays) {

pinMode(relay.pin, OUTPUT);

void setupWiFi() {

WiFi.begin("A12","Kavan@113");

void setupSinricPro() {

for (auto &relay : relays) {

SinricProSwitch &mySwitch = SinricPro[relay.deviceId];

mySwitch.onPowerState(onPowerState);

}
SinricPro.begin("df44d0f2-b2a9-411f-b61f-5f5b9bf0b30d","f098896b-818e-4424-a7e6-dec684d53c33-0846d3fa-ca0b-
4a6e-8109-c980725ad145");

BLYNK_WRITE(V0) {

int value = param.asInt();

digitalWrite(RELAYPIN_1, value);

BLYNK_WRITE(V1) {

int value = param.asInt();

digitalWrite(RELAYPIN_2, value);

BLYNK_WRITE(V2) {

int value = param.asInt();

digitalWrite(RELAYPIN_3, value);

BLYNK_WRITE(V3) {

int value = param.asInt();

digitalWrite(RELAYPIN_4, value);

void setup() {

pinMode(RELAYPIN_1, OUTPUT);

pinMode(RELAYPIN_2, OUTPUT);

pinMode(RELAYPIN_3, OUTPUT);

pinMode(RELAYPIN_4, OUTPUT);

digitalWrite(RELAYPIN_1,HIGH);

digitalWrite(RELAYPIN_2,HIGH);

digitalWrite(RELAYPIN_3,HIGH);
digitalWrite(RELAYPIN_4,HIGH);

pinMode(D0, INPUT);

Serial.begin(9600);

setupWiFi();

setupSinricPro();

Blynk.begin("C7Kmda2XoKi6R9xXpioXihfQF1CEXY1p","A12","Kavan@113");

timer.setInterval(1000L, Pirsensor);

void Pirsensor() {

pirsensor = digitalRead(PIR_PIN);

if (pirsensor == HIGH) {

Blynk.virtualWrite(pir, HIGH);

Blynk.logEvent("motion_detected");

} else if (pirsensor == LOW) {

Blynk.virtualWrite(pir, LOW);

void loop() {

switch (wifiState) {

case WIFI_CONNECTING:

if (WiFi.status() == WL_CONNECTED) {

wifiState = WIFI_CONNECTED;

Serial.println("WiFi Connected");

} else if (WiFi.status() == WL_CONNECT_FAILED) {

wifiState = WIFI_FAILED;

Serial.println("WiFi Connection Failed");

break;

case WIFI_CONNECTED:

if(Serial.available() > 0){


String command = Serial.readStringUntil('\n');

if (command.startsWith("sensorValue: ")) {

int sensorValue = command.substring(13).toInt();

if(sensorValue > 200){

Blynk.virtualWrite(gaspin, HIGH);

Blynk.logEvent("gas_detected");

} else if(sensorValue < 200){

Blynk.virtualWrite(gaspin, LOW);

if(command.startsWith("motor: ")){

int motor = command.substring(7).toInt();

if(motor == 1){

Blynk.virtualWrite(motorpin, HIGH);

} else if(motor == 0){

Blynk.virtualWrite(motorpin, LOW);

if(command.startsWith("moisture Value : ")){

int moisture = command.substring(16).toInt();

int sense = map(moisture,0,1024,100,0);

Blynk.virtualWrite(V8,sense);

if(command.startsWith("temp:")){

int t = command.substring(5).toInt();

Blynk.virtualWrite(temp,t);

if(command.startsWith("humi:")){

int h = command.substring(5).toInt();

Blynk.virtualWrite(humi,h);

}
SinricPro.handle();

Blynk.run();

timer.run();

break;

case WIFI_FAILED:

// Handle WiFi connection failure here

// You might want to implement a retry mechanism or take other actions

break;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy