-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDustCollectionAutomation_v5301.ino
1296 lines (1153 loc) · 52.3 KB
/
DustCollectionAutomation_v5301.ino
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This code is for the project at
* http://www.iliketomakestuff.com/how-to-automate-a-dust-collection-system-arduino
* All of the components are list on the url above.
*
This script was created by Bob Clagett for I Like To Make Stuff
For more projects, check out iliketomakestuff.com
Includes Modified version of "Measuring AC Current Using ACS712"
http://henrysbench.capnfatz.com/henrys-bench/arduino-current-measurements/acs712-arduino-ac-current-tutorial/
Parts of this sketch were taken from the keypad and servo sample sketches that comes with the keypad and servo libraries.
Uses https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
This new version of the code ortiginally written by Bob Clagett is modified to make it easier to implement in a new shop, and not have to re-write code
when moving the machines around. The DustGatesDefinition.cfg is to be stored on an SD card and manage the configuration of the top level parameters
have a section for dust gates and a final section for votage switches. The parameter in each voltage switch maps to a main dust gate for that maching,
and the code below rolls through the comma delimited bit map to set all the other gates open or closed based on the 0,1 flag
Because of this config file, there is no need to modify code if you are taking a machine off line, moving it around or changing gate configuration.
Simply modify the layout in the gate definition, gate bitmap or voltage sensor definition. Using a '#" at the beinning of the voltage sensor or gate
will inactivate the unit, as the code below ignores commented lines.
When an Amerpage chnage is detected, the main gate index is used to look up the main gate in the structure. Upon finding it, the gate bitmap is parsed
and gates are opened and closed.
Each voltage sensor is mapped to an analog port for monitoring. That port is set in the voltage sensor config definition, and can be modified if you
want to add/remove machines.
NEW VERSION - Cory Wiegert 06/20/2020
instead of hard coding the dust gates and machines in the setup, will use an SD Card and configure the machines via delimited config line
can add and configure the dust gates dynamically, instead of having to recompile and upload new code
Cory D. Wiegert 07/19/2020 v5.0 Adding the wifi and Blynk libraries and code to be able to control individual gates and dust collector
with the mobile app configured on the phone. moved most of the setup and reading of the config file
to functions instead of running it all in 1 routine in setup()
Cory D. Wiegert 07/24/2020 v5.1 Adding Blynk setup as stand alone function for a new Mobile app to control gates and dust collector manually
Cory D. Wiegert 07/27/2020 v5.11 Need to debug the checkForAmperageChange function to ensure it is using ampThreshold as a compare
Cory D. Wiegert 07/31/2020 v5.12 Implemented the check for the change threshold from the config file. fixed the bug in BLYNK_WRITE(V1)
Cory D. Wiegert 08/03/2020 v5.2 Added a tab menu to the Blynk app, to set the configuration of gate limits.
Virtual buttons V18-V22
Cory D. Wiegert 08/11/2020 v5.3 Added encryption to the Blynk configuration string from the config file.
Also added Encrypt tab on the Blynk app, virtual Buttons V30 - V40
Cory D. Wiegert 11/28/2020 v5.3.01 changed the delay on the open and close, testing the speed of the gate open and closing
****************************************************************************************************************************************/
#define BLYNK_PRINT Serial
#include <Adafruit_PWMServoDriver.h>
#include "SdFat.h"
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <AESLib.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); // called this way, it uses the default address 0x40
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// boolean DEBUG = false;
boolean DEBUG = true;
boolean collectorIsOn = false;
boolean toolON = false;
int DC_spindown;
byte initiation = true;
uint8_t servoCount;
int NUMBER_OF_TOOLS = 0;
int NUMBER_OF_GATES = 0;
int dustCollectionRelayPin; // the pin which will trigger the dust collector power, turns it on and off by high or low setting on the pin
int manualSwitchPin; //for button activated gate, currently NOT implemented
float mVperAmp; // use 100 for 20A Module and 66 for 30A Module
long debounce; // # of miliseconds to sample the current when reading. may not need with the way we have implemented getVPP()
int blynkSelectedGate; // used to store the selected gate in the Blynk menu item
boolean manualOveride = false;
int inspectionPin;
int highPos; // used as the open max position in the config screen of the Blynk app
int lowPos; // used as the closed max position in the config screen of the Blynk app
boolean bGo = false;
char cypherKey[17] = {'\0'};
SdFat sdCard;
SdBaseFile fConfig;
char auth[40] = {'\0'};
char ssid[32] = {'\0'};
char pass[32] = {'\0'};
char server[48] = {'\0'};
char port[16] = {'\0'};
char ESPSpeed[16] = {'\0'};
char BlynkConnection[16] = {'\0'};
long serverPort;
long ESP8266_BAUD;
// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial3
ESP8266 wifi(&EspSerial);
BlynkTimer timer;
WidgetTerminal terminal(V8);
WidgetTerminal confTerm(V22);
WidgetTerminal encryptTerm(V40);
/***********************************************
* Structure for the voltage sensor definition. each instance
* variable is read and filled from the config file
***********************************************/
struct voltageSensor {
int switchID = -1;
char tool[32] = {'\0'};
int voltSensor;
float voltBaseline = 0;
int mainGate;
float VCC;
float ampThreshold;
float voltDiff;
int isON = false;
} toolSwitch[9];
/***********************************************
* Structure for the blast gate definition. each instance
* variable is read and filled from the config file
***********************************************/
struct gateDefinition {
int gateID = -1;
int pwmSlot;
char gateName[32] = {'\0'}; // name of gate
int openPos; // min servo open position
int closePos; // max servo closed position
bool openClose; // is the gate open or closed?
char gateConfig [48] = {'\0'}; // string containing instructions on which gates to open and close
} blastGate[13];
BLYNK_WRITE(V1) // turn on and off dust collector button
{
if (manualOveride == true)
if (param.asInt() == 1)
if (collectorIsOn == false)
turnOnDustCollection();
else
if (collectorIsOn == true)
turnOffDustCollection();
}
BLYNK_WRITE(V2) // Gate drop down, selecting which gate is opened or closed
{
blynkSelectedGate = param.asInt() - 1;
}
BLYNK_WRITE(V5) // button for opening a gate. Will set highlight back to middle button after processing click
{
if (manualOveride == true)
if (param.asInt() == 1)
{
terminal.print ( "opening machine => ");
terminal.println ( blastGate[blynkSelectedGate].gateName);
openGate (blynkSelectedGate);
Blynk.virtualWrite(V3, blastGate[blynkSelectedGate].openPos);
Blynk.syncVirtual(V3);
runGateMap (blynkSelectedGate);
Blynk.virtualWrite(V5, 2 ); // set the control to hold, so we can use the open and close buttons again
}
else if (param.asInt() == 3)
{
terminal.print(" param ==> ");
terminal.println (param.asInt());
closeGate (blynkSelectedGate, false);
Blynk.virtualWrite(V4, blastGate[blynkSelectedGate].closePos);
Blynk.syncVirtual(V4);
Blynk.virtualWrite(V5, 2); // set the control to hold, so we can use the open and close buttons again
}
}
BLYNK_WRITE(V6) // changes operation from automated monitoring | manual use of Blynk app
{
if (param.asInt() ==1)
manualOveride = true;
else
manualOveride = false;
}
BLYNK_WRITE(V7) // text on screen to close all the gates in the system. Similar to a reinitialization
{
if (manualOveride == true)
if (param.asInt() == 1)
{
turnOffDustCollection();
closeAllGates(false);
}
}
BLYNK_WRITE (V9) // text button on screen to clear the terminal widget
{
terminal.clear();
}
BLYNK_WRITE(V12) // button that reads the voltage sensors of selected outlet
{
terminal.println(param.asInt());
if (param.asInt() == 2)
{
for (int c= 0; c<= 20; c++)
{
Serial.println(getVPP(inspectionPin));
terminal.println(getVPP(inspectionPin));
terminal.flush();
}
Blynk.virtualWrite(V12, 1);
//delay(5000); //want to be able to walk from band saw to computer
}
}
BLYNK_WRITE(V11) // menu drop down to choose which outlet to monitor
{
inspectionPin = toolSwitch[param.asInt()-1].voltSensor;
}
BLYNK_WRITE (V15) // text button to reset the SD card. Useful if making changes to the config file, doesn't require a restart of the Arduino script
{
if (param.asInt() == 1)
{
if (manualOveride == true)
{
char delim[2] = {char(222), '\0'}; // delimeter for parsing the servo config
char delimCheck = char(222);
char gateCheck = char(174);
char sectDelim = '^';
char checker;
int gates;
int outlets;
int SD_WRITE = 53;
char fileName[32] = "DustGatesDefinition 53.cfg";
if (fConfig.open(fileName))
{
checker = fConfig.read();
while (checker != delimCheck) // read all of the comments, stop once you find the first line for servo config
{
if (DEBUG == true)
Serial.print(checker);
checker = fConfig.read();
}
readConfig (sectDelim, delim); // reads the first global config line from the config file.
gates = readGateConfig (sectDelim, delim); // read the gates section of the config file
outlets = readSensorConfig (sectDelim, delim); // read the voltage sensor section of the config file
fConfig.close();
terminal.println("reset all gates and sernsors");
} // end of the open file If
else
Serial.println(F("couldn't open the file to re-read the SD"));
// end of the else right above open file
} // end of if manualOveride
} // end of checking for button pushed
}
BLYNK_WRITE(V16) // menu text, turns on the debug tracing. Don't need to set the config file, can turn off debugging by clicking button again
{
if (param.asInt() == 1)
{
DEBUG = true;
Serial.begin (500000);
delay(500);
}
else
DEBUG = false;
}
BLYNK_WRITE(V18)
{
blynkSelectedGate = param.asInt() - 1;
Blynk.virtualWrite(V20, blastGate[blynkSelectedGate].openPos);
Blynk.virtualWrite(V21, blastGate[blynkSelectedGate].closePos);
Blynk.syncVirtual(V20);
Blynk.syncVirtual(V21);
}
BLYNK_WRITE(V19)
{
if (manualOveride == true)
{
if (param.asInt() == 1)
{
confTerm.print ( "Opening machine => ");
confTerm.println ( blastGate[blynkSelectedGate].gateName);
confTerm.print (" setting PWM ==> ");
confTerm.print( blastGate[blynkSelectedGate].pwmSlot);
confTerm.print (" position ==> ");
confTerm.println(highPos);
confTerm.flush();
pwm.setPWM(blastGate[blynkSelectedGate].pwmSlot, 0, (highPos - 20));
delay(20);
for (int c= (highPos - 20); c <= highPos; c+=1)
pwm.setPWM (blastGate[blynkSelectedGate].pwmSlot, 0, c);
//Blynk.syncVirtual(V3);
Blynk.virtualWrite(V19, 2 ); // set the control to hold, so we can use the open and close buttons again
}
else if (param.asInt() == 3)
{
confTerm.print ( "Closing machine => ");
confTerm.println ( blastGate[blynkSelectedGate].gateName);
confTerm.print (" setting PWM ==> ");
confTerm.print( blastGate[blynkSelectedGate].pwmSlot);
confTerm.print (" position ==> ");
confTerm.println(lowPos);
confTerm.flush();
pwm.setPWM(blastGate[blynkSelectedGate].pwmSlot, 0, (lowPos - 20));
delay(20);
for (int c= (lowPos - 20); c <= lowPos; c+=1)
pwm.setPWM (blastGate[blynkSelectedGate].pwmSlot, 0, c);
}
Blynk.virtualWrite(V19, 2); // set the control to hold, so we can use the open and close buttons again
}
}
BLYNK_WRITE (V20)
{
highPos = param.asInt();
}
BLYNK_WRITE (V21)
{
lowPos = param.asInt();
}
BLYNK_WRITE(V23)
{
confTerm.clear();
}
BLYNK_WRITE(V30)
{
}
BLYNK_WRITE(V31)
{
char delim[2] = {char(222), '\0'};
char space = char(223);
char encryptString[96] = {'\0'};
if (param.asInt() == 1)
{
// figure out what the decrypt algorythm is going to be
}
else if (param.asInt() == 3)
{
encryptTerm.println(ssid);
encryptTerm.flush();
encryptString[0] = char(222);
encryptString[1] = char(174);
encryptString[2] = char(222);
strcat(encryptString, ssid);
encryptTerm.println(ssid);
encryptTerm.flush();
encryptTerm.println(encryptString);
encryptTerm.flush();
strcat(encryptString, delim);
strcat(encryptString, pass);
strcat(encryptString, delim);
strcat(encryptString, server);
strcat(encryptString, delim);
strcat(encryptString, port);
strcat(encryptString, delim);
strcat(encryptString, ESPSpeed);
strcat(encryptString, delim);
strcat(encryptString, BlynkConnection);
strcat(encryptString, delim);
strcat(encryptString, auth);
strcat(encryptString, delim);
for (int x = 0; x<= sizeof(encryptString); x++)
if (encryptString[x] == '\0')
encryptString[x] = 'z';
encryptTerm.println(encryptString);
encryptTerm.flush();
Serial.println(encryptString);
Serial.println(sizeof(encryptString));
aes128_enc_multiple(cypherKey,encryptString, 96);
Serial.println(encryptString);
aes128_dec_multiple(cypherKey,encryptString, 96);
Serial.println(encryptString);
}
// after work is finished,
Blynk.virtualWrite(V31, 2);
}
BLYNK_WRITE (V32)
{
if (param.asInt() == 1)
{
}
}
BLYNK_WRITE (V33)
{
strcpy(ssid, param.asStr());
encryptTerm.println(ssid);
encryptTerm.flush();
}
BLYNK_WRITE(V34)
{
strcpy(pass, param.asStr());
encryptTerm.println(pass);
encryptTerm.flush();
}
BLYNK_WRITE(V35)
{
strcpy(server, param.asStr());
encryptTerm.println(server);
encryptTerm.flush();
}
BLYNK_WRITE(V36)
{
strcpy(port, param.asStr());
encryptTerm.println(port);
encryptTerm.flush();
}
BLYNK_WRITE (V37)
{
strcpy(ESPSpeed, param.asStr());
encryptTerm.println (ESPSpeed);
encryptTerm.flush();
}
BLYNK_WRITE(V38)
{
strcpy(BlynkConnection, param.asStr());
encryptTerm.println(BlynkConnection);
encryptTerm.flush();
}
BLYNK_WRITE(V39)
{
strcpy( auth, param.asStr());
encryptTerm.println(auth);
encryptTerm.flush();
}
BLYNK_WRITE (V41)
{
if ( param.asInt() == 1)
{
char delim[2] = {char(222), '\0'}; // delimeter for parsing the servo config
startWifiFromConfig ('^', delim, 0);
} // end of If Param == 1
}
/*************************************
* SETUP
*
******************************************************************/
void setup()
{
char delim[2] = {char(222), '\0'}; // delimeter for parsing the servo config
char delimCheck = char(222);
char gateCheck = char(174);
char sectDelim = '^';
char checker;
int gates;
int outlets;
int SD_WRITE = 53;
char fileName[32] = "DustGatesDefinition 53.cfg";
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
// char auth[] = "-dv99jBjBpvacaTas2NNEEHs50c4aVzP";
if ( DEBUG == true)
{
Serial.begin(500000);
delay(1000);
}
pinMode(SD_WRITE, INPUT); // initiate the SD card reader
if (!sdCard.begin())
{
if (DEBUG == true)
{
writeDebug("couldn't open the SD card", 1);
delay(5000);
}
}
else
{
/*******************************************
* read the comments section of the config file, then start the other sections
* through the functions. The readConfig will read the global settings for all gates
* readGateConfig() reads the configuration for the gates
* readSensorConfig() -- reads the configurations for the outlet sensors
* startWifiConfig() -- reads the wifi settings, connects to the wifi and starts the blynk server
*
********************************************************/
if (fConfig.open(fileName))
{
checker = fConfig.read();
while (checker != delimCheck) // read all of the comments, stop once you find the first line for servo config
{
if (DEBUG == true)
Serial.print(checker);
checker = fConfig.read();
}
readConfig (sectDelim, delim); // reads the first global config line from the config file.
gates = readGateConfig (sectDelim, delim); // read the gates section of the config file
outlets = readSensorConfig (sectDelim, delim); // read the voltage sensor section of the config file
startWifiFromConfig (sectDelim, delim, 1); // read the wifi section of the config file, start the wifi, and connect to the blynk server
} else
{
Serial.print(F("Could not open config file ==> "));
Serial.println(fileName);
}
fConfig.close(); //don't need config file open any longer, configuration all into memory
if (DEBUG== true)
{
for (int index = 0; index < NUMBER_OF_GATES; index++)
{
writeDebug (blastGate[index].gateName, 1);
writeDebug (" " + String (blastGate[index].gateID) + " Gate pwmSlot ==> " + String(blastGate[index].pwmSlot) , 0);
writeDebug(" " + String (blastGate[index].openPos) + " " + String (blastGate[index].closePos) + " ", 1);
writeDebug (" "+ String (blastGate[index].openClose) + " " + String(blastGate[index].gateConfig), 1);
writeDebug (String (toolSwitch[index].tool) + " " + String (toolSwitch[index].switchID) + " ", 1);
writeDebug (" "+String (toolSwitch[index].voltSensor) + " " + String(toolSwitch[index].voltBaseline) + " ", 1);
writeDebug (" "+String(toolSwitch[index].mainGate), 1);
writeDebug ("tools ==> " + String(index) + " VCC from file ==> " +String (toolSwitch[index].VCC), 1);
}
}
pwm.begin();
pwm.setPWMFreq(60); // Default is 1000mS
pinMode ( dustCollectionRelayPin, OUTPUT);
// initialize all the gates, dust collector and the baseline voltage for the switches
resetVoltageSwitches();
setBlynkControls();
turnOffDustCollection();
closeAllGates (true);
}
}
/***************************************************
* MAIN Loop
***************************************************/
void loop()
{
int gateIndex = 0;
int activeTool = 50;
if (manualOveride == false)
{
for(int i=0;i<NUMBER_OF_TOOLS;i++) // loop through all the tools to see if current has started flowing
{
if (toolSwitch[i].switchID != -1) // only look at uncommented tools. the # as first character of cfg file will disable switch
{
if ( checkForVoltageChange(i) )
{
if (toolSwitch[i].isON == true)
{
if (DEBUG == true)
writeDebug(String (toolSwitch[i].tool) + " has been turned off, dust collector should go off", 1);
// the running tool is off, and we need to stop looking at other tools and turn off the dust collector */
toolSwitch[i].isON = false;
toolON = false;
i = NUMBER_OF_TOOLS;
activeTool = 50;
break;
} // end of toolsSwitch[i].isON
activeTool = toolSwitch[i].switchID;
gateIndex = toolSwitch[i].mainGate;
toolSwitch[i].isON = true;
toolON = true;
i = NUMBER_OF_TOOLS;
openGate(gateIndex);
} else // end of checkForVoltageChange
{
// either there is no current which has started, or the machine is still running - need to test for which
if (toolSwitch[i].isON == true)
{
i=NUMBER_OF_TOOLS;
if (DEBUG == true)
{
writeDebug(String (toolSwitch[i].tool) + " is still running, dumping out of checking loop", 1);
writeDebug(" The current active tool is ==> " + String(activeTool), 1);
}
} // end of thet toolSwitch[i].isON
} // end of else
} // end of testing active tools. This is the if statement above which test for active tool
} // end of For loop where we are monitoring the tools
if(activeTool != 50 )
{
// if activeTool is not = 50, it means there is current flowing through the sensor. read the gate config map, open and close the appropriate gates
if (collectorIsOn == false)
runGateMap (gateIndex);
} else // else of if(activeTool != 50)
{ // This means there was a current change, likely from machine ON to machine OFF
if(collectorIsOn == true && toolON == false)
{
delay(DC_spindown);
turnOffDustCollection();
activeTool = 50;
resetVoltageSwitches();
}
} // End of else section
} // end of check for manual overide
Blynk.run();
timer.run();
} // end of loop()
/***********************************************************************
* void readConfig (SdBaseFile fconfig, char sectDelim, char* delim)
*
* function to read the initial setup line in the config file. sets the
* global variables as functioning in the switch statement.
************************************************************************/
void readConfig (char sectDelim, char delim[])
{
char servoString[160] = {'\0'};
char memFile[30] = {'\0'};
char *token;
int index;
int bytesRead;
bytesRead = fConfig.fgets(servoString, sizeof(servoString));
token = strtok(servoString, delim);
if (servoString[0] != '#') // checking the commented gages
{
for (int index = 0; index < 9; index++)
{
token = strtok(NULL, delim);
switch (index)
{
case 0:
servoCount = atoi(token);
break;
case 1:
NUMBER_OF_TOOLS = atoi(token);
break;
case 2:
NUMBER_OF_GATES = atoi(token);
break;
case 3:
DC_spindown = atoi(token);
break;
case 4:
dustCollectionRelayPin = atoi(token);
break;
case 5:
manualSwitchPin = atoi(token);
break;
case 6:
mVperAmp = atof(token);
break;
case 7:
debounce = atoi (token);
break;
case 8:
DEBUG = atoi(token); // last parameter of string, will set the DEBUG variable to send messages to serial monitor
break;
} //end of case statement
} // end of for loop parsing line
} // end of test whether or not the tool was active/commented out
if (DEBUG == true)
{
writeDebug ("servo count == > " + String(servoCount) + " | NUMBER_OF_TOOLS ==> " + String(NUMBER_OF_TOOLS), 1);
writeDebug ("NUMBER_OF_GATES ==> " + String (NUMBER_OF_GATES) + " | DC_SpinDown == > " + String(DC_spindown), 1);
writeDebug ("DC Relay Pin ==> " + String(dustCollectionRelayPin) + " | Manual Switch ==> " + String(manualSwitchPin), 1);
writeDebug(" mVperAmp ==> " + String(mVperAmp) + " | debounce ==> " + String(debounce) + " | DEBUG ==> " + String(DEBUG), 1);
// delay(500);
}
bytesRead = fConfig.fgets(servoString, sizeof(servoString));
}
/***********************************************************************
* int readGateConfig (char sectDelim, char* delim)
*
* funciton to read the configuration of the gates, and remove it from the setup section
* many of the variables defined in the setup, can be moved to local variables by having
* locals, and settign the structure variables through this function
************************************************************************/
int readGateConfig (char sectDelim, char delim[])
{
char gateString[160] = {'\0'};
char *token;
int index;
char tempString[48] = {'\0'};
int counter;
char checker;
int bytesRead;
bytesRead = fConfig.fgets(gateString, sizeof(gateString));
while (gateString[0] == sectDelim) // test to see if this is a comments section
{
bytesRead = fConfig.fgets(gateString, sizeof(gateString));
}
// Loop through the configuration and get the blast gate configurations
counter = 0;
while (gateString[0] != sectDelim)
{
if ( gateString[0] != '#')
{
token = strtok(gateString, delim);
for (index = 0; index < 6; index++)
{
token = strtok(NULL, delim);
switch (index)
{
case 0:
blastGate[counter].gateID = atoi(token);
break;
case 1:
blastGate[counter].pwmSlot = atoi(token);
break;
case 2:
strcpy (blastGate[counter].gateName,token);
break;
case 3:
blastGate[counter].openPos = atoi(token);
break;
case 4:
blastGate[counter].closePos = atoi(token);
break;
case 5:
strcpy (tempString,token);
stripComma (tempString, blastGate[counter].gateConfig);
break;
} // end of case statement
} // end of the parsing of the line For loop
memset (gateString, '\0', sizeof(gateString));
counter++;
} // end of the active switch, test for the commenting of the blast gates
bytesRead = fConfig.fgets(gateString, sizeof(gateString));
} // end of the section for all the blast gates
return counter;
}
/*****************************************************************************
* int readSensorConfig (char sectDelim, char* delim)
* used to read the voltage sensor section of the configuration file. similar to GateConfig
* this function will read the next section of the config file and set the
* outlet box structures with the appropriate instance variables.
*
* fSensor - the open file pointer
* sectDelim -- the marker to known when you are reading comments and hit the end of the section
* delim -- pointer to the variable holding the special character that acts as the token flag in the config line
*
* returns, number of gates found
**********************************************************************************/
int readSensorConfig (char sectDelim, char delim[])
{
char gateString[160] = {'\0'};
char *token;
int index;
char tempString[48] = {'\0'};
int counter;
char checker;
int bytesRead;
// Loop through the configuration and get the electric switches configuration
counter = 0;
bytesRead = fConfig.fgets(gateString, sizeof(gateString));
while (gateString[0] == sectDelim)
bytesRead = fConfig.fgets(gateString, sizeof(gateString));
while (gateString[0] != sectDelim)
{
index = 0;
if ( gateString[0] != '#')
{
token = strtok(gateString, delim);
for (index = 0; index < 7; index++) // Tokenize the baseline config parameters
{
token = strtok(NULL, delim);
switch (index)
{
case 0:
toolSwitch[counter].switchID = atoi(token);
break;
case 1:
strcpy (toolSwitch[counter].tool,token);
break;
case 2:
toolSwitch[counter].voltSensor = atoi(token);
break;
case 3:
toolSwitch[counter].voltBaseline = atol(token);
break;
case 4:
toolSwitch[counter].mainGate = atol(token);
break;
case 5:
toolSwitch[counter].VCC = atof(token);
break;
case 6:
toolSwitch[counter].voltDiff = atof(token);
break;
} // end of the case statement
} // end of parsing the line
} // end of testing for an active switch and not commented in config file
memset (gateString, '\0', sizeof(gateString));
bytesRead = fConfig.fgets(gateString, sizeof(gateString));
counter++;
} // end of electronic switch section
return counter;
}
/*****************************************************************************
* void startWifiFromConfig (char sectDelim, char delim[], int WifiStart)
* used to read the Blynk section of the configuration file.
* The ssid, password and server connection are all defined in this section
* and will be used to understand how to connect to the Blynk server
*
* fSensor - the open file pointer
* sectDelim -- the marker to known when you are reading comments and hit the end of the section
* delim -- pointer to the variable holding the special character that acts as the token flag in the config line
*
*
**********************************************************************************/
void startWifiFromConfig (char sectDelim, char delim[], int WifiStart)
{
char gateString[160] = {'\0'};
int bytesRead;
int index;
char *token;
SdBaseFile fEncrypt;
// read the section for the Blynk wifi setup -- move this to a function as well, just need to make sure it works
if (WifiStart)
{
bytesRead = fConfig.fgets(gateString, sizeof(gateString));
while (gateString[0] == sectDelim) // test to see if this is a comments section
{
bytesRead = fConfig.fgets(gateString, sizeof(gateString));
}
token = strtok(gateString, delim);
strcpy(cypherKey, token); // This will be the filename of the wifi config file.
fConfig.close();
memset (gateString, '\0', sizeof (gateString));
}
if(fEncrypt.open(cypherKey))
{
fEncrypt.read(gateString, 96);
aes128_dec_multiple(cypherKey,gateString, 96);
index = 0;
token = strtok(gateString, delim);
for (index = 0; index < 7; index++) // Tokenize the baseline config parameters
{
token = strtok(NULL, delim);
switch (index)
{
case 0:
strcpy(ssid, token);
break;
case 1:
strcpy (pass,token);
break;
case 2:
strcpy (server, token);
break;
case 3:
serverPort = atol(token);
break;
case 4:
ESP8266_BAUD = atol(token);
break;
case 5:
strcpy (BlynkConnection, token);
break;
case 6:
strcpy (auth, token);
break;
} // end of the case statement
} // end of parsing the line
/* **************************************************
* setting up the Blynk functionality and connecting to the Blynk server. There are variables I shoudl be adding to the setup file, to know
* the baud rate, the address of the blynk server, the port # and other parameters necessary for Blynk to function
******************************************/
if (DEBUG == true)
{
writeDebug ("auth == > " + String (auth), 1);
writeDebug ("wifi coms speed ==> " + String(ESP8266_BAUD) + " | Connection ==> " + String(BlynkConnection), 1);
writeDebug ("ssid ==> " + String(ssid) + " | pass ==> " + String(pass) + " | server ==> " + String (server) + " | serverPort ==> " + String(serverPort), 1);
//delay(500);
}
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
if (WifiStart)
{
if ( strcmp(BlynkConnection, "Local") == 0 )
{
if (DEBUG)
writeDebug("trying to connect to the local server",1);
Blynk.begin(auth, wifi, ssid, pass, server, serverPort);
// Blynk.begin("UemFhawjdXVMaFOaSpEtM91N9ay1SzAI", wifi, "Everest", "<pass>", "192.168.1.66", <port>);
}
else if (strcmp(BlynkConnection,"Blynk") == 0)
{
Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
}
} // end of if Wifi start
} // end of If OPen Ecnrypt
else
Serial.println(F("couldn't open the encryption file"));
Blynk.virtualWrite(V33, ssid);
Blynk.virtualWrite(V34, pass);
Blynk.virtualWrite(V35, server);
Blynk.virtualWrite(V36, serverPort);
Blynk.virtualWrite(V37, ESP8266_BAUD);
Blynk.virtualWrite(V38, BlynkConnection);
Blynk.virtualWrite(V39, auth);
Blynk.syncVirtual(V33);
Blynk.syncVirtual(V34);
Blynk.syncVirtual(V35);
Blynk.syncVirtual(V36);
Blynk.syncVirtual(V37);
Blynk.syncVirtual(V38);
}
/**************************************************
* void setBlynkControls()
* used to set the lookup dropdown and other controls on the
* blynk app. That is the manual controller for the gates and
* manually control the dust collector
****************************************************/
void setBlynkControls()
{
BlynkParamAllocated clearout(256); // valiable to clear the existing values from the menu dropdown
BlynkParamAllocated switches(256);
BlynkParamAllocated items(256); // list length, in bytes
Blynk.setProperty(V11, "labels", clearout); // clear all existing values from menu
Blynk.setProperty(V2, "labels", clearout); // clear all existing values from menu
Blynk.setProperty(V18, "labels", clearout); // clear all existing values from gate menue
for (int i = 0; i < NUMBER_OF_TOOLS; i++)
items.add (toolSwitch[i].tool);
Blynk.setProperty (V11, "labels", items); // populate menu with the name of each tool
Blynk.virtualWrite (V11, 1);
for (int i=0; i< NUMBER_OF_GATES; i++)
switches.add (blastGate[i].gateName);
Blynk.setProperty(V2, "labels", switches); // populate the menu with the name of each outlet
Blynk.virtualWrite (V2, 3);
Blynk.setProperty(V18, "labels", switches);
Blynk.syncVirtual(V2);
if (manualOveride == false)
Blynk.virtualWrite(V6, false);
else
Blynk.virtualWrite(V6,true);
Blynk.virtualWrite(V3, 0);
Blynk.virtualWrite(V4, 0);
Blynk.virtualWrite(V5, 2);
Blynk.virtualWrite(V13, 0);
Blynk.virtualWrite (V12, 1);
Blynk.syncVirtual(V12);
Blynk.virtualWrite(V16, 0);
Blynk.syncVirtual(V16);