Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smart Water Management #325

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions integrations/Smart Water Management/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Introduction
The Logic Legends team presents a new water management system powered by uAgents designed to solve the problem of wastage of water in farms due to inadeduate information available to the farmers. By leveraging advanced technology and data-driven decision-making, our system ensures optimal water usage for crops at every stage of growth.

# Features
<ol>
<li>Farm Input: Input crucial farm data such as location, crop type, farm size, soil type, and crop growth stage for precise water management.</li>
<li>Weather Data Fetch: Using the location data provided, Weather Agent will seek the data from the 7timer API response such as temperature, humidity and rainfall.</li>
<li>Irrigation Measurements: The amount of water reuired by the plant is fixed but this can be fulfilled by two way: 1. Irrigation 2. Rainfall. The amount of water saved due to rainfall will be predicted in advance.</li>
<li>Pump Control: Control the irrigation pump efficiently by scheduling irrigation intervals and durations through the Pump Agent, ensuring precise water delivery to crops.</li>
</ol>

# Getting Started
To integrate our system into your environment, update the addresses of the four agents: Weather Agent, Pump Agent, Decider Agent, and User Agent. These addresses should be set in all relevant files where they are referenced. Obtain the address of any agent by accessing the respective agent's file and using the provided snippet.
<code>
PUMP_ADDRESS = "agent123...."
</code>
To obtain the address of any agent us the followig snippent it the file of that agent.
e.g.
<code>
print(pump_agent.address) #for pump_agent
</code>
To execute the project run all the 4 python scripts in 4 different terminal (user.py at the end). Then add fill the input details asked in the user.py.

# Overview
This project aims to solve the problem of water wastage at farms by using the weather forecast information such as amount of rainfall, humidity and temperature. This was possible because of the 7timer API for weather forecasting. This will take all the necessary information from the user and send to the decision agent which takes the decision that how much amount of water is fulfilled by rain and what should be irrigated for good nourishment of the crops. This projects leverages the uAgents library and uses its global communication channel Alamanac Contract.
The final actuator is the irrigation pump which can be controlled by the pump_agent according to the instructions received my the decision_agent.
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
from uagents.setup import fund_agent_if_low
from uagents import Agent, Context, Model, Protocol
import math
cities ={
"Delhi": {
"lat": 28.704,
"lon": 77.103
},
"Mumbai": {
"lat": 19.076,
"lon": 72.878
},
"Kolkata": {
"lat": 22.573,
"lon": 88.364
},
"Bangalore": {
"lat": 12.972,
"lon": 77.595
},
"Chennai": {
"lat": 13.083,
"lon": 80.271
},
"Hyderabad": {
"lat": 17.385,
"lon": 78.487
},
"Pune": {
"lat": 18.52,
"lon": 73.857
},
"Ahmedabad": {
"lat": 23.023,
"lon": 72.571
},
"Surat": {
"lat": 21.17,
"lon": 72.831
},
"Jaipur": {
"lat": 26.912,
"lon": 75.787
},
"Lucknow": {
"lat": 26.847,
"lon": 80.946
},
"Kanpur": {
"lat": 26.45,
"lon": 80.332
},
"Nagpur": {
"lat": 21.146,
"lon": 79.088
},
"Visakhapatnam": {
"lat": 17.687,
"lon": 83.219
},
"Indore": {
"lat": 22.72,
"lon": 75.858
},
"Thane": {
"lat": 19.218,
"lon": 72.978
},
"Bhopal": {
"lat": 23.26,
"lon": 77.413
},
"Agra": {
"lat": 27.177,
"lon": 78.008
}
}
#incoming data; from interface agent
sugarcane_water_required = {
"InitialStage": 3.5,
"GrandGrowth": 4.5,
"Maturation": 2.3
}
class FarmData(Model):
city: str
crop_type: str
farm_size: int
soil_type: str
stage_of_growth: str
crop_density: int
soil_moisture: int

#incoming data; from weather-agent
class WeatherData(Model):
farm_data: FarmData
temperature: float
humidity: float
rainfall: float

#outgoing data; to weather-agent
class QueryWeather(Model):
farm_data: FarmData
requester_address: str
lon : float
lat : float
unit: str
prod: str

#outgoing data; to pump-agent
class PumpingInformation(Model):
quantity: float
routines: int
per: str

class Message(Model):
msg: str

decider_agent = Agent(
name="Decision Making Agent",
port=8002,
seed="decision secret code",
endpoint=["http://127.0.0.1:8002/submit"],
)

PUMP_ADDRESS = "agent1qvrjy9qqv0q9vs9894mlrsaus9gr8esp88u24eymlplx8kemgy7aj388n62"
WEATHER_ADDRESS = "agent1qtpjw3vfd0hx7a569c02pjnk3emn6vzatva3cgw5u4zhn46344pcszw5s2j"
fund_agent_if_low(decider_agent.wallet.address())

