forked from petternygren/GoProControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoProControl.cpp
174 lines (148 loc) · 4.26 KB
/
GoProControl.cpp
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
#include "GoProControl.h"
GoProControl::GoProControl(char* ssid, char* password, char* GoProIP, int GoProPort, bool debug) {
_ssid = ssid;
_password = password;
_GoProIP = GoProIP;
_GoProPort = GoProPort;
_debug = debug;
#ifdef WiFi101
WiFiClient client;
#endif
if (_debug) {
//Om debug är true aktiveras debug/info utskrifter
#ifdef WiFi101
Serial.begin(9600);
#endif // WiFi101
#ifdef WiFi8266
Serial.begin(115200);
#endif // WiFi8266
}
}
bool GoProControl::connect() {
//Anslut till GoPro hotspot
#ifdef WiFi101
WiFi.setPins(8,7,4,2);
#endif
WiFi.begin(_ssid, _password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if (_debug) {
Serial.print("Trying to connect, current status: ");
Serial.println(WiFi.status());
}
}
if (WiFi.status() == WL_CONNECTED) {
if (_debug) {Serial.println("Connected");}
return true;
}
else {
if (_debug) {Serial.println("Failed to connect");}
return false;
}
}
void GoProControl::wake() {
//Skickar WakeOnLan(wol) signal
WiFiUDP udp; //wol skickas via UDP
if (udp.begin(_GoProPort) == 1) { //Är önskad port tillgänglig?
if (_debug) {Serial.println("UDP port open");}
/*
Bygg wol "magicpackage"
"6 bytes of all 255 (FF FF FF FF FF FF in hexadecimal),
followed by sixteen repetitions of the target computer's 48-bit MAC address,
for a total of 102 bytes."
GoPro behöver dock inte MAC adressen för att vakna. Därför skickas enbart första delen.
*/
char magicP[102];
//6 Bytes with 0xFF
for (int i=0;i<6;i++)
magicP[i] = 0xff;
if (_debug) {Serial.println("InSide UDP magicPackage loop");}
udp.beginPacket(_GoProIP, _GoProPort);
udp.write(magicP);
udp.endPacket();
udp.stop();
}
else {
if (_debug) {Serial.println("Failed to open UDP port");}
}
}
#ifdef WiFi8266
bool GoProControl::httpGET(String url) {
//Skicka GET request via http
int httpCode = 0;
HTTPClient client;
client.begin("http://" + String(_GoProIP) + url);
httpCode = client.GET();
client.end();
if (httpCode == 200) {
if (_debug) {Serial.println("OK response received");}
return true;
}
else {
if (_debug) {Serial.println("BAD response received");}
return false;
}
}
#endif
#ifdef WiFi101
bool GoProControl::httpGET(String url) {
char responseHeader[500] = "";
client.stop();
if (client.connect(_GoProIP, 80)) {
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET " + url + " HTTP/1.1");
client.println("Host: " + String(_GoProIP));
client.println("Connection: close");
client.println();
}
while(!client.available()){
if (_debug) {Serial.println("Waiting for header");}
delay(50);
}
while(client.available()){
if (_debug) {Serial.println("Receiving header");}
String c = client.readString();
strcat(responseHeader, c.c_str());
}
if (_debug) {Serial.println(responseHeader);}
if (strstr(responseHeader, "HTTP/1.1 200") != NULL) {
if (_debug) {Serial.println("OK response received");}
return true;
}
else {
if (_debug) {Serial.println("BAD response received");}
return false;
}
}
#endif
bool GoProControl::status() {
return httpGET("/gp/gpControl/status");
}
bool GoProControl::videoModeOn() {
return httpGET("/gp/gpControl/command/sub_mode?mode=0&sub_mode=0");
}
bool GoProControl::videoModeOff() {
return httpGET("/gp/gpControl/setting/10/0");
}
bool GoProControl::trigger() {
return httpGET("/gp/gpControl/command/shutter?p=1");
}
bool GoProControl::stop() {
return httpGET("/gp/gpControl/command/shutter?p=0");
}
bool GoProControl::sleep() {
return httpGET("/gp/gpControl/command/system/sleep");
}
bool GoProControl::Settings() {
int numberOfCommands = 2;
const char *commands[numberOfCommands]; // commands storlek måste matcha antalet anrop.
commands[0] = "command1";
commands[1] = "command2";
//... commands[n] = "commandn"
for (int i=0;i<numberOfCommands;i++) {
if (_debug) {Serial.println(commands[i]);}
if (!httpGET(commands[i])) {return false;} // Om requesten misslyckas avsluta loopen och retunera false.
}
return true; // Om alla requests retunerat 200.
}