Skip to content

Commit 0a4da83

Browse files
committed
HTTPClient add Authorization example
1 parent 85341ff commit 0a4da83

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Authorization.ino
3+
*
4+
* Created on: 09.12.2015
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <ESP8266WiFi.h>
11+
#include <ESP8266WiFiMulti.h>
12+
13+
#include <ESP8266HTTPClient.h>
14+
15+
#define USE_SERIAL Serial
16+
17+
ESP8266WiFiMulti WiFiMulti;
18+
19+
void setup() {
20+
21+
USE_SERIAL.begin(115200);
22+
// USE_SERIAL.setDebugOutput(true);
23+
24+
USE_SERIAL.println();
25+
USE_SERIAL.println();
26+
USE_SERIAL.println();
27+
28+
for(uint8_t t = 4; t > 0; t--) {
29+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
30+
USE_SERIAL.flush();
31+
delay(1000);
32+
}
33+
34+
WiFiMulti.addAP("SSID", "PASSWORD");
35+
36+
}
37+
38+
void loop() {
39+
// wait for WiFi connection
40+
if((WiFiMulti.run() == WL_CONNECTED)) {
41+
42+
HTTPClient http;
43+
44+
USE_SERIAL.print("[HTTP] begin...\n");
45+
// configure traged server and url
46+
47+
48+
http.begin("http://user:[email protected]/test.html");
49+
50+
/*
51+
// or
52+
http.begin("http://192.168.1.12/test.html");
53+
http.setAuthorization("user", "password");
54+
55+
// or
56+
http.begin("http://192.168.1.12/test.html");
57+
http.setAuthorization("dXNlcjpwYXN3b3Jk");
58+
*/
59+
60+
61+
USE_SERIAL.print("[HTTP] GET...\n");
62+
// start connection and send HTTP header
63+
int httpCode = http.GET();
64+
65+
// httpCode will be negative on error
66+
if(httpCode) {
67+
// HTTP header has been send and Server response header has been handled
68+
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
69+
70+
// file found at server
71+
if(httpCode == HTTP_CODE_OK) {
72+
String payload = http.getString();
73+
USE_SERIAL.println(payload);
74+
}
75+
} else {
76+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
77+
}
78+
79+
http.end();
80+
}
81+
82+
delay(10000);
83+
}
84+

0 commit comments

Comments
 (0)