forked from cubesatplatform/basestation_wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbasestation.cpp
More file actions
252 lines (211 loc) · 6.95 KB
/
cbasestation.cpp
File metadata and controls
252 lines (211 loc) · 6.95 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
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
#include "cbasestation.h"
bool CBaseStation::isCmdTime(){
if(millis() - lastCmd >= (unsigned long)CMD_INTERVAL * (unsigned long)1000) {
lastCmd=millis();
return true;
}
return false;
}
std::string CBaseStation::getField(){
long ct=millis();
std::string str;
while(millis()<ct+20000){
str=kbloop();
if(str.size())
break;
}
return str;
}
void CBaseStation::addWifi(){
std::string ssid;
std::string pwd;
writeconsoleln("______________________________________");
writeconsole("Please enter Network: ");
ssid=getField();
writeconsoleln(ssid);
writeconsole("Please enter Password: ");
pwd=getField();
writeconsoleln(pwd);
writeconsoleln("______________________________________");
if(ssid.size()&&pwd.size())
wifiMulti.addAP(ssid.c_str(),pwd.c_str());
}
void CBaseStation::connectWifi() {
Serial.begin(serialBaudRate);
writeconsole("Starting BaseStation ----------------- Connecting to wifi: ");
// You can add the id and password for several WiFi networks
// it will connect to the first one it sees.
wifiMulti.addAP("Truffle1", "Alexander1");
wifiMulti.addAP("Cabana", "alexander");
while (wifiMulti.run() != WL_CONNECTED) {
delay(delayWaitingForWifiConnection);
writeconsole(".");
}
writeconsoleln("");
writeconsole("WiFi connected to ");
writeconsole(WiFi.SSID());
writeconsole(" with IP ");
writeconsoleln(String(WiFi.localIP()));
}
void CBaseStation::insertSatData(CMsg &msg){
std::string str=msg.serialize();
str=urlencode(str);
writeconsoleln("---------------------------------------------------"); writeconsole("Inserting Data:<"); writeconsole(str.c_str()); writeconsoleln(">"); writeconsoleln("---------------------------------------------------");
client.setTimeout(10000);
if (!client.connect(host, httpsPort)) {
writeconsoleln("Connection failed");
}
else{
std::string str1=std::string("GET ")+logpath+str+" HTTP/1.1\r\n" +"Host: " + host + "\r\n" +"User-Agent: SeaKing\r\n" +"Connection: close\r\n\r\n";
client.print(str1.c_str());
writeconsoleln(str1.c_str());
client.stop();
}
}
void CBaseStation::updateAllReceivedCommands(){
if (!MSG->ReceivedList.size()) return;
int count=0;
writeconsoleln("updateAllReceivedCommands() {");
while(MSG->ReceivedList.size()){
CMsg msg=MSG->ReceivedList.front();
if (msg.getACK()=="1") {
updateAckCmd(msg);
}
else{
insertSatData(msg);
}
MSG->ReceivedList.pop_front();
count++;
if(count>10) break;
}
writeconsoleln("}END updateAllReceivedCommands()");
}
void CBaseStation::updateAllTransmittedCommands(){
int count=0;
while(MSG->TransmittedList.size()){
CMsg msg=MSG->TransmittedList.front();
updateTransmittedCmd(msg);
MSG->TransmittedList.pop_front();
count++;
if(count>10) break;
}
}
void CBaseStation::updateTransmittedCmd(CMsg &msg){
std::string cid=msg.getCID();
writeconsole("Transmit Update: ");
writeconsoleln(cid.c_str());
client.setTimeout(10000);
if (!client.connect(host, httpsPort)) {
writeconsoleln("Connection failed");
}
else{
std::string str1=std::string("GET ")+updatecmdpath+cid+" HTTP/1.1\r\n" +"Host: " + host + "\r\n" +"User-Agent: SeaKing\r\n" +"Connection: close\r\n\r\n";
client.print(str1.c_str());
// Check HTTP status
char status[32] = {0};
client.readBytesUntil('\r', status, sizeof(status));
// It should be "HTTP/1.0 200 OK" or "HTTP/1.1 200 OK"
if (strcmp(status + 9, "200 OK") != 0) {
writeconsole("Unexpected response: ");
writeconsoleln(status);
return;
}
client.stop();
}
}
void CBaseStation::updateAckCmd(CMsg &msg){
if(msg.getACK()!="1")
return;
std::string cid=msg.getCID();
if (cid.length()<1) return;
writeconsoleln();
writeconsole("Transmit Update: ");
writeconsoleln(cid.c_str());
client.setTimeout(10000);
if (!client.connect(host, httpsPort)) {
writeconsoleln("Connection failed");
}
else{
std::string str1=std::string("GET ")+updateackpath+cid.c_str()+" HTTP/1.1\r\n" +"Host: " + host + "\r\n" +"User-Agent: SeaKing\r\n" +"Connection: close\r\n\r\n";
client.print(str1.c_str());
// Check HTTP status
char status[32] = {0};
client.readBytesUntil('\r', status, sizeof(status));
// It should be "HTTP/1.0 200 OK" or "HTTP/1.1 200 OK"
if (strcmp(status + 9, "200 OK") != 0) {
writeconsole("Unexpected response: ");
writeconsoleln(status);
return;
}
client.stop();
}
}
void CBaseStation::getEchoData(const char * strdata=""){
std::string str=strdata;
str=urlencode(str);
client.setTimeout(10000);
if (!client.connect(host, httpsPort)) {
writeconsoleln("Connection failed");
}
else{
std::string str1=std::string("GET ")+consolelogpath+str+" HTTP/1.1\r\n" +"Host: " + host + "\r\n" +"User-Agent: SeaKing\r\n" +"Connection: close\r\n\r\n";
client.print(str1.c_str());
client.stop();
}
}
void CBaseStation::getLastCommand() {
long counter=0,endt=0,startt=millis();
StaticJsonDocument<500> doc;
client.setTimeout(10000);
client.setInsecure();
if (!client.connect(host, httpsPort)) {
writeconsoleln("Connection failed");
writeconsoleln(host);
writeconsoleln(httpsPort);
delay(2000);
writeconsoleln("Restarting");
connectWifi();
}
else{
std::string str1=std::string("GET ")+cmdpath+" HTTP/1.1\r\n" +"Host: " + host + "\r\n" +"User-Agent: SeaKing\r\n" +"Connection: close\r\n\r\n";
writeconsoleln(str1.c_str());
client.print(str1.c_str());
while (client.connected()) {
counter++;
char endOfHeaders[] = "\r\n\r\n"; // Skip HTTP headers
if (!client.find(endOfHeaders)) { //May return nothing
return;
}
String Stmp= client.readStringUntil('\n');
std::string line=Stmp.c_str();
if(line.length()<3) {
break;
}
line=line.substr(1,line.length()-1); //Gets rid of [] This JSon doesnt like it
DeserializationError error = deserializeJson(doc, line);
if (error)
{
writeconsole("deserializeJson() failed: ");
writeconsoleln(error.f_str());
return;
}
else
{
if(line.length()>2) {
writeconsoleln(line.c_str());
std::string datastr=doc["data"];
CMsg msg(datastr);
msg.setCID(doc["cid"]);
writeconsoleln(msg.serialize().c_str());
radio.addTransmitList(msg);
}
}
if (line == "\r") {
// break;
}
}
endt=millis();
writeconsoleln(); writeconsole(counter); writeconsole("Get Time: "); writeconsoleln((float)((endt-startt)/1000.0));
client.stop();
}
}