From 374d057e828951d3c483936909170b86da53663b Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 31 Oct 2024 14:44:40 +0100 Subject: [PATCH] Download arduino ascii logo while connected --- .../ConnectionHandlerDemo.ino | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino index 46a0c9d..7bf5c9b 100644 --- a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino +++ b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino @@ -28,6 +28,8 @@ * */ +REDIRECT_STDOUT_TO(Serial) + #include #include "arduino_secrets.h" @@ -130,7 +132,49 @@ void loop() { * which might not guarantee the correct functioning of the ConnectionHandler * object. */ - conMan.check(); + if (conMan.check() != NetworkConnectionState::CONNECTED) { + return; + } + + Client &client = conMan.getClient(); + IPAddress ip = IPAddress(104, 21, 62, 246); + int port = 80; + + Serial.println("\nStarting connection to server..."); + // if you get a connection, report back via serial: + if (!client.connect(ip, port)) { + Serial.println("unable to connect to server"); + return; + } + + Serial.println("connected to server"); + // Make a HTTP request: + size_t w = client.println("GET /asciilogo.txt HTTP/1.1"); + w += client.println("Host: arduino.tips"); + w += client.println("User-Agent: Arduino"); + w += client.println("Connection: close"); + w += client.println(); + Serial.print("Write size is "); + Serial.println(w); + + // if there are incoming bytes available + // from the server, read them and print them: + while (client.connected()) { + size_t len = client.available(); + if (len) { + uint8_t buff[len]; + client.read(buff, len); + Serial.write(buff, len); + } + delay(0); + } + + // if the server's disconnected, stop the client: + Serial.println(); + Serial.println("disconnecting from server."); + client.stop(); + delay(1000); + } void onNetworkConnect() {