-
Notifications
You must be signed in to change notification settings - Fork 1
/
tello_serial.ino
166 lines (121 loc) · 3.83 KB
/
tello_serial.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
/*******************************************
* ESP8266 esp-01 serial client Tello Drone
*
* Author: fvilmos
*/
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <WiFiServer.h>
/***********************************************
* preprocessor directives, constants, variables
***********************************************/
#define DEBUG true /*if enabled debug winfrmation will be printed on serial*/
const String telloIP = "192.168.10.1"; /*tello IP*/
const String tello_SSID = "TELLO-601909"; /*name of the tello access point*/
const String tello_SSID_pass = "tello123"; /*name of the tello access point*/
const unsigned int cuiVideoPort = 11111; /*video port*/
const unsigned int cuiCmdPort = 8889; /*command port*/
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; /*buffer for received UDP packets*/
bool processString = false; /*indicats the end of string received on serial*/
String readString = ""; /*holds the string recived from serial port*/
WiFiUDP UDPCommands; /* UDP object handler for commands*/
WiFiUDP UDPStream; /* UDP object handler for video stream*/
void setup()
{
Serial.begin(115200);
Serial.println();
/*connect to Tello AP*/
WiFi.begin(tello_SSID,tello_SSID_pass);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
/*set port to listen for UDPCommands / Video Stream*/
UDPCommands.begin(cuiCmdPort);
UDPStream.begin(cuiVideoPort);
Serial.flush();
}
void loop()
{
char c = 0; /*holds the received characters from serial*/
/*handle UDS messages received*/
int packetSize = UDPCommands.parsePacket();
if (packetSize)
{
/*erase message buffer*/
memset(packetBuffer, 0, sizeof(packetBuffer));
/*print statistics*/
if (DEBUG)
{
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, UDPCommands.remoteIP().toString().c_str(), UDPCommands.remotePort());
}
/*check received packets and print to console*/
int len = UDPCommands.read(packetBuffer, packetSize);
if (len > 0)
{
packetBuffer[len] = 0;
}
Serial.println(packetBuffer);
}
/*handle UDS messages received*/
int packetSize1 = UDPStream.parsePacket();
if (packetSize1)
{
/*erase message buffer*/
memset(packetBuffer, 0, sizeof(packetBuffer));
if (DEBUG)
{
Serial.printf("Received %d bytes from %s, port %d\n", packetSize1, UDPStream.remoteIP().toString().c_str(), UDPStream.remotePort());
}
/*check received packets and print to console*/
int len = UDPStream.read(packetBuffer, packetSize1);
if (len > 0)
{
packetBuffer[len] = 0;
}
Serial.write(packetBuffer,len);
}
/*non blocking serial read*/
if (Serial.available() > 0)
{
c = Serial.read();
if (c != '\n')
{
/*read characters till new lne received*/
readString += c;
}
else
{
/*string ready to be processed*/
processString = true;
}
} /*if*/
/*command processor*/
if(processString == true)
{
processString = false;
/*put back to serial the received string*/
if (DEBUG)
{
Serial.println(readString);
}
/*send directly the command*/
UDPCommands.beginPacket(telloIP.c_str(), cuiCmdPort);
UDPCommands.write(readString.c_str());
UDPCommands.endPacket();
/*clear string cotent*/
readString = "";
} /*if(processString == true)*/
Serial.flush();
/*recoonect on Wifi lost*/
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
ESP.reset();
}
} /*void loop() */