irmecir

googledocs.ino

Oct 8th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. /*
  2. * HTTPS with follow-redirect example
  3. * Created by Sujay S. Phadke, 2016
  4. *
  5. * Based on the WifiClientSecure example by
  6. * Ivan Grokhotkov
  7. *
  8. *
  9. * This example is in public domain.
  10. *
  11. * Modified by PDAControl
  12. * http://pdacontrolenglish.blogspot.com.co
  13. * http://pdacontrol.blogspot.com.co
  14. * https://youtu.be/5f7wCeD4gB4 -- TUTORIAL
  15. */
  16.  
  17. #include <ESP8266WiFi.h>
  18. #include "HTTPSRedirect.h"
  19.  
  20.  
  21. // Replace with your network credentials
  22. const char* ssid = "hooshmandsazan3";
  23. const char* password = "!q@w#e4r5t6y";
  24.  
  25. const char* host = "script.google.com";
  26. const char* googleRedirHost = "script.googleusercontent.com";
  27. const char *GScriptId = "AKfycbyOZ2agkz7Udy7L43aitJP0DgM1dlaAsAnJ7tudm9_wod7ZdNM";
  28.  
  29. const int httpsPort = 443;
  30. HTTPSRedirect client(httpsPort);
  31.  
  32. // http://askubuntu.com/questions/156620/how-to-verify-the-ssl-fingerprint-by-command-line-wget-curl/
  33. // echo | openssl s_client -connect script.google.com:443 |& openssl x509 -fingerprint -noout
  34. // www.grc.com doesn't seem to get the right fingerprint
  35. // SHA1 fingerprint of the certificate
  36. //const char* fingerprint = "94 2F 19 F7 A8 B4 5B 09 90 34 36 B2 2A C4 7F 17 06 AC 6A 2E";
  37. const char* fingerprint = "F0 5C 74 77 3F 6B 25 D7 3B 66 4D 43 2F 7E BC 5B E9 28 86 AD";
  38. const char* fingerprint2 = "94 64 D8 75 DE 5D 3A E6 3B A7 B6 15 52 72 CC 51 7A BA 2B BE";
  39.  
  40. // Write to Google Spreadsheet
  41. String url = String("/macros/s/") + GScriptId + "/exec?value=Hello";
  42. // Fetch Google Calendar events for 1 week ahead
  43. String url2 = String("/macros/s/") + GScriptId + "/exec?cal";
  44. // Read from Google Spreadsheet
  45. String url3 = String("/macros/s/") + GScriptId + "/exec?read";
  46.  
  47. void setup() {
  48. Serial.begin(115200);
  49. Serial.println();
  50. Serial.print("Connecting to wifi: ");
  51. Serial.println(ssid);
  52. // flush() is needed to print the above (connecting...) message reliably,
  53. // in case the wireless connection doesn't go through
  54. Serial.flush();
  55.  
  56. WiFi.begin(ssid, password);
  57. while (WiFi.status() != WL_CONNECTED) {
  58. delay(500);
  59. Serial.print(".");
  60. }
  61. Serial.println("");
  62. Serial.println("WiFi connected");
  63. Serial.println("IP address: ");
  64. Serial.println(WiFi.localIP());
  65.  
  66. // Use HTTPSRedirect class to create TLS connection
  67.  
  68. Serial.print("Connecting to ");
  69. Serial.println(host);
  70.  
  71. bool flag = false;
  72. for (int i=0; i<5; i++){
  73. int retval = client.connect(host, httpsPort);
  74. if (retval == 1) {
  75. flag = true;
  76. break;
  77. }
  78. else
  79. Serial.println("Connection failed. Retrying...");
  80. }
  81.  
  82. Serial.flush();
  83. if (!flag){
  84. Serial.print("Could not connect to server: ");
  85. Serial.println(host);
  86. Serial.println("Exiting...");
  87. return;
  88. }
  89.  
  90. Serial.flush();
  91. if (client.verify(fingerprint, host)) {
  92. Serial.println("Certificate match.");
  93. } else {
  94. Serial.println("Certificate mis-match");
  95. }
  96.  
  97. // Note: setup() must finish within approx. 1s, or the the watchdog timer
  98. // will reset the chip. Hence don't put too many requests in setup()
  99. // ref: https://github.com/esp8266/Arduino/issues/34
  100.  
  101. //Serial.print("Requesting URL: ");
  102. //Serial.println(url);
  103.  
  104. client.printRedir(url, host, googleRedirHost);
  105. Serial.println("==============================================================================");
  106.  
  107. // client.printRedir(url2, host, googleRedirHost);
  108. // Serial.println("==============================================================================");
  109.  
  110. }
  111.  
  112. void loop() {
  113. HTTPSRedirect client(httpsPort);
  114. if (!client.connected())
  115. client.connect(host, httpsPort);
  116.  
  117. client.printRedir(url3, host, googleRedirHost);
  118.  
  119.  
  120.  
  121.  
  122. //Serial.println(client.myLine);
  123.  
  124.  
  125.  
  126.  
  127. // In my testing on a ESP-01, a delay of less than 1500 resulted
  128. // in a crash and reboot after about 50 loop runs.
  129. delay(1500);
  130. }
Add Comment
Please, Sign In to add comment