-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcptest.ino
104 lines (88 loc) · 3.16 KB
/
tcptest.ino
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
// MAKE SURE you set the IP, PORT values!
#define SERVER_IP {0,0,0,0}
#define SERVER_PORT 0
#define TIMEOUT_INITIAL_RESPONSE 20000
#define TIMEOUT_RESPONSE_READ 5000
#define RECONNECT_DELAY 3000
void setup()
{
// Give a person debugging via USB a moment to connect, if they want, before we start
Serial.begin(9600);
for(int i = 10;i > 0;i--)
{
Serial.println("Starting in " + String(i,DEC));
delay(1000);
}
}
void loop()
{
Serial.println("");
Serial.println("Connecting...");
byte ip[] = SERVER_IP;
TCPClient client;
client.connect(ip, SERVER_PORT);
if (client.connected())
{
Serial.println("Connected to server.");
client.println("Hello, server!");
unsigned int waitCount = 0;
unsigned long lastTime = millis();
// Wait until the server either disconnects, gives us a response, or the timeout is exceeded
Serial.print("Waiting a for response.");
while(client.connected() && !client.available() && millis() - lastTime < TIMEOUT_INITIAL_RESPONSE)
{
// Visual wait indicator for debugging
if(waitCount++ % 100 == 0)
{
Serial.print(".");
}
// While we are waiting, which could be a while, allow cloud stuff to happen as needed
SPARK_WLAN_Loop();
}
// Show a wait summary for debugging
Serial.println("(wait loops: " + String(waitCount,DEC) + ")");
// We stopped waiting because we actually got something
if(client.connected() && client.available())
{
waitCount = 0;
int charCount = 0;
int lastReadTime = millis();
// Keep going until the server either disconnects, or stops feeding us data in a timely manner
Serial.print("Reading response.");
while(client.connected() && millis() - lastReadTime < TIMEOUT_RESPONSE_READ)
{
// Visual wait indicator for debugging
if(waitCount++ % 100 == 0)
{
Serial.print(".");
}
// We get something!
while(client.available())
{
char c = client.read();
++charCount;
// Visual data received indicator for debugging
Serial.print("!");
// Make note of when this happened
lastReadTime = millis();
}
}
// Show a wait/read summary for debugging
Serial.println("(wait loops: " + String(waitCount,DEC) + ", chars read: " + String(charCount,DEC) + ")");
}
else
{
Serial.println("No/empty response");
}
// Just to be nice, let's make sure we finish clean
Serial.println("Explicit flush");
client.flush();
Serial.println("Explicit stop");
client.stop();
}
else
{
Serial.println("Connection failed.");
}
delay(RECONNECT_DELAY);
}