-
Notifications
You must be signed in to change notification settings - Fork 0
/
telloflyer.ino
102 lines (88 loc) · 2.16 KB
/
telloflyer.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
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
// HARDWARE CONNECTION
const int ONBOARD_LED = 2;
const int BUTTON_TAKEOFF = D5;
const int BUTTON_LAND = D6;
// tello settings
const char* DEFAULT_SSID = "TELLO-AAC8EA";
const char* DEFAULT_PW = "";
const char* TELLO_IP = "192.168.10.1";
const int PORT = 8889;
// state constants
const int INIT = 0;
const int CONNECTED = 1;
WiFiUDP Udp;
char packetBuffer[255]; //buffer to hold incoming packet
int networkState = 0;
String lastMessage = "";
void setup() {
Serial.begin(115200);
pinMode(ONBOARD_LED, OUTPUT);
pinMode(BUTTON_TAKEOFF, INPUT_PULLUP);
pinMode(BUTTON_LAND, INPUT_PULLUP);
}
void loop() {
String message = listenMessage();
if (message != "") {
lastMessage = message;
}
if (networkState == INIT) {
initSequence();
} else if (networkState == CONNECTED) {
controlSequence();
}
}
void initSequence() {
connect();
}
// TODO: exit condition
void connect() {
if (lastMessage == "OK") {
//means this part has run and can skip
lastMessage = "";
networkState = CONNECTED;
return;
}
WiFi.begin(DEFAULT_SSID, DEFAULT_PW);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Udp.begin(PORT);
sendMessage("command");
}
void controlSequence() {
if (digitalRead(BUTTON_TAKEOFF) == 0) {
sendMessage("takeoff");
}
if (digitalRead(BUTTON_LAND) == 0) {
sendMessage("land");
}
}
String listenMessage() {
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = Udp.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
Serial.println("Contents:");
Serial.println(packetBuffer);
}
// this only works as tello's API doesn't return responses greater than 255 char
return (char*) packetBuffer;
}
void sendMessage(char* ReplyBuffer) {
Udp.beginPacket(TELLO_IP, PORT);
Udp.write(ReplyBuffer);
Udp.endPacket();
}