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

esp8266

Uploaded by

tranthienbao789
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

esp8266

Uploaded by

tranthienbao789
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#define BLYNK_TEMPLATE_ID "TMPL6J9G5bUoD"

#define BLYNK_TEMPLATE_NAME "Smart Plant"


#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// set the LCD number of columns and rows


int lcdColumns = 16;
int lcdRows = 2;

// set LCD address, number of columns and rows


// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);

char auth[] = "XgDn8yfVTWsotvF8_B_gdcyfyKX3EPRQ"; // Enter your Blynk


Auth token
char ssid[] = "07-05"; // Enter your WIFI SSID
char pass[] = "tido@1983"; // Enter your WIFI Password

DHT dht(D4, DHT11); // (DHT sensor pin, sensor type) D4 DHT11 Temperature
Sensor
BlynkTimer timer;

// Define component pins


#define SOIL_PIN A0 // A0 Soil Moisture Sensor
#define RELAY_PIN_1 D3 // D3 Relay
#define PUSH_BUTTON_1 D7 // D7 Button
#define VPIN_BUTTON_1 V12

int relay1State = LOW;


int pushButton1State = HIGH;

// Create variables for sensor values


double T, P;
char status;

void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();

pinMode(RELAY_PIN_1, OUTPUT);
digitalWrite(RELAY_PIN_1, relay1State);

Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);


dht.begin();

lcd.setCursor(0, 0);
lcd.print(" Initializing ");
for (int a = 5; a <= 10; a++) {
lcd.setCursor(a, 1);
lcd.print(".");
delay(500);
}
lcd.clear();
lcd.setCursor(9, 1);
lcd.print("W:OFF");

// Call the functions periodically


timer.setInterval(1000L, soilMoistureSensor); // Changed to 1000 ms to
avoid flooding
timer.setInterval(2000L, DHT11sensor); // Changed to 2000 ms
timer.setInterval(500L, checkPhysicalButton);
}

// Get the DHT11 sensor values


void DHT11sensor() {
float h = dht.readHumidity();
float t = dht.readTemperature();

if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

Blynk.virtualWrite(V0, t);
Blynk.virtualWrite(V1, h);

lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(t);
lcd.setCursor(8, 0);
lcd.print("H:");
lcd.print(h);
}

// Get the soil moisture values


void soilMoistureSensor() {
int value = analogRead(SOIL_PIN);
value = map(value, 0, 1024, 0, 100);
value = (value-100)*-1; // Invert value to represent moisture
Blynk.virtualWrite(V3, value);
lcd.setCursor(2, 1);
lcd.print("S:");
lcd.print(value);
lcd.print(" "); // Add a percentage symbol
}
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(VPIN_BUTTON_1);
}

BLYNK_WRITE(VPIN_BUTTON_1) {
relay1State = param.asInt();
digitalWrite(RELAY_PIN_1, relay1State);
}

// Check the physical button state and toggle the relay


void checkPhysicalButton()
{
if (digitalRead(PUSH_BUTTON_1) == LOW) {// pushButton1State is used to
avoid sequential toggles
if (pushButton1State != LOW) {

// Toggle Relay state


relay1State = !relay1State;
digitalWrite(RELAY_PIN_1, relay1State);

// Update Button Widget


Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
}
pushButton1State = LOW;
} else {
pushButton1State = HIGH;
}
}

void loop() {
if (relay1State == HIGH) {
lcd.setCursor(9, 1);
lcd.print("W:ON ");
} else {
lcd.setCursor(9, 1);
lcd.print("W:OFF");
}

Blynk.run(); // Run the Blynk library


timer.run(); // Run the Blynk timer
}

You might also like