-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfritzmeter.ino
More file actions
472 lines (344 loc) · 10.5 KB
/
fritzmeter.ino
File metadata and controls
472 lines (344 loc) · 10.5 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
Fritzmeter
This sketch connects to a Fritz!Box DSL router
using an Arduino Ethernet shield.
Other UPNP capable routers might work as well but are untested.
Please let me know if you have tried any other routers.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Servos attached to pins 5 (receive rate) and 6 (transmit rate)
Credits:
Parts of the code are taken from the web client example code by David A. Mellis
The XML parsing is adapted from Bob S. aka XTALKER's weather data XML extractor.
Originally created 20 Mar 2012
last modified Jan 01 2013 - release version
by Andy Reischle
www.reischle.net
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
#define MAX_STRING_LEN 35
Servo srxservo;
Servo stxservo;
// MAC address for controller.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// most variables are global for convenience
String nsb; //newSentBytes
String nrb; //nweReceiveBytes
String xml; //Buffer for xml request string
unsigned long nsbl; //bits per second send
unsigned long nrbl; //bits persecond received
unsigned long mxrx; //max bits per second receive
unsigned long mxtx; //max bits per second transmit
int rxservo=0;
int txservo=0;
int currxservo=0;
int curtxservo=180;
// Setup vars for serialEvent
char tagStr[MAX_STRING_LEN] = "";
char dataStr[MAX_STRING_LEN] = "";
char tmpStr[MAX_STRING_LEN] = "";
char endTag[3] = {'<', '/', '\0'};
int len;
// Flags to differentiate XML tags from document elements (ie. data)
boolean tagFlag = false;
boolean dataFlag = false;
// Initialize the Ethernet client library
// with the IP address and port of the server
EthernetClient client;
void setup()
{
// start the serial library:
Serial.begin(9600);
Serial.println("Fritzmeter 20130101/ARe");
Serial.println("Starting setup...");
// Attach servos to pins 5 and 6
srxservo.attach(5);
stxservo.attach(6);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
softReset();
}
// give the Ethernet shield a second to initialize:
delay(1000);
// print local IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
// print local Gateway address:
Serial.print("My Gateway address: ");
Serial.println(Ethernet.gatewayIP());
// read maximum bandwidth values
// Call the xml Processor to obtain maximum useable bandwidth
netread(1);
Serial.print("DSL maxTXspeed:");
Serial.println(mxtx);
Serial.print("DSL maxRXspeed:");
Serial.println(mxrx);
Serial.println(" Setup done.");
}
void loop()
{
// Call the xml Processor to obtain currently used bandwidth
netread(2);
//Display Send Speed
Serial.print("Current TX bits/s:");
Serial.println(nsbl);
txservo=map(nsbl,0,mxtx,179,1);
// Serial.print("Servo TX value: ");
// Serial.println(txservo);
//Display Receive Speed
Serial.print("Current RX bits/s:");
Serial.println(nrbl);
rxservo=map(nrbl,0,mxrx,1,179);
//Serial.print("Servo RX value: ");
//Serial.println(rxservo);
//to avoid jerky servo movements, let them crawl slowly towards the target value
crawlservo();
}
/////////////////////
// Functions //
// Trying to keep //
// the main loop //
// clean //
/////////////////////
//////////////////////////////////////////////
// CrawlServo //
// The Servos will slowly crawl towards //
// the target value for 2.5 seconds //
/////////////////////////////////////////////
void crawlservo()
{
for (int i=0; i <= 100; i++)
{
if (currxservo < rxservo) currxservo++;
if (currxservo > rxservo) currxservo--;
if (curtxservo < txservo) curtxservo++;
if (curtxservo > txservo) curtxservo--;
stxservo.write(curtxservo);
srxservo.write(currxservo);
delay (25);
}
}
/////////////////////
// Serial Event //
//read individual //
//chars from the //
//ethernet session //
//and assemble //
//xml tags //
/////////////////////
void serialEvent() {
// Read a char
char inChar = client.read();
//Serial.print(inChar);
if (inChar == '<') {
addChar(inChar, tmpStr);
tagFlag = true;
dataFlag = false;
} else if (inChar == '>') {
addChar(inChar, tmpStr);
if (tagFlag) {
strncpy(tagStr, tmpStr, strlen(tmpStr)+1);
}
// Clear tmp
clearStr(tmpStr);
tagFlag = false;
dataFlag = true;
} else if (inChar != 10) {
if (tagFlag) {
// Add tag char to string
addChar(inChar, tmpStr);
// Check for </XML> end tag, ignore it
if ( tagFlag && strcmp(tmpStr, endTag) == 0 ) {
clearStr(tmpStr);
tagFlag = false;
dataFlag = false;
}
}
if (dataFlag) {
// Add data char to string
addChar(inChar, dataStr);
}
}
// If a LF, process the line
if (inChar == 10 ) {
/*
Serial.print("tagStr: ");
Serial.println(tagStr);
Serial.print("dataStr: ");
Serial.println(dataStr);
*/
// Find specific tags and print data
if (matchTag("<NewByteSendRate>")) {
Serial.print("TX: ");
nsbl = strtoul(dataStr,NULL,0);
nsbl=nsbl*8; //convert bytes to bits
}
if (matchTag("<NewByteReceiveRate>")) {
Serial.print("RX: ");
// Serial.print(dataStr);
nrbl = strtoul(dataStr,NULL,0);
nrbl=nrbl*8; //mach bits
}
if (matchTag("<NewLayer1UpstreamMaxBitRate>"))
{
// Serial.print("MaxTX: ");
mxtx = strtoul(dataStr,NULL,0);
// mxtx=mxtx*8; //mach bits
// Serial.println(mxtx);
}
if (matchTag("<NewLayer1DownstreamMaxBitRate>"))
{
// Serial.print("MaxRX: ");
// Serial.print(dataStr);
mxrx = strtoul(dataStr,NULL,0);
// mxrx=mxrx*8; //mach bits
// Serial.println(mxrx);
}
// Clear all strings
clearStr(tmpStr);
clearStr(tagStr);
clearStr(dataStr);
// Clear Flags
tagFlag = false;
dataFlag = false;
}
}
/////////////////////
// ClearStr //
//seems we have to //
//do that manually //
/////////////////////
// Function to clear a string
void clearStr (char* str) {
int len = strlen(str);
for (int c = 0; c < len; c++) {
str[c] = 0;
}
}
/////////////////////
// addChar //
// assemble String //
//from characters //
/////////////////////
//Function to add a char to a string and check its length
void addChar (char ch, char* str) {
char *tagMsg = "<TRUNCATED_TAG>";
char *dataMsg = "-TRUNCATED_DATA-";
// Check the max size of the string to make sure it doesn't grow too
// big. If string is beyond MAX_STRING_LEN assume it is unimportant
// and replace it with a warning message.
if (strlen(str) > MAX_STRING_LEN - 2) {
if (tagFlag) {
clearStr(tagStr);
strcpy(tagStr,tagMsg);
}
if (dataFlag) {
clearStr(dataStr);
strcpy(dataStr,dataMsg);
}
// Clear the temp buffer and flags to stop current processing
clearStr(tmpStr);
tagFlag = false;
dataFlag = false;
} else {
// Add char to string
str[strlen(str)] = ch;
}
}
/////////////////////
// matchTag //
/////////////////////
// Function to check the current tag for a specific string
boolean matchTag (char* searchTag) {
if ( strcmp(tagStr, searchTag) == 0 ) {
return true;
} else {
return false;
}
}
/////////////////////
// softReset //
// just in case. //
// Not recommended //
/////////////////////
// In case all goes wrong
void softReset(){
asm volatile (" jmp 0");
}
/////////////////////
// netread //
/////////////////////
// Do the network reading stuff
void netread(int fselector)
{
Serial.println("connecting...");
// make the request to the server
if (client.connect(Ethernet.gatewayIP(), 49000)) {
Serial.println("connected");
// Make a HTTP POST request:
Serial.println("Sending POST request");
client.println("POST /upnp/control/WANIPConn1 HTTP/1.1");
client.print("HOST: ");
client.print(Ethernet.gatewayIP());
client.println(":49000");
if (fselector==2){
client.println("SOAPACTION: \"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1#GetAddonInfos\"");
}
if (fselector==1){
client.println("SOAPACTION: \"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1#GetCommonLinkProperties\"");
}
client.println("Content-Type: text/xml; charset=\"utf-8\"");
client.print("Content-Length: ");
// client.println(xml.length());
// With fselector set to 1 we request the maximum possible DSL bandwidth
if (fselector==1)
{
// Serial.print("fselector ist auf: ");
// Serial.println(fselector);
// client.println("309");
client.println("292");
client.println();
client.println(F("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n<s:Body>\n<u:GetCommonLinkProperties xmlns:u=\"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1\" />\n</s:Body>\n</s:Envelope>"));
}
// With fselector 2 we read the currently consumed bandwidth
if (fselector==2)
{
// Serial.print("fselector ist auf: ");
// Serial.println(fselector);
client.println("282");
client.println();
client.println(F("<?xml version=\"1.0\" encodin=\"utf-8\"?>\n<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n<s:Body>\n<u:GetAddonInfos xmlns:u=\"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1\" />\n</s:Body>\n</s:Envelope>"));
}
//client.println(xml);
client.println();
Serial.println("Post request sent");
}
else {
// no connection to the server:
Serial.println("connection failed in requesttraffic");
//for(;;)
//;
softReset();
}
//wait for the server to answer
while (!client.available())
{
// Serial.println("Waiting for server...");
;
}
//get data
while (client.available()) {
//char c = client.read();
//delay(1);
//Serial.print(c);
serialEvent();
}
client.flush();
Serial.println();
Serial.print("Disconnecting...");
client.stop();
Serial.println("done.");
client.flush();
}