45.iot - Based Smart Irrigation System Using Reinforcement Learningdocx
45.iot - Based Smart Irrigation System Using Reinforcement Learningdocx
CHAPTER-8: IMPLEMENTATION
8.1 Modules 20
8.1.1 Module Description 20
CHAPTER-9: SOFTWARE ENVIRONMENT
9.1 Python 21-61
9.2 Source Code 62-68
CHAPTER-10: RESULTS/DISCUSSIONS
10.1 System Testing 69-72
10.1.1 Test Cases 72
10.2 Screen shots 73-77
CHAPTER-11: CONCLUSION
11.1 Conclusion 78
11.2 Future Scope 78
CHAPTER-12: REFERENCES 79
LIST OF FIGURES
1 System Architecture 11
2 UML Diagrams 11-17
2.8 ER Diagram 15
ABSTRACT
INTRODUCTION
Traditional irrigation systems often rely on fixed schedules, leading to over-irrigation
or under-irrigation based on fluctuating environmental conditions. This project
introduces a dynamic solution by incorporating IoT sensors to monitor soil moisture,
weather conditions, and other relevant parameters. Reinforcement Learning
algorithms are employed to optimize irrigation decisions, learning from feedback
loops to adjust watering schedules for maximum efficiency and crop yield.
1
CHAPTER-2
LITERATURE SURVEY
TITLE:Smart Irrigation System Using Machine Learning and IOT
ABSTRACT:In the urban areas, such as developed cities, the citizens have all the
basic amenities like Electricity supply with minimal number of power cuts, Food
supply, comparatively proper roads and infrastructure of buildings, etc. But same is
not the case with rural areas, where majority of villages suffer electricity shortage,
agricultural issues, improper supply and distribution of water for various purposes, etc.
Hence through our project, Now, there are numerous factors that can influence the
formation of a smart village. In this main focus on Automatic Irrigation System,
followed by an appropriate rainfall prediction Algorithm that can help us determine
which crops are favorable to grow in a particular area. A new method which is
Romyan's method that crops need to be watered for plants and time required for motor
to be ON is also introduced. Also by saving water and electricity which is normally
wasted in watering crops, we can utilize it for other necessary purposes for the
residents of villages.
TITLE:An IoT based Smart Irrigation Management System using Reinforcement
Learning modeled through a Markov Decision Process
2
setting. Conducted experiments show the water and energy saving by the use of the
proposed Smart Irrigation system.
ABSTRACT:In this study, we propose a smart plant irrigation IoT system that
autonomously adapts itself to a defined irrigation habit. The automated plant irrigation
systems generally make decisions based on static models derived from the plant's
characteristics. In contrast, in our proposed solution, irrigation decisions are
dynamically adjusted based on the changing environmental conditions. The learning
mechanism of the model reveals the mathematical connections of the environmental
variables used in the determination of the irrigation habit and progressively enhances
its learning procedure as the irrigation data accumulates in the model. We evaluated
the success of our irrigation model with four different supervised machine learning
algorithms and adapted the Gradient Boosting Regression Trees(GBRT) method in
our IoT solution. We established a test bed for the sensor edge, mobile client, and the
decision service on the cloud to analyze the overall system performance. The early
results from our prototype system that is tested with two indoor plants; namely
Sardinia and Peace-lily are very encouraging. The results reveal that the proposed
system can learn the irrigation habits of different plants successfully.
3
photovoltaic panels as a power source. A voltage constant and current measured by
the ACS712 sensor, we have measured the power and energy of the solar panels every
5 min. These measures will be shown on the Node-RED platform and stored as a
database in the SQLite programming language SQLite is introduced to reduce the
database complexity. Because of this database, we can make accurate projections
about water requirements and soil moisture. The last part consists of commanding our
system by the best method(algorithm) of prediction for our case. Theratio of the
reserved water was predicted with the use of machine learning (a model decision tree),
which enabled us to generate these forecasts. By these forecasts command the valve.
In practice, we use an electronic card that can support this type of machine learning
algorithm. For this, we used the Raspberry pi card. Node-RED is the most suitable
interface to apply this algorithm also it allows us to monitor in real-time with the
laptop(local) and with the smartphone(4G) all measured by a dashboard. The IP
address of raspberry needs with port1880 requires. This approach allows us to manage
our system in a more efficient, automated, and intelligent manner.
4
conserve resources. Through these advanced systems effective soil and weather
monitoring takes place along with efficient water management. Irrigation systems
have been determined as positive contributor toward optimized irrigation systems that
could enhance the use of continuous research and development which focus on
enhancing the sustainable operations and cost reduction. Lastly, the challenges and
benefits for the implementation of sensory based irrigation systems are discussed.
This review will assist researchers and farmers to better understand irrigation
techniques and provide an adequate approach would be sufficient to carry out
irrigation related activities.
5
CHAPTER-3
SYSTEM ANALSIS
3.1 EXISTING SYSTEM
Conventional irrigation systems may lack adaptability to changing environmental
factors, resulting in water wastage, increased operational costs, and potential
negative impacts on crop health. Fixed schedules or rule-based approaches might not
account for real-time conditions, leading to suboptimal irrigation practices.
DISADVANATGES
The existing irrigation systems, although effective to some extent, often come
with certain disadvantages that can be addressed with the implementation of IoT-
based smart irrigation systems using reinforcement learning.
Conventional systems often lack precise control over water distribution, leading
to inefficient use of water resources. This can result in over-watering certain areas
while leaving others under-watered, impacting crop health and water
conservation efforts.
By integrating IoT technologies and reinforcement learning into irrigation
systems, these disadvantages can be mitigated. IoT sensors can provide real-time
data on soil moisture, weather conditions, and crop health, enabling more precise
and timely irrigation decisions. Reinforcement learning algorithms can
continuously learn and optimize irrigation strategies based on feedback from the
environment, helping to improve water efficiency, crop yields, and resource
utilization.
6
ADVANTAGES
The system utilizes IoT sensors to gather real-time data on soil moisture levels,
weather conditions, and crop requirements. This information enables precise
irrigation scheduling tailored to the specific needs of each crop and field area,
leading to optimal water usage.
By accurately monitoring soil moisture and environmental factors, the system can
significantly reduce water wastage by avoiding over-watering or under-watering.
This not only conserves water resources but also minimizes energy consumption
and fertilizer runoff, contributing to environmental sustainability.
7
CHAPTER-4
SYSTEM REQUIREMENTS
System : i3 or above.
Ram : 4 GB.
Hard Disk : 40 GB
8
CHAPTER-5
SYSTEM STUDY
5.2 FEASIBILITYANALYSIS
Three key considerations involved in the feasibility analysis are
ECONOMICAL FEASIBILITY
TECHNICAL FEASIBILITY
SOCIAL FEASIBILITY
ECONOMICAL FEASIBILITY
This study is carried out to check the economic impact that the system will
have on the organization. The amount of fund that the company can pour into the
research and development of the system is limited. The expenditures must be justified.
Thus the developed system as well within the budget and this was achieved because
most of the technologies used are freely available. Only the customized products had
to be purchased.
TECHNICAL FEASIBILITY
This study is carried out to check the technical feasibility, that is, the
technical requirements of the system. Any system developed must not have a high
demand on the available technical resources. This will lead to high demands on the
available technical resources. This will lead to high demands being placed on the
client. The developed system must have a modest requirement, as only minimal or
null changes are required for implementing this system.
9
SOCIAL FEASIBILITY
The aspect of study is to check the level of acceptance of the system by the user.
This includes the process of training the user to use the system efficiently. The user
must not feel threatened by the system, instead must accept it as a necessity. The level
of acceptance by the users solely depends on the methods that are employed to
educate the user about the system and to make him familiar with it. His level of
confidence must be raised so that he is also able to make some constructive criticism,
which is welcomed, as he is the final user of the system.
10
CHAPTER-6
SYSTEM DESIGN
6.1 SYSTEM ARCHITECTURE
Application
Farmer
Iot system
IOTSSYSTEM
FARMER
11
6.2.2 CLASS DIAGRAM
reinforcement signals
irrigation control
12
6.2.4 COLLABRATION DIAGRAM
6: irrigation
5: contril
control
irrigation system
1: mointor soil moistur
irrigate farmer
sysetm
13
6.2.6 COMPONENT DIAGRAM
Application
Farmer iot
application
momitoe soil
moisture controlirrigation
collect
applyreinforcementlearnin
weatherdata
g
Application
Farmer
iot
sysetm
Monitor siol
moisture collect weather apply controlirrigation
data reinforcement system
learing
14
6.2.8 ER DIAGRAM
Application Farmer
monitorsoilmoisture
Iot system
Apply reinforcement
learning
15
6.2.9 DATA SETS
IRRIGATION SCHEDULING DATASET
16
TEST DATA DATASET
17
CHAPTER-7
INPUT AND OUTPUT DESIGN
7.1 INPUT DESIGN
The input design is the link between the information system and the user.
It comprises the developing specification and procedures for data preparation and
those steps are necessary to put transaction data in to a usable form for processing can
be achieved by inspecting the computer to read data from a written or printed
document or it can occur by having people keying the data directly into the system.
The design of input focuses on controlling the amount of input required, controlling
the errors, avoiding delay, avoiding extra steps and keeping the process simple. The
input is designed in such a way so that it provides security and ease of use with
retaining the privacy. Input Design considered the following things:
2. It is achieved by creating user-friendly screens for the data entry to handle large
volume of data. The goal of designing input is to make data entry easier and to be free
from errors. The data entry screen is designed in such a way that all the data
manipulates can be performed. It also provides record viewing facilities.
3.When the data is entered it will check for its validity. Data can be entered with the
help of screens. Appropriate messages are provided as when needed so that the user
will not be in maize of instant. Thus the objective of input design is to create an input
layout that is easy to follow.
18
7.2 OUTPUT DESIGN
A quality output is one, which meets the requirements of the end user and presents the
information clearly. In any system results of processing are communicated to the
users and to other system through outputs. In output design it is determined how the
information is to be displaced for immediate need and also the hard copy output. It is
the most important and direct source information to the user. Efficient and intelligent
output design improves the system’s relationship to help user decision-making.
3.Create document, report, or other formats that contain information produced by the
system.
The output form of an information system should accomplish one or more of the
following objectives.
19
CHAPTER-8
IMPLEMENTATION
8.1 MODULES
Farmer
Iot system
8.1.1 MODULE DESCRIPTION
FARMER:A crucial role in facilitating communication between the system and the
farmers.The farmer module includes a user-friendly interface accessible via web or
mobile applications. This interface allows farmers to interact with the smart irrigation
system, view real-time data, and configure settings.
The farmer module includes a user-friendly interface accessible via web or mobile
applications. This interface allows farmers to interact with the smart irrigation system,
view real-time data, and configure settings
IOT SYSTEM:The IoT system module in an IoT-based smart irrigation system using
reinforcement learning acts as the backbone of the entire infrastructure, enabling
seamless communication between sensors, actuators, and the central control unit.
20
CHAPTER-9
SOFTWARE ENVIRONMENT
9.1 PYTHON
Python is Interactive: You can actually sit at a Python prompt and interact
with the interpreter directly to write your programs.
History of Python
Python was developed by Guido van Rossum in the late eighties and early nineties at
the National Research Institute for Mathematics and Computer Science in the
Netherlands.
Python is derived from many other languages, including ABC, Modula-3, C, C++,
Algol-68, SmallTalk, and Unix shell and other scripting languages.
Python is copyrighted. Like Perl, Python source code is now available under the
GNU General Public License (GPL).
21
Python is now maintained by a core development team at the institute, although
Guido van Rossum still holds a vital role in directing its progress.
Python Features
Python's features include:
Easy-to-read: Python code is more clearly defined and visible to the eyes.
A broad standard library: Python's bulk of the library is very portable and
cross-platform compatible on UNIX, Windows, and Macintosh.
Interactive Mode: Python has support for an interactive mode which allows
interactive testing and debugging of snippets of code.
Portable: Python can run on a wide variety of hardware platforms and has the
same interface on all platforms.
Extendable: You can add low-level modules to the Python interpreter. These
modules enable programmers to add to or customize their tools to be more
efficient.
Scalable: Python provides a better structure and support for large programs
than shell scripting.
22
It supports functional and structured programming methods as well as OOP.
It provides very high-level dynamic data types and supports dynamic type
checking.
It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
ARITHMETIC OPERATORS
23
30
- Subtraction Subtracts right hand operand from left hand operand. a–b=-
10
% Modulus Divides left hand operand by right hand operand and b%a=
returns remainder 0
24
ASSIGNMENT OPERATOR
= Assigns values from right side operands to left side operand c=a+b
assigns
value of a
+ b into c
+= Add AND It adds right operand to the left operand and assign the result to c += a is
left operand equivalent
to c = c +
a
-= Subtract It subtracts right operand from the left operand and assign the c -= a is
AND result to left operand equivalent
to c = c -
a
*= Multiply It multiplies right operand with the left operand and assign the c *= a is
AND result to left operand equivalent
to c = c *
a
/= Divide AND It divides left operand with the right operand and assign the result c /= a is
to left operand equivalent
to c = c /
ac /= a is
equivalent
25
to c = c /
a
%= Modulus It takes modulus using two operands and assign the c %= a is equivalent to
AND result to left operand c=c%a
//= Floor It performs floor division on operators and assign c //= a is equivalent to
Division value to the left operand c = c // a
IDENTITY OPERATOR
26
COMPARISON OPERATOR
& Binary AND Operator copies a bit to the result if it exists in both (a & b)
operands (means
0000 1100)
^ Binary XOR It copies the bit if it is set in one operand but not both. (a ^ b) = 49
(means
0011 0001)
~ Binary Ones It is unary and has the effect of 'flipping' bits. (~a ) = -61
Complement (means
1100 0011
in 2's
complement
form due to
a signed
binary
number.
<< Binary Left Shift The left operands value is moved left by the number of bits a << 2 =
specified by the right operand. 240 (means
1111 0000)
27
>> Binary Right Shift The left operands value is moved right by the number of a >> 2 = 15
bits specified by the right operand. (means
0000 1111)
LOGICAL OPERATOR
and Logical If both the operands are true then condition (a and b)
AND becomes true. is true.
not Logical Used to reverse the logical state of its operand. Not(a
NOT and b) is
false.
Membership Operators
not in Evaluates to true if it does not finds a variable in the x not in y, here
not in results in a
28
specified sequence and false otherwise. 1 if x is not a
member of
sequence y.
Operator Description
~+- Complement, unary plus and minus (method names for the last two are
+@ and -@)
29
= %= /= //= -= += *= Assignment operators
**=
LIST
The list is a most versatile data type available in Python which can be written as a list
of comma-separated values (items) between square brackets. Important thing about a
list is that items in a list need not be of the same type.
list2 = [1, 2, 3, 4, 5 ];
30
['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] Repetition
1 cmp(list1, list2)
2 len(list)
3 max(list)
4 min(list)
5 list(seq)
31
SN Methods with Description
1 list.append(obj)
2 list.count(obj)
3 list. extend(seq)
4 list.index(obj)
5 list.insert(index, obj)
6 list.pop(obj=list[-1])
7 list.remove(obj)
8 list.reverse()
9 list.sort([func])
32
TUPLES
A tuple is a sequence of immutable Python objects. Tuples are sequences, just like
lists. The differences between tuples and lists are, the tuples cannot be changed
unlike lists and tuples use parentheses, whereas lists use square brackets.
tup2 = (1, 2, 3, 4, 5 );
tup1 = ();
To write a tuple containing a single value you have to include a comma, even though
there is only one value −
tup1 = (50,);
Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and
so on.
tup2 = (1, 2, 3, 4, 5, 6, 7 );
33
When the code is executed, it produces the following result −
tup1[0]: physics
tup2[1:5]: [2, 3, 4, 5]
Updating Tuples:
Tuples are immutable which means you cannot update or change the values of tuple
elements. We are able to take portions of existing tuples to create new tuples as the
following example demonstrates −
print tup3
To explicitly remove an entire tuple, just use the del statement. For example:
print tup
del tup;
34
print tup
Built-inTuple Functions
35
5 tuple(seq):Converts a list into tuple.
DICTIONARY
Each key is separated from its value by a colon (:), the items are separated by
commas, and the whole thing is enclosed in curly braces. An empty dictionary
without any items is written with just two curly braces, like this: {}.
Keys are unique within a dictionary while values may not be. The values of a
dictionary can be of any type, but the keys must be of an immutable data type such
as strings, numbers, or tuples.
Result –
dict['Name']: Zara
dict['Age']: 7
Updating Dictionary
We can update a dictionary by adding a new entry or a key-value pair, modifying an
existing entry, or deleting an existing entry as shown below in the simple example −
36
dict['Age'] = 8; # update existing entry
Result −
dict['Age']: 8
dict['School']: DPS School
To explicitly remove an entire dictionary, just use the del statement. Following is a
simple example –
37
Python includes the following dictionary functions −
1 cmp(dict1, dict2)
2 len(dict)
Gives the total length of the dictionary. This would be equal to the number of item
the dictionary.
3 str(dict)
4 type(variable)
Returns the type of the passed variable. If passed variable is dictionary, then it w
return a dictionary type.
38
2 dict. Copy():Returns a shallow copy of dictionary dict
3 dict.fromkeys():Create a new dictionary with keys from seq and values set to value.
4 dict.get(key, default=None):For key key, returns value or default if key not in dictionary
Defining a Function
39
Simple rules to define a function in Python.
Function blocks begin with the keyword def followed by the function name
and parentheses ( ( ) ).
The code block within every function starts with a colon (:) and is indented.
"function_docstring"
function_suite
return [expression]
Calling a Function
Defining a function only gives it a name, specifies the parameters that are to be
included in the function and structures the blocks of code.Once the basic structure of
a function is finalized, you can execute it by calling it from another function or
directly from the Python prompt. Following is the example to call printme() function
−
print str
40
return;
Function Arguments
You can call a function by using the following types of formal
arguments:
Required arguments
Keyword arguments
Default arguments
Variable-length arguments
Scope of Variables
All variables in a program may not be accessible at all locations in that program.
This depends on where you have declared a variable.
The scope of a variable determines the portion of the program where you can access
a particular identifier. There are two basic scopes of variables in Python −
41
Global vs. Localvariables
Variables that are defined inside a function body have a local scope, and those
defined outside have a global scope.
This means that local variables can be accessed only inside the function in which
they are declared, whereas global variables can be accessed throughout the program
body by all functions. When you call a function, the variables declared inside it are
brought into scope. Following is a simple example.
return total;
sum( 10, 20 );
Result −
A module allows you to logically organize your Python code. Grouping related code
into a module makes the code easier to understand and use. A module is a Python
object with arbitrarily named attributes that you can bind and reference.Simply, a
module is a file consisting of Python code. A module can define functions, classes
and variables. A module can also include runnable code.
42
Example:
The Python code for a module named aname normally resides in a file named
aname.py. Here's an example of a simple module, support.py
return
When the interpreter encounters an import statement, it imports the module if the
module is present in the search path. A search path is a list of directories that the
interpreter searches before importing a module. For example, to import the module
support.py, you need to put the following command at the top of the script −
A module is loaded only once, regardless of the number of times it is imported. This
prevents the module execution from happening over and over again if multiple
imports occur.
Packages in Python
A package is a hierarchical file directory structure that defines a single Python
application environment that consists of modules and sub packages and sub-sub
packages.
Consider a file Pots.py available in Phone directory. This file has following line of
source code −
def Pots():
43
print "I'm Pots Phone"
Similar way, we have another two files having different functions with the same
name as above −
Phone/__init__.py
To make all of your functions available when you've imported Phone,to put explicit import
statements in __init__.py as follows −
from G3 import G3
After you add these lines to __init__.py, you have all of these classes available when
you import the Phone package.
import Phone
Phone.Pots()
Phone.Isdn()
Phone.G3()
RESULT:
I'm 3G Phone
44
In the above example, we have taken example of a single functions in each file, but
you can keep multiple functions in your files. You can also define different Python
classes in those files and then you can create your packages out of those classes.
This chapter covers all the basic I/O functions available in Python.
Result:
ReadingKeyboardInput
Python provides two built-in functions to read a line of text from standard input,
which by default comes from the keyboard. These functions are −
raw_input
input
Theraw_inputFunction
The raw_input([prompt]) function reads one line from standard input and returns it
as a string (removing the trailing newline).
This prompts you to enter any string and it would display same string on the screen.
When I typed "Hello Python!", its output is like this −
45
Enter your input: Hello Python
TheinputFunction
The input([prompt]) function is equivalent to raw_input, except that it assumes the
input is a valid Python expression and returns the evaluated result to you.
This would produce the following result against the entered input −
OpeningandClosingFiles
Until now, you have been reading and writing to the standard input and output. Now,
we will see how to use actual data files.
TheopenFunction
Before you can read or write a file, you have to open it using Python's built-in open()
function. This function creates a file object, which would be utilized to call other
support methods associated with it.
Syntax
file_name: The file_name argument is a string value that contains the name
of the file that you want to access.
46
access_mode: The access_mode determines the mode in which the file has to
be opened, i.e., read, write, append, etc. A complete list of possible values is
given below in the table. This is optional parameter and the default file access
mode is read (r).
Modes Description
r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the
default mode.
rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the
file. This is the default mode.
r+ Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
rb+ Opens a file for both reading and writing in binary format. The file pointer placed at the
beginning of the file.
w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist,
creates a new file for writing.
wb Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does
47
not exist, creates a new file for writing.
w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file
does not exist, creates a new file for reading and writing.
wb+ Opens a file for both writing and reading in binary format. Overwrites the existing file if the file
exists. If the file does not exist, creates a new file for reading and writing.
a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the
file is in the append mode. If the file does not exist, it creates a new file for writing.
ab Opens a file for appending in binary format. The file pointer is at the end of the file if the file
exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for
writing.
a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file
exists. The file opens in the append mode. If the file does not exist, it creates a new file for
reading and writing.
ab+ Opens a file for both appending and reading in binary format. The file pointer is at the end of the
file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new
file for reading and writing.
ThefileObjectAttributes
Once a file is opened and you have one file object, you can get various information
related to that file.
48
Attribute Description
file.softspace Returns false if space explicitly required with print, true otherwise.
Example
# Open a file
fo = open("foo.txt", "wb")
Theclose()Method
The close() method of a file object flushes any unwritten information and closes the
file object, after which no more writing can be done.Python automatically closes a
49
file when the reference object of a file is reassigned to another file. It is a good
practice to use the close() method to close a file
Syntax
fileObject.close();
Example
# Open a file
fo = open("foo.txt", "wb")
fo.close()
Result −
ReadingandWritingFiles
The file object provides a set of access methods to make our lives easier. We would
see how to use read() and write() methods to read and write files.
Thewrite()Method
The write() method writes any string to an open file. It is important to note that
Python strings can have binary data and not just text.The write() method does not
add a newline character ('\n') to the end of the string Syntax
fileObject.write(string);
Here, passed parameter is the content to be written into the opened file. Example
# Open a file
fo = open("foo.txt", "wb")
50
# Close opend file
fo.close()
The above method would create foo.txt file and would write given content in that file
and finally it would close that file. If you would open this file, it would have
following content.
Theread()Method
The read() method reads a string from an open file. It is important to note that
Python strings can have binary data. apart from text data.
Syntax
fileObject.read([count]);
Here, passed parameter is the number of bytes to be read from the opened file. This
method starts reading from the beginning of the file and if count is missing, then it
tries to read as much as possible, maybe until the end of file.
Example
Let's take a file foo.txt, which we created above.
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
fo.close()
51
This produces the following result −
FilePositions
The tell() method tells you the current position within the file; in other words, the
next read or write will occur at that many bytes from the beginning of the file.
32
The seek(offset[, from]) method changes the current file position. The offset
argument indicates the number of bytes to be moved. The from argument specifies
the reference position from where the bytes are to be moved.
If from is set to 0, it means use the beginning of the file as the reference position and
1 means use the current position as the reference position and if it is set to 2 then the
end of the file would be taken as the reference position.
Example
Let us take a file foo.txt, which we created above.
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
position = fo.tell();
52
position = fo.seek(0, 0);
str = fo.read(10);
fo.close()
RenamingandDeletingFiles
Python os module provides methods that help you perform file-processing operations,
such as renaming and deleting files.
To use this module you need to import it first and then you can call any related
functions.
Therename()Method
The rename() method takes two arguments, the current filename and the new
filename.
Syntax
os.rename(current_file_name, new_file_name)
Example
Following is the example to rename an existing file test1.txt:
import os
53
# Rename a file from test1.txt to test2.txt
Theremove()Method
You can use the remove() method to delete files by supplying the name of the file to
be deleted as the argument.
Syntax
os.remove(file_name)
Example
Following is the example to delete an existing file test2.txt −
#!/usr/bin/python
import os
os.remove("text2.txt")
DirectoriesinPython
All files are contained within various directories, and Python has no problem
handling these too. The os module has several methods that help you create, remove,
and change directories.
Themkdir()Method
You can use the mkdir() method of the os module to create directories in the current
directory. You need to supply an argument to this method which contains the name
of the directory to be created.
Syntax
os.mkdir("newdir")
54
Example
Following is the example to create a directory test in the current directory −
#!/usr/bin/python
import os
os.mkdir("test")
Thechdir()Method
You can use the chdir() method to change the current directory. The chdir() method
takes an argument, which is the name of the directory that you want to make the
current directory.
Syntax
os.chdir("newdir")
Example
Following is the example to go into "/home/newdir" directory −
#!/usr/bin/python
import os
os.chdir("/home/newdir")
Thegetcwd()Method
The getcwd() method displays the current working directory.
55
Syntax
os.getcwd()
Example
Following is the example to give current directory −
import os
os.getcwd()
Thermdir()Method
The rmdir() method deletes the directory, which is passed as an argument in the
method.
Syntax:
os.rmdir('dirname')
Example
Following is the example to remove "/tmp/test" directory. It is required to give fully
qualified name of the directory, otherwise it would search for that directory in the
current directory.
import os
os.rmdir( "/tmp/test" )
56
File& DirectoryRelatedMethods
There are three important sources, which provide a wide range of utility methods to
handle and manipulate files & directories on Windows and Unix operating systems.
They are as follows −
File Object Methods: The file object provides functions to manipulate files.
EXCEPTION DESCRIPTION
NAME
StopIteration Raised when the next() method of an iterator does not point to any
object.
57
SystemExit Raised by the sys.exit() function.
StandardError Base class for all built-in exceptions except StopIteration and
SystemExit.
ArithmeticError Base class for all errors that occur for numeric calculation.
ZeroDivisionError Raised when division or modulo by zero takes place for all numeric
types.
EOFError Raised when there is no input from either the raw_input() or input()
function and the end of file is reached.
58
KeyboardInterrupt Raised when the user interrupts program execution, usually by
pressing Ctrl+c.
KeyError Raised when the specified key is not found in the dictionary.
IOError Raised when an input/ output operation fails, such as the print
statement or the open() function when trying to open a file that does
IOError
not exist.
SystemError Raised when the interpreter finds an internal problem, but when this
59
error is encountered the Python interpreter does not exit.
ValueError Raised when the built-in function for a data type has the valid type
of arguments, but the arguments have invalid values specified.
RuntimeError Raised when a generated error does not fall into any category.
What is Exception?
An exception is an event, which occurs during the execution of a
program that disrupts the normal flow of the program's instructions. In
general, when a Python script encounters a situation that it cannot cope
with, it raises an exception. An exception is a Python object that
represents an error.
Handlingan exception
If you have some suspicious code that may raise an exception, you can
defend your program by placing the suspicious code in a try: block.
After the try: block, include an except: statement, followed by a block of
code which handles the problem as elegantly as possible.
60
The Python standard for database interfaces is the Python DB-API. Most Python
database interfaces adhere to this standard.
You can choose the right database for your application. Python Database API
supports a wide range of database servers such as −
GadFly
mSQL
MySQL
PostgreSQL
Informix
Interbase
Oracle
Systembase
The DB API provides a minimal standard for working with databases using Python
structures and syntax wherever possible. This API includes the following:
61
9.2 SOURCE CODE
AGENT.PY
class Agent:
class Environment:
62
#Agent will call environment action function to predict field condition
def action(self, train_data, train_label, test_data):
label = None
score = 0
for i in range(0, 100):
predict = dot(train_data[i], test_data) / (norm(train_data[i]) * norm(test_data))
if predict > score:
score = predict
label = train_label[i]
return label
SMARTIRRIGATION.PY
from tkinter import messagebox
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import simpledialog
import tkinter
import numpy as np
from tkinter import filedialog
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from Environment import *
from Agent import *
main = tkinter.Tk()
main.title("IOT Based Smart Irrigation System using Reinforcement Learning")
main.geometry("1300x1200")
global filename
global dataset
63
global X, Y, X_train, X_test, y_train, y_test, scaler
global rewards, penalty, env, agent
def uploadDataset():
global filename, dataset
filename = filedialog.askopenfilename(initialdir = "Dataset")
pathlabel.config(text=filename)
text.delete('1.0', END)
text.insert(END,'Smart Irrigation Dataset loaded\n\n')
dataset = pd.read_csv(filename)
dataset.fillna(0, inplace = True)
text.insert(END,str(dataset))
labels, count = np.unique(dataset['class'], return_counts = True)
height = count
bars = labels
y_pos = np.arange(len(bars))
plt.figure(figsize = (4, 3))
plt.bar(y_pos, height)
plt.xticks(y_pos, bars)
plt.xlabel("Irrigation Condition Labels")
plt.ylabel("Count")
plt.xticks(rotation=90)
plt.tight_layout()
plt.show()
def processDataset():
global filename, dataset, scaler
global X, Y
text.delete('1.0', END)
dataset = dataset.values
X = dataset[:,1:7]
Y = dataset[:,7]
scaler = StandardScaler()
X = scaler.fit_transform(X)
64
text.insert(END,"Dataset Processing & Normalization Completed\n\n")
text.insert(END,"Normalized Dataset = "+str(X))
def splitDataset():
text.delete('1.0', END)
global X, Y, X_train, X_test, y_train, y_test
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2)
text.insert(END,"Dataset Train & Test Splits for Reinforcement Learning Rewards
& Penalty\n\n")
text.insert(END,"80% Dataset used to train Reinforcement Learning :
"+str(X_train.shape[0])+"\n")
text.insert(END,"20% Dataset used to test Reinforcement Learning :
"+str(X_test.shape[0])+"\n")
def trainRL():
text.delete('1.0', END)
global X, Y, X_train, X_test, y_train, y_test
global rewards, penalty, env, agent
env = Environment()
agent = Agent(env)
rewards, penalty = agent.step(X_train, y_train, X_test, y_test)
text.insert(END,"Reinforcement Learning Completed\n\n")
text.insert(END,"Total Training Rewards = "+str(rewards)+"\n")
text.insert(END,"Total Training Penalties = "+str(penalty)+"\n")
def graph():
global rewards, penalty
height = [rewards, penalty]
bars = ('Rewards', 'Penalty')
y_pos = np.arange(len(bars))
plt.bar(y_pos, height)
plt.xticks(y_pos, bars)
plt.xlabel("Type")
plt.xlabel("Count")
65
plt.title("Rewards & Penalty Graph")
plt.show()
def predict():
global env, agent
global X_train, X_test, y_train, y_test, scaler
text.delete('1.0', END)
filename = filedialog.askopenfilename(initialdir = "Dataset")
data = pd.read_csv(filename)
data.fillna(0, inplace = True)
temp = data.values
testData = data.values
testData = testData[:,1:7]
print(testData)
testData = scaler.transform(testData)
for i in range(len(testData)):
predict = agent.predictCondition(X_train, y_train, testData[i])
text.insert(END,"Test Data : "+str(temp[i])+" Irrigation Status : "+predict+"\n\n")
def close():
main.destroy()
66
upload.config(font=font1)
pathlabel = Label(main)
pathlabel.config(bg='lawn green', fg='dodger blue')
pathlabel.config(font=font1)
pathlabel.place(x=800,y=150)
67
text=Text(main,height=30,width=90)
scroll=Scrollbar(text)
text.configure(yscrollcommand=scroll.set)
text.place(x=10,y=100)
text.config(font=font1)
main.config(bg='light salmon')
main.mainloop()
68
CHAPTER-10
RESULTS/DISCUSSION
10.1 SYSTEM TESTING
The purpose of testing is to discover errors. Testing is the process of trying to
discover every conceivable fault or weakness in a work product. It provides a way to
check the functionality of components, sub-assemblies, assemblies and/or a finished
product It is the process of exercising software with the intent of ensuring that the
Software system meets its requirements and user expectations and does not fail in an
unacceptable manner. There are various types of test. Each test type addresses a
specific testing requirement.
TYPES OF TESTS
UNIT TESTING
Unit testing involves the design of test cases that validate that the internal
program logic is functioning properly, and that program inputs produce valid outputs.
All decision branches and internal code flow should be validated. It is the testing of
individual software units of the application .it is done after the completion of an
individual unit before integration. This is a structural testing, that relies on knowledge
of its construction and is invasive. Unit tests perform basic tests at component level
and test a specific business process, application, and/or system configuration. Unit
tests ensure that each unique path of a business process performs accurately to the
documented specifications and contains clearly defined inputs and expected results.
INTEGRATION TESTING
Integration tests are designed to test integrated software components
to determine if they actually run as one program. Testing is event driven and is more
concerned with the basic outcome of screens or fields. Integration tests demonstrate
that although the components were individually satisfaction, as shown by successfully
unit testing, the combination of components is correct and consistent. Integration
testing is specifically aimed at exposing the problems that arise from the
combination of components.
69
FUNCTIONAL TEST
Functional tests provide systematic demonstrations that functions tested
are available as specified by the business and technical requirements, system
documentation, and user manuals.
Functional testing is centered on the following items:
Valid Input : identified classes of valid input must be accepted.
SYSTEM TEST
System testing ensures that the entire integrated software system meets
requirements. It tests a configuration to ensure known and predictable results. An
example of system testing is the configuration-oriented system integration test.
System testing is based on process descriptions and flows, emphasizing pre-driven
process links and integration points.
70
BLACK BOX TESTING
Black Box Testing is testing the software without any knowledge of the
inner workings, structure or language of the module being tested. Black box tests, as
most other kinds of tests, must be written from a definitive source document, such as
specification or requirements document, such as specification or requirements
document. It is a testing in which the software under test is treated, as a black box you
cannot “see” into it. The test provides inputs and responds to outputs without
considering how the software works.
UNIT TESTING
Test objectives
Features to be tested
71
The task of the integration test is to check that components or software applications,
e.g. components in a software system or – one step up – software applications at the
company level – interact without error.
Test Results: All the test cases mentioned above passed successfully. No defects
encountered.
ACCEPTANCE TESTING
User Acceptance Testing is a critical phase of any project and requires significant
participation by the end user. It also ensures that the system meets the functional
requirements.
Test Results: All the test cases mentioned above passed successfully. No defects
encountered.
10.1.TESTCASES
Test case1:
FUNCTION USER
72
10.2 SCREENSHOTS
To run project double click on ‘run.bat’ file to get below screen
FIG:1 In above screen click on ‘Upload Irrigation Dataset’ button to upload dataset
and get below output
FIG:2 In above screen selecting and uploading Irrigation dataset and then click on
73
‘Open’ button to load dataset and get below output
FIG-3 In above screen dataset loaded and in graph can see types of field condition
where x-axis represents Condition and y-axis represents number of instances that
condition hold in dataset and now close above graph and then click on “Pre-process
Dataset” button to clean and normalized dataset and get below output
74
FIG-4 In above screen can see normalized dataset values and then click on ‘Dataset
Train & Test Split’ button to split dataset into train and test and then will get below
output
FIG-5 In above screen can see train and test size and now click on ‘Train
Reinforcement Learning Algorithm’ button to train RL and get below output
FIG-6In above screen can see number of rewards and penalties earned by RL by
predicting on test data and can see penalties are very few compared to rewards so RL
75
can predict field condition accurately and now click on Rewards & penalty Graph’ to
get below graph
FIG-7 In above graph x-axis represents earned type and y-axis represents values and
can see Rewards are more compare to Penalties and now close above graph and then
click on ‘Predict Irrigation Status’ button to upload test data and get prediction
76
FIG-8 In above screen uploading test data and then click on ‘Open’ button to load test
data and get below prediction
FIG-9 In above screen in square bracket can see test data and after square bracket can
see predicted field condition as Wet, Dry, Very Wet or Very Dry and based on above
prediction IOT will give water to crop
77
CHAPTER-11
CONCLUSION
11.1 CONCLUSION
78
CHAPTER-12
REFERENCES
9. Kim, H., Park, S., Cho, K., & Ko, J. (2017). A smart irrigation model using
decision tree and reinforcement learning. In Proceedings of the 19th Asia-
Pacific Network Operations and Management Symposium (APNOMS) (pp. 1-
4). IEEE.
10. Das, M., & Chowdhury, A. K. (2020). Smart irrigation system using IoT and
machine learning: A review. Journal of Ambient Intelligence and Humanized
Computing,
11. 11(4), 1733-1750.Liakos, K. G., Busato, P., Moshou, D., Pearson, S., &
Bochtis, D. (2018). Machine learning in agriculture: A review. Sensors, 18(8),
2674.Khan, M. I., Ullah, S., Ahmad, F., Khan, A., & Din, S. U. (2019). IoT
based smart irrigation system using machine learning and low-cost sensor. In
2019 International Conference on Computing, Mathematics and Engineering
Technologies (iCoMET) (pp. 1-5). IEEE.
79