-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJAVA GUI Code.txt
More file actions
144 lines (125 loc) · 6.07 KB
/
JAVA GUI Code.txt
File metadata and controls
144 lines (125 loc) · 6.07 KB
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
140
141
142
143
144
//*NOTE THIS IS JUST A BASIC GUI U CAN ADD YOUR OWN AND CREATE ALSO IT IS JUST A PROTOTYPE VERISON ONLY.
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
public class SmartWaterLevelApp extends JFrame {
private JLabel waterLevelLabel;
private JButton motorToggleButton;
private JLabel motorStatusLabel;
private boolean motorState = false;
private final String esp32IpAddress = "http://---.---.-.---"; // Change to your ESP32 IP YOU GET IT FROM SERIAL MONITOR OS ARDUINO IDE
public SmartWaterLevelApp() {
setTitle("Smart Water Level System");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(4, 1));
waterLevelLabel = new JLabel("Water Level: Fetching...", SwingConstants.CENTER);
waterLevelLabel.setFont(new Font("Arial", Font.BOLD, 20));
add(waterLevelLabel);
motorToggleButton = new JButton("Turn Motor ON");
motorToggleButton.setFont(new Font("Arial", Font.PLAIN, 18));
motorToggleButton.addActionListener(e -> toggleMotor());
add(motorToggleButton);
motorStatusLabel = new JLabel("Motor is OFF", SwingConstants.CENTER);
motorStatusLabel.setFont(new Font("Arial", Font.PLAIN, 18));
add(motorStatusLabel);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
fetchWaterLevel();
checkMotorState();
}
}, 0, 5000);
setVisible(true);
}
private void toggleMotor() {
new Thread(() -> {
try {
String motorUrl = esp32IpAddress + "/motor?state=" + (motorState ? "off" : "on");
HttpURLConnection connection = (HttpURLConnection) new URL(motorUrl).openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
motorState = !motorState;
SwingUtilities.invokeLater(() -> {
motorToggleButton.setText(motorState ? "Turn Motor OFF" : "Turn Motor ON");
motorStatusLabel.setText(motorState ? "Motor is ON" : "Motor is OFF");
});
JOptionPane.showMessageDialog(this, "Motor " + (motorState ? "ON" : "OFF"));
} else {
JOptionPane.showMessageDialog(this, "Failed to toggle motor.");
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
}).start();
}
private void checkMotorState() {
new Thread(() -> {
try {
String motorUrl = esp32IpAddress + "/motor?state=check";
HttpURLConnection connection = (HttpURLConnection) new URL(motorUrl).openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = in.readLine();
in.close();
SwingUtilities.invokeLater(() -> {
if ("on".equals(response)) {
motorState = true;
motorToggleButton.setText("Turn Motor OFF");
motorStatusLabel.setText("Motor is ON");
} else if ("off".equals(response)) {
motorState = false;
motorToggleButton.setText("Turn Motor ON");
motorStatusLabel.setText("Motor is OFF");
}
});
} else {
JOptionPane.showMessageDialog(this, "Failed to check motor state.");
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error fetching motor state: " + ex.getMessage());
}
}).start(); // Run motor state check in a separate thread
}
private void fetchWaterLevel() {
new Thread(() -> {
try {
String waterLevelUrl = esp32IpAddress + "/waterlevel";
HttpURLConnection connection = (HttpURLConnection) new URL(waterLevelUrl).openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String waterLevel = response.toString();
SwingUtilities.invokeLater(() -> waterLevelLabel.setText("Water Level: " + waterLevel + "%"));
} else {
JOptionPane.showMessageDialog(this, "Failed to fetch water level.");
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error fetching water level: " + ex.getMessage());
}
}).start(); // Run water level fetch in a separate thread
}
public static void main(String[] args) {
SwingUtilities.invokeLater(SmartWaterLevelApp::new);
}
}