-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSendTo.h
242 lines (204 loc) · 7.58 KB
/
SendTo.h
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
/*
Copyright (C) 2021 SFini
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file SendTo.h
*
* Helper function to send the data to the raspberry server.
*/
#pragma once
#include <WiFi.h>
#include <WiFiClient.h>
WiFiClient client;
/** Increment the amount of wakups */
void IncrementWakeupCount()
{
int wakeupCount = NVS.getInt("wakeupCount");
wakeupCount++;
NVS.setInt("wakeupCount", wakeupCount);
}
/** Increment the amount of equal images */
void IncrementEqualCount()
{
int equalCount = NVS.getInt("equalCount");
equalCount++;
NVS.setInt("equalCount", equalCount);
}
/** Returns the time in which the system was active over all deep sleep phases */
int64_t GetAllActiveTime()
{
int64_t allActiveTime = NVS.getInt("activeTime");
allActiveTime += millis();
return allActiveTime;
}
/** Added the current active time to the overall time. */
void IncrementAllActiveTime()
{
int64_t activeTime = GetAllActiveTime();
NVS.setInt("activeTime", activeTime);
}
/** Send the image to the server. */
bool SendImage()
{
bool ret = false;
Serial.println("SendImage");
if (!jpg_fb) {
Serial.println(" -> Missing Image");
} else {
String getAll;
String getBody;
Serial.println("Connecting to server: " + (String) serverName);
if (!client.connect(serverName, serverPort)) {
getBody = "Connection to " + (String) serverName + " failed.";
Serial.println(getBody);
} else {
Serial.println("Connection successful!");
String head = "--RandomNerdTutorials\r\nContent-Disposition: form-data; name=\"imageFile\"; filename=\"EspCamSend.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
String tail = "\r\n--RandomNerdTutorials--\r\n";
uint32_t imageLen = jpg_fb->len;
uint32_t extraLen = head.length() + tail.length();
uint32_t totalLen = imageLen + extraLen;
client.println("POST " + (String) serverPath + " HTTP/1.1");
client.println("Host: " + (String) serverName);
client.println("Content-Length: " + String(totalLen));
client.println("Content-Type: multipart/form-data; boundary=RandomNerdTutorials");
client.println();
client.print(head);
uint8_t *fbBuf = jpg_fb->buf;
size_t fbLen = jpg_fb->len;
for (size_t n=0; n<fbLen; n=n+1024) {
if (n+1024 < fbLen) {
client.write(fbBuf, 1024);
fbBuf += 1024;
} else if (fbLen%1024>0) {
size_t remainder = fbLen%1024;
client.write(fbBuf, remainder);
}
}
client.print(tail);
int timoutTimer = 10000;
long startTimer = millis();
boolean state = false;
while ((startTimer + timoutTimer) > millis()) {
Serial.print(".");
delay(100);
while (client.available()) {
char c = client.read();
if (c == '\n') {
if (getAll.length()==0) {
state=true;
}
getAll = "";
} else if (c != '\r') {
getAll += String(c);
}
if (state==true) {
getBody += String(c);
}
startTimer = millis();
}
if (getBody.length()>0) {
break;
}
}
}
Serial.println();
client.stop();
Serial.println(getBody);
ret = true;
}
return ret;
}
/** Send the calculated and environment data to the server. */
bool SendInfo(float voltage, int capacity, float frameDiff, float frameAvg, float frameSum, int rssi)
{
Serial.println("SendInfo");
String info;
char filename[50];
int allActiveTimeSec = GetAllActiveTime() / 1000;
int idx = NVS.getInt("infoIdx");
int wakeupCount = NVS.getInt("wakeupCount");
int equalCount = NVS.getInt("equalCount");
int sendCount = NVS.getInt("sendCount");
int errorCount = NVS.getInt("errorCount");
sprintf(filename, "Info%02d.txt", idx);
info += "WakeupCount: " + String(wakeupCount) + "\r\n";
info += "SendCount: " + String(sendCount) + "\r\n";
info += "EqualCount: " + String(equalCount) + "\r\n";
info += "ErrorCount: " + String(errorCount) + "\r\n";
info += "OverallTime: " + String(allActiveTimeSec) + " sec\r\n";
info += "CurrentTime: " + String(millis()) + " ms\r\n";
info += "Battery: " + String(voltage) + "V " + "(" + String(capacity) + " %)\r\n";
info += "Rssi: " + String(rssi) + " %\r\n";
info += "FrameDiff: " + String(frameDiff, 2) + "\r\n";
info += "FrameAvg: " + String(frameAvg, 2) + "\r\n";
info += "FrameSum: " + String(frameSum, 2) + "\r\n";
if (idx++ > 20) {
idx = 0;
}
NVS.setInt("infoIdx", idx);
String getAll;
String getBody;
Serial.println("Connecting to server: " + (String) serverName);
if (!client.connect(serverName, serverPort)) {
errorCount++;
getBody = "Connection to " + (String) serverName + " failed.";
Serial.println(getBody);
} else {
sendCount++;
Serial.println("Connection successful!");
String head = "--RandomNerdTutorials\r\nContent-Disposition: form-data; name=\"imageFile\"; filename=\"EspCamSend.txt\"\r\nContent-Type: text/plain\r\n\r\n";
String tail = "\r\n--RandomNerdTutorials--\r\n";
uint32_t extraLen = head.length() + tail.length();
uint32_t totalLen = info.length() + extraLen;
client.println("POST " + (String) serverPath + " HTTP/1.1");
client.println("Host: " + (String) serverName);
client.println("Content-Length: " + String(totalLen));
client.println("Content-Type: multipart/form-data; boundary=RandomNerdTutorials");
client.println();
client.print(head);
client.write(info.c_str(), info.length());
client.print(tail);
int timoutTimer = 10000;
long startTimer = millis();
boolean state = false;
while ((startTimer + timoutTimer) > millis()) {
Serial.print(".");
delay(100);
while (client.available()) {
char c = client.read();
if (c == '\n') {
if (getAll.length()==0) {
state=true;
}
getAll = "";
} else if (c != '\r') {
getAll += String(c);
}
if (state==true) {
getBody += String(c);
}
startTimer = millis();
}
if (getBody.length()>0) {
break;
}
}
}
NVS.setInt("sendCount", sendCount);
NVS.setInt("errorCount", errorCount);
Serial.println();
client.stop();
Serial.println(getBody);
return true;
}