|
| 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 | +} |
0 commit comments