You are on page 1of 28

Institute of Space Technology

Progress Report

Submitted By:
Muhammad Umair 170401022
Abdullah Waraitch 170401020

Submitted to:
Sir Bilal khan
Sir Ghayas Udin
Section: EE-16-B

BE-Electrical Engineering.
IoT Based Agriculture Monitoring System
Objective:
• To take Sensors data from nodes
• Reduction of power consumption of Nodemcu
• Measurement of Current rating of different components
• Coding on Arduino IDE Software
• observe data collected by the nodemcu ESP 8266 over the internet
Procedure:
Data from nodes:
We have connected Dht11 and Soil Moisture Sensor to NodeMcu-Esp8266
module. We powered the module by connecting it to 5v supply. Then we write
code for Dht11 and Soil moisture in Arduino IDE Software.We upload this code to
Nodemcu module by using this software.When we power the module we able to
see sensors data on serial monitor.
Reduction of power consumption
The ESP8266 power consumption is between 15μA and 400mA depending on
different cases. In idle state with powered Wi-Fi the current consumption around
70mA.With an operating voltage of 3.3V a NodeMCU V2 needs the following
power in idle state:
W = U * I = 3.3V * 70mA = 231mW
If we buy a battery with 1000mAh (Operation voltage: 3.7V), how long will it take
until we must change the battery pack?
T = 1000mAh / 70mA = 14.3h
So we must change the battery every day. That does not make sense.
We have two options to extend the time to charge the battery pack.
• Buy a battery with a higher capacity.
• Reduce the power consumption of the ESP8266 microcontroller.
Deep sleep Mode:
To enable the deep-sleep mode we only must use the following line of code:

// ESP.deepSleep(uint32 time_in_us);

where we define a time, when the ESP8266 will wake up from the deep-sleep
mode. The maximum time in deep-sleep is 4,294,967,295 μs, which is about ~71
minutes.
In this mode, the chip will turn off Wi-Fi connectivity and data connection; only
the RTC module is still working, responsible for periodic wake-ups.
Experimental Measurement:

NodeMcu esp8266

Mode Current rating


Normal 70.81 mA
Deep Sleep 1.686 mA

DHT11 Sensor

Mode Current rating


Normal 2.31 mA
Deep Sleep 2.28 mA

Soil Moisture Sensor


Mode Current rating
Normal 3.56 mA
Deep Sleep 3.554 mA

Overall Agriculture Monitoring System


Mode Current rating
Normal 78.65 mA
Deep Sleep 7.4299 mA
If we have external battery with 1000mAh will last a lot longer.
T = 1000mAh / 7.429mA
T = 134.6076h
T = 5.6 days
Arduino IDE Coding:
Arduino IDE is an open source programming which is basically used to write &
compile code using a module that is Arduino. This is an official programming
software which makes compiling of code simple so a typical man can understand
the learning procedure. Here we use this software for coding of our module. The
code which we have uploaded is given below:
Output of Serial Monitor:

Conclusion:
By using deep sleep mode we have extended the time to charge battery. By normal
working of nodemcu, we must recharge battery after 14 hours but by using deep
sleep mode we can charge it after a week if we have 1000mAh of battery.If we
have a battery with high capacity then it can work for months or a year.
Software Part
To observe data collected by the node mcu ESP 8266 over the internet and on the
mobile application as well.
Procedure:
First, we used the usb to connect the node mcu with laptop to power the node mcu
and insert the code so it could identify the sensors attached to the node mcu. Code
is as follows:
#include <ESP8266WiFi.h>
#include "DHT.h"
#define DHTPIN D1
#define DHTTYPE DHT11

const char* ssid = "mabw17";


const char* password = "12345678";
const char* host = "mabw17.000webhostapp.com";
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
delay(100);
dht.begin();
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Netmask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
}
void loop() {
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}

String url = "/api/weather/insert.php?temp=" + String(t) + "&hum="+ String(h);


Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(1000);

while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
delay(3000);
}
In this code we directed the node mcu to send the data to the webhosting site where
we will the save the data and further we will use this data to display on the mobile
application as well. Currently the node mcu collects data after 3 secs we will
change this to single reading per hour.
This is our web host where we are saving the data collected.
Link to the website is
“https://www.000webhost.com/members/website/mabw17/dashboar
d”

Further these are screenshots of where and how it collects the data and
saves it. Ill add codes as well.
These are the APIS where all the codes that were requires by the node mcu in order
to connect to this web hosting server and after connecting sending data to it. These
are coded in the way like if any needs the passwords it will the file named
db_config.php. separate files are doing separate work and interconnecting through
function calling wherever one needs another.
Here are the codes of each file shown above in the Snapshot.
db_connect.php

