Smart Garden - Soil Moisture Sensor - Jurnalku by Fajar Himawan
Sensor - Smart Garden
Model 4 - Soil Moisture
Sensor - Smart Garden
Model 5:
Soil Moisture dan LCD i2c
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h" // DHT11 temperature and humidity sensor Predefined library
#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 = "irigasiBOt";
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");
}
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() {
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.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");
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>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}
#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