Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. /**
  2. BasicHTTPSClient.ino
  3.  
  4. Created on: 20.08.2018
  5.  
  6. */
  7.  
  8. #include <Arduino.h>
  9.  
  10. #include <ESP8266WiFi.h>
  11. #include <ESP8266WiFiMulti.h>
  12.  
  13. #include <ESP8266HTTPClient.h>
  14.  
  15. #include <WiFiClientSecureBearSSL.h>
  16. // Fingerprint for demo URL, expires on June 2, 2019, needs to be updated well before this date
  17. const uint8_t fingerprint[20] = {0x5A, 0xCF, 0xFE, 0xF0, 0xF1, 0xA6, 0xF4, 0x5F, 0xD2, 0x11, 0x11, 0xC6, 0x1D, 0x2F, 0x0E, 0xBC, 0x39, 0x8D, 0x50, 0xE0};
  18.  
  19. ESP8266WiFiMulti WiFiMulti;
  20.  
  21. void setup() {
  22.  
  23. Serial.begin(115200);
  24. // Serial.setDebugOutput(true);
  25.  
  26. Serial.println();
  27. Serial.println();
  28. Serial.println();
  29.  
  30. for (uint8_t t = 4; t > 0; t--) {
  31. Serial.printf("[SETUP] WAIT %d...\n", t);
  32. Serial.flush();
  33. delay(1000);
  34. }
  35.  
  36. WiFi.mode(WIFI_STA);
  37. WiFiMulti.addAP("SSID", "PASSWORD");
  38. }
  39.  
  40. void loop() {
  41. // wait for WiFi connection
  42. if ((WiFiMulti.run() == WL_CONNECTED)) {
  43.  
  44. std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
  45.  
  46. client->setFingerprint(fingerprint);
  47.  
  48. HTTPClient https;
  49.  
  50. Serial.print("[HTTPS] begin...\n");
  51. if (https.begin(*client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS
  52.  
  53. Serial.print("[HTTPS] GET...\n");
  54. // start connection and send HTTP header
  55. int httpCode = https.GET();
  56.  
  57. // httpCode will be negative on error
  58. if (httpCode > 0) {
  59. // HTTP header has been send and Server response header has been handled
  60. Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
  61.  
  62. // file found at server
  63. if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
  64. String payload = https.getString();
  65. Serial.println(payload);
  66. }
  67. } else {
  68. Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
  69. }
  70.  
  71. https.end();
  72. } else {
  73. Serial.printf("[HTTPS] Unable to connect\n");
  74. }
  75. }
  76.  
  77. Serial.println("Wait 10s before next round...");
  78. delay(10000);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement