-
Notifications
You must be signed in to change notification settings - Fork 0
/
BilndAutomation.ino
556 lines (428 loc) · 12.5 KB
/
BilndAutomation.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
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#import "MainPageHtml.h"
#include <EEPROM.h>
const char* ssid = "MyWifi"; //"MyWifi"; //"CGOffice"; //
const char* password = "3132143880";
ESP8266WebServer server(80);
//Properties
int eepromStepsStart = 0;
int eepromStepsLen = 5;
int eepromLocationStart = 5;
int eepromLocationLen = 2;
int eepromOpenCloseStepsStart = 7;
int eepromOpenCloseStepsLen = 5;
const int led = 13;
int stepsPerRotation = 4096;
int currentLocation = 1; //1 = Up , 2 = Middle, 3 = Down
int OpenCloseSteps = 0;
String rotationInfo = "";
boolean Direction = true;
int MotorType = 2; // 1 = Nema , 2 = SmallMotor 284 something
#if(1 == 1)
uint8_t ConfigPin1 = 5; //D1;
uint8_t ConfigPin2 = 4; //D2;
uint8_t ConfigPin3 = 0; //D3;
uint8_t ConfigPin4 = 2; //D4;
#else
uint8_t ConfigPin1 = D1;
uint8_t ConfigPin2 = D2;
uint8_t ConfigPin3 = D3;
uint8_t ConfigPin4 = D4;
#endif
uint8_t ActivatePin = 16; //D0;
const int enablePin = ConfigPin1;
const int stepPin = ConfigPin3;
const int dirPin = ConfigPin4;
uint8_t IN1 = ConfigPin1;
uint8_t IN2 = ConfigPin2;
uint8_t IN3 = ConfigPin3;
uint8_t IN4 = ConfigPin4;
int Step28MotorSteps = 0;
void setup(void) {
EEPROM.begin(512);
pinMode(led, OUTPUT);
pinMode(ConfigPin1, OUTPUT);
pinMode(ConfigPin2, OUTPUT);
pinMode(ConfigPin3, OUTPUT);
pinMode(ConfigPin4, OUTPUT);
pinMode(ActivatePin, OUTPUT);
digitalWrite(enablePin,LOW);
delay(1000);
digitalWrite(led, 0);
Serial.begin(115200);
Serial.println("----");
Serial.println(stepsPerRotation);
int valSteps = EepromGetInt(eepromStepsStart,eepromStepsLen);
if(valSteps > 0) { stepsPerRotation = valSteps; }
int valLocation = EepromGetInt(eepromLocationStart,eepromLocationLen);
if(valLocation > 0) { currentLocation = valLocation; }
int valOpenCloseSteps = EepromGetInt(eepromOpenCloseStepsStart,eepromOpenCloseStepsLen);
if(valOpenCloseSteps > 0) { OpenCloseSteps = valOpenCloseSteps; }
//EepromSaveInt(30, 2, 12);
//int val2 = EepromGetInt(30,2);
WiFi.persistent(false);
WiFi.disconnect(true);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
delay(3000);
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266"))
{
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/inline", []() { server.send(200, "text/plain", "this works as well"); });
server.on("/getMotorStatus", []() { server.send(200, "text/plain", "Motor Status Result"); });
server.on("/test", handleRoot);
server.on("/paramtest", []() { server.send(200, "text/plain", server.arg("PARAM1")); });
server.on("/SetMotor", ProcessMotorAction);
server.on("/SetStepsPerRotation", SetStepPerRotation);
server.on("/SetOpenCloseSteps", SetStepToOpenAndClose);
server.on("/SetEnableMotor", SetEnableMotor);
server.on("/SetBlindOpen", SetBlindOpen);
server.on("/SetBlindClosed", SetBlindClosed);
server.on("/TurnRelayOn", []() { server.send(200, "text/plain", "Turn Relay On"); digitalWrite(ActivatePin, HIGH); });
server.on("/TurnRelayOff", []() { server.send(200, "text/plain", "Turn Relay Off"); digitalWrite(ActivatePin, LOW); });
server.on("/TurnOff", Step28TurnOffMotor);
//server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
//===============================================================
// This routine is executed when you open its IP in browser
//===============================================================
void handleRoot() {
String s = MAIN_page; //Read HTML contents
s.replace("%stepsPerRotation%", String(stepsPerRotation));
s.replace("%currentLocation%", String(currentLocation));
s.replace("%OpenCloseSteps%", String(OpenCloseSteps));
server.send(200, "text/html", s); //Send web page
}
void ProcessMotorAction()
{
String msg = "";
Serial.println("starting");
rotationInfo = "";
if (server.hasArg("CLOCK") && (server.hasArg("STEPS") || server.hasArg("ROTATIONS")))
{
msg += "Params Found - 1 \n\n";
int steps;
int delayValue = 100;
bool turnOffMotor = true;
bool clockWise;
if(server.hasArg("ROTATIONS"))
{
float rotations = server.arg("ROTATIONS").toFloat();
steps = rotations * stepsPerRotation;
}
else
{
steps = server.arg("STEPS").toInt();
}
clockWise = server.arg("CLOCK") == "1";
Direction = clockWise; //!Direction;
if (server.hasArg("TIMEDELAY")){
delayValue = server.arg("TIMEDELAY").toInt();
}
if(server.hasArg("TURNOFFMOTOR")){
msg += "Motor Param Found \n\n";
turnOffMotor = server.arg("TURNOFFMOTOR") == "1";
}
msg += "Value A: " + String(clockWise) + " \n\n";
msg += "Value B: " + String(steps) + " \n\n";
msg += "Value D: " + String(Direction) + " \n\n";
msg += "Stage - 2 \n\n";
rotationInfo = rotationInfo + "Starting Rotation Set" + "\n";
ActionMotor(steps, delayValue, turnOffMotor);
msg += rotationInfo;
}
else
{
msg += " not all params are found : STEPS /CLOCK \n\n";
}
Serial.println("finished");
Serial.println(msg);
server.send(200, "text/plain", msg);
}
void SetEnableMotor()
{
String msg = "";
if(server.hasArg("ENABLEPIN")){
msg += "Motor Param Found \n\n";
bool enablePinBool = server.arg("ENABLEPIN") == "1";
if(enablePinBool){
digitalWrite(stepPin,HIGH);
server.send(200, "text/plain", "Pin Set HIGH");
}
else{
digitalWrite(stepPin,LOW);
server.send(200, "text/plain", "Pin Set LOW");
}
}
server.send(200, "text/plain", "Now Param Found: ENABLEPIN");
}
void SetStepPerRotation()
{
String msg = "";
if(server.hasArg("STEPS")){
msg += "Steps Param Found \n\n";
stepsPerRotation = server.arg("STEPS").toInt();
EepromSaveInt(eepromStepsStart, eepromStepsLen, stepsPerRotation);
msg += "Steps Updated and saved";
server.send(200, "text/plain", msg);
}
server.send(200, "text/plain", "Now Param Found: STEPS");
}
void SetStepToOpenAndClose()
{
String msg = "";
if(server.hasArg("STEPS")){
msg += "Steps Param Found \n\n";
OpenCloseSteps = server.arg("STEPS").toInt();
EepromSaveInt(eepromOpenCloseStepsStart, eepromOpenCloseStepsLen, OpenCloseSteps);
msg += "OpenCloseSteps Updated and saved";
server.send(200, "text/plain", msg);
}
server.send(200, "text/plain", "Now Param Found: STEPS");
}
void SetBlindOpen()
{
String msg = "";
if(currentLocation == 1)
{
msg += "Opening Blinds \n\n";
Direction = true;
ActionMotor(OpenCloseSteps, 1, true);
currentLocation = 2;
EepromSaveInt(eepromLocationStart, eepromLocationLen, currentLocation);
msg += "Blinds Open";
server.send(200, "text/plain", msg);
}
server.send(200, "text/plain", "Now In a valid State");
}
void SetBlindClosed()
{
String msg = "";
if(currentLocation == 2)
{
msg += "Closing Blinds \n\n";
Direction = false;
ActionMotor(OpenCloseSteps, 1, true);
currentLocation = 1;
EepromSaveInt(eepromLocationStart, eepromLocationLen, currentLocation);
msg += "Blinds Closed";
server.send(200, "text/plain", msg);
}
server.send(200, "text/plain", "Now In a valid State");
}
void ActionMotor(int steps, int delayValue, bool turnOffMotor)
{
digitalWrite(ActivatePin, HIGH);
if(MotorType == 1){
ProcessNemaMotorAction(steps, delayValue);
}
else if(MotorType == 2){
ProcessStep28MotorAction(steps, delayValue, turnOffMotor);
}
digitalWrite(ActivatePin, LOW);
}
void ProcessNemaMotorAction(int steps, int delayValue)
{
if(Direction == true){
digitalWrite(dirPin,LOW);
}
else{
digitalWrite(dirPin,HIGH);
}
NemaStepper(steps, delayValue);
}
void ProcessStep28MotorAction(int steps, int delayValue, bool turnOffMotor)
{
Step28Stepper(steps, delayValue);
if(turnOffMotor){
rotationInfo = rotationInfo + "Done - Turning Off motors" + "\n";
Step28TurnOffMotor();
}
}
void NemaStepper(int xw, int delayValue)
{
for (int x = 0; x < xw; x++)
{
digitalWrite(stepPin,HIGH);
delay(delayValue);
digitalWrite(stepPin,LOW);
delay(delayValue);
}
}
void Step28TurnOffMotor()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
void Step28Stepper(int xw, int delayValue)
{
for (int x = 0; x < xw; x++)
{
switch (Step28MotorSteps)
{
case 0:
Serial.println("Step 0");
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
case 1:
Serial.println("Step 1");
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
break;
case 2:
Serial.println("Step 2");
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 3:
Serial.println("Step 3");
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 4:
Serial.println("Step 4");
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 5:
Serial.println("Step 5");
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 6:
Serial.println("Step 6");
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 7:
Serial.println("Step 7");
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
default:
Serial.println("Step Default");
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
}
delay(delayValue);
if (Direction == 1) {
Step28MotorSteps++;
}
if (Direction == 0) {
Step28MotorSteps--;
}
if (Step28MotorSteps > 7) {
Step28MotorSteps = 0;
}
if (Step28MotorSteps < 0) {
Step28MotorSteps = 7;
}
}
}
void handleNotFound()
{
digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
String EepromGet(int loc, int locLen){
int maxLen = (loc + locLen);
char rtnValue[locLen];
String rtnString = "";
for (int i = 0; i < locLen; i++)
{
char newVal;
rtnValue[i] = newVal;
}
for (int i = loc; i < maxLen; i++) {
int col = i - loc;
rtnValue[col] = EEPROM.read(i);
rtnString += rtnValue[col];
}
return rtnString;
}
void EepromSave(int loc, int ValueLength, String ValueString)
{
for (int i = loc; i < (loc + ValueLength); i++) {
char emptyValue = ' ';
EEPROM.write(i, emptyValue);
}
for (int i = loc; i < (loc + ValueString.length()); i++) {
int charLoc = (i - loc);
char val = ValueString.charAt(charLoc);
EEPROM.write(i, val);
}
EEPROM.commit();
}
void EepromSaveInt(int loc, int ValueLength, int ValueInt)
{
EepromSave(loc, ValueLength, String(ValueInt));
}
int EepromGetInt(int loc, int locLen){
String val = EepromGet(loc, locLen);
if(is_number(val)){
int i = val.toInt();
return i;
}
return 0;
}
bool is_number(String str){
for(byte i=0;i<str.length();i++)
{
str.trim();
if(isDigit(str.charAt(i)) == false) return false;
}
return true;
}