-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollo.ino
149 lines (123 loc) · 4.27 KB
/
rollo.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
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>
#include <AccelStepper.h>
const char* wlan_ssid = "hsmr";
const char* wlan_psk = "xxx";
const char* host_name = "rollo";
const char* ota_password = "xxx";
const char* password = "xxx";
const int travelling_distance = 1750 * -1;
const int stepper_pin_enable = 13; // D7
const int stepper_pin_step = 12; // D6
const int stepper_pin_dir = 14; // D5
const int toggle_button_pin = 5; // D1
ESP8266WebServer server(80);
AccelStepper stepper(AccelStepper::DRIVER, stepper_pin_step, stepper_pin_dir);
/* JSON-Response:
* { "auth": AUTH, "pos": POS, "position": POSITION, \"oldpos\": OLDPOS, "state": STATE }
* AUTH (String): Authentification succeed {okay, failed}
* POS (Int): Position [0, travelling_distance]
* POSITION (String): State of the curtain {up, down}
* OLDPOS (Int): Old position [travelling_distance, 0]
* STATE (String): Was the curtain moved {changed, unchanged}
*/
typedef enum { CURTAIN_UP, CURTAIN_DOWN, CURTAIN_STATUS } CurtainStatus;
String curtainChange(CurtainStatus target) {
long currPos = stepper.currentPosition();
long trgtPos;
String pos;
String state;
switch(target) {
case CURTAIN_UP:
trgtPos = 0;
pos = "up";
break;
case CURTAIN_DOWN:
trgtPos = travelling_distance;
pos = "down";
break;
case CURTAIN_STATUS:
default:
trgtPos = currPos;
pos = String((currPos == 0) ? "up" : "down");
}
state = String((currPos == trgtPos) ? "unchanged" : "changed");
if(target != CURTAIN_STATUS && currPos != trgtPos)
stepper.moveTo(trgtPos);
return "{ \"auth\": \"okay\", \"pos\": " + String(trgtPos) + ", \"position\": \"" +
pos + "\", \"oldpos\": " + String(currPos) + ", \"state\": \"" + state + "\" }";
}
String curtainUp() { return curtainChange(CURTAIN_UP); }
String curtainDown() { return curtainChange(CURTAIN_DOWN); }
String curtainToggle() {
return curtainChange((stepper.currentPosition() == 0) ? CURTAIN_DOWN : CURTAIN_UP);
}
String curtainStatus() { return curtainChange(CURTAIN_STATUS); }
void serverResponse(String (*response)(void)) {
if(server.arg("password") == password) {
server.send(200, "text/plain", response());
} else {
server.send(401, "text/plain", "{ \"auth\": \"failed\" }");
}
}
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(wlan_ssid, wlan_psk);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
ArduinoOTA.setHostname(host_name);
ArduinoOTA.setPassword(ota_password);
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
pinMode(stepper_pin_enable, OUTPUT);
digitalWrite(stepper_pin_enable, HIGH);
stepper.setMaxSpeed(340.0);
stepper.setAcceleration(500.0);
curtainDown();
pinMode(toggle_button_pin, INPUT);
server.on("/up", []() { serverResponse(curtainUp); });
server.on("/down", []() { serverResponse(curtainDown); });
server.on("/toggle", []() { serverResponse(curtainToggle); });
server.on("/status", []() { serverResponse(curtainStatus); });
server.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
ArduinoOTA.handle();
if (stepper.distanceToGo() == 0) {
digitalWrite(stepper_pin_enable, HIGH);
server.handleClient(); // avoid timing problems
if (digitalRead(toggle_button_pin) == HIGH) {
curtainToggle();
}
} else {
digitalWrite(stepper_pin_enable, LOW);
}
stepper.run();
}