forked from hephaestus9/Power_Monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmpAndRHT_Serial.pde
353 lines (293 loc) · 9.95 KB
/
AmpAndRHT_Serial.pde
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
Modified by J.Brian Apr 2014
BBCC_Plotter
by Tim Hirzel, Feb 2008
v 1.1
with gratitude, based on:
Grapher Pro!
by Tom Igoe
This Processing application is designed to plot and display values
coming from the BBCC PID controller for Arduino. With minimal modification,
it could be setup to plot and show different incoming data
Please refer to the top section for alterations of plot ranges, graph size etc.
All code released under
Creative Commons Attribution-Noncommercial-Share Alike 3.0
*/
import processing.serial.*;
// ********* unlikely that you want to change these *********
int BAUDRATE = 9600;
char DELIM = ':'; // the delimeter for parsing incoming data
//char INIT_MARKER = '!'; //Look for this character before sending init message
//String INIT_MESSAGE = "?gu"; //Send these characters to the arduino after init to start up messages
// this gets the help string from teh device, and then turns on plotting mode with constant update strings
// ********* SETINGS *********
int yBorder = 60; // Size of the background area around the plot in screen pixels
int xBorder = 90;
// the plot size in screen pixels
int plotHeight = 450;
int plotWidth = 750;
int ExpectUpdateSpeed = 200; // milliseconds. This just allows the axis labels in the X direction accurate
// These are all in real number space
// all X values measured in ExpectedUpdateSpeed Intervals
// all y measured in degrees
int gridSpaceX = 50;
int gridSpaceY = 10;
int startX = 0;
int endX = 600;
int startY = 0;
int endY = 120;
// leave these to be calculated
float pixPerRealY = float(plotHeight)/(endY - float(startY));
float pixPerRealX = float(plotWidth)/(endX - float(startX));
// These are calculated here, but could be changed if you wanted
int windowWidth = plotWidth + 2*xBorder;
int windowHeight = plotHeight + 2*yBorder;
// ******* Legend ********
// Define the location and size of the Legend area
int legendWidth = 125;
int legendHeight = 130;
int legendX = windowWidth - 140;
int legendY = 15;
// ******* Help Window ********
// Define the size of the help area. It always sits in the middle
int helpWidth = 600;
int helpHeight = 400;
String title = "Home Monitor Data"; // Plot Title
String names = "Temp. Humidity MainA MainB Combined"; // The names of the values that get sent over serial
String yLabel = "T\ne\nm\np\ne A\nr m\na p\nt s\nu\nr\ne\n\nF A"; // this is kind of a hack to make the vertical label
String xLabel = "Time"; // X axis label
String fontType = "Courier"; // Y axis label
boolean[] plotThisName = {
true, true, true, true, true}; // For each of the values, you can choose if you want it plotted or not here
// **************** end of Settings area ****************
String helpBase = "-Plotter Help-\n(all characters are case sensitive)\nh : toggle this help screen\nl : toggle the Legend\nS : save screen\nE : export shown data as text\n\n-Device Help- \n";
String helpString = "";
Serial myPort; // The serial port
boolean displayLegend = true;
boolean displayHelp = true;
int sensorCount = 5; // number of values to expect
float[][] sensorValues = new float[endX-startX][sensorCount]; // array to hold the incoming values
int currentValue = -1;
int hPosition = startX; // horizontal position on the plot
int displayChannel = 0; // which of the five readings is being displayed
int threshold = 50; // threshold for whether or not to write
// data to a file
boolean updatePlot = false;
//int [] lastSet = new int[sensorCount];
int[][] colors = new int[sensorCount][3];
PFont titleFont;
PFont labelFont;
void setupColors() {
// Thanks to colorbrewer for this pallete
colors[0][0] = 102;
colors[0][1] =194;
colors[0][2] = 165;
colors[1][0] = 252;
colors[1][1] = 141;
colors[1][2]= 98;
colors[2][0] = 141;
colors[2][1] = 160;
colors[2][2]= 203;
colors[3][0] = 231;
colors[3][1] = 138;
colors[3][2]= 195;
colors[4][0] = 166;
colors[4][1] = 216;
colors[4][2]= 84;
//colors[5][0] = 255;
//colors[5][1] = 217;
//colors[5][2]= 47;
}
void setup () {
size(windowWidth, windowHeight); // window size
setupColors();
smooth();
// println(PFont.list());
titleFont = createFont(fontType, 18);
labelFont = createFont(fontType, 14 );
clearPlot();
// List all the available serial ports
println(Serial.list());
// On my mac, the arduino is the first on this list.
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[1], BAUDRATE);
// clear the serial buffer:
myPort.clear();
}
void draw () {
// if the value for the given channel is valid, plot it:
if (updatePlot) {
// draw the plot:
plot();
updatePlot = false;
}
}
void clearPlot() {
background(5);
strokeWeight(1.5);
stroke(10);
fill(40);
// draw boundary
rect(xBorder,yBorder,plotWidth, plotHeight);
textAlign(CENTER);
fill(70);
textFont(titleFont);
text(title, windowWidth/2, yBorder/2);
textFont(labelFont);
stroke(10);
//draw grid
fill(70);
textAlign(RIGHT);
for (int i = startY; i <= endY; i+= gridSpaceY) {
line(xBorder - 3, realToScreenY(i), xBorder + plotWidth - 1, realToScreenY(i));
text(str(i), xBorder - 10, realToScreenY(i));
}
textAlign(LEFT);
for (int i = startX; i <= endX ; i+= gridSpaceX) {
line(realToScreenX(i), yBorder+1, realToScreenX(i), yBorder + plotHeight + 3);
text(str((i)/ (1000 / ExpectUpdateSpeed)), realToScreenX(i), yBorder + plotHeight + 20);
}
// Draw Axis Labels
fill(70);
text(yLabel, xBorder - 70, yBorder + 100 );
textAlign(CENTER);
text(xLabel, windowWidth/2, yBorder + plotHeight + 50);
}
float realToScreenX(float x) {
float shift = x - startX;
return (xBorder + shift * pixPerRealX);
}
float realToScreenY(float y) {
float shift = y - startY;
return yBorder + plotHeight - 1 - (shift) * pixPerRealY;
}
void plot () {
clearPlot();
// draw the line:
for (int i = 0; i < sensorCount; i++) {
// assign color to each plot
stroke(colors[i][0], colors[i][1],colors[i][2]);
for (int x = 1; x < currentValue; x++) {
if(plotThisName[i]) {
line(realToScreenX(x-1),
realToScreenY(sensorValues[x-1][i]) ,
realToScreenX(x),
realToScreenY(sensorValues[x][i])
);
}
}
}
if (hPosition >= endX) {
hPosition = startX;
// wipe the screen clean:
clearPlot();
}
else {
hPosition += 1;
}
noStroke();
// DRAW LEGEND
if (displayLegend) {
fill(128,128,128,80);
rect(legendX, legendY, legendWidth, legendHeight);
// print the name of the channel being graphed:
String line;
for (int i = 0; i < sensorCount; i++) {
fill(colors[i][0], colors[i][1],colors[i][2]);
textAlign(LEFT);
text(split(names,' ')[i] , legendX+5, legendY + (i+1) * 20);
textAlign(RIGHT);
text(nf(sensorValues[currentValue][i], 0,2), legendX+legendWidth - 5, legendY + (i+1) * 20);
}
}
if (displayHelp) {
textAlign(LEFT);
fill(128,128,128,80);
noStroke();
rect(windowWidth/2 - helpWidth/2, windowHeight/2 - helpHeight / 2, helpWidth, helpHeight);
fill(255,255,255);
helpWidth -= 20;
helpHeight -=20;
text(helpString,windowWidth/2 - helpWidth/2, windowHeight/2 - helpHeight / 2, helpWidth, helpHeight);
helpWidth += 20;
helpHeight +=20;
}
}
void keyPressed() {
// if the key pressed is "0" through "4"
if (key == 'l') {
// set the display channel accordingly
displayLegend = ! displayLegend;
updatePlot = true;
}
if (key == 'h') {
// set the display channel accordingly
displayHelp = ! displayHelp;
updatePlot = true;
}
if (key == 'S') {
// set the display channel accordingly
save(str(hour()) + "h" + str(minute()) + "m" + str(second()) + "s" + str(month()) + "." + str(day()) + "." + str(year())+".jpg") ;
}
if (key == 'E') {
exportText();
}
myPort.write(key);
}
void exportText() {
// string for the new data you'll write to the file:
String[] outStrings = new String[currentValue+1];
outStrings[0] = names;
for (int i =0; i < currentValue; i++) {
outStrings[i+1] = "";
for (int j=0; j < sensorCount; j++) {
outStrings[i+1] += str(sensorValues[i][j]);
if (j < sensorCount - 1) {
outStrings[i+1] += ", ";
}
}
}
saveStrings(str(hour()) + "h" + str(minute()) + "m" + str(second()) + "s" + str(month()) + "." + str(day()) + "." + str(year())+".txt", outStrings);
}
// make up a timeStamp string for writing data to the file:
String timeStamp() {
String now = hour()+ ":" + minute()+ ":" + second()+ " " +
month() + "/" + day() + "/" + year();
return now;
}
void serialEvent(Serial myPort) {
// read incoming data until you get a newline:
String serialString = myPort.readStringUntil('\n');
// if the read data is a real string, parse it:
if (serialString != null) {
println(serialString);
// split it into substrings on the DELIM character:
String[] numbers = split(serialString, DELIM);
// convert each subastring into an int
if (numbers.length == sensorCount) {
currentValue += 1;
if (currentValue >= (endX-startX))
{
currentValue = 0;
}
for (int i = 0; i < numbers.length; i++) {
// make sure you're only reading as many numbers as
// you can fit in the array:
if (i <= sensorCount) {
// trim off any whitespace from the substring:
numbers[i] = trim(numbers[i]);
sensorValues[currentValue][i] = float(numbers[i]);
}
}
updatePlot = true;
}
else if (currentValue == -1){
// The help string from the first '?' character gets appended to the plotter help string
helpString += serialString;
}
else {
// Things we don't handle in particular can get output to the text window
print(serialString);
}
}
}