<?php

class DB_CONNECT {

// Constructor
function __construct() {
// Trying to connect to the database
$this->connect();
}
// Destructor
function __destruct() {
// Closing the connection to database
$this->close();
}

// Function to connect to the database


function connect() {

//importing dbconfig.php file which contains database credentials


$filepath = realpath (dirname(__FILE__));

require_once($filepath."/dbconfig.php");

// Connecting to mysql (phpmyadmin) database


$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or
die(mysql_error());

// Selecing database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or
die(mysql_error());

// returing connection cursor


return $con;
}
// Function to close the database
function close() {
// Closing data base connection
mysql_close();
}

?>

db_config.php

<?php

define('DB_USER', "id16525364_mabw17299"); // Your database user name


define('DB_PASSWORD', ">\9YBFs0IY_dgPiI"); // Your database
password (mention your db password here)
define('DB_DATABASE', "id16525364_mabw17"); // Your database name
define('DB_SERVER', "localhost"); // db server (Mostly will be
'local' host)

?>

delete.php

<?php

header('content-type: application/json; charset=utf-8');


header("access-control-allow-origin: *");

//Creating Array for JSON response


$response = array();

// Check if we got the field from the user


if (isset($_GET['id'])) {
$id = $_GET['id'];

// Include data base connect class


$filepath = realpath (dirname(__FILE__));
require_once($filepath."/db_connect.php");

// Connecting to database
$db = new DB_CONNECT();

// Fire SQL query to delete weather data by id


$result = mysql_query("DELETE FROM weather WHERE id = $id");

// Check for succesfull execution of query


if (mysql_affected_rows() > 0) {
// successfully deleted
$response["success"] = 1;
$response["message"] = "Data successfully deleted";

// Show JSON response


echo json_encode($response);
} else {
// no matched id found
$response["success"] = 0;
$response["message"] = "No weather data found by given id";

// Echo the failed response


echo json_encode($response);
}
} else {
// If required parameter is missing
$response["success"] = 0;
$response["message"] = "Parameter(s) are missing. Please check the request";

// Show JSON response


echo json_encode($response);
}
?>

insert.php

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//Creating Array for JSON response
$response = array();

// Check if we got the field from the user


