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

Esp 8266

Uploaded by

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

Esp 8266

Uploaded by

tranthienbao789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#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() {
[Link](9600);
[Link]();
[Link]();

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

[Link](auth, ssid, pass, "[Link]", 80);


[Link]();

[Link](0, 0);
[Link](" Initializing ");
for (int a = 5; a <= 10; a++) {
[Link](a, 1);
[Link](".");
delay(500);
}
[Link]();
[Link](9, 1);
[Link]("W:OFF");

// Call the functions periodically


[Link](1000L, soilMoistureSensor); // Changed to 1000 ms to
avoid flooding
[Link](2000L, DHT11sensor); // Changed to 2000 ms
[Link](500L, checkPhysicalButton);
}

// Get the DHT11 sensor values


void DHT11sensor() {
float h = [Link]();
float t = [Link]();

if (isnan(h) || isnan(t)) {
[Link]("Failed to read from DHT sensor!");
return;
}

[Link](V0, t);
[Link](V1, h);

[Link](0, 0);
[Link]("T:");
[Link](t);
[Link](8, 0);
[Link]("H:");
[Link](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
[Link](V3, value);
[Link](2, 1);
[Link]("S:");
[Link](value);
[Link](" "); // Add a percentage symbol
}
BLYNK_CONNECTED() {
// Request the latest state from the server
[Link](VPIN_BUTTON_1);
}

BLYNK_WRITE(VPIN_BUTTON_1) {
relay1State = [Link]();
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


[Link](VPIN_BUTTON_1, relay1State);
}
pushButton1State = LOW;
} else {
pushButton1State = HIGH;
}
}

void loop() {
if (relay1State == HIGH) {
[Link](9, 1);
[Link]("W:ON ");
} else {
[Link](9, 1);
[Link]("W:OFF");
}

[Link](); // Run the Blynk library


[Link](); // Run the Blynk timer
}

You might also like