File size: 5,413 Bytes
83616b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afb0a8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83616b0
afb0a8a
83616b0
 
 
 
 
 
 
 
 
 
 
 
 
 
afb0a8a
83616b0
 
 
 
 
 
 
 
 
 
 
 
 
afb0a8a
83616b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9baab4
83616b0
 
 
 
 
 
 
 
 
 
 
 
afb0a8a
83616b0
 
 
afb0a8a
 
 
 
 
 
 
 
83616b0
 
 
 
afb0a8a
83616b0
 
 
afb0a8a
 
 
83616b0
 
 
 
 
 
 
 
 
 
afb0a8a
83616b0
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import gradio as gr
import random
import time

# thresholds for soil moisture (in %)
PRIMARY_THRESHOLD = 30  # Below this, irrigation is necessary
SECONDARY_THRESHOLD = 20  # Below this, irrigation is critical

def calculate_irrigation_runtime(soil_moisture):
    """Calculate irrigation runtime based on soil moisture."""
    if soil_moisture < SECONDARY_THRESHOLD:
        return 10  # Critical moisture, longer irrigation
    elif soil_moisture < PRIMARY_THRESHOLD:
        return 7  # Moderate moisture, shorter irrigation
    else:
        return 0  # No irrigation needed

def irrigation_decision_with_runtime(soil_moisture, rainfall_prediction):
    """Make irrigation decision and simulate runtime."""
    # Initial irrigation decision
    decision = irrigation_decision(soil_moisture, rainfall_prediction)
    yield decision, ""

    # Simulate irrigation runtime if irrigation is ON
    if "Irrigation ON" in decision:
        runtime = calculate_irrigation_runtime(soil_moisture)
        if runtime > 0:
            for minute in range(1, runtime + 1):
                time.sleep(1)  # Simulate a minute as 1 second for demonstration
                status = f"Irrigation running... ⏳ ({minute}/{runtime} minutes)"
                yield decision, status

            # Turn off irrigation after the duration
            yield decision, "Irrigation OFF β›” (Completed runtime)"
    else:
        yield decision, "Irrigation remains OFF β›”"

def irrigation_decision(soil_moisture, rainfall_prediction):
    """Decide whether to turn irrigation ON or OFF."""
    if soil_moisture < SECONDARY_THRESHOLD:
        if rainfall_prediction == "Yes":
            return "Irrigation ON 🌧️🚿 (Critical soil moisture)"
        else:
            return "Irrigation ON 🚿 (Critical soil moisture)"
    elif soil_moisture < PRIMARY_THRESHOLD:
        if rainfall_prediction == "Yes":
            return "Irrigation OFF β›” (Rainfall expected, conserving water)"
        else:
            return "Irrigation ON 🚿 (Low soil moisture)"
    else:
        return "Irrigation OFF β›” (Soil moisture sufficient)"

def simulate_values_and_runtime():
    """Simulate IoT data collection and runtime."""
    # Show loading message for data collection
    yield "Collecting data from IoT device... πŸŒπŸ“‘", "", "", "", ""

    time.sleep(2)  # Simulate delay for fetching data

    # Simulate IoT data
    simulated_moisture = random.uniform(10, 50)
    rainfall_prediction = random.choice(["Yes", "No"])
    decision = irrigation_decision(simulated_moisture, rainfall_prediction)

    # Display simulated data and initial decision
    yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, ""

    # Simulate irrigation runtime 
    if "Irrigation ON" in decision:
        runtime = calculate_irrigation_runtime(simulated_moisture)
        if runtime > 0:
            for minute in range(1, runtime + 1):
                time.sleep(1)  # Simulate a minute as 1 second for demonstration
                status = f"Irrigation running... ⏳ ({minute}/{runtime} minutes)"
                yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, status

            # Turn off irrigation after the duration
            yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, "Irrigation OFF β›” (Completed runtime)"
    else:
        yield "", f"{simulated_moisture:.2f}", rainfall_prediction, decision, "Irrigation remains OFF β›”"

# Gradio Interface
with gr.Blocks(title="Smart Irrigation System πŸŒ±πŸ’§") as demo:
    gr.Markdown("# Smart Irrigation System πŸŒ±πŸ’§")
    gr.Markdown(
        """
        This app helps farmers optimize water usage by predicting irrigation needs based on soil moisture and rainfall forecast.
        """
    )

    with gr.Row():
        soil_moisture_input = gr.Slider(
            minimum=0,
            maximum=100,
            step=1,
            label="🌑️ Soil Moisture (%)",
            value=30
        )
        rainfall_prediction_input = gr.Radio(
            ["Yes", "No"], label="🌧️ Rainfall Prediction in the Next 24 Hour", value="No"
        )

    decision_output = gr.Textbox(label="πŸ’‘ Irrigation Decision")
    runtime_status_output = gr.Textbox(label="⏳ Irrigation Runtime Status")

    with gr.Row():
        submit_button = gr.Button("Submit & Simulate Runtime πŸš€")

    with gr.Row():
        simulate_button = gr.Button("Simulate IoT Data & Runtime 🌐⏳")
        collecting_data_output = gr.Textbox(label="Status")

    simulate_output_moisture = gr.Textbox(label="Simulated Soil Moisture (%)")
    simulate_output_rainfall = gr.Textbox(label="Simulated Rainfall Prediction")
    simulate_decision_output = gr.Textbox(label="Simulated Irrigation Decision")
    simulate_runtime_output = gr.Textbox(label="Irrigation Runtime Status")

    # Function connections
    submit_button.click(
        irrigation_decision_with_runtime,
        inputs=[soil_moisture_input, rainfall_prediction_input],
        outputs=[decision_output, runtime_status_output]
    )

    simulate_button.click(
        simulate_values_and_runtime,
        inputs=[],
        outputs=[
            collecting_data_output,
            simulate_output_moisture,
            simulate_output_rainfall,
            simulate_decision_output,
            simulate_runtime_output
        ]
    )

# Launch the app
demo.launch()