LCD i2c DHT11 Soil Moisture Sensor Webserver Esp8266 - Smart Garden & Weather
https://www.tinkercad.com/things/l1jBzTPvEjJ-soil-moisture-sensor-with-lcd-fajar-himawan
/*******************************************************************
BOARD ESP 2.5.0
ArduinoJson versi 5.13.5
*******************************************************************/
// The version of ESP8266 core needs to be 2.5 or higher
// or else your bot will not connect.
// ----------------------------
// Standard ESP8266 Libraries
// ----------------------------
// ----------------------------
// Additional Libraries - each one of these will need to be installed.
// ----------------------------
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ArduinoOTA.h>
#define SCLPIN D1
#define SDAPIN D2
#define DHTPIN D5
#define relay1 D6
#define relay2 D7
#define DHTTYPE DHT11 // DHT 11
//LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);
// Initialize Wifi connection to the router
char ssid[] = = "wificarjurnalku"; // diisi nama wifi
char password[] = "12345678"; // diisi password wifi
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Initialize Telegram BOT
#define BOTtoken "6600660280:AAFgfmLn8TAAkvdamx_4SRMqIe1HshxlgwI" // diisi Token Bot (Dapat dari Telegram Botfather)
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
//Checks for new messages every (default) 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i=0; i<numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "") from_name = "Guest";
//Menyalakan dan Mematikan Lampu Background LCD
if (text == "/lcdon") {
lcd.backlight();
bot.sendMessage(chat_id, "Backlight LCD ON", "");
}
if (text == "/lcdoff") {
lcd.noBacklight();
bot.sendMessage(chat_id, "Backlight LCD OFF", "");
}
//Cek Pembacaan Sensor DHT11
if (text == "/ceksuhu") {
int t = dht.readTemperature()-0;
int h = dht.readHumidity();
String temp = "Suhu saat ini : ";
temp += int(t);
temp +=" *C\n";
temp +="Kelembaban: ";
temp += int(h);
temp += " %";
bot.sendMessage(chat_id,temp, "");
}
//Kontrol Modul Relay (nyala Pompa)
if (text == "/pumpon") {
digitalWrite(relay1, LOW);
bot.sendMessage(chat_id, "Pompa sudah nyala", "");
}
if (text == "/pumpoff") {
digitalWrite(relay1, HIGH);
bot.sendMessage(chat_id, "Pompa sudah mati", "");
}
//Kontrol Modul Relay (doorlock aktif)
if (text == "/dooropen") {
digitalWrite(relay2, LOW);
bot.sendMessage(chat_id, "Doorlock On (terbuka)", "");
}
if (text == "/doorlock") {
digitalWrite(relay2, HIGH);
bot.sendMessage(chat_id, "Doorlock Off (terkunci)", "");
}
//Cek Command untuk setiap aksi
if (text == "/start") {
String welcome = "Welcome " + from_name + ".\n";
welcome += "/lcdoff : Mematikan backlight LCD\n";
welcome += "/lcdon : Menyalakan backlight LCD\n";
welcome += "/ceksuhu : Cek suhu ruangan\n";
welcome += "/pumpon : Nyalakan pompa\n";
welcome += "/pumpoff : Matikan pompa\n";
welcome += "/dooropen : Doorlock On\n";
welcome += "/doorlock : Doorlock Off\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
void setup() {
//pinMode(led, OUTPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
// digitalWrite(led, HIGH); // turn off the led (inverted logic!)
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
Serial.begin(115200);
dht.begin();
Wire.begin(SDAPIN, SCLPIN);
lcd.begin(16,2);
//lcd.init();
lcd.backlight();
/*
lcd.setCursor(0, 0);
lcd.print("Smart Garden");
lcd.setCursor(0, 1);
lcd.print("Pertanian Pintar");
*/
// This is the simplest way of getting this working
// if you are passing sensitive information, or controlling
// something important, please either use certStore or at
// least client.setFingerPrint
client.setInsecure();
// Set WiFi to station mode and disconnect from an AP if it was Previously
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
// attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
lcd.setCursor(0, 0);
lcd.print("Connecting...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
/*
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Smart Garden");
lcd.setCursor(0, 1);
lcd.print("Pertanian Pintar");
*/
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Connected");
lcd.setCursor(0,1);
lcd.print(WiFi.localIP());
delay(3000);
lcd.clear();
ArduinoOTA.begin();
}
void loop() {
int t = dht.readTemperature()-0;
int h = dht.readHumidity();
lcd.setCursor(0,0);
lcd. print("TEMPERATUR:");
lcd.setCursor(13,0);
lcd.print(t);
lcd.setCursor(15,0);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("HUMIDITY :");
lcd.setCursor(13,1);
lcd.print(h);
lcd.setCursor(15,1);
lcd.print("%");
if (millis() > lastTimeBotRan + botRequestDelay) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while(numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotRan = millis();
}
ArduinoOTA.handle();
}
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h" // DHT11 temperature and humidity sensor Predefined library
#include <ArduinoOTA.h>
#define DHTTYPE DHT11 // DHT 11
#define dht_dpin 0 //GPIO-0 D3 pin of nodemcu
int Raw = A0; //Analog channel A0 as used to measure temperature
int threshold = 16; //Nodemcu digital pin water sensor read-GPIO16---D0 of NodeMCU
int Solenoid = D7; // GPIO13---D7 of NodeMCU--Motor connection
const char* ssid = "qulilhamdulillah";
const char* password = "12345678";
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(dht_dpin, DHTTYPE);
WiFiServer server(80);
void setup(void)
{
dht.begin();
Serial.begin(9600);
delay(10);
pinMode(threshold,INPUT_PULLUP); //Pin#13 as output-Activate pullup at pin 13
pinMode(Solenoid, OUTPUT); //D7 as output
digitalWrite(Solenoid, LOW); //Deactivate Solenoid
// Connect to WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password); //Begin WiFi
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address on serial monitor
Serial.print("Use this URL to connect: ");
Serial.print("http://"); //URL IP to be typed in mobile/desktop browser
Serial.print(WiFi.localIP());
Serial.println("/");
// initialize the LCD
// lcd.begin(16, 2);
// lcd.print("Smart Garden");
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Smart Garden");
lcd.setCursor(0, 1);
lcd.print("Pertanian Pintar");
ArduinoOTA.begin();
}
void handleTampilanLCD() {
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read humidity (percent)
float h = dht.readHumidity();
float percentage2 = 0.0; // Calculating percentage of moisture
float reading = 0.0; //Analog channel moisture read
reading = analogRead(Raw); //Analog pin reading output voltage by water moisture rain sensor
percentage2 = (reading/1024) * 100; //Converting the raw value in percentage
// Read soil moisture
//int sm = analogRead(Raw);
/*
// Match the request
int value = LOW;
// coding pompa otomatis varian 2
if (percentage2>=80){ // If less mositure in soil start the motor otherwise stop
digitalWrite(Solenoid, HIGH);
value = HIGH;
delay(5000);
digitalWrite(Solenoid, LOW);
value = LOW;
delay(1);
}
else
{
digitalWrite(Solenoid, LOW);
value = LOW;
}
*/
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Clear the LCD
lcd.clear();
// Print temperature and humidity to the LCD
lcd.setCursor(0, 0);
lcd.print("");
lcd.print(t);
lcd.print((char)223);
lcd.print("C ");
//lcd.setCursor(0, 1);
lcd.print("");
lcd.print(h);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Tanah:");
lcd.print(percentage2);
lcd.print("%");
}
void loop() {
ArduinoOTA.handle();
handleTampilanLCD();
// Wait for ... seconds
delay(1000);
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
float h =0.0; //Humidity level
float t =0.0; //Temperature in celcius
float f =0.0; //Temperature in fahrenheit
float percentage = 0.0; // Calculating percentage of moisture
float reading = 0.0; //Analog channel moisture read
// Match the request
int value = LOW;
if (request.indexOf("/Up=ON") != -1) {
h = dht.readHumidity(); //Read humidity level
t = dht.readTemperature(); //Read temperature in celcius
f = (h * 1.8) + 32; //Temperature converted to Fahrenheit
reading = analogRead(Raw); //Analog pin reading output voltage by water moisture rain sensor
percentage = (reading/1024) * 100; //Converting the raw value in percentage
// coding pompa otomatis varian 1
/*
if (reading<=110){ // If less mositure in soil start the motor otherwise stop
digitalWrite(Solenoid, HIGH);
value = HIGH;
}
else {
digitalWrite(Solenoid, LOW);
value = LOW;
}
*/
// coding pompa otomatis varian 2
/*
if (percentage2>=70){ // If less mositure in soil start the motor otherwise stop
digitalWrite(Solenoid, HIGH);
value = HIGH;
}
else {
digitalWrite(Solenoid, LOW);
value = LOW;
}
*/
// Match the request
int value = LOW;
// coding pompa otomatis varian 2
if (percentage>=99){ // If less mositure in soil start the motor otherwise stop
digitalWrite(Solenoid, HIGH);
value = HIGH;
delay(5000);
digitalWrite(Solenoid, LOW);
value = LOW;
delay(1);
}
else
{
digitalWrite(Solenoid, LOW);
value = LOW;
}
/*
// Print to the LCD
lcd.setCursor(0, 0);
//lcd.print("Suhu:");
lcd.print(t);
lcd.print((char)223);
lcd.print("C ");
lcd.print(h);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Tanah:");
lcd.print(percentage);
lcd.print("% ");
//lcd.print(" % Pump:");
//lcd.print(value == HIGH ? "ON " : "OFF");
}
// Add running text animation
for (int positionCounter = 0; positionCounter < 9; positionCounter++) {
lcd.scrollDisplayLeft();
delay(500);
}
// After the text has been displayed to the end, it will move to the right
for (int positionCounter = 0; positionCounter < 9; positionCounter++) {
lcd.scrollDisplayRight();
delay(500);
}
*/
}
if (request.indexOf("/Solenoid=ON") != -1) { //Motor ON
digitalWrite(Solenoid, HIGH);
value = HIGH;
}
if (request.indexOf("/Solenoid=OFF") != -1) { //Motor OFF
digitalWrite(Solenoid, LOW);
value = LOW;
}
/*
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<h1 align=center>Smart Garden - Weather Station</h1><br><br>");
client.println("<h1 align=center>Jurnalku by Fajar Himawan</h1><br><br>");
client.print("Temperature in Celsius =");
client.println(t);
client.println("<br>");
client.print("Temperature in Fahrenheit =");
client.println(f);
client.println("<br>");
client.print("Humidity =");
client.println(h);
client.print(" %");
client.println("<br>");
client.println();
client.print("Moisture Level Percentage =");
client.print(percentage);
client.print("%");
if(digitalRead(threshold)==HIGH){ // Read digital output of soil sensor
client.println("Threshold Reached = Rain detected / Moisture exceeded / Water detected");
}
client.println("<br><br>");
if(value == HIGH)
client.println("Motor/Pump Operational");
else
client.print("Motor/Pump at Halt");
*/
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// Add some CSS for a more elegant look
client.println("<head>");
client.println("<style>");
client.println("body {font-family: Arial, Helvetica, sans-serif; font-size: 40px;}");
client.println(".container {width: 90%; margin: auto; padding: 20px;}");
client.println(".data {margin: 10px 0;}");
client.println("</style>");
client.println("</head>");
client.println("<body>");
client.println("<div class='container'>");
client.println("<h1 align=center>Smart Garden - Weather Station</h1>");
client.println("<h2 align=center>Jurnalku by Fajar Himawan</h2>");
client.println("<div class='data'>");
client.print("<strong>Temperature in Celsius:</strong> ");
client.println(t);
client.println("</div>");
client.println("<div class='data'>");
client.print("<strong>Temperature in Fahrenheit:</strong> ");
client.println(f);
client.println("</div>");
client.println("<div class='data'>");
client.print("<strong>Humidity:</strong> ");
client.println(h);
client.println("%</div>");
client.println("<div class='data'>");
client.print("<strong>Moisture Level Percentage:</strong> ");
client.print(percentage);
client.println("%</div>");
if(digitalRead(threshold)==HIGH){ // Read digital output of soil sensor
client.println("<div class='data'>");
client.println("<strong>Threshold Reached:</strong> Rain detected / Moisture exceeded / Water detected");
client.println("</div>");
}
client.println("<div class='data'>");
if(value == HIGH)
client.println("<strong>Motor/Pump Status:</strong> Operational");
else
client.println("<strong>Motor/Pump Status:</strong> Halt");
/*
client.println("<br><br>");
client.println("<a href=\"/Up=ON\"\"><button>Update = Temperature Humidity Moisture Values</button></a><br />");
client.println("<a href=\"/Solenoid=ON\"\"><button>Motor Pump On </button></a>");
client.println("<a href=\"/Solenoid=OFF\"\"><button>Motor Pump Off </button></a><br />");
client.println("</html>");
*/
client.println("</div><br><br>");
client.println("<div style=\"display: flex; justify-content: center;\"><br><br>");
client.println("<a href=\"/Up=ON\"\"><button style=\"width: 500px; height: 200px; font-size: 40px; margin-left: 5px; padding: 10px;\">Update = Temperature Humidity Moisture Values</button></a><br />");
client.println("</div><br><br>");
client.println("<div style=\"display: flex; justify-content: center;\"><br><br>");
client.println("<a href=\"/Solenoid=ON\"\"><button style=\"width: 250px; height: 200px; font-size: 40px; margin-left: 5px; padding: 10px;\">Motor Pump ON </button></a>");
//client.println("</div><br><br>");
client.println("<div style=\"display: flex; justify-content: center;\"><br><br>");
client.println("<a href=\"/Solenoid=OFF\"\"><button style=\"width: 250px; height: 200px; font-size: 40px; margin-left: 5px; padding: 10px;\">Motor Pump OFF </button></a><br />");
client.println("</div></html>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}
No comments