@decider_agent.on_message(model=FarmData)
async def farm_data_handler(ctx: Context, sender: str,fd: FarmData):
ctx.logger.info(f'''
###############################################################
Data received from user agent
City: {fd.city}
Crop Type: {fd.crop_type}
Farm Size: {fd.farm_size}
Soil Type: {fd.soil_type}
Stage of Growth: {fd.stage_of_growth}
Crop Density: {fd.crop_density}
Soil Moisture: {fd.soil_moisture}
###############################################################
''')

ctx.logger.info("Fetching weather information from 7timer API via weather agent")
city = fd.city
await ctx.send(WEATHER_ADDRESS,QueryWeather(farm_data=fd,requester_address=sender,lon=cities[city]['lon'],lat=cities[city]['lat'],unit="Metric",prod="civil"))

@decider_agent.on_message(model=WeatherData)
async def weather_data_handler(ctx: Context,sender: str,wd: WeatherData):
fd = wd.farm_data
ctx.logger.info(f'''
###############################################################
Received weather data from Weather Agent
Temperature: {wd.temperature}
Rainfall: {wd.rainfall}
Humidity: {wd.humidity}
For the farm detailed below:
City: {fd.city}
Crop Type: {fd.crop_type}
Farm Size: {fd.farm_size}
Soil Type: {fd.soil_type}
Stage of Growth: {fd.stage_of_growth}
Crop Density: {fd.crop_density}
Soil Moisture: {fd.soil_moisture}
###############################################################

''')
required_water = sugarcane_water_required[fd.stage_of_growth]*fd.farm_size*fd.crop_density
rain_water = wd.rainfall*fd.farm_size*fd.crop_density
pump_water = max(0,required_water-rain_water)
routines = 0
per = "day"
if(fd.soil_type=="sandy"):
routines = 4
if(fd.soil_type=="salty"):
routines = 3
if(fd.soil_type=="loamy"):
routines = 2
if(fd.soil_type=="clayey"):
routines = 1

await ctx.send(PUMP_ADDRESS,PumpingInformation(quantity=pump_water,routines=routines,per=per))

if __name__ == "__main__":
decider_agent.run()
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from uagents.setup import fund_agent_if_low
from uagents import Agent, Context, Model

class PumpingInformation(Model):
quantity: float
routines: int
per: str

class Message(Model):
message: str

pump_agent = Agent(
name="Pump Controlling Agent",
port=8000,
seed="pump secret code",
endpoint=["http://127.0.0.1:8000/submit"],
)

DECIDER_ADDRESS = "agent1qw29fkzs8jesevtd4wc5du7av9l24rf6vjswaz6krk20j449ar302m958ly"
fund_agent_if_low(pump_agent.wallet.address())

@pump_agent.on_message(model=PumpingInformation)
async def message_handler(ctx: Context, sender: str, msg: PumpingInformation):
ctx.logger.info(f"Received pumping instruction from Decision Agent")
ctx.logger.info(
f'''
##############################################################
Irrigation Instructions:
Amount of water: {msg.quantity}
Intervals: {msg.routines} per {msg.per}
##############################################################
'''
)
if __name__ == "__main__":
pump_agent.run()

# print(pump_agent.address)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from uagents.setup import fund_agent_if_low
from uagents import Agent, Context, Model

class FarmData(Model):
city: str
crop_type: str
farm_size: int
soil_type: str
stage_of_growth: str
crop_density: int
soil_moisture: int


user = Agent(
name="USER1",
port=8003,
seed="user secret code",
endpoint=["http://127.0.0.1:8003/submit"],
)

fund_agent_if_low(user.wallet.address())

DECIDER_ADDRESS = "agent1qw29fkzs8jesevtd4wc5du7av9l24rf6vjswaz6krk20j449ar302m958ly"

@user.on_event("startup")
async def start(ctx: Context):
city = input("Enter city: ")
crop_type = input("Enter crop type: ")
farm_size = int(input("Enter farm size: "))
soil_type = input("Enter soil type: ")
stage_of_growth = input("Enter stage of growth: ")
crop_density = int(input("Enter crop density: "))
soil_moisture = int(input("Enter soil moisture: "))
await ctx.send(DECIDER_ADDRESS,FarmData(city=city,crop_type=crop_type,farm_size=farm_size,soil_type=soil_type,stage_of_growth=stage_of_growth,crop_density=crop_density,soil_moisture=soil_moisture))
ctx.logger.info(
f'''
###########################################################
Following information was sent to the Decision Agent
City: {city}
Crop Type: {crop_type}
Farm Size: {farm_size}
Soil Type: {soil_type}
Stage of Growth: {stage_of_growth}
Crop Density: {crop_density}
Soil Moisture: {soil_moisture}
###########################################################
'''
)
user.run()
Loading
Loading