-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifi_yi_1_0.ino
354 lines (280 loc) · 9.96 KB
/
wifi_yi_1_0.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
/**************************************************************
xiaomi_yi_rc_switch is for the ESP8266/Arduino platform
it contains the WifiManger from: AlexT https://github.com/tzapu (thx)
You can controll an Xiaomi Yi with an radio controlled receiver.
You can switch between Foto and Video Mode during flight (drive ...)
https://youtu.be/xFy65uZmHA8
Licensed under MIT license
**************************************************************/
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
const int pin_gipo2 = 14;
const int pin_gipo0 = 0;
const int BUILTIN_LED1 = 2; //GPIO2
#define RC_FILTER 100 // Toleranz to find the command
#define WIFI_TX_POWER 0 // TX power of ESP module (0 -> 0.25dBm) (0...85)
#define TIMEOUT_AP 120 //sets timeout until configuration portal gets turned off
//#define DEBUG
#define DEBUG_SMALL
#define DEBUG_CAMERA_OUTPUT
int switch_signal;
int record_status = 0;
String old_token = "0";
int old_duration = 0;
int counter_while = 0;
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//ESP8266WebServer server(80);
WiFiClient client;
void setup() {
#ifdef ESP8266
system_phy_set_max_tpw(WIFI_TX_POWER); //set as lower TX power as possible
#endif
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(BUILTIN_LED1, OUTPUT); // Initialize the BUILTIN_LED1 pin as an output
ESP.wdtDisable();
ESP.wdtEnable(WDTO_8S);
//reset saved settings
//wifiManager.resetSettings();
//set custom ip for portal
//wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
digitalWrite(BUILTIN_LED1, LOW);
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
wifiManager.setTimeout(TIMEOUT_AP);
digitalWrite(BUILTIN_LED1, LOW);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
if (!wifiManager.autoConnect("Xiaomi Yi RC Switch")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
//ESP.reset();
ESP.restart();
delay(5000);
}
//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
// wifiManager.autoConnect("Xiaomi Yi RC Switch");
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
digitalWrite(BUILTIN_LED1, HIGH);
//ab hier neu:
pinMode(pin_gipo2, INPUT);
pinMode(pin_gipo0, OUTPUT);
digitalWrite(pin_gipo0, LOW);
//record_status = 0;
}
bool connectToServerRC() {
bool result = true;
const int jsonPort = 7878;
// Open Tcp connection
if (!client.connect("192.168.42.1", jsonPort)) {
result = false;
}
return result;
}
String requestToken() {
String token = "" ;
String INcamera = "";
//Serial.println("stark token");
// This will send the request token msg to the server
digitalWrite(BUILTIN_LED1, !digitalRead(BUILTIN_LED1)); // blink
//digitalWrite(BUILTIN_LED1, LOW);
while (1) {
if (INcamera.substring(29, 34) == "param") {
//param finden und string davor abschneiden
int startid = INcamera.indexOf("param");
String reststring = INcamera.substring(startid);
// 7 weil param": wegschneiden und dann starten, solange kein leer, Klammer oder , gefunden wird
for (int i = 8; i < reststring.length(); ++i) {
//wenn leer, klammer Ende, oder , dann ist der param bereich zuende
if ((reststring.charAt(i) == ' ') || (reststring.charAt(i) == '}') || (reststring.charAt(i) == ',')) {
//Serial.println("Ende gefunden");
break;
}
token.concat(reststring.charAt(i));
}
break;
} else {
//Serial.println("token request done: ");
client.print("{\"msg_id\":257,\"token\":0}\n\r");
}
INcamera = "";
while (!client.available()) {}
//counter_while_client++;
//yield();
while (client.available()) {
char k = client.read();
INcamera += k;
if (k == '}') {
//Serial.print(INcamera);
}
}
//Serial.println(INcamera);
}
//digitalWrite(BUILTIN_LED1, HIGH);
//Serial.println("While ende");
//Serial.println(token);
return token;
}
void shoot(String token) {
String INcamera = "";
int conn = 0, state = 0;
client.flush();
client.print("{\"msg_id\":769,\"token\":");
client.print(token);
client.print("}\n\r");
while (1) {
if (INcamera.substring(24, 32) == "vf_start") {
//delay(800); // show "CAPTURED" string on OSD screen for 0.8s, if camera confirmed capturing
break;
}
INcamera = "";
while (!client.available()) {}
while (client.available()) {
char k = client.read();
INcamera += k;
if (k == '}') {
#ifdef DEBUG_CAMERA_OUTPUT
Serial.println(INcamera);
#endif
}
}
}
#ifdef DEBUG
Serial.println("shot finished");
#endif
//delay(1000);
}
int record_on(String token) {
String INcamera = "";
client.flush();
client.print("{\"msg_id\":513,\"token\":");
client.print(token);
client.print("}\n\r");
int status_video = 0;
while (1) {
if (INcamera.substring(24, 42) == "start_video_record") {
#ifdef DEBUG
Serial.println("video record ON");
#endif
status_video = 1;
break;
}
INcamera = "";
while (!client.available()) {}
while (client.available()) {
char k = client.read();
INcamera += k;
if (k == '}') {
#ifdef DEBUG_CAMERA_OUTPUT
Serial.println(INcamera);
#endif
}
}
}
//Serial.print("video status on : ");
//Serial.println(status_video);
return status_video;
}
int record_off(String token) {
String INcamera = "";
client.flush();
client.print("{\"msg_id\":514,\"token\":");
client.print(token);
client.print("}\n\r");
int status_video = 1;
while (1) {
if (INcamera.substring(24, 32) == "vf_start") {
#ifdef DEBUG
Serial.println("video record OFF");
#endif
status_video = 0;
break;
}
INcamera = "";
while (!client.available()) {}
while (client.available()) {
char k = client.read();
INcamera += k;
if (k == '}') {
#ifdef DEBUG_CAMERA_OUTPUT
Serial.println(INcamera);
#endif
}
}
}
return status_video;
}
void loop() {
ESP.wdtFeed();
if (!connectToServerRC()) {
//Serial.println ("return connectedToServer ");
return;
}
unsigned long duration = pulseIn(pin_gipo2, HIGH);
#ifdef DEBUG
Serial.print ("duration: ");
Serial.print (duration);
Serial.print (" old_duration: ");
Serial.println (old_duration);
#endif
//filter um falsche Werte raus zu filtern, wenn man ein Signal falsch ist, Sender muss ein Signal senden, sonst darf er nicht in die Schleife gehen
if ((duration >= 100) && (old_duration != 0) && ((old_duration + RC_FILTER) >= duration) && ((old_duration - RC_FILTER) <= duration) ) {
String token = "";
token = requestToken();
//Serial.println("");
#ifdef DEBUG
Serial.print("Token: ");
Serial.println(token);
#endif
//pruefen ob der token i.O ist (-1), wenn - im Token vorkommt ist er nicht i.O.
int check_token = token.lastIndexOf('-');
//ev noch einbauen, das entweder das eine oder das andere usw. gewaehlt werden kann, was passiert, wenn von Start Record auf Foto geschalten wird ohne Stop record
//if (token.length() != 0 && (check_token == -1) && (old_token != token) && (switch_signal <= -150 )) { }
if (token.length() != 0 && (check_token == -1) && (record_status == 0) && (old_token != token) && (duration <= 1300 )) {
#ifdef DEBUG_SMALL
Serial.println("Foto");
#endif
shoot(token);
//alten Token speichern, damit nicht 2 mal mit den gleichen Token etwas aufgerufen wird
old_token = token;
}
if (token.length() != 0 && (check_token == -1) && (old_token != token) && (record_status == 0) && (duration >= 1700 )) {
// if (token.length() != 0 && (check_token == -1) && (old_token != token) && (record_status == 0) && (switch_signal >= 150 )) {}
//shoot(token);
#ifdef DEBUG_SMALL
Serial.println("Start record");
#endif
record_status = record_on(token);
//alten Token speichern, damit nicht 2 mal mit den gleichen Token etwas aufgerufen wird
old_token = token;
}
//hier sicher stellen, dass wenn von Video aufnahme direkt auf Foto gestellt wird, das hier vorher noch die Aufnahme gestopt wird, sonst hängt sich die Kamera auf
if (token.length() != 0 && (check_token == -1) && (old_token != token) && (record_status == 1) && (duration < 1700 )) {
// if (token.length() != 0 && (check_token == -1) && (old_token != token) && (record_status == 1) && (duration > 1300 ) && (duration < 1700 )) {}
//if (token.length() != 0 && (check_token == -1) && (old_token != token) && (record_status == 1) && (switch_signal > -120 ) && (switch_signal < 120 )) {}
//shoot(token);
#ifdef DEBUG_SMALL
Serial.println("Stop record");
#endif
record_status = record_off(token);
//alten Token speichern, damit nicht 2 mal mit den gleichen Token etwas aufgerufen wird
old_token = token;
}
}
old_duration = duration;
}