-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkManager.cpp
278 lines (227 loc) · 7.54 KB
/
NetworkManager.cpp
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "NetworkManager.h"
NetworkManager::NetworkManager():
radio(33, 27), // RADIO CE-PINS
network(radio),
mesh(radio, network),
client(server.c_str(), port, NULL, wifiClient),
timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000)
{
lastReconnectAttempt = 0;
count = 0;
status = WL_IDLE_STATUS;
}
void NetworkManager::init() {
Serial.println(F("Master Node - Gateway"));
mesh.setNodeID(0);
Serial.print(F("NodeID: "));
Serial.println(mesh.getNodeID());
mesh.begin();
connectWiFi();
timeClient.begin();
timeClient.update();
setConnectionString(connectionString);
connectCloud();
}
void NetworkManager::updateTime() {
timeClient.update();
}
// ---- - MESH
void NetworkManager::updateMesh() {
mesh.update();
}
void NetworkManager::updateDHCP() {
mesh.DHCP();
}
void NetworkManager::printInfo() {
Serial.println(F(" "));
Serial.println(F("********WIFI INFO***********"));
Serial.print(F("SSID: "));
Serial.print(ssid);
Serial.print(F(" ** IP: "));
Serial.print(WiFi.localIP());
Serial.println(" ** Time:" + timeClient.getFormattedTime());
Serial.print(F("-----------------------------"));
Serial.println(F(" "));
Serial.println(F("********Mesh Network********"));
Serial.println(" ");
Serial.println(F("********Assigned Addresses********"));
for (int i = 0; i < mesh.addrListTop; i++) {
Serial.print("NodeID: ");
Serial.print(mesh.addrList[i].nodeID);
Serial.print(" RF24Network Address: 0");
Serial.println(mesh.addrList[i].address, OCT);
}
Serial.println(F("**********************************"));
}
void NetworkManager::sendPayloadToCloud() {
RF24NetworkHeader header;
network.peek(header);
String quote = "\"";
switch (header.type) {
case 'M':
jsonString = "";
network.read(header, &incommingDataArray, sizeof(incommingDataArray));
nodeID = mesh.getNodeID(header.from_node);
temp = incommingDataArray[0];
hum = incommingDataArray[1];
voc = incommingDataArray[2];
c02 = incommingDataArray[3];
motion = incommingDataArray[4];
sound = incommingDataArray[5];
error = incommingDataArray[6];
count = count + 1;
delay(500);
jsonString =
"{\"ID\":" + quote + nodeID + quote + "," +
"\"t\":" + temp + "," +
"\"h\":" + hum + "," +
"\"v\":" + voc + "," +
"\"c\":" + c02 + "," +
"\"m\":" + motion + "," +
"\"time\":" + timeClient.getEpochTime() + "," +
"\"s\":" + sound + "}";
delay(500);
Serial.println(jsonString);
if (client.publish(pubEndpoint.c_str(), jsonString.c_str())) {
wifiClient.flush();
Serial.println("Publish ok");
} else {
Serial.println("Publish failed");
Serial.println(errorCodes(client.state()));
if (client.state() < 0 || client.state() > 0) {
wifiClient.flush();
wifiClient.stop();
yield();
connectWiFi();
connectCloud();
}
}
break;
default:
network.read(header, 0, 0);
Serial.println(header.type);
break;
}
}
// ------ WIFI
void NetworkManager::connectWiFi() {
while (WiFi.status() != WL_CONNECTED) {
delay(10);
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid.c_str(), pass.c_str());
// wait 10 seconds for connection:
delay(10000);
Serial.println("");
if (WiFi.localIP() != 0) {
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());
Serial.println();
} else {
Serial.println(F("Connection to WiFi failed...Reconnecting"));
}
delay(500);
}
}
/// --- CLOUD FUNCTIONS ------------
void NetworkManager::connectCloud() {
Serial.println(F("Connecting to Cloud"));
Serial.println();
client.setServer(server.c_str(), port);
while (!client.connected()) {
//Serial.println("Fastnat på clientconnect");
// CREATE A NEW SAS TimeNow + 24 hours
newSas = createSas(key, url);
if (client.connect(clientId.c_str(), hub_user.c_str(), newSas.c_str())) { // Here is where the connection to the cloud starts
Serial.println(F("Client Connected To Cloud"));
client.subscribe(subEndpoint.c_str());
yield();
} else {
Serial.println();
Serial.println(F("Client Failed To Connect To Cloud"));
Serial.print(F("Error Code: "));
Serial.println(errorCodes(client.state()));
Serial.println("Reconnecting....");
delay(500);
}
}
}
String NetworkManager::errorCodes(int errorCode) {
switch (errorCode) {
case -4: return "The server didn't respond within the keepalive time.";
case -3: return "The network connection was broken.";
case -2: return "The network connection failed.";
case -1: return "The client disconnected cleanly.";
case 0: return "The client connected.";
case 1: return "The server doesn't support the requested version of MQTT.";
case 2: return "The server rejected the client identifier.";
case 3: return "The server was unable to accept the connection.";
case 4: return "The username/password were rejected.";
case 5: return "The client was not authorized to connect.";
default: break;
}
}
String NetworkManager::createSas(char* key, String url) {
sasExpiryTime = timeClient.getEpochTime() + sasExpiryPeriodInSeconds;
String stringToSign = url + "\n" + sasExpiryTime;
int keyLength = strlen(key);
int decodedKeyLength = base64_dec_len(key, keyLength);
char decodedKey[decodedKeyLength]; //allocate char array big enough for the base64 decoded key
base64_decode(decodedKey, key, keyLength); //decode key
Sha256.initHmac((const uint8_t*)decodedKey, decodedKeyLength);
Sha256.print(stringToSign);
char* sign = (char*) Sha256.resultHmac();
// END: Create signature
// START: Get base64 of signature
int encodedSignLen = base64_enc_len(HASH_LENGTH);
char encodedSign[encodedSignLen];
base64_encode(encodedSign, sign, HASH_LENGTH);
// SharedAccessSignature
return "SharedAccessSignature sr=" + url + "&sig=" + urlEncode(encodedSign) + "&se=" + sasExpiryTime;
// END: create SAS
}
String NetworkManager::urlEncode(const char* msg)
{
const char *hex = "0123456789abcdef";
String encodedMsg = "";
while (*msg != '\0') {
if ( ('a' <= *msg && *msg <= 'z')
|| ('A' <= *msg && *msg <= 'Z')
|| ('0' <= *msg && *msg <= '9') ) {
encodedMsg += *msg;
} else {
encodedMsg += '%';
encodedMsg += hex[*msg >> 4];
encodedMsg += hex[*msg & 15];
}
msg++;
}
return encodedMsg;
}
const char* NetworkManager::GetStringValue(String value) {
int len = value.length() + 1;
char *temp = new char[len];
value.toCharArray(temp, len);
return temp;
}
void NetworkManager::setConnectionString(String cs) {
host = GetStringValue(splitStringByIndex(splitStringByIndex(cs, ';', 0), '=', 1));
deviceId = GetStringValue(splitStringByIndex(splitStringByIndex(cs, ';', 1), '=', 1));
key = (char*)GetStringValue(splitStringByIndex(splitStringByIndex(cs, ';', 2), '=', 1));
}
String NetworkManager::splitStringByIndex(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}