-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava
More file actions
178 lines (136 loc) · 5.28 KB
/
Java
File metadata and controls
178 lines (136 loc) · 5.28 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
Class: Main
package sample;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.OutputStream;
import java.io.IOException;
public class Main extends Application {
private final static int MAX_MOISTURE_VALUE = 1<<10;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
var controller = new Controller(); //create the controller
var serialPort = MinorProject.SerialPortService.getSerialPort("COM3");
var outputStream = serialPort.getOutputStream();
serialPort.addDataListener(controller);
var pane = new BorderPane();
var button = new Button("PUMP");
var slider = new Slider();
slider.setMin(0.0);
slider.setMax(3000.0);
button.setOnMousePressed(value -> {
try {
outputStream.write(255);
System.out.println("Watering manually!");
} catch (IOException e) {
e.printStackTrace();
}
});
button.setOnMouseReleased(value -> {
try {
outputStream.write(0);
} catch (IOException e) {
e.printStackTrace();
}
});
slider.valueProperty().addListener((observableValue, oldValue, newValue) -> {
try {
outputStream.write(newValue.byteValue());
if(newValue.byteValue()>0){
outputStream.write(110);
}
} catch (IOException e) {
e.printStackTrace();
}
});
stage.setTitle("Automatic Watering System");
var now = System.currentTimeMillis();
var xAxis = new NumberAxis("Time Elapsed (seconds)", now, now + 50000, 10000); //creates the x-axis (which automatically updates)
var yAxis = new NumberAxis("Voltage", 0, MAX_MOISTURE_VALUE, 10); //creates the y-axis
var series = new XYChart.Series<>(controller.getDataPoints()); // creates the series (all the data)
var lineChart = new LineChart<>(xAxis, yAxis, FXCollections.singletonObservableList(series)); //creates the chart
lineChart.setTitle("Voltage from the Moisture Sensor");
pane.setTop(button);
pane.setBottom(lineChart);
pane.setCenter(slider);
pane.setPadding(new Insets(0, 20, 0, 20));
Scene scene = new Scene(pane, 800,600); //creates the JavaFX window
stage.setScene(scene);
stage.show();
}
}
class :Controller
package sample;
import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortEvent;
import com.fazecast.jSerialComm.SerialPortMessageListenerWithExceptions;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.XYChart;
import java.nio.ByteBuffer;
public class Controller implements SerialPortMessageListenerWithExceptions {
private static final byte[] DELIMITER = new byte[]{'\n'};
private final ObservableList<XYChart.Data<Number,Number>> dataPoints;
public Controller() {
this.dataPoints = FXCollections.observableArrayList();
}
public ObservableList<XYChart.Data<Number, Number>> getDataPoints() {
return dataPoints;
}
@Override
public int getListeningEvents() {
return SerialPort.LISTENING_EVENT_DATA_RECEIVED;
}
@Override
public void serialEvent(SerialPortEvent serialPortEvent) {
if(serialPortEvent.getEventType() != SerialPort.LISTENING_EVENT_DATA_RECEIVED) {
return;
}
byte[] data = serialPortEvent.getReceivedData();
int rawB = ByteBuffer.wrap(data).getInt();
long time = System.currentTimeMillis();
var dataPoint = new XYChart.Data<Number,Number>(time, rawB);
Platform.runLater(() -> this.dataPoints.add(dataPoint));
// TODO: Implement this method. Refer to the documentation for more details.
}
@Override
public void catchException(Exception e) {
e.printStackTrace();
}
@Override
public byte[] getMessageDelimiter() {
return DELIMITER;
}
@Override
public boolean delimiterIndicatesEndOfMessage() {
return true;
}
}
Class : SerialPortService
package MinorProject;
import com.fazecast.jSerialComm.SerialPort;
public class SerialPortService {
private SerialPortService() {}
public static SerialPort getSerialPort(String portDescriptor) {
var sp = SerialPort.getCommPort(portDescriptor);
sp.setComPortParameters(9600, Byte.SIZE, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY);
sp.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0);
var hasOpened = sp.openPort();
if (!hasOpened) {
throw new IllegalStateException("Failed to open port.");
}
return sp;
}
}