-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimelapse.ino
128 lines (98 loc) · 4.33 KB
/
Timelapse.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
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h>
extern "C" {
#include "user_interface.h"
}
#include <Servo.h>
Servo servo;
const int SERVO_MAX_DEGREE = 180;
const int SERVO_MIN_PULSE_WIDTH = 600;
const int SERVO_MAX_PULSE_WIDTH = 2300;
const float deg2Pul = (SERVO_MAX_PULSE_WIDTH - SERVO_MIN_PULSE_WIDTH) / SERVO_MAX_DEGREE;
const int BAT_MAX = 990;
const int BAT_MIN = 730;
ESP8266WebServer server(80);
float duration;
float degrees;
float interval;
float numShoots;
float step;
float pos;
boolean timerStarted;
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println("Board started");
pinMode(A0, INPUT);
WiFi.mode(WIFI_AP);
wifi_set_sleep_type(LIGHT_SLEEP_T);
boolean wifi = WiFi.softAP("TLAPSE-ESP8266-AP", "tl123456");
if(wifi == true) {
Serial.println("Access Point Ready");
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.on("/start", handleStart);
server.begin();
server.begin();
Serial.println("HTTP server started");
} else {
Serial.println("Access Point Failed!");
}
pos = 0;
servo.attach(D4, SERVO_MIN_PULSE_WIDTH, SERVO_MAX_PULSE_WIDTH);
servo.write(1500);
}
void loop()
{
if(timerStarted){
if(pos <= degrees && pos <= SERVO_MAX_DEGREE){
servo.writeMicroseconds(SERVO_MIN_PULSE_WIDTH + pos * deg2Pul);
pos = pos + step;
Serial.println("Pos Pulse: " + String(SERVO_MIN_PULSE_WIDTH + pos * deg2Pul));
Serial.println("Pos Deg: " + String(pos));
delay(interval * 1000);
} else {
pos = 0;
servo.writeMicroseconds(SERVO_MIN_PULSE_WIDTH);
WiFi.forceSleepWake();
timerStarted = false;
}
} else {
server.handleClient();
}
}
void handleRoot() {
int rawBattery = analogRead(A0);
float batteryPercentage = (float) (rawBattery - BAT_MIN) / (BAT_MAX - BAT_MIN) * 100;
Serial.println("Battery percentage: " + String(batteryPercentage));
Serial.println("Battery AnalogRead: " + String(rawBattery));
// https://codepen.io/makebit/pen/bjzrBK
String response = " <!DOCTYPE html> <html> <head> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\"> </head> <body> <h2> Timelapse Settings</h2> Battery Percentage: " + String(batteryPercentage) + "%<br> Battery AnalogRead: " + String(rawBattery) + "<br> <br> <form action=\"/start\"> Duration:<br> <input type=\"text\" value=\"10\" name=\"duration\" id=\"duration\" onclick=\"computeStep()\"> <br> Degrees:<br> <input type=\"text\" value=\"180\" name=\"degrees\" id=\"degrees\" onclick=\"computeStep()\"> <br> Interval:<br> <input type=\"text\" value=\"10\" name=\"interval\" id=\"interval\" onclick=\"computeStep()\"> <br> <br> Step:<br> <input type=\"number\" name=\"step\" id=\"step\" readonly> <br> <br> <input type=\"submit\" value=\"Start\"> </form> <script> function computeStep(){var numShoots = document.getElementById(\"duration\").value * 60 / document.getElementById(\"interval\").value; var step = document.getElementById(\"degrees\").value / numShoots; document.getElementById(\"step\").value = step; } </script> </body> </html>";
server.send(200, "text/html", response);
}
void handleStart() {
if (server.hasArg("duration") && server.hasArg("degrees") && server.hasArg("interval")){
duration = server.arg("duration").toFloat();
Serial.println("Duration: " + String(duration));
degrees = server.arg("degrees").toFloat();
Serial.println("Degrees: " + String(degrees));
interval = server.arg("interval").toFloat();
Serial.println("Interval: " + String(interval));
numShoots = duration * 60 / interval;
step = degrees / numShoots; // step in degrees
Serial.println("numShoots: " + String(numShoots));
Serial.println("step: " + String(step));
String response = "<!DOCTYPE html> <html> <head> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\"> </head> <body> <h2>Timelapse Started</h2> Number of shoots: " + String(numShoots) + "<br>Step: " + String(step) + "<br><a href=\"/\">Home</a> </body> </html>";
server.send(200, "text/html", response);
delay(1000);
timerStarted = true;
WiFi.forceSleepBegin();
} else {
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
}
}