0% found this document useful (0 votes)
5 views2 pages

Dummy

The document contains two Arduino code snippets: one for controlling a relay based on commands received from an ESP32, and another for reading temperature, humidity, and air quality data while sending commands to the Arduino. The first code sets up a relay that turns on or off based on the input signal from the ESP32, while the second code reads sensor data and sends control signals to the relay. Both codes utilize serial communication for debugging and have defined pin configurations for their respective functionalities.
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)
5 views2 pages

Dummy

The document contains two Arduino code snippets: one for controlling a relay based on commands received from an ESP32, and another for reading temperature, humidity, and air quality data while sending commands to the Arduino. The first code sets up a relay that turns on or off based on the input signal from the ESP32, while the second code reads sensor data and sends control signals to the relay. Both codes utilize serial communication for debugging and have defined pin configurations for their respective functionalities.
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/ 2

ArudinoCode.

ino

#define RELAY_PIN 7 // Connect relay IN pin to D7


#define CONTROL_PIN 8 // Pin to receive command from ESP32 (e.g., GPIO8)

void setup() {
Serial.begin(9600); // Start serial communication
pinMode(RELAY_PIN, OUTPUT); // Set the relay pin as an output
pinMode(CONTROL_PIN, INPUT); // Set CONTROL_PIN as input to receive signals from
ESP32
digitalWrite(RELAY_PIN, LOW); // Ensure relay is off initially
}

void loop() {
int command = digitalRead(CONTROL_PIN); // Read the signal from ESP32

// Read the signal from GPIO pin (either HIGH or LOW)


if (command == HIGH) {
digitalWrite(RELAY_PIN, HIGH); // Turn relay ON
Serial.println("Relay turned OFF");
} else {
digitalWrite(RELAY_PIN, LOW); // Turn relay OFF
Serial.println("Relay turned ON");
}

delay(100); // Wait for 100ms before checking again


}

/////////////////////////////////////////////////////////////////////////
Esp32Code.ino

#include <DHT.h>

#define DHTPIN 4 // DHT11 data pin


#define DHTTYPE DHT11
#define MQ135_PIN 32 // MQ135 analog pin
#define CONTROL_PIN 12 // GPIO to send signal to Arduino (e.g., GPIO 12)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600); // For debugging
pinMode(CONTROL_PIN, OUTPUT); // Set CONTROL_PIN as output to control relay on
Arduino
dht.begin();
}

void loop() {
// Read temperature and humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int mq135_value = analogRead(MQ135_PIN);

Serial.print("Temp: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.print(" %, MQ135: ");
Serial.println(mq135_value);

// Example condition to control the relay via GPIO pin


String command = "0"; // Default to 0 (OFF)

// Send the command to Arduino via GPIO


if (command == "1") {
digitalWrite(CONTROL_PIN, HIGH); // Send HIGH signal to Arduino (turn relay
ON)
Serial.println("Relay command: OFF");
} else if (command == "0") {
digitalWrite(CONTROL_PIN, LOW); // Send LOW signal to Arduino (turn relay
OFF)
Serial.println("Relay command: ON");
}

delay(2000); // Wait for 2 seconds before checking again


}

You might also like