|
| 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 continuous integration |
| 8 | + build. |
| 9 | +
|
| 10 | + Limitations: |
| 11 | + only RSA certificates |
| 12 | + no support of Perfect Forward Secrecy (PFS) |
| 13 | + TLSv1.2 is supported since version 2.4.0-rc1 |
| 14 | +
|
| 15 | + Created by Ivan Grokhotkov, 2015. |
| 16 | + This example is in public domain. |
| 17 | +*/ |
| 18 | + |
| 19 | +#include <ESP8266WiFi.h> |
| 20 | + |
| 21 | +// force use of AxTLS (BearSSL is now default) |
| 22 | +#include <WiFiClientSecureAxTLS.h> |
| 23 | +using namespace axTLS; |
| 24 | + |
| 25 | +#ifndef STASSID |
| 26 | +#define STASSID "your-ssid" |
| 27 | +#define PSK "your-password" |
| 28 | +#endif |
| 29 | + |
| 30 | +const char* ssid = STASSID; |
| 31 | +const char* password = PSK; |
| 32 | + |
| 33 | +const char* host = "api.github.com"; |
| 34 | +const int httpsPort = 443; |
| 35 | + |
| 36 | +// Use web browser to view and copy |
| 37 | +// SHA1 fingerprint of the certificate |
| 38 | +const char* fingerprint = "5F F1 60 31 09 04 3E F2 90 D2 B0 8A 50 38 04 E8 37 9F BC 76"; |
| 39 | + |
| 40 | +void setup() { |
| 41 | + Serial.begin(115200); |
| 42 | + Serial.println(); |
| 43 | + Serial.print("connecting to "); |
| 44 | + Serial.println(ssid); |
| 45 | + WiFi.mode(WIFI_STA); |
| 46 | + WiFi.begin(ssid, password); |
| 47 | + while (WiFi.status() != WL_CONNECTED) { |
| 48 | + delay(500); |
| 49 | + Serial.print("."); |
| 50 | + } |
| 51 | + Serial.println(""); |
| 52 | + Serial.println("WiFi connected"); |
| 53 | + Serial.println("IP address: "); |
| 54 | + Serial.println(WiFi.localIP()); |
| 55 | + |
| 56 | + // Use WiFiClientSecure class to create TLS connection |
| 57 | + WiFiClientSecure client; |
| 58 | + Serial.print("connecting to "); |
| 59 | + Serial.println(host); |
| 60 | + if (!client.connect(host, httpsPort)) { |
| 61 | + Serial.println("connection failed"); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if (client.verify(fingerprint, host)) { |
| 66 | + Serial.println("certificate matches"); |
| 67 | + } else { |
| 68 | + Serial.println("certificate doesn't match"); |
| 69 | + } |
| 70 | + |
| 71 | + String url = "/repos/esp8266/Arduino/commits/master/status"; |
| 72 | + Serial.print("requesting URL: "); |
| 73 | + Serial.println(url); |
| 74 | + |
| 75 | + client.print(String("GET ") + url + " HTTP/1.1\r\n" + |
| 76 | + "Host: " + host + "\r\n" + |
| 77 | + "User-Agent: BuildFailureDetectorESP8266\r\n" + |
| 78 | + "Connection: close\r\n\r\n"); |
| 79 | + |
| 80 | + Serial.println("request sent"); |
| 81 | + while (client.connected()) { |
| 82 | + String line = client.readStringUntil('\n'); |
| 83 | + if (line == "\r") { |
| 84 | + Serial.println("headers received"); |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + String line = client.readStringUntil('\n'); |
| 89 | + if (line.startsWith("{\"state\":\"success\"")) { |
| 90 | + Serial.println("esp8266/Arduino CI successfull!"); |
| 91 | + } else { |
| 92 | + Serial.println("esp8266/Arduino CI has failed"); |
| 93 | + } |
| 94 | + Serial.println("reply was:"); |
| 95 | + Serial.println("=========="); |
| 96 | + Serial.println(line); |
| 97 | + Serial.println("=========="); |
| 98 | + Serial.println("closing connection"); |
| 99 | +} |
| 100 | + |
| 101 | +void loop() { |
| 102 | +} |
0 commit comments