Skip to content

Commit bdb8c02

Browse files
committed
Add new basic auth example
1 parent 68aebb1 commit bdb8c02

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
GET client with HTTP basic authentication for ArduinoHttpClient library
3+
Connects to server once every five seconds, sends a GET request
4+
5+
note: WiFi SSID and password are stored in config.h file.
6+
If it is not present, add a new tab, call it "config.h"
7+
and add the following variables:
8+
char ssid[] = "ssid"; // your network SSID (name)
9+
char pass[] = "password"; // your network password
10+
11+
created 14 Feb 2016
12+
by Tom Igoe
13+
modified 3 Jan 2017 to add HTTP basic authentication
14+
by Sandeep Mistry
15+
16+
this example is in the public domain
17+
*/
18+
#include <ArduinoHttpClient.h>
19+
#include <WiFi101.h>
20+
#include "config.h"
21+
22+
char serverAddress[] = "192.168.0.3"; // server address
23+
int port = 8080;
24+
25+
WiFiClient wifi;
26+
HttpClient client = HttpClient(wifi, serverAddress, port);
27+
int status = WL_IDLE_STATUS;
28+
String response;
29+
int statusCode = 0;
30+
31+
void setup() {
32+
Serial.begin(9600);
33+
while ( status != WL_CONNECTED) {
34+
Serial.print("Attempting to connect to Network named: ");
35+
Serial.println(ssid); // print the network name (SSID);
36+
37+
// Connect to WPA/WPA2 network:
38+
status = WiFi.begin(ssid, pass);
39+
}
40+
41+
// print the SSID of the network you're attached to:
42+
Serial.print("SSID: ");
43+
Serial.println(WiFi.SSID());
44+
45+
// print your WiFi shield's IP address:
46+
IPAddress ip = WiFi.localIP();
47+
Serial.print("IP Address: ");
48+
Serial.println(ip);
49+
}
50+
51+
void loop() {
52+
Serial.println("making GET request with HTTP basic authentication");
53+
client.beginRequest();
54+
client.get("/secure");
55+
client.sendBasicAuth("username", "password"); // send the username and password for authentication
56+
client.endRequest();
57+
58+
// read the status code and body of the response
59+
statusCode = client.responseStatusCode();
60+
response = client.responseBody();
61+
62+
Serial.print("Status code: ");
63+
Serial.println(statusCode);
64+
Serial.print("Response: ");
65+
Serial.println(response);
66+
Serial.println("Wait five seconds");
67+
delay(5000);
68+
}
69+

examples/BasicAuthGet/config.h

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
char ssid[] = "ssid"; // your network SSID (name)
2+
char pass[] = "password"; // your network password
3+

0 commit comments

Comments
 (0)