if (isset($_GET['temp']) && isset($_GET['hum'])) {

$temp = $_GET['temp'];
$hum = $_GET['hum'];

// Include data base connect class


$filepath = realpath (dirname(__FILE__));
require_once($filepath."/db_connect.php");

// Connecting to database
$db = new DB_CONNECT();

// Fire SQL query to insert data in weather


$result = mysql_query("INSERT INTO weather(temp,hum)
VALUES('$temp','$hum')");

// Check for succesfull execution of query


if ($result) {
// successfully inserted
$response["success"] = 1;
$response["message"] = "Weather successfully created.";

// Show JSON response


echo json_encode($response);
} else {
// Failed to insert data in database
$response["success"] = 0;
$response["message"] = "Something has been wrong";

// Show JSON response


echo json_encode($response);
}
} else {
// If required parameter is missing
$response["success"] = 0;
$response["message"] = "Parameter(s) are missing. Please check the request";

// Show JSON response


echo json_encode($response);
}
?>

readall.php

<<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//Creating Array for JSON response
$response = array();

// Include data base connect class


$filepath = realpath (dirname(__FILE__));
require_once($filepath."/db_connect.php");
// Connecting to database
$db = new DB_CONNECT();

// Fire SQL query to get all data from weather


$result = mysql_query("SELECT *FROM weather") or die(mysql_error());

// Check for succesfull execution of query and no results found


if (mysql_num_rows($result) > 0) {

// Storing the returned array in response


$response["weather"] = array();

// While loop to store all the returned response in variable


while ($row = mysql_fetch_array($result)) {
// temperoary user array
$weather = array();
$weather["id"] = $row["id"];
$weather["temp"] = $row["temp"];
$weather["hum"] = $row["hum"];
// Push all the items
array_push($response["weather"], $weather);
}
// On success
$response["success"] = 1;

// Show JSON response


echo json_encode($response);
}
else
{
// If no data is found
$response["success"] = 0;
$response["message"] = "No data on weather found";
// Show JSON response
echo json_encode($response);
}
?>

specific.php

?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//Creating Array for JSON response
$response = array();

// Include data base connect class


$filepath = realpath (dirname(__FILE__));
require_once($filepath."/db_connect.php");
// Connecting to database
$db = new DB_CONNECT();

// Check if we got the field from the user


if (isset($_GET["id"])) {
$id = $_GET['id'];

// Fire SQL query to get weather data by id


$result = mysql_query("SELECT *FROM weather WHERE id = '$id'");
//If returned result is not empty
if (!empty($result)) {
// Check for succesfull execution of query and no results found
if (mysql_num_rows($result) > 0) {

// Storing the returned array in response


$result = mysql_fetch_array($result);

// temperoary user array


$weather = array();
$weather["id"] = $result["id"];
$weather["temp"] = $result["temp"];
$weather["hum"] = $result["hum"];

$response["success"] = 1;
$response["weather"] = array();

// Push all the items


array_push($response["weather"], $weather);

// Show JSON response


echo json_encode($response);
} else {
// If no data is found
$response["success"] = 0;
$response["message"] = "No data on weather found";

// Show JSON response


echo json_encode($response);
}
} else {
// If no data is found
$response["success"] = 0;
$response["message"] = "No data on weather found";

// Show JSON response


echo json_encode($response);
}
} else {
// If required parameter is missing
$response["success"] = 0;
$response["message"] = "Parameter(s) are missing. Please check the request";

// echoing JSON response


echo json_encode($response);
}
?>

table.php

<?php
// Username is root
$user = 'id16525364_mabw17299';
$password = '>\9YBFs0IY_dgPiI';

// Database name is gfg


$database = 'id16525364_mabw17';

// Server is localhost with


// port number 3308
$servername='localhost';
$mysqli = new mysqli($servername, $user,
$password, $database);

// Checking for connections


if ($mysqli->connect_error) {
die('Connect Error (' .
$mysqli->connect_errno . ') '.
$mysqli->connect_error);
}

// SQL query to select data from database


$sql = "SELECT * FROM weather ";
$result = $mysqli->query($sql);
$mysqli->close();
?>
// HTML code to display data in tabular format
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>GFG User Details</title>
<!-- CSS FOR STYLING THE PAGE -->
<style>
table {
margin: 0 auto;
font-size: large;
border: 1px solid black;
}
h1 {
text-align: center;
color: #006600;
font-size: xx-large;
font-family: 'Gill Sans', 'Gill Sans MT',
' Calibri', 'Trebuchet MS', 'sans-serif';
}

td {
background-color: #E4F5D4;
border: 1px solid black;
}

th,
td {
font-weight: bold;
border: 1px solid black;
padding: 10px;
text-align: center;
}

td {
font-weight: lighter;
}
</style>
</head>

<body>
<section>
<h1>mabw17</h1>
<!-- TABLE CONSTRUCTION-->
<table>
<tr>
<th>id</th>
<th>temp</th>
<th>hum</th>

</tr>
<!-- PHP CODE TO FETCH DATA FROM ROWS-->
<?php // LOOP TILL END OF DATA
while($rows=$result->fetch_assoc())
{
?>
<tr>
<!--FETCHING DATA FROM EACH
ROW OF EVERY COLUMN-->
<td><?php echo $rows['Id'];?></td>
<td><?php echo $rows['temp'];?></td>
<td><?php echo $rows['hum'];?></td>

</tr>
<?php
}
?>
</table>
</section>
</body>

</html>

update.php

<
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//Creating Array for JSON response
$response = array();

// Check if we got the field from the user


if (isset($_GET['id']) && isset($_GET['temp'])) {
$id = $_GET['id'];
$temp= $_GET['temp'];

// Include data base connect class


$filepath = realpath (dirname(__FILE__));
require_once($filepath."/db_connect.php");
// Connecting to database
$db = new DB_CONNECT();

// Fire SQL query to update weather data by id


$result = mysql_query("UPDATE weather SET temp= '$temp' WHERE id =
'$id'");

// Check for succesfull execution of query and no results found


if ($result) {
// successfully updation of temp (temperature)
$response["success"] = 1;
$response["message"] = "Weather Data successfully updated.";

// Show JSON response


echo json_encode($response);
} else {

}
} else {
// If required parameter is missing
$response["success"] = 0;
$response["message"] = "Parameter(s) are missing. Please check the request";

// Show JSON response


echo json_encode($response);
}
?>

Here is the snapshot our data base myphpadmin where them data gets stored in the
query named weather.
In code files there is a code file named Table.php which allows us to the showcase
the data over the html page here is the link to the page
https://mabw17.000webhostapp.com/api/weather/table.php?

Conclusion
Basically we had covered the objective in which we had to collect and showcase
the data collected over the internet. Now, the part in which in which we have to
showcase the data in the mobile application in the progress and we are facing
problems. First one is we were unable to make connection between the app and our
database. We have found few solutions over the internet and we are working on
them to eradicate this issue and complete our project.

You might also like