doktorinjh

HTTPSRequest_Liftie

Mar 11th, 2018
119
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. * 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 = "liftie.info";
  21. const int httpsPort = 443;
  22.  
  23. // Use web browser to view and copy
  24. // SHA1 fingerprint of the certificate
  25. const char* fingerprint = "bf 19 62 f1 ee a4 d0 2e ee a7 d8 5a de 38 6e ff 95 ce a6 3c";
  26.  
  27. void setup() {
  28. Serial.begin(115200);
  29. Serial.println();
  30. Serial.print("connecting to ");
  31. Serial.println(ssid);
  32. WiFi.begin(ssid, password);
  33. while (WiFi.status() != WL_CONNECTED) {
  34. delay(500);
  35. Serial.print(".");
  36. }
  37. Serial.println("");
  38. Serial.println("WiFi connected");
  39. Serial.println("IP address: ");
  40. Serial.println(WiFi.localIP());
  41.  
  42. // Use WiFiClientSecure class to create TLS connection
  43. WiFiClientSecure client;
  44. Serial.print("connecting to ");
  45. Serial.println(host);
  46. if (!client.connect(host, httpsPort)) {
  47. Serial.println("connection failed");
  48. return;
  49. }
  50.  
  51. if (client.verify(fingerprint, host)) {
  52. Serial.println("certificate matches");
  53. } else {
  54. Serial.println("certificate doesn't match");
  55. }
  56.  
  57. String url = "/api/resort/gunstock";
  58. Serial.print("requesting URL: ");
  59. Serial.println(url);
  60.  
  61. client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  62. "Host: " + host + "\r\n" +
  63. "User-Agent: BuildFailureDetectorESP8266\r\n" +
  64. "Connection: close\r\n\r\n");
  65.  
  66. Serial.println("request sent");
  67. while (client.connected()) {
  68. String line = client.readStringUntil('\n');
  69. if (line == "\r") {
  70. Serial.println("headers received");
  71. break;
  72. }
  73. }
  74. String line = client.readStringUntil('\n');
  75. if (line.startsWith("{\"state\":\"success\"")) {
  76. Serial.println("esp8266/Arduino CI successfull!");
  77. } else {
  78. Serial.println("esp8266/Arduino CI has failed");
  79. }
  80. Serial.println("reply was:");
  81. Serial.println("==========");
  82. Serial.println(line);
  83. Serial.println("==========");
  84. Serial.println("closing connection");
  85. }
  86.  
  87. void loop() {
  88. }
RAW Paste Data