Skip to content

Commit 374d057

Browse files
committed
Download arduino ascii logo while connected
1 parent 8dcb05d commit 374d057

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino

+45-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
*
2929
*/
3030

31+
REDIRECT_STDOUT_TO(Serial)
32+
3133
#include <Arduino_ConnectionHandler.h>
3234

3335
#include "arduino_secrets.h"
@@ -130,7 +132,49 @@ void loop() {
130132
* which might not guarantee the correct functioning of the ConnectionHandler
131133
* object.
132134
*/
133-
conMan.check();
135+
if (conMan.check() != NetworkConnectionState::CONNECTED) {
136+
return;
137+
}
138+
139+
Client &client = conMan.getClient();
140+
IPAddress ip = IPAddress(104, 21, 62, 246);
141+
int port = 80;
142+
143+
Serial.println("\nStarting connection to server...");
144+
// if you get a connection, report back via serial:
145+
if (!client.connect(ip, port)) {
146+
Serial.println("unable to connect to server");
147+
return;
148+
}
149+
150+
Serial.println("connected to server");
151+
// Make a HTTP request:
152+
size_t w = client.println("GET /asciilogo.txt HTTP/1.1");
153+
w += client.println("Host: arduino.tips");
154+
w += client.println("User-Agent: Arduino");
155+
w += client.println("Connection: close");
156+
w += client.println();
157+
Serial.print("Write size is ");
158+
Serial.println(w);
159+
160+
// if there are incoming bytes available
161+
// from the server, read them and print them:
162+
while (client.connected()) {
163+
size_t len = client.available();
164+
if (len) {
165+
uint8_t buff[len];
166+
client.read(buff, len);
167+
Serial.write(buff, len);
168+
}
169+
delay(0);
170+
}
171+
172+
// if the server's disconnected, stop the client:
173+
Serial.println();
174+
Serial.println("disconnecting from server.");
175+
client.stop();
176+
delay(1000);
177+
134178
}
135179

136180
void onNetworkConnect() {

0 commit comments

Comments
 (0)