-
Notifications
You must be signed in to change notification settings - Fork 1
/
esp.ino
178 lines (155 loc) · 4.12 KB
/
esp.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
#include <ConfigManager.h>
#include <Stepper.h>
#include <SPI.h>
#include <HTTPClient.h>
#include <WiFi.h>
#define LIMIT_COM 27
#define LIMIT_SWITCH 26
#define STEPPER_PIN_1 16
#define STEPPER_PIN_2 17
#define STEPPER_PIN_3 5
#define STEPPER_PIN_4 18
#define LED_PIN 15
#define CYCLE_DELAY 1
#define REQUEST_COUNTDOWN (1000 * 60 * 5) / CYCLE_DELAY
#define stepsPerRevolution 2048
#define CALIBRATED_POSITION 130
struct Config {
char name[20];
char password[20];
} config;
int prevStepperPos = 0;
int stepperPos = 0;
int dataCountdown = 0;
int previousBlink = 0;
int blinkDuration = 500;
bool ledOn = false;
Stepper stepper = Stepper(stepsPerRevolution, STEPPER_PIN_1, STEPPER_PIN_3, STEPPER_PIN_2, STEPPER_PIN_4);
ConfigManager configManager;
String serverPath = "http://tides.mamota.net/itchenor";
bool calibrated = false;
void setup() {
DEBUG_MODE = true;
pinMode(STEPPER_PIN_1, OUTPUT);
pinMode(STEPPER_PIN_2, OUTPUT);
pinMode(STEPPER_PIN_3, OUTPUT);
pinMode(STEPPER_PIN_4, OUTPUT);
pinMode(LIMIT_COM, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(LIMIT_SWITCH, INPUT_PULLDOWN);
Serial.begin(115200);
stepper.setSpeed(10);
digitalWrite(LIMIT_COM, HIGH);
digitalWrite(LED_PIN, HIGH);
// put your setup code here, to run once:
configManager.setAPName("Tide Clock");
configManager.setAPFilename("/index.html");
configManager.addParameter("name", config.name, 20);
configManager.addParameter("password", config.password, 20, set);
configManager.setAPICallback(APICallback);
configManager.begin(config);
// configManager.clearWifiSettings(false);
}
bool requested = false;
void loop() {
calibrateStepper();
configManager.loop();
if(configManager.wifiConnected()){
digitalWrite(LED_PIN, LOW);
if(!calibrated) {
calibrateStepper();
}
if(dataCountdown == 0) {
httpRequest();
dataCountdown = REQUEST_COUNTDOWN;
} else {
dataCountdown--;
}
} else {
// Blink the LED
int timeNow = millis();
if ((timeNow - previousBlink > blinkDuration) || (previousBlink > timeNow)) {
if(ledOn) {
digitalWrite(LED_PIN, LOW);
} else {
digitalWrite(LED_PIN, HIGH);
}
ledOn = !ledOn;
previousBlink = timeNow;
}
}
}
void httpRequest() {
Serial.println("Requesting...");
HTTPClient http;
http.begin(serverPath.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
String payload = http.getString();
Serial.print("Payload: ");
Serial.println(payload);
stepperPos = payload.toInt();
updateAngle();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
void updateAngle() {
Serial.print("Stepper Pos: ");
Serial.println(stepperPos);
Serial.print("Previous Stepper Pos: ");
Serial.println(prevStepperPos);
if (stepperPos == prevStepperPos){
return;
}else if (stepperPos > prevStepperPos) {
Serial.print("Stepping forward:");
Serial.println(stepperPos - prevStepperPos);
stepper.step(stepperPos - prevStepperPos);
} else {
Serial.print("Stepping forward (positive to negative):");
Serial.println(stepperPos - prevStepperPos);
stepper.step((stepsPerRevolution - prevStepperPos) + stepperPos);
}
prevStepperPos = stepperPos;
}
void calibrateStepper() {
Serial.println("Calibrate");
while(digitalRead(LIMIT_SWITCH) == LOW){
// Serial.print("L");
stepper.step(1);
}
while(true){
stepper.step(1);
// Serial.print("H");
if(digitalRead(LIMIT_SWITCH) == LOW){
// Serial.println("L");
break;
}
}
// Serial.println("ZERO");
// while(true){
// if(digitalRead(LIMIT_SWITCH) == LOW){
// Serial.print("L");
// } else {
// Serial.println("H");
// }
// delay(500);
// }
prevStepperPos = CALIBRATED_POSITION;
// stepperPos = 0;
// updateAngle();
// Serial.println("Zero position");
// delay(100000);
// stepper.step(stepsPerRevolution);
// Serial.println("Full rotation");
// delay(5000);
calibrated = true;
// digitalWrite(LED_PIN, LOW);
}
void APICallback(WebServer *server) {
configManager.stopWebserver();
}