Skip to content

Commit c970dec

Browse files
committed
Add HTTPS request sample (esp8266#43)
1 parent 098c71c commit c970dec

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* HTTP over TLS (HTTPS) example sketch
3+
*
4+
* This example demonstrates how to use
5+
* WiFiClientSecure class to access HTTPS API.
6+
* We fetch and display the status of
7+
* esp8266/Arduino project continous integration
8+
* build.
9+
*
10+
* Created by Ivan Grokhotkov, 2015.
11+
* This example is in public domain.
12+
*/
13+
14+
#include <ESP8266WiFi.h>
15+
#include <WiFiClientSecure.h>
16+
17+
const char* ssid = "........";
18+
const char* password = "........";
19+
20+
const char* host = "api.github.com";
21+
const int httpsPort = 443;
22+
23+
void setup() {
24+
Serial.begin(115200);
25+
Serial.println();
26+
Serial.print("connecting to ");
27+
Serial.println(ssid);
28+
WiFi.begin(ssid, password);
29+
while (WiFi.status() != WL_CONNECTED) {
30+
delay(500);
31+
Serial.print(".");
32+
}
33+
Serial.println("");
34+
Serial.println("WiFi connected");
35+
Serial.println("IP address: ");
36+
Serial.println(WiFi.localIP());
37+
38+
39+
// Use WiFiClientSecure class to create TLS connection
40+
41+
WiFiClientSecure client;
42+
Serial.print("connecting to ");
43+
Serial.println(host);
44+
if (!client.connect(host, httpsPort)) {
45+
Serial.println("connection failed");
46+
return;
47+
}
48+
49+
String url = "/repos/esp8266/Arduino/commits/esp8266/status";
50+
Serial.print("requesting URL: ");
51+
Serial.println(url);
52+
53+
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
54+
"Host: " + host + "\r\n" +
55+
"User-Agent: BuildFailureDetectorESP8266\r\n" +
56+
"Connection: close\r\n\r\n");
57+
58+
Serial.println("request sent");
59+
while (client.connected()) {
60+
String line = client.readStringUntil('\n');
61+
if (line == "\r") {
62+
Serial.println("headers received");
63+
break;
64+
}
65+
}
66+
String line = client.readStringUntil('\n');
67+
if (line.startsWith("{\"state\":\"success\"")) {
68+
Serial.println("esp8266/Arduino CI successfull!");
69+
} else {
70+
Serial.println("esp8266/Arduino CI has failed");
71+
}
72+
Serial.println("reply was:");
73+
Serial.println("==========");
74+
Serial.println(line);
75+
Serial.println("==========");
76+
Serial.println("closing connection");
77+
}
78+
79+
void loop() {
80+
}

libraries/ESP8266WiFi/keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ WiFi KEYWORD1
1616
WiFiClient KEYWORD1
1717
WiFiServer KEYWORD1
1818
WiFiUDP KEYWORD1
19+
WiFiClientSecure KEYWORD1
1920

2021
#######################################
2122
# Methods and Functions (KEYWORD2)
@@ -64,4 +65,3 @@ scanNetworks KEYWORD2
6465
WIFI_AP LITERAL1
6566
WIFI_STA LITERAL1
6667
WIFI_AP_STA LITERAL1
67-

0 commit comments

Comments
 (0)