-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppCameraStream.cpp
More file actions
226 lines (178 loc) · 5.89 KB
/
Copy pathAppCameraStream.cpp
File metadata and controls
226 lines (178 loc) · 5.89 KB
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
//
// Created by codexzier on 07.07.26.
//
#include "AppCameraStream.h"
void AppCameraStream::initExtend() {
_applicationName = "Camera Stream";
}
void AppCameraStream::drawUpdate() {
if (_hasDraw) {
return;
}
_tft->setFont(FontDefault);
_tft->setTextColor(_colorOn, _tft->C_BLACK);
if (_wlanConnected) {
_tft->setCursor(50, 50);
_tft->print(WiFi.localIP());
_tft->print(":");
char buffer[12];
sprintf(buffer, "%d", _port);
_tft->print(buffer);
_tft->setCursor(50, 70);
_tft->print("Warte auf Client!");
_client = _server.accept();
_tft->writeBuffer();
if (_client) {
_tft->setCursor(50, 50);
_tft->print(WiFi.localIP());
_tft->setCursor(50, 70);
_tft->print("Client verbunden!");
_tft->writeBuffer();
bool onlyOneTimeTftWrite = false;
bool pictureError = false;
while (_client.connected()) {
if (onlyOneTimeTftWrite) {
_tft->setCursor(50, 90);
_tft->print("Stream aktiv...");
_tft->writeBuffer();
onlyOneTimeTftWrite = true;
}
// call camera buffer
CameraRecordResult camRec = _cameraManager->getPictureFast();
if (!camRec.Success || camRec.Buffer == nullptr || camRec.Length == 0) {
_tft->setCursor(50, 110);
_tft->print("Bildfehler!");
_tft->writeBuffer();
delay(10);
continue;
}
size_t written = _client.write(camRec.Buffer, camRec.Length);
//Serial.print("write returned: ");
//Serial.print(written);
//Serial.print(" / ");
//Serial.println(camRec.Length);
//_client.flush();
delay(10);
}
_client.stop();
_tft->setCursor(50, 70);
_tft->print("Getrennt!");
_tft->setCursor(50, 90);
_tft->print("Stream aus!");
_tft->setCursor(50, 110);
_tft->print(WiFi.localIP());
delay(1000);
return;
}
else {
_tft->setCursor(50, 90);
_tft->print("Keine Verbindung!");
_tft->setCursor(20, 110);
_tft->print("Warte 5 für neuen Versuch!");
_tft->setCursor(50, 130);
for (int sec = 0; sec < 5; sec++) {
sprintf(buffer, "%d", sec);
_tft->setCursor(50, 130);
_tft->print(buffer);
_tft->print(" Sekunden");
_tft->writeBuffer();
delay(1000);
}
_tft->clearBuffer();
_tft->fillScreen(_tft->C_BLACK);
return;
}
}
else {
_tft->setCursor(20, 110);
_tft->print("Keine Verbindung");
_tft->setCursor(20, 130);
_tft->print("hergestellt!");
}
_hasDraw = true;
}
bool writeAll(WiFiClient& client, const uint8_t* buffer, size_t length)
{
size_t sent = 0;
unsigned long lastProgress = millis();
while (sent < length) {
if (!client.connected()) {
Serial.println("Client nicht mehr verbunden");
return false;
}
size_t n = client.write(buffer + sent, length - sent);
if (n > 0) {
sent += n;
lastProgress = millis();
continue;
}
if (millis() - lastProgress > 2000) {
Serial.println("Timeout beim Senden");
return false;
}
delay(1);
}
client.flush();
return true;
}
void AppCameraStream::setButton2() {
_tft->clearBuffer();
_tft->fillScreen(_tft->C_BLACK);
_tft->setFont(FontDefault);
_tft->setTextColor(_colorOn, _tft->C_BLACK);
WiFi.mode(WIFI_STA);
Serial.print("SSID: "); Serial.println(wd.getSsid());
Serial.print("Password: "); Serial.println(wd.getPassword());
int posX = 40;
int posY = 45;
_tft->setCursor(posX, posY);
_tft->print("Verbinde mit WLAN");
_tft->setCursor(posX, posY + 20);
_tft->print(wd.getSsid());
WiFi.begin(wd.getSsid(), wd.getPassword());
int count = 0;
Serial.println("Connecting to WiFi ..");
_tft->writeBuffer();
while (WiFi.status() != WL_CONNECTED) {
_tft->setCursor(posX, posY + 40);
_tft->print(WiFi.status());
_tft->setCursor(posX, posY + 40);
_tft->print(WiFi.getHostname());
_tft->setCursor(posX, posY + 60);
_tft->print("MAC: ");
_tft->print(WiFi.macAddress());
_tft->setCursor(posX, posY + 80);
_tft->print("Auto-Reconnect:");
_tft->print(WiFi.getAutoReconnect());
if(count < 100) {
Serial.print('.');
count++;
}
else {
_tft->drawFastHLine(20, posY + 100, count * 2, _tft->C_BLACK);
_tft->drawFastHLine(20, posY + 101, count * 2, _tft->C_BLACK);
_tft->drawFastHLine(20, posY + 102, count * 2, _tft->C_BLACK);
count = 0;
Serial.println("");
}
_tft->drawFastHLine(20, posY + 100, count * 2, _colorOn);
_tft->drawFastHLine(20, posY + 101, count * 2, _colorOn);
_tft->drawFastHLine(20, posY + 102, count * 2, _colorOn);
_tft->writeBuffer();
delay(50);
}
Serial.print("WiFi connected! ");
Serial.println(WiFi.localIP());
_tft->setCursor(posX, posY + 130);
_tft->print("WiFi connected!");
_tft->setCursor(posX, posY + 150);
_tft->print(WiFi.localIP());
_tft->writeBuffer();
delay(2000);
_tft->clearBuffer();
// TCP-Server anlegen
_server.begin(_port);
_wifiServerRun = true;
_hasDraw = false;
_wlanConnected = true;
}