Skip to content

Commit c02facd

Browse files
committed
add WebClient example.
Note we do not have a serial terminal to report back received data as in original example.
1 parent c847e4c commit c02facd

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

examples/WebClient/WebClient.ino

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Web client
3+
4+
This sketch connects to a website (http://www.google.com)
5+
using the SerialToTCPBridgeProtocol.
6+
7+
created 18 Dec 2009
8+
by David A. Mellis
9+
modified 26 Nov 2017
10+
by Roan Brand, to work over:
11+
https://github.com/RoanBrand/SerialToTCPBridgeProtocol
12+
13+
*/
14+
15+
#include <ArduinoSerialToTCPBridgeClient.h>
16+
17+
// if you don't want to use DNS (and reduce your sketch size)
18+
// use the numeric IP instead of the name for the server:
19+
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
20+
char server[] = "www.google.com"; // name address for Google (using DNS)
21+
22+
// Initialize the Protocol client library
23+
// with the IP address and port of the server
24+
// that you want to connect to (port 80 is default for HTTP):
25+
ArduinoSerialToTCPBridgeClient* client;
26+
27+
void setup() {
28+
client = new ArduinoSerialToTCPBridgeClient();
29+
//delay(1000);
30+
31+
if (client->connect(server, 80)) {
32+
//Serial.println("connected");
33+
// Make a HTTP request:
34+
client->println("GET /search?q=arduino HTTP/1.1");
35+
client->println("Host: www.google.com");
36+
client->println("Connection: close");
37+
client->println();
38+
} else {
39+
// if you didn't get a connection to the server:
40+
//Serial.println("connection failed");
41+
}
42+
}
43+
44+
void loop() {
45+
// if there are incoming bytes available
46+
// from the server, read them and print them:
47+
if (client->available()) {
48+
char c = client->read();
49+
//Serial.print(c);
50+
}
51+
52+
// if the server's disconnected, stop the client:
53+
if (!client->connected()) {
54+
//Serial.println();
55+
//Serial.println("disconnecting.");
56+
client->stop();
57+
58+
// do nothing forevermore:
59+
while (true);
60+
}
61+
}

0 commit comments

Comments
 (0)