|
28 | 28 | *
|
29 | 29 | */
|
30 | 30 |
|
| 31 | +REDIRECT_STDOUT_TO(Serial) |
| 32 | + |
31 | 33 | #include <Arduino_ConnectionHandler.h>
|
32 | 34 |
|
33 | 35 | #include "arduino_secrets.h"
|
@@ -130,7 +132,49 @@ void loop() {
|
130 | 132 | * which might not guarantee the correct functioning of the ConnectionHandler
|
131 | 133 | * object.
|
132 | 134 | */
|
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 | + |
134 | 178 | }
|
135 | 179 |
|
136 | 180 | void onNetworkConnect() {
|
|
0 commit comments